From cebbe3a60d71a9c967c16d3f0fc1fdc971aae258 Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Thu, 27 Nov 2014 09:34:28 +0100 Subject: [PATCH] add CircleExample --- .../java/jsprit/examples/CircleExample.java | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 jsprit-examples/src/main/java/jsprit/examples/CircleExample.java diff --git a/jsprit-examples/src/main/java/jsprit/examples/CircleExample.java b/jsprit-examples/src/main/java/jsprit/examples/CircleExample.java new file mode 100644 index 00000000..6aa64a9a --- /dev/null +++ b/jsprit-examples/src/main/java/jsprit/examples/CircleExample.java @@ -0,0 +1,98 @@ +/******************************************************************************* + * Copyright (C) 2014 Stefan Schroeder + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + ******************************************************************************/ + +package jsprit.examples; + +import jsprit.analysis.toolbox.AlgorithmEventsRecorder; +import jsprit.analysis.toolbox.AlgorithmEventsViewer; +import jsprit.analysis.toolbox.GraphStreamViewer; +import jsprit.analysis.toolbox.Plotter; +import jsprit.core.algorithm.VehicleRoutingAlgorithm; +import jsprit.core.algorithm.box.GreedySchrimpfFactory; +import jsprit.core.problem.VehicleRoutingProblem; +import jsprit.core.problem.job.Service; +import jsprit.core.problem.solution.VehicleRoutingProblemSolution; +import jsprit.core.problem.vehicle.VehicleImpl; +import jsprit.core.util.Coordinate; +import jsprit.core.util.Solutions; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collection; + +/** + * Created by schroeder on 27.11.14. + */ +public class CircleExample { + + public static Collection createCoordinates(double center_x, double center_y, double radius, double step){ + Collection coords = new ArrayList(); + for(double theta = 0; theta < 2*Math.PI; theta += step){ + double x = center_x + radius*Math.cos(theta); + double y = center_y - radius*Math.sin(theta); + coords.add(Coordinate.newInstance(x,y)); + } + return coords; + } + + public static void main(String[] args) { + File dir = new File("output"); + // if the directory does not exist, create it + if (!dir.exists()){ + System.out.println("creating directory ./output"); + boolean result = dir.mkdir(); + if(result) System.out.println("./output created"); + } + + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(Coordinate.newInstance(0,0)).build(); + vrpBuilder.addVehicle(v); + + double step = 2*Math.PI/50.; + Collection circle = createCoordinates(0,0,20,step); + int id = 1; + for(Coordinate c : circle){ + Service s = Service.Builder.newInstance(Integer.toString(id)).setCoord(c).build(); + vrpBuilder.addJob(s); + id++; + } + VehicleRoutingProblem vrp = vrpBuilder.build(); + + //only works with latest snapshot: 1.4.3 + AlgorithmEventsRecorder eventsRecorder = new AlgorithmEventsRecorder(vrp,new File("output/events.dgs.gz")); + eventsRecorder.setRecordingRange(0,50); + + VehicleRoutingAlgorithm vra = new GreedySchrimpfFactory().createAlgorithm(vrp); + vra.setMaxIterations(50); + + //only works with latest snapshot: 1.4.3 + vra.addListener(eventsRecorder); + + VehicleRoutingProblemSolution solution = Solutions.bestOf(vra.searchSolutions()); + + new Plotter(vrp,solution).plot("output/circle.png","circleProblem"); + new GraphStreamViewer(vrp,solution).display(); + + //only works with latest snapshot: 1.4.3 + AlgorithmEventsViewer viewer = new AlgorithmEventsViewer(); + viewer.setRuinDelay(16); + viewer.setRecreationDelay(8); + viewer.display("output/events.dgs.gz"); + + } + +}