1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

simplify adding new strategies by moving all general stuff to abstract ruin and insertion class

This commit is contained in:
oblonski 2014-11-21 22:42:51 +01:00
parent 29756e8b1a
commit 5d7d662124
2 changed files with 206 additions and 0 deletions

View file

@ -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 <http://www.gnu.org/licenses/>.
******************************************************************************/
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<Job> insertJobs(Collection<VehicleRoute> vehicleRoutes, Collection<Job> unassignedJobs){
insertionsListeners.informInsertionStarts(vehicleRoutes, unassignedJobs);
Collection<Job> badJobs = insertUnassignedJobs(vehicleRoutes,unassignedJobs);
insertionsListeners.informInsertionEndsListeners(vehicleRoutes);
return badJobs;
}
public abstract Collection<Job> insertUnassignedJobs(Collection<VehicleRoute> vehicleRoutes, Collection<Job> unassignedJobs);
@Override
public void removeListener(InsertionListener insertionListener) {
insertionsListeners.removeListener(insertionListener);
}
@Override
public Collection<InsertionListener> 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);
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
******************************************************************************/
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<Job> ruin(Collection<VehicleRoute> vehicleRoutes){
ruinListeners.ruinStarts(vehicleRoutes);
Collection<Job> unassigned = ruinRoutes(vehicleRoutes);
ruinListeners.ruinEnds(vehicleRoutes,unassigned);
return unassigned;
}
public abstract Collection<Job> ruinRoutes(Collection<VehicleRoute> vehicleRoutes);
@Override
public Collection<Job> ruin(Collection<VehicleRoute> vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved){
ruinListeners.ruinStarts(vehicleRoutes);
Collection<Job> unassigned = ruinRoutes(vehicleRoutes, targetJob, nOfJobs2BeRemoved);
ruinListeners.ruinEnds(vehicleRoutes,unassigned);
return unassigned;
}
public abstract Collection<Job> ruinRoutes(Collection<VehicleRoute> 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<RuinListener> getListeners() {
return ruinListeners.getListeners();
}
protected void removeJob(Job job, Collection<VehicleRoute> 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;
}
}