Created
September 19, 2015 16:44
-
-
Save frodrigo/8003bb3d84b577b4e088 to your computer and use it in GitHub Desktop.
sprit mapotempo optimizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
import jsprit.analysis.toolbox.AlgorithmSearchProgressChartListener; | |
import jsprit.core.algorithm.VehicleRoutingAlgorithm; | |
import jsprit.core.algorithm.box.Jsprit; | |
import jsprit.core.problem.AbstractVehicle; | |
import jsprit.core.problem.Location; | |
import jsprit.core.problem.VehicleRoutingProblem; | |
import jsprit.core.problem.VehicleRoutingProblem.FleetSize; | |
import jsprit.core.problem.cost.VehicleRoutingTransportCosts; | |
import jsprit.core.problem.job.Job; | |
import jsprit.core.problem.job.Service; | |
import jsprit.core.problem.solution.VehicleRoutingProblemSolution; | |
import jsprit.core.problem.vehicle.VehicleImpl; | |
import jsprit.core.problem.vehicle.VehicleType; | |
import jsprit.core.problem.vehicle.VehicleTypeImpl; | |
import jsprit.core.reporting.SolutionPrinter; | |
import jsprit.core.util.Solutions; | |
import jsprit.core.util.VehicleRoutingTransportCostsMatrix; | |
public class Run { | |
public static void main(String[] args) throws IOException { | |
Run run = new Run(); | |
run.readFile("b"); | |
run.run(); | |
} | |
private VehicleRoutingTransportCosts costMatrix; | |
private List<Job> jobs = new ArrayList<Job>(); | |
private void readFile(String path) throws IOException { | |
int n = 0; | |
int matrixSize = 0; | |
int restSize = 0; | |
VehicleRoutingTransportCostsMatrix.Builder costMatrixBuilder = VehicleRoutingTransportCostsMatrix.Builder | |
.newInstance(true); | |
for (String line : Files.readAllLines(Paths.get(path))) { | |
if (n == 0) { | |
matrixSize = Integer.valueOf(line); | |
} else if (n == 1) { | |
restSize = Integer.valueOf(line); | |
} else if (n >= 2 && n < 2 + matrixSize) { | |
int nn = 0; | |
for (String f : line.split(" ")) { | |
if (nn % 2 == 0) { | |
costMatrixBuilder.addTransportDistance(String.valueOf(n - 2), String.valueOf(nn / 2), | |
Float.valueOf(f)); | |
} else { | |
costMatrixBuilder.addTransportTime(String.valueOf(n - 2), String.valueOf(nn / 2), | |
Float.valueOf(f)); | |
} | |
nn++; | |
} | |
} else if (n >= 2 + matrixSize && n < 2 + matrixSize + matrixSize) { | |
String[] l = line.split(" "); | |
// TODO open TW | |
// TODO close TW | |
// TODO soft window | |
String id = String.valueOf(n - 2 - matrixSize); | |
jobs.add(Service.Builder.newInstance(id).setLocation(Location.newInstance(id)) | |
.setServiceTime(Double.valueOf(l[2])).build()); | |
} else if (n >= 2 + matrixSize + matrixSize && n < 2 + matrixSize + matrixSize + restSize) { | |
String[] l = line.split(" "); | |
// TODO open TW | |
// TODO close TW | |
// TODO soft window | |
String id = String.valueOf(n - 2 - matrixSize); | |
jobs.add(Service.Builder.newInstance(id).setServiceTime(Double.valueOf(l[2])).build()); | |
} | |
n++; | |
} | |
costMatrix = costMatrixBuilder.build(); | |
} | |
private void run() { | |
// specify type of both vehicles | |
VehicleType vehicleType = VehicleTypeImpl.Builder.newInstance("vehicleType").addCapacityDimension(0, 30) | |
.build(); | |
// FIXME setEndLocation mettre la fin | |
AbstractVehicle vehicle1 = VehicleImpl.Builder.newInstance("vehicle1Id").setType(vehicleType) | |
.setStartLocation(Location.newInstance("0")).setEndLocation(Location.newInstance("0")).build(); | |
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); | |
vrpBuilder.addAllJobs(jobs).addVehicle(vehicle1).setRoutingCost(costMatrix); | |
vrpBuilder.setFleetSize(FleetSize.FINITE); | |
VehicleRoutingProblem problem = vrpBuilder.build(); | |
VehicleRoutingAlgorithm algorithm = Jsprit.createAlgorithm(problem); | |
algorithm.setMaxIterations(50); | |
algorithm.addListener(new AlgorithmSearchProgressChartListener("progress.png")); | |
Collection<VehicleRoutingProblemSolution> solutions = algorithm.searchSolutions(); | |
VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions); | |
SolutionPrinter.print(bestSolution); | |
System.out.println(bestSolution.getRoutes().iterator().next().getDepartureTime()); | |
System.out.println(bestSolution.getRoutes().iterator().next().getEnd().getArrTime() / 60 / 60); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment