From 837cdbbbc8a13fd6771cf8fdde334423d9d226a9 Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Sat, 10 Nov 2018 23:43:32 +0100 Subject: [PATCH] reindent java code --- ...dVehicleRoutingProblemWithTimeWindows.java | 188 ++++++++---------- examples/java/FlowExample.java | 24 +-- examples/java/IntegerProgramming.java | 17 +- examples/java/Knapsack.java | 31 ++- examples/java/LinearAssignmentAPI.java | 22 +- examples/java/LinearProgramming.java | 14 +- examples/java/LsApi.java | 28 ++- examples/java/RabbitsPheasants.java | 19 +- examples/java/Tsp.java | 25 +-- examples/java/Vrp.java | 78 +++----- ortools/sat/samples/BinPackingProblem.java | 6 +- ortools/sat/samples/ChannelingSample.java | 33 ++- ortools/sat/samples/CpIsFun.java | 10 +- ortools/sat/samples/IntervalSample.java | 3 +- ortools/sat/samples/NoOverlapSample.java | 14 +- .../sat/samples/OptionalIntervalSample.java | 4 +- ortools/sat/samples/RabbitsAndPheasants.java | 7 +- ortools/sat/samples/RankingSample.java | 50 ++--- ortools/sat/samples/ReifiedSample.java | 14 +- ortools/sat/samples/SolveAllSolutions.java | 10 +- .../SolveWithIntermediateSolutions.java | 10 +- ortools/sat/samples/StopAfterNSolutions.java | 10 +- 22 files changed, 292 insertions(+), 325 deletions(-) diff --git a/examples/java/CapacitatedVehicleRoutingProblemWithTimeWindows.java b/examples/java/CapacitatedVehicleRoutingProblemWithTimeWindows.java index bdac7ae083..06fe5ef527 100644 --- a/examples/java/CapacitatedVehicleRoutingProblemWithTimeWindows.java +++ b/examples/java/CapacitatedVehicleRoutingProblemWithTimeWindows.java @@ -48,12 +48,10 @@ class Pair { 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. @@ -103,13 +101,9 @@ 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, + 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) { @@ -118,9 +112,13 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows { 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); } } @@ -137,11 +135,8 @@ 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, + private void buildFleet(int numberOfVehicles, int xMax, int yMax, + int startTime, int endTime, int capacity, int costCoefficientMax) { logger.info("Building fleet."); vehicleCapacity = capacity; @@ -156,7 +151,8 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows { randomGenerator.nextInt(yMax + 1))); vehicleStartTime.add(startTime); vehicleEndTime.add(endTime); - vehicleCostCoefficients.add(randomGenerator.nextInt(costCoefficientMax) + 1); + vehicleCostCoefficients.add(randomGenerator.nextInt(costCoefficientMax) + + 1); } } @@ -165,83 +161,84 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows { */ private void solve(final int numberOfOrders, final int numberOfVehicles) { logger.info("Creating model with " + numberOfOrders + " orders and " + - numberOfVehicles + " vehicles."); + 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; - NodeEvaluator2 timeCallback = new NodeEvaluator2(){ - @Override - public long run(int firstIndex, int secondIndex) { - try { - Pair firstLocation = locations.get(firstIndex); - Pair secondLocation = locations.get(secondIndex); - Integer distance = 0; - Integer duration = 0; - distance = Math.abs(firstLocation.first - secondLocation.first) + - Math.abs(firstLocation.second - secondLocation.second); - // Deal with Order duration shipment - if (firstIndex < numberOfOrders) { - // shipment duration - duration += orderDurations.get(firstIndex); - } - return distance + duration; - } catch (Throwable throwed) { - logger.warning(throwed.getMessage()); - return 0; + NodeEvaluator2 timeCallback = new NodeEvaluator2() { + @Override + public long run(int firstIndex, int secondIndex) { + try { + Pair firstLocation = locations.get(firstIndex); + Pair secondLocation = locations.get(secondIndex); + Integer distance = 0; + Integer duration = 0; + distance = Math.abs(firstLocation.first - secondLocation.first) + + Math.abs(firstLocation.second - secondLocation.second); + // Deal with Order duration shipment + if (firstIndex < numberOfOrders) { + // shipment duration + duration += orderDurations.get(firstIndex); } + return distance + duration; + } catch (Throwable throwed) { + logger.warning(throwed.getMessage()); + return 0; } - }; + } + }; model.addDimension(timeCallback, bigNumber, bigNumber, false, "time"); - NodeEvaluator2 demandCallback = new NodeEvaluator2(){ - @Override - public long run(int firstIndex, int secondIndex) { - try { - if (firstIndex < numberOfOrders) { - return orderDemands.get(firstIndex); - } - return 0; - } catch (Throwable throwed) { - logger.warning(throwed.getMessage()); - return 0; + NodeEvaluator2 demandCallback = new NodeEvaluator2() { + @Override + public long run(int firstIndex, int secondIndex) { + try { + if (firstIndex < numberOfOrders) { + return orderDemands.get(firstIndex); } + return 0; + } catch (Throwable throwed) { + logger.warning(throwed.getMessage()); + return 0; } - }; + } + }; model.addDimension(demandCallback, 0, vehicleCapacity, true, "capacity"); // Setting up vehicles for (int vehicle = 0; vehicle < numberOfVehicles; ++vehicle) { final int costCoefficient = vehicleCostCoefficients.get(vehicle); NodeEvaluator2 manhattanCostCallback = new NodeEvaluator2() { - @Override - public long run(int firstIndex, int secondIndex) { - try { - Pair firstLocation = locations.get(firstIndex); - Pair secondLocation = locations.get(secondIndex); - return costCoefficient * - (Math.abs(firstLocation.first - secondLocation.first) + - Math.abs(firstLocation.second - secondLocation.second)); - } catch (Throwable throwed) { - logger.warning(throwed.getMessage()); - return 0; - } + @Override + public long run(int firstIndex, int secondIndex) { + try { + Pair firstLocation = locations.get(firstIndex); + Pair secondLocation = locations.get(secondIndex); + return costCoefficient * + (Math.abs(firstLocation.first - secondLocation.first) + + Math.abs(firstLocation.second - secondLocation.second)); + } catch (Throwable throwed) { + logger.warning(throwed.getMessage()); + return 0; } - }; + } + }; 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); + model.cumulVar(model.nodeToIndex(order), "time") + .setRange(orderTimeWindows.get(order).first, + orderTimeWindows.get(order).second); int[] orders = {order}; model.addDisjunction(orders, orderPenalties.get(order)); } @@ -249,9 +246,10 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows { // Solving RoutingSearchParameters parameters = RoutingSearchParameters.newBuilder() - .mergeFrom(RoutingModel.defaultSearchParameters()) - .setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC) - .build(); + .mergeFrom(RoutingModel.defaultSearchParameters()) + .setFirstSolutionStrategy( + FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC) + .build(); logger.info("Search"); Assignment solution = model.solveWithParameters(parameters); @@ -277,19 +275,19 @@ 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) + - ") -> "; + route += order + " Load(" + solution.value(load) + ") " + + "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) + ")"; + route += order + " Load(" + solution.value(load) + ") " + + "Time(" + solution.min(time) + ", " + solution.max(time) + + ")"; } output += route + "\n"; } @@ -320,21 +318,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, + problem.buildOrders(orders, xMax, yMax, demandMax, timeWindowMin, + timeWindowMax, timeWindowWidth, penaltyMin, penaltyMax); + problem.buildFleet(vehicles, xMax, yMax, startTime, endTime, capacity, costCoefficientMax); problem.solve(orders, vehicles); } diff --git a/examples/java/FlowExample.java b/examples/java/FlowExample.java index b724501034..d00007d3d2 100644 --- a/examples/java/FlowExample.java +++ b/examples/java/FlowExample.java @@ -21,10 +21,7 @@ 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"); @@ -38,8 +35,8 @@ public class FlowExample { 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) { @@ -51,9 +48,9 @@ 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 { @@ -72,11 +69,12 @@ 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: diff --git a/examples/java/IntegerProgramming.java b/examples/java/IntegerProgramming.java index 03b9671ba9..0f3e34eeab 100644 --- a/examples/java/IntegerProgramming.java +++ b/examples/java/IntegerProgramming.java @@ -24,12 +24,12 @@ import com.google.ortools.linearsolver.MPVariable; public class IntegerProgramming { static { System.loadLibrary("jniortools"); } - private static MPSolver createSolver (String solverType) { + private static MPSolver createSolver(String solverType) { try { return new MPSolver("IntegerProgrammingExample", MPSolver.OptimizationProblemType.valueOf(solverType)); } catch (java.lang.IllegalArgumentException e) { - System.err.println("Bad solver type: " + e); + System.err.println("Bad solver type: " + e); return null; } } @@ -71,22 +71,25 @@ public class IntegerProgramming { 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"); diff --git a/examples/java/Knapsack.java b/examples/java/Knapsack.java index 235b4f359d..2e1ab8a128 100644 --- a/examples/java/Knapsack.java +++ b/examples/java/Knapsack.java @@ -22,21 +22,20 @@ 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}; @@ -51,7 +50,5 @@ public class Knapsack { optimalProfit); } - public static void main(String[] args) throws Exception { - Knapsack.solve(); - } + public static void main(String[] args) throws Exception { Knapsack.solve(); } } diff --git a/examples/java/LinearAssignmentAPI.java b/examples/java/LinearAssignmentAPI.java index aa5e4286de..2f35205822 100644 --- a/examples/java/LinearAssignmentAPI.java +++ b/examples/java/LinearAssignmentAPI.java @@ -22,18 +22,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(); @@ -44,11 +41,12 @@ 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."); diff --git a/examples/java/LinearProgramming.java b/examples/java/LinearProgramming.java index 83708fc737..070b1d527d 100644 --- a/examples/java/LinearProgramming.java +++ b/examples/java/LinearProgramming.java @@ -24,7 +24,7 @@ import com.google.ortools.linearsolver.MPVariable; public class LinearProgramming { static { System.loadLibrary("jniortools"); } - private static MPSolver createSolver (String solverType) { + private static MPSolver createSolver(String solverType) { try { return new MPSolver("LinearProgrammingExample", MPSolver.OptimizationProblemType.valueOf(solverType)); @@ -95,10 +95,12 @@ public class LinearProgramming { 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()); @@ -108,7 +110,8 @@ 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()); @@ -121,7 +124,8 @@ 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); diff --git a/examples/java/LsApi.java b/examples/java/LsApi.java index 16e76fe0c7..e2ed5ae42a 100644 --- a/examples/java/LsApi.java +++ b/examples/java/LsApi.java @@ -30,16 +30,10 @@ 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() { @@ -133,10 +127,11 @@ 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); @@ -151,10 +146,11 @@ 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); @@ -169,8 +165,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]; diff --git a/examples/java/RabbitsPheasants.java b/examples/java/RabbitsPheasants.java index 760dbc678f..576715ec02 100644 --- a/examples/java/RabbitsPheasants.java +++ b/examples/java/RabbitsPheasants.java @@ -22,12 +22,10 @@ import java.util.logging.Logger; * */ public class RabbitsPheasants { - private static Logger logger = Logger.getLogger(RabbitsPheasants.class.getName()); - - static { - System.loadLibrary("jniortools"); - } + private static Logger logger = + Logger.getLogger(RabbitsPheasants.class.getName()); + static { System.loadLibrary("jniortools"); } /** * Solves the rabbits + pheasants problem. We are seing 20 heads @@ -43,12 +41,15 @@ public class RabbitsPheasants { 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.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()); diff --git a/examples/java/Tsp.java b/examples/java/Tsp.java index 9dd0663874..67c666bbf9 100644 --- a/examples/java/Tsp.java +++ b/examples/java/Tsp.java @@ -23,9 +23,7 @@ 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) { @@ -55,8 +53,7 @@ class Tsp { } } - static void solve(int size, int forbidden, int seed) - { + static void solve(int size, int forbidden, int seed) { RoutingModel routing = new RoutingModel(size, 1, 0); // Setting the cost function. @@ -80,19 +77,16 @@ 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) - .build(); + .mergeFrom(RoutingModel.defaultSearchParameters()) + .setFirstSolutionStrategy( + FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC) + .build(); Assignment solution = routing.solveWithParameters(search_parameters); if (solution != null) { @@ -101,8 +95,7 @@ class Tsp { // Inspect solution. // Only one route here; otherwise iterate from 0 to routing.vehicles() - 1 int route_number = 0; - for (long node = routing.start(route_number); - !routing.isEnd(node); + for (long node = routing.start(route_number); !routing.isEnd(node); node = solution.value(routing.nextVar(node))) { System.out.print("" + node + " -> "); } diff --git a/examples/java/Vrp.java b/examples/java/Vrp.java index ed77ee912d..809503d880 100644 --- a/examples/java/Vrp.java +++ b/examples/java/Vrp.java @@ -24,37 +24,29 @@ 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 // here we use: 114m x 80m city block // src: https://nyti.ms/2GDoRIe "NY Times: Know Your distance" - int[] cityBlock = {228/2, 80}; - for (int i=0; i < locations_.length; i++) { + int[] cityBlock = {228 / 2, 80}; + for (int i = 0; i < locations_.length; i++) { locations_[i][0] = locations_[i][0] * cityBlock[0]; locations_[i][1] = locations_[i][1] * cityBlock[1]; } } /// @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. @@ -72,9 +64,10 @@ 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]); } } } @@ -87,19 +80,16 @@ 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); + routing.addDimension(new ManhattanDistance(data), + 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 @@ -107,20 +97,15 @@ 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. - for (int i=0; i < data.getVehicleNumber(); ++i) { + for (int i = 0; i < data.getVehicleNumber(); ++i) { System.out.println("Route for Vehicle " + i + ":"); long distance = 0; - for (long index = routing.start(i); - !routing.isEnd(index);) - { + for (long index = routing.start(i); !routing.isEnd(index);) { System.out.print(routing.indexToNode(index) + " -> "); long previousIndex = index; @@ -139,9 +124,7 @@ class Vrp { // Create Routing Model RoutingModel routing = new RoutingModel( - data.getLocationNumber(), - data.getVehicleNumber(), - data.getDepot()); + data.getLocationNumber(), data.getVehicleNumber(), data.getDepot()); // Setting the cost function. // [todo]: protect callback from the GC @@ -151,17 +134,16 @@ class Vrp { // Setting first solution heuristic (cheapest addition). RoutingSearchParameters search_parameters = - RoutingSearchParameters.newBuilder() - .mergeFrom(RoutingModel.defaultSearchParameters()) - .setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC) - .build(); + RoutingSearchParameters.newBuilder() + .mergeFrom(RoutingModel.defaultSearchParameters()) + .setFirstSolutionStrategy( + FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC) + .build(); Assignment solution = routing.solveWithParameters(search_parameters); printSolution(data, routing, solution); } /// @brief Entry point of the program. - public static void main(String[] args) throws Exception { - solve(); - } + public static void main(String[] args) throws Exception { solve(); } } diff --git a/ortools/sat/samples/BinPackingProblem.java b/ortools/sat/samples/BinPackingProblem.java index eafe0d4910..2b229831e3 100644 --- a/ortools/sat/samples/BinPackingProblem.java +++ b/ortools/sat/samples/BinPackingProblem.java @@ -81,7 +81,8 @@ public class BinPackingProblem { // slack[b] => load[b] <= safeCapacity. model.addLessOrEqual(load[b], safeCapacity).onlyEnforceIf(slacks[b]); // not(slack[b]) => load[b] > safeCapacity. - model.addGreaterOrEqual(load[b], safeCapacity + 1).onlyEnforceIf(slacks[b].not()); + model.addGreaterOrEqual(load[b], safeCapacity + 1) + .onlyEnforceIf(slacks[b].not()); } // Maximize sum of slacks. @@ -92,7 +93,8 @@ public class BinPackingProblem { CpSolverStatus status = solver.solve(model); System.out.println("Solve status: " + status); if (status == CpSolverStatus.OPTIMAL) { - System.out.printf("Optimal objective value: %f%n", solver.objectiveValue()); + System.out.printf("Optimal objective value: %f%n", + solver.objectiveValue()); for (int b = 0; b < numBins; ++b) { System.out.printf("load_%d = %d%n", b, solver.value(load[b])); for (int i = 0; i < numItems; ++i) { diff --git a/ortools/sat/samples/ChannelingSample.java b/ortools/sat/samples/ChannelingSample.java index 9804d9d781..fd4c2814ed 100644 --- a/ortools/sat/samples/ChannelingSample.java +++ b/ortools/sat/samples/ChannelingSample.java @@ -51,26 +51,25 @@ public class ChannelingSample { CpSolver solver = new CpSolver(); // Forces the solver to follow the decision strategy exactly. - solver.getParameters().setSearchBranching(SatParameters.SearchBranching.FIXED_SEARCH); + solver.getParameters().setSearchBranching( + SatParameters.SearchBranching.FIXED_SEARCH); // Solves the problem with the printer callback. - solver.searchAllSolutions( - model, - new CpSolverSolutionCallback() { - public CpSolverSolutionCallback init(IntVar[] variables) { - variableArray = variables; - return this; - } + solver.searchAllSolutions(model, new CpSolverSolutionCallback() { + public CpSolverSolutionCallback init(IntVar[] variables) { + variableArray = variables; + return this; + } - @Override - public void onSolutionCallback() { - for (IntVar v : variableArray) { - System.out.printf("%s=%d ", v.getName(), value(v)); - } - System.out.println(); - } + @Override + public void onSolutionCallback() { + for (IntVar v : variableArray) { + System.out.printf("%s=%d ", v.getName(), value(v)); + } + System.out.println(); + } - private IntVar[] variableArray; - }.init(new IntVar[] {x, y, b})); + private IntVar[] variableArray; + }.init(new IntVar[] {x, y, b})); } } diff --git a/ortools/sat/samples/CpIsFun.java b/ortools/sat/samples/CpIsFun.java index c27098ca01..12db1f31b2 100644 --- a/ortools/sat/samples/CpIsFun.java +++ b/ortools/sat/samples/CpIsFun.java @@ -34,9 +34,7 @@ public class CpIsFun { solutionCount++; } - public int getSolutionCount() { - return solutionCount; - } + public int getSolutionCount() { return solutionCount; } private int solutionCount; private final IntVar[] variableArray; @@ -70,7 +68,9 @@ public class CpIsFun { // CP + IS + FUN = TRUE model.addScalProdEqual(new IntVar[] {c, p, i, s, f, u, n, t, r, u, e}, new long[] {base, 1, base, 1, base * base, base, 1, - -base * base * base, -base * base, -base, -1}, 0); + -base * base * base, -base * base, -base, + -1}, + 0); // [END constraints] // [START solve] @@ -83,7 +83,7 @@ public class CpIsFun { System.out.println("Statistics"); System.out.println(" - conflicts : " + solver.numConflicts()); System.out.println(" - branches : " + solver.numBranches()); - System.out.println(" - wall time : " + solver.wallTime() + " s"); + System.out.println(" - wall time : " + solver.wallTime() + " s"); System.out.println(" - solutions : " + cb.getSolutionCount()); } } diff --git a/ortools/sat/samples/IntervalSample.java b/ortools/sat/samples/IntervalSample.java index de74b11893..e6e7adcbd5 100644 --- a/ortools/sat/samples/IntervalSample.java +++ b/ortools/sat/samples/IntervalSample.java @@ -26,7 +26,8 @@ public class IntervalSample { IntVar endVar = model.newIntVar(0, horizon, "end"); // Java code supports IntVar or integer constants in intervals. int duration = 10; - IntervalVar interval = model.newIntervalVar(startVar, duration, endVar, "interval"); + IntervalVar interval = + model.newIntervalVar(startVar, duration, endVar, "interval"); System.out.println(interval); } diff --git a/ortools/sat/samples/NoOverlapSample.java b/ortools/sat/samples/NoOverlapSample.java index b777785667..868f85ca6a 100644 --- a/ortools/sat/samples/NoOverlapSample.java +++ b/ortools/sat/samples/NoOverlapSample.java @@ -18,8 +18,8 @@ import com.google.ortools.sat.IntVar; import com.google.ortools.sat.IntervalVar; /** - * We want to schedule 3 tasks on 3 weeks excluding weekends, making the final day as early as - * possible. + * We want to schedule 3 tasks on 3 weeks excluding weekends, making the final + * day as early as possible. */ public class NoOverlapSample { @@ -53,10 +53,12 @@ public class NoOverlapSample { IntervalVar weekend1 = model.newFixedInterval(12, 2, "weekend1"); IntervalVar weekend2 = model.newFixedInterval(19, 2, "weekend2"); - // No Overlap constraint. This constraint enforces that no two intervals can overlap. - // In this example, as we use 3 fixed intervals that span over weekends, this constraint makes - // sure that all tasks are executed on weekdays. - model.addNoOverlap(new IntervalVar[] {task0, task1, task2, weekend0, weekend1, weekend2}); + // No Overlap constraint. This constraint enforces that no two intervals can + // overlap. In this example, as we use 3 fixed intervals that span over + // weekends, this constraint makes sure that all tasks are executed on + // weekdays. + model.addNoOverlap( + new IntervalVar[] {task0, task1, task2, weekend0, weekend1, weekend2}); // Makespan objective. IntVar obj = model.newIntVar(0, horizon, "makespan"); diff --git a/ortools/sat/samples/OptionalIntervalSample.java b/ortools/sat/samples/OptionalIntervalSample.java index ac740b9338..9144ce0a64 100644 --- a/ortools/sat/samples/OptionalIntervalSample.java +++ b/ortools/sat/samples/OptionalIntervalSample.java @@ -28,8 +28,8 @@ public class OptionalIntervalSample { // Java code supports IntVar or integer constants in intervals. int duration = 10; Literal presence = model.newBoolVar("presence"); - IntervalVar interval = - model.newOptionalIntervalVar(startVar, duration, endVar, presence, "interval"); + IntervalVar interval = model.newOptionalIntervalVar( + startVar, duration, endVar, presence, "interval"); System.out.println(interval); } diff --git a/ortools/sat/samples/RabbitsAndPheasants.java b/ortools/sat/samples/RabbitsAndPheasants.java index 5bb2681220..b59e0b438a 100644 --- a/ortools/sat/samples/RabbitsAndPheasants.java +++ b/ortools/sat/samples/RabbitsAndPheasants.java @@ -17,8 +17,8 @@ import com.google.ortools.sat.CpSolver; import com.google.ortools.sat.IntVar; /** - * In a field of rabbits and pheasants, there are 20 heads and 56 legs. How many rabbits and - * pheasants are there? + * In a field of rabbits and pheasants, there are 20 heads and 56 legs. How many + * rabbits and pheasants are there? */ public class RabbitsAndPheasants { @@ -40,7 +40,8 @@ public class RabbitsAndPheasants { CpSolverStatus status = solver.solve(model); if (status == CpSolverStatus.FEASIBLE) { - System.out.println(solver.value(r) + " rabbits, and " + solver.value(p) + " pheasants"); + System.out.println(solver.value(r) + " rabbits, and " + solver.value(p) + + " pheasants"); } } } diff --git a/ortools/sat/samples/RankingSample.java b/ortools/sat/samples/RankingSample.java index 624a50acb4..e6abed8a42 100644 --- a/ortools/sat/samples/RankingSample.java +++ b/ortools/sat/samples/RankingSample.java @@ -20,15 +20,16 @@ import com.google.ortools.sat.Literal; import java.util.ArrayList; import java.util.List; -// This code takes a list of interval variables in a noOverlap constraint, and a parallel list of -// integer variables and enforces the following constraint: +// This code takes a list of interval variables in a noOverlap constraint, and a +// parallel list of integer variables and enforces the following constraint: // - rank[i] == -1 iff interval[i] is not active. // - rank[i] == number of active intervals that precede interval[i]. public class RankingSample { static { System.loadLibrary("jniortools"); } - static void rankTasks(CpModel model, IntVar[] starts, Literal[] presences, IntVar[] ranks) { + static void rankTasks(CpModel model, IntVar[] starts, Literal[] presences, + IntVar[] ranks) { int numTasks = starts.length; // Creates precedence variables between pairs of intervals. @@ -41,7 +42,8 @@ public class RankingSample { IntVar prec = model.newBoolVar(String.format("%d before %d", i, j)); precedences[i][j] = prec; // Ensure that task i precedes task j if prec is true. - model.addLessOrEqualWithOffset(starts[i], starts[j], 1).onlyEnforceIf(prec); + model.addLessOrEqualWithOffset(starts[i], starts[j], 1) + .onlyEnforceIf(prec); } } } @@ -64,9 +66,10 @@ public class RankingSample { // i precedes j or j precedes i or at least one interval is not // performed. model.addBoolOr(list.toArray(new Literal[0])); - // For efficiency, we add a redundant constraint declaring that only one of i precedes j and - // j precedes i are true. This will speed up the solve because the reason of this - // propagation is shorter that using interval bounds is true. + // For efficiency, we add a redundant constraint declaring that only one + // of i precedes j and j precedes i are true. This will speed up the + // solve because the reason of this propagation is shorter that using + // interval bounds is true. model.addImplication(precedences[i][j], precedences[j][i].not()); model.addImplication(precedences[j][i], precedences[i][j].not()); } @@ -77,7 +80,7 @@ public class RankingSample { IntVar[] vars = new IntVar[numTasks + 1]; int[] coefs = new int[numTasks + 1]; for (int j = 0; j < numTasks; ++j) { - vars[j] = (IntVar) precedences[j][i]; + vars[j] = (IntVar)precedences[j][i]; coefs[j] = 1; } vars[numTasks] = ranks[i]; @@ -106,13 +109,13 @@ public class RankingSample { int duration = t + 1; ends[t] = model.newIntVar(0, horizon, "end_" + t); if (t < numTasks / 2) { - intervals[t] = model.newIntervalVar(starts[t], duration, ends[t], "interval_" + t); + intervals[t] = + model.newIntervalVar(starts[t], duration, ends[t], "interval_" + t); presences[t] = trueVar; } else { presences[t] = model.newBoolVar("presence_" + t); - intervals[t] = - model.newOptionalIntervalVar( - starts[t], duration, ends[t], presences[t], "o_interval_" + t); + intervals[t] = model.newOptionalIntervalVar( + starts[t], duration, ends[t], presences[t], "o_interval_" + t); } // The rank will be -1 iff the task is not performed. @@ -133,17 +136,17 @@ public class RankingSample { for (int t = 0; t < numTasks; ++t) { model.addLessOrEqual(ends[t], makespan).onlyEnforceIf(presences[t]); } - // The objective function is a mix of a fixed gain per task performed, and a fixed cost for each - // additional day of activity. - // The solver will balance both cost and gain and minimize makespan * per-day-penalty - number - // of tasks performed * per-task-gain. + // The objective function is a mix of a fixed gain per task performed, and a + // fixed cost for each additional day of activity. The solver will balance + // both cost and gain and minimize makespan * per-day-penalty - number of + // tasks performed * per-task-gain. // - // On this problem, as the fixed cost is less that the duration of the last interval, the solver - // will not perform the last interval. + // On this problem, as the fixed cost is less that the duration of the last + // interval, the solver will not perform the last interval. IntVar[] objectiveVars = new IntVar[numTasks + 1]; int[] objectiveCoefs = new int[numTasks + 1]; for (int t = 0; t < numTasks; ++t) { - objectiveVars[t] = (IntVar) presences[t]; + objectiveVars[t] = (IntVar)presences[t]; objectiveCoefs[t] = -7; } objectiveVars[numTasks] = makespan; @@ -159,12 +162,11 @@ public class RankingSample { System.out.println("Makespan: " + solver.value(makespan)); for (int t = 0; t < numTasks; ++t) { if (solver.booleanValue(presences[t])) { - System.out.printf( - "Task %d starts at %d with rank %d%n", - t, solver.value(starts[t]), solver.value(ranks[t])); + System.out.printf("Task %d starts at %d with rank %d%n", t, + solver.value(starts[t]), solver.value(ranks[t])); } else { - System.out.printf( - "Task %d in not performed and ranked at %d%n", t, solver.value(ranks[t])); + System.out.printf("Task %d in not performed and ranked at %d%n", t, + solver.value(ranks[t])); } } } else { diff --git a/ortools/sat/samples/ReifiedSample.java b/ortools/sat/samples/ReifiedSample.java index 55cd984180..a8d2b1adcd 100644 --- a/ortools/sat/samples/ReifiedSample.java +++ b/ortools/sat/samples/ReifiedSample.java @@ -16,14 +16,16 @@ import com.google.ortools.sat.IntVar; import com.google.ortools.sat.Literal; /** - * Reification is the action of associating a Boolean variable to a constraint. This boolean - * enforces or prohibits the constraint according to the value the Boolean variable is fixed to. + * Reification is the action of associating a Boolean variable to a constraint. + * This boolean enforces or prohibits the constraint according to the value the + * Boolean variable is fixed to. * - *

Half-reification is defined as a simple implication: If the Boolean variable is true, then the - * constraint holds, instead of an complete equivalence. + *

Half-reification is defined as a simple implication: If the Boolean + * variable is true, then the constraint holds, instead of an complete + * equivalence. * - *

The SAT solver offers half-reification. To implement full reification, two half-reified - * constraints must be used. + *

The SAT solver offers half-reification. To implement full reification, two + * half-reified constraints must be used. */ public class ReifiedSample { diff --git a/ortools/sat/samples/SolveAllSolutions.java b/ortools/sat/samples/SolveAllSolutions.java index 8faf7c3a0d..7233ba62b2 100644 --- a/ortools/sat/samples/SolveAllSolutions.java +++ b/ortools/sat/samples/SolveAllSolutions.java @@ -27,16 +27,15 @@ public class SolveAllSolutions { @Override public void onSolutionCallback() { - System.out.printf("Solution #%d: time = %.02f s%n", solutionCount, wallTime()); + System.out.printf("Solution #%d: time = %.02f s%n", solutionCount, + wallTime()); for (IntVar v : variableArray) { System.out.printf(" %s = %d%n", v.getName(), value(v)); } solutionCount++; } - public int getSolutionCount() { - return solutionCount; - } + public int getSolutionCount() { return solutionCount; } private int solutionCount; private final IntVar[] variableArray; @@ -56,7 +55,8 @@ public class SolveAllSolutions { // Create a solver and solve the model. CpSolver solver = new CpSolver(); - VarArraySolutionPrinter cb = new VarArraySolutionPrinter(new IntVar[] {x, y, z}); + VarArraySolutionPrinter cb = + new VarArraySolutionPrinter(new IntVar[] {x, y, z}); solver.searchAllSolutions(model, cb); System.out.println(cb.getSolutionCount() + " solutions found."); diff --git a/ortools/sat/samples/SolveWithIntermediateSolutions.java b/ortools/sat/samples/SolveWithIntermediateSolutions.java index 5a5dc6bcbb..3f012caff8 100644 --- a/ortools/sat/samples/SolveWithIntermediateSolutions.java +++ b/ortools/sat/samples/SolveWithIntermediateSolutions.java @@ -20,14 +20,16 @@ public class SolveWithIntermediateSolutions { static { System.loadLibrary("jniortools"); } - static class VarArraySolutionPrinterWithObjective extends CpSolverSolutionCallback { + static class VarArraySolutionPrinterWithObjective + extends CpSolverSolutionCallback { public VarArraySolutionPrinterWithObjective(IntVar[] variables) { variableArray = variables; } @Override public void onSolutionCallback() { - System.out.printf("Solution #%d: time = %.02f s%n", solutionCount, wallTime()); + System.out.printf("Solution #%d: time = %.02f s%n", solutionCount, + wallTime()); System.out.printf(" objective value = %f%n", objectiveValue()); for (IntVar v : variableArray) { System.out.printf(" %s = %d%n", v.getName(), value(v)); @@ -35,9 +37,7 @@ public class SolveWithIntermediateSolutions { solutionCount++; } - public int getSolutionCount() { - return solutionCount; - } + public int getSolutionCount() { return solutionCount; } private int solutionCount; private final IntVar[] variableArray; diff --git a/ortools/sat/samples/StopAfterNSolutions.java b/ortools/sat/samples/StopAfterNSolutions.java index 23a3439b8b..0e43ad8566 100644 --- a/ortools/sat/samples/StopAfterNSolutions.java +++ b/ortools/sat/samples/StopAfterNSolutions.java @@ -20,7 +20,8 @@ public class StopAfterNSolutions { static { System.loadLibrary("jniortools"); } - static class VarArraySolutionPrinterWithLimit extends CpSolverSolutionCallback { + static class VarArraySolutionPrinterWithLimit + extends CpSolverSolutionCallback { public VarArraySolutionPrinterWithLimit(IntVar[] variables, int limit) { variableArray = variables; solutionLimit = limit; @@ -28,7 +29,8 @@ public class StopAfterNSolutions { @Override public void onSolutionCallback() { - System.out.printf("Solution #%d: time = %.02f s%n", solutionCount, wallTime()); + System.out.printf("Solution #%d: time = %.02f s%n", solutionCount, + wallTime()); for (IntVar v : variableArray) { System.out.printf(" %s = %d%n", v.getName(), value(v)); } @@ -39,9 +41,7 @@ public class StopAfterNSolutions { } } - public int getSolutionCount() { - return solutionCount; - } + public int getSolutionCount() { return solutionCount; } private int solutionCount; private final IntVar[] variableArray;