reindent java code

This commit is contained in:
Laurent Perron
2018-11-10 23:43:32 +01:00
parent 333cf82f0f
commit 837cdbbbc8
22 changed files with 292 additions and 325 deletions

View File

@@ -48,12 +48,10 @@ 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.
@@ -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<Integer, Integer> firstLocation = locations.get(firstIndex);
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);
// 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<Integer, Integer> firstLocation = locations.get(firstIndex);
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);
// 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<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));
} catch (Throwable throwed) {
logger.warning(throwed.getMessage());
return 0;
}
@Override
public long run(int firstIndex, int secondIndex) {
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));
} 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);
}