mostly reindent of examples

This commit is contained in:
Laurent Perron
2019-05-06 10:31:03 +02:00
parent 9b8a26beee
commit cd6bf20fe5
10 changed files with 77 additions and 91 deletions

View File

@@ -14,9 +14,12 @@
import com.google.ortools.graph.MaxFlow;
import com.google.ortools.graph.MinCostFlow;
/** Sample showing how to model using the flow solver. */
public class FlowExample {
/**
* Sample showing how to model using the flow solver.
*
*/
public class FlowExample {
static {
System.loadLibrary("jniortools");
}
@@ -26,11 +29,7 @@ public class FlowExample {
final int numSources = 4;
final int numTargets = 4;
final int[][] costs = {
{90, 75, 75, 80},
{35, 85, 55, 65},
{125, 95, 90, 105},
{45, 110, 95, 115}
};
{90, 75, 75, 80}, {35, 85, 55, 65}, {125, 95, 90, 105}, {45, 110, 95, 115}};
final int expectedCost = 275;
MinCostFlow minCostFlow = new MinCostFlow();
for (int source = 0; source < numSources; ++source) {
@@ -48,13 +47,8 @@ public class FlowExample {
System.out.println("total flow = " + totalFlowCost + "/" + expectedCost);
for (int i = 0; i < minCostFlow.getNumArcs(); ++i) {
if (minCostFlow.getFlow(i) > 0) {
System.out.println(
"From source "
+ minCostFlow.getTail(i)
+ " to target "
+ minCostFlow.getHead(i)
+ ": cost "
+ minCostFlow.getUnitCost(i));
System.out.println("From source " + minCostFlow.getTail(i) + " to target "
+ minCostFlow.getHead(i) + ": cost " + minCostFlow.getUnitCost(i));
}
}
} else {
@@ -75,15 +69,8 @@ public class FlowExample {
if (maxFlow.solve(0, 5) == MaxFlow.Status.OPTIMAL) {
System.out.println("Total flow " + maxFlow.getOptimalFlow() + "/" + expectedTotalFlow);
for (int i = 0; i < maxFlow.getNumArcs(); ++i) {
System.out.println(
"From source "
+ maxFlow.getTail(i)
+ " to target "
+ maxFlow.getHead(i)
+ ": "
+ maxFlow.getFlow(i)
+ " / "
+ maxFlow.getCapacity(i));
System.out.println("From source " + maxFlow.getTail(i) + " to target " + maxFlow.getHead(i)
+ ": " + maxFlow.getFlow(i) + " / " + maxFlow.getCapacity(i));
}
// TODO(user): Our SWIG configuration does not currently handle these
// functions correctly in Java:

View File

@@ -16,7 +16,11 @@ import com.google.ortools.linearsolver.MPObjective;
import com.google.ortools.linearsolver.MPSolver;
import com.google.ortools.linearsolver.MPVariable;
/** Integer programming example that shows how to use the API. */
/**
* Integer programming example that shows how to use the API.
*
*/
public class IntegerProgramming {
static {
System.loadLibrary("jniortools");
@@ -27,7 +31,6 @@ public class IntegerProgramming {
return new MPSolver(
"IntegerProgrammingExample", MPSolver.OptimizationProblemType.valueOf(solverType));
} catch (java.lang.IllegalArgumentException e) {
System.err.println("Bad solver type: " + e);
return null;
}
}
@@ -63,10 +66,9 @@ public class IntegerProgramming {
// Verify that the solution satisfies all constraints (when using solvers
// others than GLOP_LINEAR_PROGRAMMING, this is highly recommended!).
if (!solver.verifySolution(/*tolerance=*/ 1e-7, /*logErrors=*/ true)) {
System.err.println(
"The solution returned by the solver violated the"
+ " problem constraints by at least 1e-7");
if (!solver.verifySolution(/*tolerance=*/1e-7, /* log_errors= */ true)) {
System.err.println("The solution returned by the solver violated the"
+ " problem constraints by at least 1e-7");
return;
}

View File

@@ -13,35 +13,26 @@
import com.google.ortools.algorithms.KnapsackSolver;
/** Sample showing how to model using the knapsack solver. */
/**
* Sample showing how to model using the knapsack solver.
*
*/
public class Knapsack {
static {
System.loadLibrary("jniortools");
}
private static void solve() {
KnapsackSolver solver =
new KnapsackSolver(
KnapsackSolver.SolverType.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER, "test");
final long[] profits = {
360, 83, 59, 130, 431, 67, 230, 52, 93,
125, 670, 892, 600, 38, 48, 147, 78, 256,
63, 17, 120, 164, 432, 35, 92, 110, 22,
42, 50, 323, 514, 28, 87, 73, 78, 15,
26, 78, 210, 36, 85, 189, 274, 43, 33,
10, 19, 389, 276, 312
};
KnapsackSolver solver = new KnapsackSolver(
KnapsackSolver.SolverType.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER, "test");
final long[] profits = {360, 83, 59, 130, 431, 67, 230, 52, 93, 125, 670, 892, 600, 38, 48, 147,
78, 256, 63, 17, 120, 164, 432, 35, 92, 110, 22, 42, 50, 323, 514, 28, 87, 73, 78, 15, 26,
78, 210, 36, 85, 189, 274, 43, 33, 10, 19, 389, 276, 312};
final long[][] weights = {
{
7, 0, 30, 22, 80, 94, 11, 81, 70,
64, 59, 18, 0, 36, 3, 8, 15, 42,
9, 0, 42, 47, 52, 32, 26, 48, 55,
6, 29, 84, 2, 4, 18, 56, 7, 29,
93, 44, 71, 3, 86, 66, 31, 65, 0,
79, 20, 65, 52, 13
}
};
final long[][] weights = {{7, 0, 30, 22, 80, 94, 11, 81, 70, 64, 59, 18, 0, 36, 3, 8, 15, 42, 9,
0, 42, 47, 52, 32, 26, 48, 55, 6, 29, 84, 2, 4, 18, 56, 7, 29, 93, 44, 71, 3, 86, 66, 31,
65, 0, 79, 20, 65, 52, 13}};
final long[] capacities = {850};

View File

@@ -15,25 +15,21 @@ import com.google.ortools.graph.LinearSumAssignment;
/**
* Test assignment on a 4x4 matrix. Example taken from
* http://www.ee.oulu.fi/~mpa/matreng/eem1_2-1.htm with kCost[0][1] modified so the optimum solution
* is unique.
* http://www.ee.oulu.fi/~mpa/matreng/eem1_2-1.htm with kCost[0][1]
* modified so the optimum solution is unique.
*
*/
public class LinearAssignmentAPI {
public class LinearAssignmentAPI {
static {
System.loadLibrary("jniortools");
}
private static void runAssignmentOn4x4Matrix() {
final int numSources = 4;
final int numTargets = 4;
final int[][] cost = {
{90, 76, 75, 80},
{35, 85, 55, 65},
{125, 95, 90, 105},
{45, 110, 95, 115}
};
{90, 76, 75, 80}, {35, 85, 55, 65}, {125, 95, 90, 105}, {45, 110, 95, 115}};
final int expectedCost = cost[0][3] + cost[1][2] + cost[2][1] + cost[3][0];
LinearSumAssignment assignment = new LinearSumAssignment();
@@ -46,13 +42,8 @@ public class LinearAssignmentAPI {
if (assignment.solve() == LinearSumAssignment.Status.OPTIMAL) {
System.out.println("Total cost = " + assignment.getOptimalCost() + "/" + expectedCost);
for (int node = 0; node < assignment.getNumNodes(); ++node) {
System.out.println(
"Left node "
+ node
+ " assigned to right node "
+ assignment.getRightMate(node)
+ " with cost "
+ assignment.getAssignmentCost(node));
System.out.println("Left node " + node + " assigned to right node "
+ assignment.getRightMate(node) + " with cost " + assignment.getAssignmentCost(node));
}
} else {
System.out.println("No solution found.");

View File

@@ -12,12 +12,15 @@
// limitations under the License.
import com.google.ortools.linearsolver.MPConstraint;
import com.google.ortools.linearsolver.MPModelExportOptions;
import com.google.ortools.linearsolver.MPObjective;
import com.google.ortools.linearsolver.MPSolver;
import com.google.ortools.linearsolver.MPVariable;
/** Linear programming example that shows how to use the API. */
/**
* Linear programming example that shows how to use the API.
*
*/
public class LinearProgramming {
static {
System.loadLibrary("jniortools");
@@ -73,8 +76,7 @@ public class LinearProgramming {
System.out.println("Number of constraints = " + solver.numConstraints());
if (printModel) {
MPModelExportOptions options = new MPModelExportOptions();
String model = solver.exportModelAsLpFormat(options);
String model = solver.exportModelAsLpFormat();
System.out.println(model);
}
@@ -88,10 +90,9 @@ public class LinearProgramming {
// Verify that the solution satisfies all constraints (when using solvers
// others than GLOP_LINEAR_PROGRAMMING, this is highly recommended!).
if (!solver.verifySolution(/*tolerance=*/ 1e-7, /*logErrors=*/ true)) {
System.err.println(
"The solution returned by the solver violated the"
+ " problem constraints by at least 1e-7");
if (!solver.verifySolution(/*tolerance=*/1e-7, /* log_errors= */ true)) {
System.err.println("The solution returned by the solver violated the"
+ " problem constraints by at least 1e-7");
return;
}

View File

@@ -16,7 +16,10 @@ import com.google.ortools.constraintsolver.IntVar;
import com.google.ortools.constraintsolver.Solver;
import java.util.logging.Logger;
/** Sample showing how to model using the constraint programming solver. */
/**
* Sample showing how to model using the constraint programming solver.
*
*/
public class RabbitsPheasants {
private static Logger logger = Logger.getLogger(RabbitsPheasants.class.getName());
@@ -25,8 +28,9 @@ public class RabbitsPheasants {
}
/**
* Solves the rabbits + pheasants problem. We are seing 20 heads and 56 legs. How many rabbits and
* how many pheasants are we thus seeing?
* Solves the rabbits + pheasants problem. We are seing 20 heads
* and 56 legs. How many rabbits and how many pheasants are we thus
* seeing?
*/
private static void solve(boolean traceSearch) {
ConstraintSolverParameters parameters = ConstraintSolverParameters.newBuilder()