From a022a2c262642af1167dac0dff8d8cbbd1d12b82 Mon Sep 17 00:00:00 2001 From: Stefan Schroeder <4sschroeder@gmail.com> Date: Tue, 18 Jun 2013 09:29:06 +0200 Subject: [PATCH] add RefuseCollectionExample --- .../input/RefuseCollectionExample_Distances | 46 ++++ .../input/RefuseCollectionExample_Quantities | 10 + .../examples/CompareAlgorithmExample.java | 22 +- .../examples/RefuseCollectionExample.java | 241 ++++++++++++++++++ .../src/main/java/examples/SimpleExample.java | 20 ++ 5 files changed, 333 insertions(+), 6 deletions(-) create mode 100644 jsprit-examples/input/RefuseCollectionExample_Distances create mode 100644 jsprit-examples/input/RefuseCollectionExample_Quantities create mode 100644 jsprit-examples/src/main/java/examples/RefuseCollectionExample.java diff --git a/jsprit-examples/input/RefuseCollectionExample_Distances b/jsprit-examples/input/RefuseCollectionExample_Distances new file mode 100644 index 00000000..ad147506 --- /dev/null +++ b/jsprit-examples/input/RefuseCollectionExample_Distances @@ -0,0 +1,46 @@ +from,to,distance +1,2,25 +1,3,43 +1,4,57 +1,5,43 +1,6,61 +1,7,29 +1,8,41 +1,9,48 +1,10,71 +2,3,29 +2,4,34 +2,5,43 +2,6,68 +2,7,49 +2,8,66 +2,9,48 +2,10,91 +3,4,52 +3,5,72 +3,6,96 +3,7,72 +3,8,81 +3,9,89 +3,10,114 +4,5,45 +4,6,71 +4,7,71 +4,8,95 +4,9,99 +4,10,108 +5,6,27 +5,7,36 +5,8,65 +5,9,65 +5,10,65 +6,7,40 +6,8,66 +6,9,62 +6,10,46 +7,8,31 +7,9,31 +7,10,43 +8,9,11 +8,10,46 +9,10,36 \ No newline at end of file diff --git a/jsprit-examples/input/RefuseCollectionExample_Quantities b/jsprit-examples/input/RefuseCollectionExample_Quantities new file mode 100644 index 00000000..4645bac0 --- /dev/null +++ b/jsprit-examples/input/RefuseCollectionExample_Quantities @@ -0,0 +1,10 @@ +node,quantity +2,4 +3,6 +4,5 +5,4 +6,7 +7,3 +8,5 +9,4 +10,4 \ No newline at end of file diff --git a/jsprit-examples/src/main/java/examples/CompareAlgorithmExample.java b/jsprit-examples/src/main/java/examples/CompareAlgorithmExample.java index 9a8c68b5..65ce758b 100644 --- a/jsprit-examples/src/main/java/examples/CompareAlgorithmExample.java +++ b/jsprit-examples/src/main/java/examples/CompareAlgorithmExample.java @@ -1,11 +1,19 @@ /******************************************************************************* - * Copyright (c) 2011 Stefan Schroeder. - * eMail: stefan.schroeder@kit.edu + * Copyright (C) 2013 Stefan Schroeder * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the GNU Public License v2.0 - * which accompanies this distribution, and is available at - * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Contributors: * Stefan Schroeder - initial API and implementation @@ -71,6 +79,8 @@ public class CompareAlgorithmExample { vra_greedy.searchSolutions(); + + } } diff --git a/jsprit-examples/src/main/java/examples/RefuseCollectionExample.java b/jsprit-examples/src/main/java/examples/RefuseCollectionExample.java new file mode 100644 index 00000000..02138a14 --- /dev/null +++ b/jsprit-examples/src/main/java/examples/RefuseCollectionExample.java @@ -0,0 +1,241 @@ +/******************************************************************************* + * Copyright (C) 2013 Stefan Schroeder + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributors: + * Stefan Schroeder - initial API and implementation + ******************************************************************************/ +package examples; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import util.Solutions; +import algorithms.GreedySchrimpfFactory; +import analysis.SolutionPrinter; +import analysis.SolutionPrinter.Print; +import basics.Service; +import basics.VehicleRoutingAlgorithm; +import basics.VehicleRoutingProblem; +import basics.VehicleRoutingProblem.FleetSize; +import basics.VehicleRoutingProblemSolution; +import basics.costs.VehicleRoutingTransportCosts; +import basics.io.VrpXMLWriter; +import basics.route.Driver; +import basics.route.Vehicle; +import basics.route.VehicleImpl; +import basics.route.VehicleImpl.VehicleType; + + +/** + * This example is base on + * http://web.mit.edu/urban_or_book/www/book/chapter6/6.4.12.html + * + * @author stefan schroeder + * + */ +public class RefuseCollectionExample { + + static class RelationKey { + + static RelationKey newKey(String from, String to){ + int fromInt = Integer.parseInt(from); + int toInt = Integer.parseInt(to); + if(fromInt < toInt){ + return new RelationKey(from, to); + } + else { + return new RelationKey(to, from); + } + } + + final String from; + final String to; + + public RelationKey(String from, String to) { + super(); + this.from = from; + this.to = to; + } + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((from == null) ? 0 : from.hashCode()); + result = prime * result + ((to == null) ? 0 : to.hashCode()); + return result; + } + + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + RelationKey other = (RelationKey) obj; + if (from == null) { + if (other.from != null) + return false; + } else if (!from.equals(other.from)) + return false; + if (to == null) { + if (other.to != null) + return false; + } else if (!to.equals(other.to)) + return false; + return true; + } + } + + static class RoutingCosts implements VehicleRoutingTransportCosts { + + private Map distances; + + public RoutingCosts(Map distances) { + super(); + this.distances = distances; + } + + @Override + public double getTransportTime(String fromId, String toId, double departureTime, Driver driver, Vehicle vehicle) { + return getTransportCost(fromId, toId, departureTime, driver, vehicle); + } + + @Override + public double getBackwardTransportTime(String fromId, String toId, double arrivalTime, Driver driver, Vehicle vehicle) { + return getTransportCost(fromId, toId, arrivalTime, driver, vehicle); + } + + @Override + public double getTransportCost(String fromId, String toId,double departureTime, Driver driver, Vehicle vehicle) { + if(fromId.equals(toId)) return 0.0; + RelationKey key = RelationKey.newKey(fromId, toId); + return distances.get(key); + } + + @Override + public double getBackwardTransportCost(String fromId, String toId,double arrivalTime, Driver driver, Vehicle vehicle) { + return getTransportCost(fromId, toId, arrivalTime, driver, vehicle); + } + + } + + /** + * @param args + * @throws IOException + */ + public static void main(String[] args) throws IOException { + + /* + * create vehicle-type and vehicle + */ + VehicleType.Builder typeBuilder = VehicleType.Builder.newInstance("vehicle-type", 23); + typeBuilder.setCostPerDistance(1.0); + VehicleType bigType = typeBuilder.build(); + + VehicleImpl.VehicleBuilder vehicleBuilder = VehicleImpl.VehicleBuilder.newInstance("vehicle"); + vehicleBuilder.setLocationId("1"); + vehicleBuilder.setType(bigType); + Vehicle bigVehicle = vehicleBuilder.build(); + + /* + * start building the problem + */ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + vrpBuilder.setFleetSize(FleetSize.INFINITE); + vrpBuilder.addVehicle(bigVehicle); + + /* + * read demand quantities + */ + readDemandQuantities(vrpBuilder); + + /* + * read distances + */ + Map distances = new HashMap(); + readDistances(distances); + + VehicleRoutingTransportCosts routingCosts = new RoutingCosts(distances); + vrpBuilder.setRoutingCost(routingCosts); + + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = new GreedySchrimpfFactory().createAlgorithm(vrp); + vra.setPrematureBreak(10); + Collection solutions = vra.searchSolutions(); + + SolutionPrinter.print(Solutions.getBest(solutions),Print.VERBOSE); + + new VrpXMLWriter(vrp, solutions).write("output/refuseCollectionExampleSolution.xml"); + + } + + private static void readDemandQuantities(VehicleRoutingProblem.Builder vrpBuilder) throws FileNotFoundException, IOException { + BufferedReader reader = new BufferedReader(new FileReader(new File("input/RefuseCollectionExample_Quantities"))); + String line = null; + boolean firstLine = true; + while((line = reader.readLine()) != null){ + if(firstLine) { + firstLine = false; + continue; + } + String[] lineTokens = line.split(","); + /* + * build service + */ + Service service = Service.Builder.newInstance(lineTokens[0], Integer.parseInt(lineTokens[1])).setLocationId(lineTokens[0]).build(); + /* + * and add it to problem + */ + vrpBuilder.addService(service); + } + reader.close(); + } + + private static void readDistances(Map distances) throws IOException { + BufferedReader reader = new BufferedReader(new FileReader(new File("input/RefuseCollectionExample_Distances"))); + String line = null; + boolean firstLine = true; + while((line = reader.readLine()) != null){ + if(firstLine) { + firstLine = false; + continue; + } + String[] lineTokens = line.split(","); + RelationKey key = RelationKey.newKey(lineTokens[0],lineTokens[1]); + distances.put(key, Integer.parseInt(lineTokens[2])); + } + reader.close(); + + } + +} diff --git a/jsprit-examples/src/main/java/examples/SimpleExample.java b/jsprit-examples/src/main/java/examples/SimpleExample.java index a378987c..a0bf60dc 100644 --- a/jsprit-examples/src/main/java/examples/SimpleExample.java +++ b/jsprit-examples/src/main/java/examples/SimpleExample.java @@ -1,3 +1,23 @@ +/******************************************************************************* + * Copyright (C) 2013 Stefan Schroeder + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributors: + * Stefan Schroeder - initial API and implementation + ******************************************************************************/ package examples; import java.util.Collection;