polish java reindent
This commit is contained in:
@@ -47,11 +47,12 @@ class Pair<K, V> {
|
||||
*/
|
||||
|
||||
public class CapacitatedVehicleRoutingProblemWithTimeWindows {
|
||||
static {
|
||||
System.loadLibrary("jniortools");
|
||||
}
|
||||
|
||||
static { System.loadLibrary("jniortools"); }
|
||||
|
||||
private static Logger logger = Logger.getLogger(
|
||||
CapacitatedVehicleRoutingProblemWithTimeWindows.class.getName());
|
||||
private static Logger logger =
|
||||
Logger.getLogger(CapacitatedVehicleRoutingProblemWithTimeWindows.class.getName());
|
||||
|
||||
// Locations representing either an order location or a vehicle route
|
||||
// start/end.
|
||||
@@ -101,24 +102,17 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
|
||||
* @param penaltyMin minimum pernalty cost if order is dropped.
|
||||
* @param penaltyMax maximum pernalty cost if order is dropped.
|
||||
*/
|
||||
private void buildOrders(int numberOfOrders, int xMax, int yMax,
|
||||
int demandMax, int timeWindowMin, int timeWindowMax,
|
||||
int timeWindowWidth, int penaltyMin,
|
||||
int penaltyMax) {
|
||||
private void buildOrders(int numberOfOrders, int xMax, int yMax, int demandMax, int timeWindowMin,
|
||||
int timeWindowMax, int timeWindowWidth, int penaltyMin, int penaltyMax) {
|
||||
logger.info("Building orders.");
|
||||
for (int order = 0; order < numberOfOrders; ++order) {
|
||||
locations.add(Pair.of(randomGenerator.nextInt(xMax + 1),
|
||||
randomGenerator.nextInt(yMax + 1)));
|
||||
locations.add(Pair.of(randomGenerator.nextInt(xMax + 1), randomGenerator.nextInt(yMax + 1)));
|
||||
orderDemands.add(randomGenerator.nextInt(demandMax + 1));
|
||||
/** @todo 1) Specify deliver duration for each shipment*/
|
||||
orderDurations.add(2); // in minutes
|
||||
int timeWindowStart =
|
||||
randomGenerator.nextInt(timeWindowMax - timeWindowMin) +
|
||||
timeWindowMin;
|
||||
orderTimeWindows.add(
|
||||
Pair.of(timeWindowStart, timeWindowStart + timeWindowWidth));
|
||||
orderPenalties.add(randomGenerator.nextInt(penaltyMax - penaltyMin + 1) +
|
||||
penaltyMin);
|
||||
int timeWindowStart = randomGenerator.nextInt(timeWindowMax - timeWindowMin) + timeWindowMin;
|
||||
orderTimeWindows.add(Pair.of(timeWindowStart, timeWindowStart + timeWindowWidth));
|
||||
orderPenalties.add(randomGenerator.nextInt(penaltyMax - penaltyMin + 1) + penaltyMin);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,24 +129,20 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
|
||||
* @param costCoefficientMax maximum cost per distance unit of a vehicle
|
||||
* (mimimum is 1),
|
||||
*/
|
||||
private void buildFleet(int numberOfVehicles, int xMax, int yMax,
|
||||
int startTime, int endTime, int capacity,
|
||||
int costCoefficientMax) {
|
||||
private void buildFleet(int numberOfVehicles, int xMax, int yMax, int startTime, int endTime,
|
||||
int capacity, int costCoefficientMax) {
|
||||
logger.info("Building fleet.");
|
||||
vehicleCapacity = capacity;
|
||||
vehicleStarts = new int[numberOfVehicles];
|
||||
vehicleEnds = new int[numberOfVehicles];
|
||||
for (int vehicle = 0; vehicle < numberOfVehicles; ++vehicle) {
|
||||
vehicleStarts[vehicle] = locations.size();
|
||||
locations.add(Pair.of(randomGenerator.nextInt(xMax + 1),
|
||||
randomGenerator.nextInt(yMax + 1)));
|
||||
locations.add(Pair.of(randomGenerator.nextInt(xMax + 1), randomGenerator.nextInt(yMax + 1)));
|
||||
vehicleEnds[vehicle] = locations.size();
|
||||
locations.add(Pair.of(randomGenerator.nextInt(xMax + 1),
|
||||
randomGenerator.nextInt(yMax + 1)));
|
||||
locations.add(Pair.of(randomGenerator.nextInt(xMax + 1), randomGenerator.nextInt(yMax + 1)));
|
||||
vehicleStartTime.add(startTime);
|
||||
vehicleEndTime.add(endTime);
|
||||
vehicleCostCoefficients.add(randomGenerator.nextInt(costCoefficientMax) +
|
||||
1);
|
||||
vehicleCostCoefficients.add(randomGenerator.nextInt(costCoefficientMax) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,13 +150,13 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
|
||||
* Solves the current routing problem.
|
||||
*/
|
||||
private void solve(final int numberOfOrders, final int numberOfVehicles) {
|
||||
logger.info("Creating model with " + numberOfOrders + " orders and " +
|
||||
numberOfVehicles + " vehicles.");
|
||||
logger.info(
|
||||
"Creating model with " + numberOfOrders + " orders and " + numberOfVehicles + " vehicles.");
|
||||
// Finalizing model
|
||||
final int numberOfLocations = locations.size();
|
||||
|
||||
RoutingModel model = new RoutingModel(numberOfLocations, numberOfVehicles,
|
||||
vehicleStarts, vehicleEnds);
|
||||
RoutingModel model =
|
||||
new RoutingModel(numberOfLocations, numberOfVehicles, vehicleStarts, vehicleEnds);
|
||||
|
||||
// Setting up dimensions
|
||||
final int bigNumber = 100000;
|
||||
@@ -178,8 +168,8 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
|
||||
Pair<Integer, Integer> secondLocation = locations.get(secondIndex);
|
||||
Integer distance = 0;
|
||||
Integer duration = 0;
|
||||
distance = Math.abs(firstLocation.first - secondLocation.first) +
|
||||
Math.abs(firstLocation.second - secondLocation.second);
|
||||
distance = Math.abs(firstLocation.first - secondLocation.first)
|
||||
+ Math.abs(firstLocation.second - secondLocation.second);
|
||||
// Deal with Order duration shipment
|
||||
if (firstIndex < numberOfOrders) {
|
||||
// shipment duration
|
||||
@@ -218,9 +208,9 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
|
||||
try {
|
||||
Pair<Integer, Integer> firstLocation = locations.get(firstIndex);
|
||||
Pair<Integer, Integer> secondLocation = locations.get(secondIndex);
|
||||
return costCoefficient *
|
||||
(Math.abs(firstLocation.first - secondLocation.first) +
|
||||
Math.abs(firstLocation.second - secondLocation.second));
|
||||
return costCoefficient
|
||||
* (Math.abs(firstLocation.first - secondLocation.first)
|
||||
+ Math.abs(firstLocation.second - secondLocation.second));
|
||||
} catch (Throwable throwed) {
|
||||
logger.warning(throwed.getMessage());
|
||||
return 0;
|
||||
@@ -228,17 +218,14 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
|
||||
}
|
||||
};
|
||||
model.setArcCostEvaluatorOfVehicle(manhattanCostCallback, vehicle);
|
||||
model.cumulVar(model.start(vehicle), "time")
|
||||
.setMin(vehicleStartTime.get(vehicle));
|
||||
model.cumulVar(model.end(vehicle), "time")
|
||||
.setMax(vehicleEndTime.get(vehicle));
|
||||
model.cumulVar(model.start(vehicle), "time").setMin(vehicleStartTime.get(vehicle));
|
||||
model.cumulVar(model.end(vehicle), "time").setMax(vehicleEndTime.get(vehicle));
|
||||
}
|
||||
|
||||
// Setting up orders
|
||||
for (int order = 0; order < numberOfOrders; ++order) {
|
||||
model.cumulVar(model.nodeToIndex(order), "time")
|
||||
.setRange(orderTimeWindows.get(order).first,
|
||||
orderTimeWindows.get(order).second);
|
||||
.setRange(orderTimeWindows.get(order).first, orderTimeWindows.get(order).second);
|
||||
int[] orders = {order};
|
||||
model.addDisjunction(orders, orderPenalties.get(order));
|
||||
}
|
||||
@@ -247,8 +234,7 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
|
||||
RoutingSearchParameters parameters =
|
||||
RoutingSearchParameters.newBuilder()
|
||||
.mergeFrom(RoutingModel.defaultSearchParameters())
|
||||
.setFirstSolutionStrategy(
|
||||
FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
|
||||
.setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
|
||||
.build();
|
||||
|
||||
logger.info("Search");
|
||||
@@ -275,19 +261,16 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
|
||||
route += "/!\\Empty Route/!\\ ";
|
||||
}
|
||||
{
|
||||
for (; !model.isEnd(order);
|
||||
order = solution.value(model.nextVar(order))) {
|
||||
for (; !model.isEnd(order); order = solution.value(model.nextVar(order))) {
|
||||
IntVar load = model.cumulVar(order, "capacity");
|
||||
IntVar time = model.cumulVar(order, "time");
|
||||
route += order + " Load(" + solution.value(load) + ") "
|
||||
+ "Time(" + solution.min(time) + ", " +
|
||||
solution.max(time) + ") -> ";
|
||||
+ "Time(" + solution.min(time) + ", " + solution.max(time) + ") -> ";
|
||||
}
|
||||
IntVar load = model.cumulVar(order, "capacity");
|
||||
IntVar time = model.cumulVar(order, "time");
|
||||
route += order + " Load(" + solution.value(load) + ") "
|
||||
+ "Time(" + solution.min(time) + ", " + solution.max(time) +
|
||||
")";
|
||||
+ "Time(" + solution.min(time) + ", " + solution.max(time) + ")";
|
||||
}
|
||||
output += route + "\n";
|
||||
}
|
||||
@@ -318,10 +301,9 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
|
||||
final int vehicles = 20;
|
||||
final int capacity = 50;
|
||||
|
||||
problem.buildOrders(orders, xMax, yMax, demandMax, timeWindowMin,
|
||||
timeWindowMax, timeWindowWidth, penaltyMin, penaltyMax);
|
||||
problem.buildFleet(vehicles, xMax, yMax, startTime, endTime, capacity,
|
||||
costCoefficientMax);
|
||||
problem.buildOrders(orders, xMax, yMax, demandMax, timeWindowMin, timeWindowMax,
|
||||
timeWindowWidth, penaltyMin, penaltyMax);
|
||||
problem.buildFleet(vehicles, xMax, yMax, startTime, endTime, capacity, costCoefficientMax);
|
||||
problem.solve(orders, vehicles);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,23 +20,22 @@ import com.google.ortools.graph.MinCostFlow;
|
||||
*/
|
||||
|
||||
public class FlowExample {
|
||||
|
||||
static { System.loadLibrary("jniortools"); }
|
||||
static {
|
||||
System.loadLibrary("jniortools");
|
||||
}
|
||||
|
||||
private static void solveMinCostFlow() {
|
||||
System.out.println("Min Cost Flow Problem - Simple interface");
|
||||
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}};
|
||||
final int[][] costs = {
|
||||
{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) {
|
||||
for (int target = 0; target < numTargets; ++target) {
|
||||
minCostFlow.addArcWithCapacityAndUnitCost(source, numSources + target,
|
||||
1, costs[source][target]);
|
||||
minCostFlow.addArcWithCapacityAndUnitCost(
|
||||
source, numSources + target, 1, costs[source][target]);
|
||||
}
|
||||
}
|
||||
for (int node = 0; node < numSources; ++node) {
|
||||
@@ -48,9 +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 {
|
||||
@@ -69,12 +67,10 @@ public class FlowExample {
|
||||
maxFlow.addArcWithCapacity(tails[i], heads[i], capacities[i]);
|
||||
}
|
||||
if (maxFlow.solve(0, 5) == MaxFlow.Status.OPTIMAL) {
|
||||
System.out.println("Total flow " + maxFlow.getOptimalFlow() + "/" +
|
||||
expectedTotalFlow);
|
||||
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:
|
||||
|
||||
@@ -22,12 +22,14 @@ import com.google.ortools.linearsolver.MPVariable;
|
||||
*/
|
||||
|
||||
public class IntegerProgramming {
|
||||
static { System.loadLibrary("jniortools"); }
|
||||
static {
|
||||
System.loadLibrary("jniortools");
|
||||
}
|
||||
|
||||
private static MPSolver createSolver(String solverType) {
|
||||
try {
|
||||
return new MPSolver("IntegerProgrammingExample",
|
||||
MPSolver.OptimizationProblemType.valueOf(solverType));
|
||||
return new MPSolver(
|
||||
"IntegerProgrammingExample", MPSolver.OptimizationProblemType.valueOf(solverType));
|
||||
} catch (java.lang.IllegalArgumentException e) {
|
||||
System.err.println("Bad solver type: " + e);
|
||||
return null;
|
||||
@@ -67,29 +69,25 @@ public class IntegerProgramming {
|
||||
// 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");
|
||||
+ " problem constraints by at least 1e-7");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("Problem solved in " + solver.wallTime() +
|
||||
" milliseconds");
|
||||
System.out.println("Problem solved in " + solver.wallTime() + " milliseconds");
|
||||
|
||||
// The objective value of the solution.
|
||||
System.out.println("Optimal objective value = " +
|
||||
solver.objective().value());
|
||||
System.out.println("Optimal objective value = " + solver.objective().value());
|
||||
|
||||
// The value of each variable in the solution.
|
||||
System.out.println("x1 = " + x1.solutionValue());
|
||||
System.out.println("x2 = " + x2.solutionValue());
|
||||
|
||||
System.out.println("Advanced usage:");
|
||||
System.out.println("Problem solved in " + solver.nodes() +
|
||||
" branch-and-bound nodes");
|
||||
System.out.println("Problem solved in " + solver.nodes() + " branch-and-bound nodes");
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.out.println(
|
||||
"---- Integer programming example with SCIP (recommended) ----");
|
||||
System.out.println("---- Integer programming example with SCIP (recommended) ----");
|
||||
runIntegerProgrammingExample("SCIP_MIXED_INTEGER_PROGRAMMING");
|
||||
System.out.println("---- Integer programming example with CBC ----");
|
||||
runIntegerProgrammingExample("CBC_MIXED_INTEGER_PROGRAMMING");
|
||||
|
||||
@@ -19,23 +19,20 @@ import com.google.ortools.algorithms.KnapsackSolver;
|
||||
*/
|
||||
|
||||
public class Knapsack {
|
||||
static { System.loadLibrary("jniortools"); }
|
||||
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};
|
||||
|
||||
@@ -46,9 +43,10 @@ public class Knapsack {
|
||||
|
||||
final long computedProfit = solver.solve();
|
||||
|
||||
System.out.println("Optimal_Profit = " + computedProfit + "/" +
|
||||
optimalProfit);
|
||||
System.out.println("Optimal_Profit = " + computedProfit + "/" + optimalProfit);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception { Knapsack.solve(); }
|
||||
public static void main(String[] args) throws Exception {
|
||||
Knapsack.solve();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,16 +21,15 @@ import com.google.ortools.graph.LinearSumAssignment;
|
||||
*/
|
||||
|
||||
public class LinearAssignmentAPI {
|
||||
|
||||
static { System.loadLibrary("jniortools"); }
|
||||
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}};
|
||||
final int[][] cost = {
|
||||
{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();
|
||||
@@ -41,12 +40,10 @@ public class LinearAssignmentAPI {
|
||||
}
|
||||
|
||||
if (assignment.solve() == LinearSumAssignment.Status.OPTIMAL) {
|
||||
System.out.println("Total cost = " + assignment.getOptimalCost() + "/" +
|
||||
expectedCost);
|
||||
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.");
|
||||
|
||||
@@ -22,19 +22,20 @@ import com.google.ortools.linearsolver.MPVariable;
|
||||
*/
|
||||
|
||||
public class LinearProgramming {
|
||||
static { System.loadLibrary("jniortools"); }
|
||||
static {
|
||||
System.loadLibrary("jniortools");
|
||||
}
|
||||
|
||||
private static MPSolver createSolver(String solverType) {
|
||||
try {
|
||||
return new MPSolver("LinearProgrammingExample",
|
||||
MPSolver.OptimizationProblemType.valueOf(solverType));
|
||||
return new MPSolver(
|
||||
"LinearProgrammingExample", MPSolver.OptimizationProblemType.valueOf(solverType));
|
||||
} catch (java.lang.IllegalArgumentException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static void runLinearProgrammingExample(String solverType,
|
||||
boolean printModel) {
|
||||
private static void runLinearProgrammingExample(String solverType, boolean printModel) {
|
||||
MPSolver solver = createSolver(solverType);
|
||||
if (solver == null) {
|
||||
System.out.println("Could not create solver " + solverType);
|
||||
@@ -91,16 +92,14 @@ public class LinearProgramming {
|
||||
// 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");
|
||||
+ " problem constraints by at least 1e-7");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("Problem solved in " + solver.wallTime() +
|
||||
" milliseconds");
|
||||
System.out.println("Problem solved in " + solver.wallTime() + " milliseconds");
|
||||
|
||||
// The objective value of the solution.
|
||||
System.out.println("Optimal objective value = " +
|
||||
solver.objective().value());
|
||||
System.out.println("Optimal objective value = " + solver.objective().value());
|
||||
|
||||
// The value of each variable in the solution.
|
||||
System.out.println("x1 = " + x1.solutionValue());
|
||||
@@ -110,8 +109,7 @@ public class LinearProgramming {
|
||||
final double[] activities = solver.computeConstraintActivities();
|
||||
|
||||
System.out.println("Advanced usage:");
|
||||
System.out.println("Problem solved in " + solver.iterations() +
|
||||
" iterations");
|
||||
System.out.println("Problem solved in " + solver.iterations() + " iterations");
|
||||
System.out.println("x1: reduced cost = " + x1.reducedCost());
|
||||
System.out.println("x2: reduced cost = " + x2.reducedCost());
|
||||
System.out.println("x3: reduced cost = " + x3.reducedCost());
|
||||
@@ -124,8 +122,7 @@ public class LinearProgramming {
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.out.println(
|
||||
"---- Linear programming example with GLOP (recommended) ----");
|
||||
System.out.println("---- Linear programming example with GLOP (recommended) ----");
|
||||
runLinearProgrammingExample("GLOP_LINEAR_PROGRAMMING", true);
|
||||
System.out.println("---- Linear programming example with CLP ----");
|
||||
runLinearProgrammingExample("CLP_LINEAR_PROGRAMMING", false);
|
||||
|
||||
@@ -29,11 +29,14 @@ import com.google.ortools.constraintsolver.Solver;
|
||||
*
|
||||
*/
|
||||
public class LsApi {
|
||||
|
||||
static { System.loadLibrary("jniortools"); }
|
||||
static {
|
||||
System.loadLibrary("jniortools");
|
||||
}
|
||||
|
||||
static class OneVarLns extends BaseLns {
|
||||
public OneVarLns(IntVar[] vars) { super(vars); }
|
||||
public OneVarLns(IntVar[] vars) {
|
||||
super(vars);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initFragments() {
|
||||
@@ -127,11 +130,10 @@ public class LsApi {
|
||||
IntVar[] vars = solver.makeIntVarArray(4, 0, 4, "vars");
|
||||
IntVar sumVar = solver.makeSum(vars).var();
|
||||
OptimizeVar obj = solver.makeMinimize(sumVar, 1);
|
||||
DecisionBuilder db = solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND,
|
||||
Solver.ASSIGN_MAX_VALUE);
|
||||
DecisionBuilder db =
|
||||
solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MAX_VALUE);
|
||||
OneVarLns oneVarLns = new OneVarLns(vars);
|
||||
LocalSearchPhaseParameters lsParams =
|
||||
solver.makeLocalSearchPhaseParameters(oneVarLns, db);
|
||||
LocalSearchPhaseParameters lsParams = solver.makeLocalSearchPhaseParameters(oneVarLns, db);
|
||||
DecisionBuilder ls = solver.makeLocalSearchPhase(vars, db, lsParams);
|
||||
SolutionCollector collector = solver.makeLastSolutionCollector();
|
||||
collector.addObjective(sumVar);
|
||||
@@ -146,11 +148,10 @@ public class LsApi {
|
||||
IntVar[] vars = solver.makeIntVarArray(4, 0, 4, "vars");
|
||||
IntVar sumVar = solver.makeSum(vars).var();
|
||||
OptimizeVar obj = solver.makeMinimize(sumVar, 1);
|
||||
DecisionBuilder db = solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND,
|
||||
Solver.ASSIGN_MAX_VALUE);
|
||||
DecisionBuilder db =
|
||||
solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MAX_VALUE);
|
||||
MoveOneVar moveOneVar = new MoveOneVar(vars);
|
||||
LocalSearchPhaseParameters lsParams =
|
||||
solver.makeLocalSearchPhaseParameters(moveOneVar, db);
|
||||
LocalSearchPhaseParameters lsParams = solver.makeLocalSearchPhaseParameters(moveOneVar, db);
|
||||
DecisionBuilder ls = solver.makeLocalSearchPhase(vars, db, lsParams);
|
||||
SolutionCollector collector = solver.makeLastSolutionCollector();
|
||||
collector.addObjective(sumVar);
|
||||
@@ -165,8 +166,8 @@ public class LsApi {
|
||||
IntVar[] vars = solver.makeIntVarArray(4, 0, 4, "vars");
|
||||
IntVar sumVar = solver.makeSum(vars).var();
|
||||
OptimizeVar obj = solver.makeMinimize(sumVar, 1);
|
||||
DecisionBuilder db = solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND,
|
||||
Solver.ASSIGN_MAX_VALUE);
|
||||
DecisionBuilder db =
|
||||
solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MAX_VALUE);
|
||||
MoveOneVar moveOneVar = new MoveOneVar(vars);
|
||||
SumFilter filter = new SumFilter(vars);
|
||||
IntVarLocalSearchFilter[] filters = new IntVarLocalSearchFilter[1];
|
||||
|
||||
@@ -22,10 +22,11 @@ import java.util.logging.Logger;
|
||||
*
|
||||
*/
|
||||
public class RabbitsPheasants {
|
||||
private static Logger logger =
|
||||
Logger.getLogger(RabbitsPheasants.class.getName());
|
||||
private static Logger logger = Logger.getLogger(RabbitsPheasants.class.getName());
|
||||
|
||||
static { System.loadLibrary("jniortools"); }
|
||||
static {
|
||||
System.loadLibrary("jniortools");
|
||||
}
|
||||
|
||||
/**
|
||||
* Solves the rabbits + pheasants problem. We are seing 20 heads
|
||||
@@ -33,23 +34,18 @@ public class RabbitsPheasants {
|
||||
* seeing?
|
||||
*/
|
||||
private static void solve(boolean traceSearch) {
|
||||
ConstraintSolverParameters parameters =
|
||||
ConstraintSolverParameters.newBuilder()
|
||||
.mergeFrom(Solver.defaultSolverParameters())
|
||||
.setTraceSearch(traceSearch)
|
||||
.build();
|
||||
ConstraintSolverParameters parameters = ConstraintSolverParameters.newBuilder()
|
||||
.mergeFrom(Solver.defaultSolverParameters())
|
||||
.setTraceSearch(traceSearch)
|
||||
.build();
|
||||
Solver solver = new Solver("RabbitsPheasants", parameters);
|
||||
IntVar rabbits = solver.makeIntVar(0, 100, "rabbits");
|
||||
IntVar pheasants = solver.makeIntVar(0, 100, "pheasants");
|
||||
solver.addConstraint(
|
||||
solver.makeEquality(solver.makeSum(rabbits, pheasants), 20));
|
||||
solver.addConstraint(
|
||||
solver.makeEquality(solver.makeSum(solver.makeProd(rabbits, 4),
|
||||
solver.makeProd(pheasants, 2)),
|
||||
56));
|
||||
solver.addConstraint(solver.makeEquality(solver.makeSum(rabbits, pheasants), 20));
|
||||
solver.addConstraint(solver.makeEquality(
|
||||
solver.makeSum(solver.makeProd(rabbits, 4), solver.makeProd(pheasants, 2)), 56));
|
||||
DecisionBuilder db =
|
||||
solver.makePhase(rabbits, pheasants, Solver.CHOOSE_FIRST_UNBOUND,
|
||||
Solver.ASSIGN_MIN_VALUE);
|
||||
solver.makePhase(rabbits, pheasants, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MIN_VALUE);
|
||||
solver.newSearch(db);
|
||||
solver.nextSolution();
|
||||
logger.info(rabbits.toString());
|
||||
|
||||
@@ -23,7 +23,9 @@ import com.google.ortools.constraintsolver.FirstSolutionStrategy;
|
||||
import com.google.ortools.constraintsolver.RoutingSearchParameters;
|
||||
|
||||
class Tsp {
|
||||
static { System.loadLibrary("jniortools"); }
|
||||
static {
|
||||
System.loadLibrary("jniortools");
|
||||
}
|
||||
|
||||
static class RandomManhattan extends NodeEvaluator2 {
|
||||
public RandomManhattan(int size, int seed) {
|
||||
@@ -38,8 +40,8 @@ class Tsp {
|
||||
|
||||
@Override
|
||||
public long run(int firstIndex, int secondIndex) {
|
||||
return Math.abs(xs[firstIndex] - xs[secondIndex]) +
|
||||
Math.abs(ys[firstIndex] - ys[secondIndex]);
|
||||
return Math.abs(xs[firstIndex] - xs[secondIndex])
|
||||
+ Math.abs(ys[firstIndex] - ys[secondIndex]);
|
||||
}
|
||||
|
||||
private int[] xs;
|
||||
@@ -77,15 +79,13 @@ class Tsp {
|
||||
}
|
||||
|
||||
// Add dummy dimension to test API.
|
||||
routing.addDimension(new ConstantCallback(), size + 1, size + 1, true,
|
||||
"dummy");
|
||||
routing.addDimension(new ConstantCallback(), size + 1, size + 1, true, "dummy");
|
||||
|
||||
// Solve, returns a solution if any (owned by RoutingModel).
|
||||
RoutingSearchParameters search_parameters =
|
||||
RoutingSearchParameters.newBuilder()
|
||||
.mergeFrom(RoutingModel.defaultSearchParameters())
|
||||
.setFirstSolutionStrategy(
|
||||
FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
|
||||
.setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
|
||||
.build();
|
||||
|
||||
Assignment solution = routing.solveWithParameters(search_parameters);
|
||||
|
||||
@@ -24,9 +24,8 @@ class DataProblem {
|
||||
private int[][] locations_;
|
||||
|
||||
public DataProblem() {
|
||||
locations_ = new int[][] {{4, 4}, {2, 0}, {8, 0}, {0, 1}, {1, 1}, {5, 2},
|
||||
{7, 2}, {3, 3}, {6, 3}, {5, 5}, {8, 5}, {1, 6},
|
||||
{2, 6}, {3, 7}, {6, 7}, {0, 8}, {7, 8}};
|
||||
locations_ = new int[][] {{4, 4}, {2, 0}, {8, 0}, {0, 1}, {1, 1}, {5, 2}, {7, 2}, {3, 3},
|
||||
{6, 3}, {5, 5}, {8, 5}, {1, 6}, {2, 6}, {3, 7}, {6, 7}, {0, 8}, {7, 8}};
|
||||
|
||||
// Compute locations in meters using the block dimension defined as follow
|
||||
// Manhattan average block: 750ft x 264ft -> 228m x 80m
|
||||
@@ -40,13 +39,21 @@ class DataProblem {
|
||||
}
|
||||
|
||||
/// @brief Gets the number of vehicles.
|
||||
public int getVehicleNumber() { return 4; }
|
||||
public int getVehicleNumber() {
|
||||
return 4;
|
||||
}
|
||||
/// @brief Gets the locations.
|
||||
public int[][] getLocations() { return locations_; }
|
||||
public int[][] getLocations() {
|
||||
return locations_;
|
||||
}
|
||||
/// @brief Gets the number of locations.
|
||||
public int getLocationNumber() { return locations_.length; }
|
||||
public int getLocationNumber() {
|
||||
return locations_.length;
|
||||
}
|
||||
/// @brief Gets the depot NodeIndex.
|
||||
public int getDepot() { return 0; }
|
||||
public int getDepot() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Manhattan distance implemented as a callback.
|
||||
@@ -64,10 +71,9 @@ class ManhattanDistance extends NodeEvaluator2 {
|
||||
if (fromNode == toNode)
|
||||
distances_[fromNode][toNode] = 0;
|
||||
else
|
||||
distances_[fromNode][toNode] = abs(data.getLocations()[toNode][0] -
|
||||
data.getLocations()[fromNode][0]) +
|
||||
abs(data.getLocations()[toNode][1] -
|
||||
data.getLocations()[fromNode][1]);
|
||||
distances_[fromNode][toNode] =
|
||||
abs(data.getLocations()[toNode][0] - data.getLocations()[fromNode][0])
|
||||
+ abs(data.getLocations()[toNode][1] - data.getLocations()[fromNode][1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,16 +86,18 @@ class ManhattanDistance extends NodeEvaluator2 {
|
||||
}
|
||||
|
||||
class Vrp {
|
||||
static { System.loadLibrary("jniortools"); }
|
||||
static {
|
||||
System.loadLibrary("jniortools");
|
||||
}
|
||||
|
||||
/// @brief Add Global Span constraint.
|
||||
static void addDistanceDimension(RoutingModel routing, DataProblem data) {
|
||||
String distance = "Distance";
|
||||
routing.addDimension(new ManhattanDistance(data),
|
||||
0, // null slack
|
||||
3000, // maximum distance per vehicle
|
||||
true, // start cumul to zero
|
||||
distance);
|
||||
0, // null slack
|
||||
3000, // maximum distance per vehicle
|
||||
true, // start cumul to zero
|
||||
distance);
|
||||
RoutingDimension distanceDimension = routing.getDimensionOrDie(distance);
|
||||
// Try to minimize the max distance among vehicles.
|
||||
// /!\ It doesn't mean the standard deviation is minimized
|
||||
@@ -97,8 +105,7 @@ class Vrp {
|
||||
}
|
||||
|
||||
/// @brief Print the solution
|
||||
static void printSolution(DataProblem data, RoutingModel routing,
|
||||
Assignment solution) {
|
||||
static void printSolution(DataProblem data, RoutingModel routing, Assignment solution) {
|
||||
// Solution cost.
|
||||
System.out.println("Objective : " + solution.objectiveValue());
|
||||
// Inspect solution.
|
||||
@@ -123,8 +130,8 @@ class Vrp {
|
||||
DataProblem data = new DataProblem();
|
||||
|
||||
// Create Routing Model
|
||||
RoutingModel routing = new RoutingModel(
|
||||
data.getLocationNumber(), data.getVehicleNumber(), data.getDepot());
|
||||
RoutingModel routing =
|
||||
new RoutingModel(data.getLocationNumber(), data.getVehicleNumber(), data.getDepot());
|
||||
|
||||
// Setting the cost function.
|
||||
// [todo]: protect callback from the GC
|
||||
@@ -136,8 +143,7 @@ class Vrp {
|
||||
RoutingSearchParameters search_parameters =
|
||||
RoutingSearchParameters.newBuilder()
|
||||
.mergeFrom(RoutingModel.defaultSearchParameters())
|
||||
.setFirstSolutionStrategy(
|
||||
FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
|
||||
.setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
|
||||
.build();
|
||||
|
||||
Assignment solution = routing.solveWithParameters(search_parameters);
|
||||
@@ -145,5 +151,7 @@ class Vrp {
|
||||
}
|
||||
|
||||
/// @brief Entry point of the program.
|
||||
public static void main(String[] args) throws Exception { solve(); }
|
||||
public static void main(String[] args) throws Exception {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user