diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/AbstractInsertionStrategy.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/AbstractInsertionStrategy.java new file mode 100644 index 00000000..ab32c40c --- /dev/null +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/AbstractInsertionStrategy.java @@ -0,0 +1,112 @@ +/******************************************************************************* + * 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.core.algorithm.recreate; + + +import jsprit.core.algorithm.recreate.listener.InsertionListener; +import jsprit.core.algorithm.recreate.listener.InsertionListeners; +import jsprit.core.problem.VehicleRoutingProblem; +import jsprit.core.problem.driver.Driver; +import jsprit.core.problem.job.Job; +import jsprit.core.problem.solution.route.VehicleRoute; +import jsprit.core.problem.vehicle.Vehicle; +import jsprit.core.util.RandomNumberGeneration; + +import java.util.Collection; +import java.util.Collections; +import java.util.Random; + +public abstract class AbstractInsertionStrategy implements InsertionStrategy{ + + protected class Insertion { + + private final VehicleRoute route; + + private final InsertionData insertionData; + + public Insertion(VehicleRoute vehicleRoute, InsertionData insertionData) { + super(); + this.route = vehicleRoute; + this.insertionData = insertionData; + } + + public VehicleRoute getRoute() { + return route; + } + + public InsertionData getInsertionData() { + return insertionData; + } + + } + + protected Random random = RandomNumberGeneration.getRandom(); + + protected final static double NO_NEW_DEPARTURE_TIME_YET = -12345.12345; + + protected final static Vehicle NO_NEW_VEHICLE_YET = null; + + protected final static Driver NO_NEW_DRIVER_YET = null; + + private InsertionListeners insertionsListeners; + + private Inserter inserter; + + protected VehicleRoutingProblem vrp; + + public AbstractInsertionStrategy(VehicleRoutingProblem vrp) { + this.insertionsListeners = new InsertionListeners(); + this.vrp = vrp; + inserter = new Inserter(insertionsListeners, vrp); + } + + public void setRandom(Random random) { + this.random = random; + } + + @Override + public Collection insertJobs(Collection vehicleRoutes, Collection unassignedJobs){ + insertionsListeners.informInsertionStarts(vehicleRoutes, unassignedJobs); + Collection badJobs = insertUnassignedJobs(vehicleRoutes,unassignedJobs); + insertionsListeners.informInsertionEndsListeners(vehicleRoutes); + return badJobs; + } + + public abstract Collection insertUnassignedJobs(Collection vehicleRoutes, Collection unassignedJobs); + + @Override + public void removeListener(InsertionListener insertionListener) { + insertionsListeners.removeListener(insertionListener); + } + + @Override + public Collection getListeners() { + return Collections.unmodifiableCollection(insertionsListeners.getListeners()); + } + + @Override + public void addListener(InsertionListener insertionListener) { + insertionsListeners.addListener(insertionListener); + + } + + protected void insertJob(Job unassignedJob, InsertionData iData, VehicleRoute inRoute){ + inserter.insertJob(unassignedJob, iData, inRoute); + } + +} diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/ruin/AbstractRuinStrategy.java b/jsprit-core/src/main/java/jsprit/core/algorithm/ruin/AbstractRuinStrategy.java new file mode 100644 index 00000000..0583830e --- /dev/null +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/ruin/AbstractRuinStrategy.java @@ -0,0 +1,94 @@ +/******************************************************************************* + * 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.core.algorithm.ruin; + + +import jsprit.core.algorithm.ruin.listener.RuinListener; +import jsprit.core.algorithm.ruin.listener.RuinListeners; +import jsprit.core.problem.job.Job; +import jsprit.core.problem.solution.route.VehicleRoute; +import jsprit.core.util.RandomNumberGeneration; + +import java.util.Collection; +import java.util.Random; + +public abstract class AbstractRuinStrategy implements RuinStrategy{ + + private RuinListeners ruinListeners; + + protected Random random = RandomNumberGeneration.getRandom(); + + public void setRandom(Random random) { + this.random = random; + } + + protected AbstractRuinStrategy() { + ruinListeners = new RuinListeners(); + } + + @Override + public Collection ruin(Collection vehicleRoutes){ + ruinListeners.ruinStarts(vehicleRoutes); + Collection unassigned = ruinRoutes(vehicleRoutes); + ruinListeners.ruinEnds(vehicleRoutes,unassigned); + return unassigned; + } + + public abstract Collection ruinRoutes(Collection vehicleRoutes); + + @Override + public Collection ruin(Collection vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved){ + ruinListeners.ruinStarts(vehicleRoutes); + Collection unassigned = ruinRoutes(vehicleRoutes, targetJob, nOfJobs2BeRemoved); + ruinListeners.ruinEnds(vehicleRoutes,unassigned); + return unassigned; + } + + public abstract Collection ruinRoutes(Collection vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved); + + @Override + public void addListener(RuinListener ruinListener) { + ruinListeners.addListener(ruinListener); + } + + @Override + public void removeListener(RuinListener ruinListener) { + ruinListeners.removeListener(ruinListener); + } + + @Override + public Collection getListeners() { + return ruinListeners.getListeners(); + } + + protected void removeJob(Job job, Collection vehicleRoutes) { + for (VehicleRoute route : vehicleRoutes) { + if (removeJob(job, route)) break; + } + } + + protected boolean removeJob(Job job, VehicleRoute route) { + boolean removed; + removed = route.getTourActivities().removeJob(job); + if (removed) { + ruinListeners.removed(job,route); + return true; + } + return false; + } +}