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

add editorconfig - #174

This commit is contained in:
oblonski 2015-09-11 12:49:01 +02:00
parent b9c7dc3324
commit 25e5c096f2
43 changed files with 625 additions and 645 deletions

View file

@ -178,8 +178,8 @@ public final class BestInsertionConcurrent extends AbstractInsertionStrategy {
} else {
vehicleRoutes.add(VehicleRoute.emptyRoute());
}
/*
* distribute routes to batches equally
/*
* distribute routes to batches equally
*/
int count = 0;
for (VehicleRoute route : vehicleRoutes) {

View file

@ -32,63 +32,61 @@ import java.util.List;
import java.util.concurrent.*;
/**
* Insertion based on regret approach.
*
* <p>Basically calculates the insertion cost of the firstBest and the secondBest alternative. The score is then calculated as difference
* between secondBest and firstBest, plus additional scoring variables that can defined in this.ScoringFunction.
* The idea is that if the cost of the secondBest alternative is way higher than the first best, it seems to be important to insert this
* customer immediatedly. If difference is not that high, it might not impact solution if this customer is inserted later.
*
* @author stefan schroeder
*
*/
* Insertion based on regret approach.
* <p/>
* <p>Basically calculates the insertion cost of the firstBest and the secondBest alternative. The score is then calculated as difference
* between secondBest and firstBest, plus additional scoring variables that can defined in this.ScoringFunction.
* The idea is that if the cost of the secondBest alternative is way higher than the first best, it seems to be important to insert this
* customer immediatedly. If difference is not that high, it might not impact solution if this customer is inserted later.
*
* @author stefan schroeder
*/
public class RegretInsertionConcurrent extends AbstractInsertionStrategy {
private static Logger logger = LogManager.getLogger(RegretInsertionConcurrent.class);
private ScoringFunction scoringFunction;
private ScoringFunction scoringFunction;
private final JobInsertionCostsCalculator insertionCostsCalculator;
private final ExecutorCompletionService<ScoredJob> completionService;
/**
* Sets the scoring function.
*
* <p>By default, the this.TimeWindowScorer is used.
*
* @param scoringFunction to score
*/
public void setScoringFunction(ScoringFunction scoringFunction) {
this.scoringFunction = scoringFunction;
}
* Sets the scoring function.
* <p/>
* <p>By default, the this.TimeWindowScorer is used.
*
* @param scoringFunction to score
*/
public void setScoringFunction(ScoringFunction scoringFunction) {
this.scoringFunction = scoringFunction;
}
public RegretInsertionConcurrent(JobInsertionCostsCalculator jobInsertionCalculator, VehicleRoutingProblem vehicleRoutingProblem, ExecutorService executorService) {
super(vehicleRoutingProblem);
public RegretInsertionConcurrent(JobInsertionCostsCalculator jobInsertionCalculator, VehicleRoutingProblem vehicleRoutingProblem, ExecutorService executorService) {
super(vehicleRoutingProblem);
this.scoringFunction = new DefaultScorer(vehicleRoutingProblem);
this.insertionCostsCalculator = jobInsertionCalculator;
this.insertionCostsCalculator = jobInsertionCalculator;
this.vrp = vehicleRoutingProblem;
completionService = new ExecutorCompletionService<ScoredJob>(executorService);
logger.debug("initialise " + this);
}
logger.debug("initialise " + this);
}
@Override
public String toString() {
return "[name=regretInsertion][additionalScorer="+scoringFunction+"]";
}
@Override
public String toString() {
return "[name=regretInsertion][additionalScorer=" + scoringFunction + "]";
}
/**
* Runs insertion.
*
* <p>Before inserting a job, all unassigned jobs are scored according to its best- and secondBest-insertion plus additional scoring variables.
/**
* Runs insertion.
* <p/>
* <p>Before inserting a job, all unassigned jobs are scored according to its best- and secondBest-insertion plus additional scoring variables.
*
* @throws java.lang.RuntimeException if smth went wrong with thread execution
*
*/
@Override
public Collection<Job> insertUnassignedJobs(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) {
*/
@Override
public Collection<Job> insertUnassignedJobs(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) {
List<Job> badJobs = new ArrayList<Job>(unassignedJobs.size());
List<Job> jobs = new ArrayList<Job>(unassignedJobs);
@ -96,14 +94,14 @@ public class RegretInsertionConcurrent extends AbstractInsertionStrategy {
List<Job> unassignedJobList = new ArrayList<Job>(jobs);
List<Job> badJobList = new ArrayList<Job>();
ScoredJob bestScoredJob = nextJob(routes, unassignedJobList, badJobList);
if(bestScoredJob != null){
if(bestScoredJob.isNewRoute()){
if (bestScoredJob != null) {
if (bestScoredJob.isNewRoute()) {
routes.add(bestScoredJob.getRoute());
}
insertJob(bestScoredJob.getJob(),bestScoredJob.getInsertionData(),bestScoredJob.getRoute());
insertJob(bestScoredJob.getJob(), bestScoredJob.getInsertionData(), bestScoredJob.getRoute());
jobs.remove(bestScoredJob.getJob());
}
for(Job j : badJobList) {
for (Job j : badJobList) {
jobs.remove(j);
badJobs.add(j);
}
@ -125,26 +123,23 @@ public class RegretInsertionConcurrent extends AbstractInsertionStrategy {
});
}
try{
for(int i=0; i < unassignedJobList.size(); i++){
try {
for (int i = 0; i < unassignedJobList.size(); i++) {
Future<ScoredJob> fsj = completionService.take();
ScoredJob sJob = fsj.get();
if(sJob instanceof RegretInsertion.BadJob){
if (sJob instanceof RegretInsertion.BadJob) {
badJobList.add(sJob.getJob());
continue;
}
if(bestScoredJob == null){
if (bestScoredJob == null) {
bestScoredJob = sJob;
}
else if(sJob.getScore() > bestScoredJob.getScore()){
} else if (sJob.getScore() > bestScoredJob.getScore()) {
bestScoredJob = sJob;
}
}
}
catch(InterruptedException e){
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
catch (ExecutionException e) {
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
@ -152,9 +147,4 @@ public class RegretInsertionConcurrent extends AbstractInsertionStrategy {
}
}

View file

@ -11,7 +11,7 @@
* 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
* 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;
@ -33,160 +33,158 @@ import java.util.*;
/**
* Ruin strategy that ruins current solution randomly. I.e.
* customer are removed randomly from current solution.
*
*
* @author stefan schroeder
*
*/
public final class RuinClusters extends AbstractRuinStrategy implements IterationStartsListener {
@Override
public void informIterationStarts(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
minPts = 1 + random.nextInt(2);
epsFactor = 0.5 + random.nextDouble();
}
@Override
public void informIterationStarts(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
minPts = 1 + random.nextInt(2);
epsFactor = 0.5 + random.nextDouble();
}
public static class JobActivityWrapper implements Clusterable {
public static class JobActivityWrapper implements Clusterable {
private TourActivity.JobActivity jobActivity;
private TourActivity.JobActivity jobActivity;
public JobActivityWrapper(TourActivity.JobActivity jobActivity) {
this.jobActivity = jobActivity;
}
public JobActivityWrapper(TourActivity.JobActivity jobActivity) {
this.jobActivity = jobActivity;
}
@Override
public double[] getPoint() {
return new double[]{ jobActivity.getLocation().getCoordinate().getX(), jobActivity.getLocation().getCoordinate().getY() };
}
@Override
public double[] getPoint() {
return new double[]{jobActivity.getLocation().getCoordinate().getX(), jobActivity.getLocation().getCoordinate().getY()};
}
public TourActivity.JobActivity getActivity(){
return jobActivity;
}
}
public TourActivity.JobActivity getActivity() {
return jobActivity;
}
}
private Logger logger = LogManager.getLogger(RuinClusters.class);
private Logger logger = LogManager.getLogger(RuinClusters.class);
private VehicleRoutingProblem vrp;
private VehicleRoutingProblem vrp;
private JobNeighborhoods jobNeighborhoods;
private JobNeighborhoods jobNeighborhoods;
private int noClusters = 2;
private int noClusters = 2;
private int minPts = 1;
private int minPts = 1;
private double epsFactor = 0.8;
private double epsFactor = 0.8;
public RuinClusters(VehicleRoutingProblem vrp, final int initialNumberJobsToRemove, JobNeighborhoods jobNeighborhoods) {
super(vrp);
this.vrp = vrp;
setRuinShareFactory(new RuinShareFactory() {
@Override
public int createNumberToBeRemoved() {
return initialNumberJobsToRemove;
}
});
this.jobNeighborhoods = jobNeighborhoods;
public RuinClusters(VehicleRoutingProblem vrp, final int initialNumberJobsToRemove, JobNeighborhoods jobNeighborhoods) {
super(vrp);
this.vrp = vrp;
setRuinShareFactory(new RuinShareFactory() {
@Override
public int createNumberToBeRemoved() {
return initialNumberJobsToRemove;
}
});
this.jobNeighborhoods = jobNeighborhoods;
logger.debug("initialise {}", this);
}
}
public void setNoClusters(int noClusters) {
this.noClusters = noClusters;
}
public void setNoClusters(int noClusters) {
this.noClusters = noClusters;
}
/**
* Removes a fraction of jobs from vehicleRoutes.
*
* <p>The number of jobs is calculated as follows: Math.ceil(vrp.getJobs().values().size() * fractionOfAllNodes2beRuined).
*/
@Override
public Collection<Job> ruinRoutes(Collection<VehicleRoute> vehicleRoutes) {
/**
* Removes a fraction of jobs from vehicleRoutes.
* <p/>
* <p>The number of jobs is calculated as follows: Math.ceil(vrp.getJobs().values().size() * fractionOfAllNodes2beRuined).
*/
@Override
public Collection<Job> ruinRoutes(Collection<VehicleRoute> vehicleRoutes) {
List<Job> unassignedJobs = new ArrayList<Job>();
int nOfJobs2BeRemoved = getRuinShareFactory().createNumberToBeRemoved();
ruin(vehicleRoutes, nOfJobs2BeRemoved, unassignedJobs);
int nOfJobs2BeRemoved = getRuinShareFactory().createNumberToBeRemoved();
ruin(vehicleRoutes, nOfJobs2BeRemoved, unassignedJobs);
return unassignedJobs;
}
}
/**
* Removes nOfJobs2BeRemoved from vehicleRoutes, including targetJob.
*/
@Override
public Collection<Job> ruinRoutes(Collection<VehicleRoute> vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved) {
/**
* Removes nOfJobs2BeRemoved from vehicleRoutes, including targetJob.
*/
@Override
public Collection<Job> ruinRoutes(Collection<VehicleRoute> vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved) {
throw new IllegalStateException("not supported");
}
}
private void ruin(Collection<VehicleRoute> vehicleRoutes, int nOfJobs2BeRemoved, List<Job> unassignedJobs) {
if(vrp.getJobs().values().size() == 0) return;
Map<Job,VehicleRoute> mappedRoutes = map(vehicleRoutes);
int toRemove = nOfJobs2BeRemoved;
private void ruin(Collection<VehicleRoute> vehicleRoutes, int nOfJobs2BeRemoved, List<Job> unassignedJobs) {
if (vrp.getJobs().values().size() == 0) return;
Map<Job, VehicleRoute> mappedRoutes = map(vehicleRoutes);
int toRemove = nOfJobs2BeRemoved;
Collection<Job> lastRemoved = new ArrayList<Job>();
Set<VehicleRoute> ruined = new HashSet<VehicleRoute>();
Set<Job> removed = new HashSet<Job>();
Set<VehicleRoute> cycleCandidates = new HashSet<VehicleRoute>();
while(toRemove > 0) {
Job target;
VehicleRoute targetRoute = null;
if(lastRemoved.isEmpty()){
target = RandomUtils.nextJob(vrp.getJobs().values(), random);
targetRoute = mappedRoutes.get(target);
}
else{
target = RandomUtils.nextJob(lastRemoved, random);
Iterator<Job> neighborIterator = jobNeighborhoods.getNearestNeighborsIterator(nOfJobs2BeRemoved,target);
while(neighborIterator.hasNext()){
Job j = neighborIterator.next();
if(!removed.contains(j) && !ruined.contains(mappedRoutes.get(j))){
targetRoute = mappedRoutes.get(j);
break;
}
}
lastRemoved.clear();
}
if(targetRoute == null) break;
if(cycleCandidates.contains(targetRoute)) break;
if(ruined.contains(targetRoute)) {
cycleCandidates.add(targetRoute);
break;
}
DBSCANClusterer dbscan = new DBSCANClusterer(vrp.getTransportCosts());
dbscan.setRandom(random);
dbscan.setMinPts(minPts);
dbscan.setEpsFactor(epsFactor);
List<Job> cluster = dbscan.getRandomCluster(targetRoute);
for(Job j : cluster){
if(toRemove == 0) break;
if(removeJob(j, vehicleRoutes)) {
lastRemoved.add(j);
unassignedJobs.add(j);
}
toRemove--;
}
ruined.add(targetRoute);
}
}
Collection<Job> lastRemoved = new ArrayList<Job>();
Set<VehicleRoute> ruined = new HashSet<VehicleRoute>();
Set<Job> removed = new HashSet<Job>();
Set<VehicleRoute> cycleCandidates = new HashSet<VehicleRoute>();
while (toRemove > 0) {
Job target;
VehicleRoute targetRoute = null;
if (lastRemoved.isEmpty()) {
target = RandomUtils.nextJob(vrp.getJobs().values(), random);
targetRoute = mappedRoutes.get(target);
} else {
target = RandomUtils.nextJob(lastRemoved, random);
Iterator<Job> neighborIterator = jobNeighborhoods.getNearestNeighborsIterator(nOfJobs2BeRemoved, target);
while (neighborIterator.hasNext()) {
Job j = neighborIterator.next();
if (!removed.contains(j) && !ruined.contains(mappedRoutes.get(j))) {
targetRoute = mappedRoutes.get(j);
break;
}
}
lastRemoved.clear();
}
if (targetRoute == null) break;
if (cycleCandidates.contains(targetRoute)) break;
if (ruined.contains(targetRoute)) {
cycleCandidates.add(targetRoute);
break;
}
DBSCANClusterer dbscan = new DBSCANClusterer(vrp.getTransportCosts());
dbscan.setRandom(random);
dbscan.setMinPts(minPts);
dbscan.setEpsFactor(epsFactor);
List<Job> cluster = dbscan.getRandomCluster(targetRoute);
for (Job j : cluster) {
if (toRemove == 0) break;
if (removeJob(j, vehicleRoutes)) {
lastRemoved.add(j);
unassignedJobs.add(j);
}
toRemove--;
}
ruined.add(targetRoute);
}
}
private List<JobActivityWrapper> wrap(List<TourActivity> activities) {
List<JobActivityWrapper> wl = new ArrayList<JobActivityWrapper>();
for(TourActivity act : activities){
wl.add(new JobActivityWrapper((TourActivity.JobActivity) act));
}
return wl;
}
private List<JobActivityWrapper> wrap(List<TourActivity> activities) {
List<JobActivityWrapper> wl = new ArrayList<JobActivityWrapper>();
for (TourActivity act : activities) {
wl.add(new JobActivityWrapper((TourActivity.JobActivity) act));
}
return wl;
}
private Map<Job, VehicleRoute> map(Collection<VehicleRoute> vehicleRoutes) {
Map<Job,VehicleRoute> map = new HashMap<Job, VehicleRoute>(vrp.getJobs().size());
for(VehicleRoute r : vehicleRoutes){
for(Job j : r.getTourActivities().getJobs()){
map.put(j,r);
}
}
return map;
}
private Map<Job, VehicleRoute> map(Collection<VehicleRoute> vehicleRoutes) {
Map<Job, VehicleRoute> map = new HashMap<Job, VehicleRoute>(vrp.getJobs().size());
for (VehicleRoute r : vehicleRoutes) {
for (Job j : r.getTourActivities().getJobs()) {
map.put(j, r);
}
}
return map;
}
@Override
public String toString() {
return "[name=clusterRuin]";
}
@Override
public String toString() {
return "[name=clusterRuin]";
}
}

View file

@ -25,52 +25,49 @@ import jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
/**
* Ensures load constraint for inserting ServiceActivity.
*
* <p/>
* <p>When using this, you need to use<br>
*
*
* @author schroeder
*
*/
public class ServiceLoadActivityLevelConstraint implements HardActivityConstraint {
private RouteAndActivityStateGetter stateManager;
private RouteAndActivityStateGetter stateManager;
private Capacity defaultValue;
public ServiceLoadActivityLevelConstraint(RouteAndActivityStateGetter stateManager) {
super();
this.stateManager = stateManager;
public ServiceLoadActivityLevelConstraint(RouteAndActivityStateGetter stateManager) {
super();
this.stateManager = stateManager;
defaultValue = Capacity.Builder.newInstance().build();
}
}
@Override
public ConstraintsStatus fulfilled(JobInsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double prevActDepTime) {
Capacity futureMaxLoad;
Capacity prevMaxLoad;
if(prevAct instanceof Start){
futureMaxLoad = stateManager.getRouteState(iFacts.getRoute(), InternalStates.MAXLOAD, Capacity.class);
if(futureMaxLoad == null) futureMaxLoad = defaultValue;
prevMaxLoad = stateManager.getRouteState(iFacts.getRoute(), InternalStates.LOAD_AT_BEGINNING, Capacity.class);
if(prevMaxLoad == null) prevMaxLoad = defaultValue;
}
else{
futureMaxLoad = stateManager.getActivityState(prevAct, InternalStates.FUTURE_MAXLOAD, Capacity.class);
if(futureMaxLoad == null) futureMaxLoad = defaultValue;
prevMaxLoad = stateManager.getActivityState(prevAct, InternalStates.PAST_MAXLOAD, Capacity.class);
if(prevMaxLoad == null) prevMaxLoad = defaultValue;
@Override
public ConstraintsStatus fulfilled(JobInsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double prevActDepTime) {
Capacity futureMaxLoad;
Capacity prevMaxLoad;
if (prevAct instanceof Start) {
futureMaxLoad = stateManager.getRouteState(iFacts.getRoute(), InternalStates.MAXLOAD, Capacity.class);
if (futureMaxLoad == null) futureMaxLoad = defaultValue;
prevMaxLoad = stateManager.getRouteState(iFacts.getRoute(), InternalStates.LOAD_AT_BEGINNING, Capacity.class);
if (prevMaxLoad == null) prevMaxLoad = defaultValue;
} else {
futureMaxLoad = stateManager.getActivityState(prevAct, InternalStates.FUTURE_MAXLOAD, Capacity.class);
if (futureMaxLoad == null) futureMaxLoad = defaultValue;
prevMaxLoad = stateManager.getActivityState(prevAct, InternalStates.PAST_MAXLOAD, Capacity.class);
if (prevMaxLoad == null) prevMaxLoad = defaultValue;
}
if(newAct instanceof PickupService || newAct instanceof ServiceActivity){
if(!Capacity.addup(newAct.getSize(), futureMaxLoad).isLessOrEqual(iFacts.getNewVehicle().getType().getCapacityDimensions())){
return ConstraintsStatus.NOT_FULFILLED;
}
}
if(newAct instanceof DeliverService){
if(!Capacity.addup(Capacity.invert(newAct.getSize()), prevMaxLoad).isLessOrEqual(iFacts.getNewVehicle().getType().getCapacityDimensions())){
return ConstraintsStatus.NOT_FULFILLED_BREAK;
}
}
return ConstraintsStatus.FULFILLED;
}
}
if (newAct instanceof PickupService || newAct instanceof ServiceActivity) {
if (!Capacity.addup(newAct.getSize(), futureMaxLoad).isLessOrEqual(iFacts.getNewVehicle().getType().getCapacityDimensions())) {
return ConstraintsStatus.NOT_FULFILLED;
}
}
if (newAct instanceof DeliverService) {
if (!Capacity.addup(Capacity.invert(newAct.getSize()), prevMaxLoad).isLessOrEqual(iFacts.getNewVehicle().getType().getCapacityDimensions())) {
return ConstraintsStatus.NOT_FULFILLED_BREAK;
}
}
return ConstraintsStatus.FULFILLED;
}
}

View file

@ -72,7 +72,7 @@ class TimeWindowConstraint implements HardActivityConstraint {
return ConstraintsStatus.NOT_FULFILLED_BREAK;
}
/*
* if the latest operation start-time of new activity is smaller than the earliest start of prev. activity,
* if the latest operation start-time of new activity is smaller than the earliest start of prev. activity,
* then
*
* |--- prevAct ---|
@ -83,7 +83,7 @@ class TimeWindowConstraint implements HardActivityConstraint {
}
/*
* |--- prevAct ---|
* |--- prevAct ---|
* |- earliest arrival of vehicle
* |--- nextAct ---|
*/

View file

@ -79,7 +79,7 @@ public class VehicleDependentTimeWindowConstraints implements HardActivityConstr
return ConstraintsStatus.NOT_FULFILLED_BREAK;
}
/*
* if the latest operation start-time of new activity is smaller than the earliest start of prev. activity,
* if the latest operation start-time of new activity is smaller than the earliest start of prev. activity,
* then
*
* |--- prevAct ---|
@ -90,7 +90,7 @@ public class VehicleDependentTimeWindowConstraints implements HardActivityConstr
}
/*
* |--- prevAct ---|
* |--- prevAct ---|
* |- earliest arrival of vehicle
* |--- nextAct ---|
*/