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:
parent
b9c7dc3324
commit
25e5c096f2
43 changed files with 625 additions and 645 deletions
|
|
@ -32,16 +32,15 @@ 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 {
|
||||
|
||||
|
||||
|
|
@ -55,7 +54,7 @@ public class RegretInsertionConcurrent extends AbstractInsertionStrategy {
|
|||
|
||||
/**
|
||||
* Sets the scoring function.
|
||||
*
|
||||
* <p/>
|
||||
* <p>By default, the this.TimeWindowScorer is used.
|
||||
*
|
||||
* @param scoringFunction to score
|
||||
|
|
@ -75,17 +74,16 @@ public class RegretInsertionConcurrent extends AbstractInsertionStrategy {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[name=regretInsertion][additionalScorer="+scoringFunction+"]";
|
||||
return "[name=regretInsertion][additionalScorer=" + scoringFunction + "]";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
|
|
@ -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 {
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,6 @@ import java.util.*;
|
|||
* customer are removed randomly from current solution.
|
||||
*
|
||||
* @author stefan schroeder
|
||||
*
|
||||
*/
|
||||
|
||||
public final class RuinClusters extends AbstractRuinStrategy implements IterationStartsListener {
|
||||
|
|
@ -56,10 +55,10 @@ public final class RuinClusters extends AbstractRuinStrategy implements Iteratio
|
|||
|
||||
@Override
|
||||
public double[] getPoint() {
|
||||
return new double[]{ jobActivity.getLocation().getCoordinate().getX(), jobActivity.getLocation().getCoordinate().getY() };
|
||||
return new double[]{jobActivity.getLocation().getCoordinate().getX(), jobActivity.getLocation().getCoordinate().getY()};
|
||||
}
|
||||
|
||||
public TourActivity.JobActivity getActivity(){
|
||||
public TourActivity.JobActivity getActivity() {
|
||||
return jobActivity;
|
||||
}
|
||||
}
|
||||
|
|
@ -96,7 +95,7 @@ public final class RuinClusters extends AbstractRuinStrategy implements Iteratio
|
|||
|
||||
/**
|
||||
* 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
|
||||
|
|
@ -116,36 +115,35 @@ public final class RuinClusters extends AbstractRuinStrategy implements Iteratio
|
|||
}
|
||||
|
||||
private void ruin(Collection<VehicleRoute> vehicleRoutes, int nOfJobs2BeRemoved, List<Job> unassignedJobs) {
|
||||
if(vrp.getJobs().values().size() == 0) return;
|
||||
Map<Job,VehicleRoute> mappedRoutes = map(vehicleRoutes);
|
||||
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) {
|
||||
while (toRemove > 0) {
|
||||
Job target;
|
||||
VehicleRoute targetRoute = null;
|
||||
if(lastRemoved.isEmpty()){
|
||||
if (lastRemoved.isEmpty()) {
|
||||
target = RandomUtils.nextJob(vrp.getJobs().values(), random);
|
||||
targetRoute = mappedRoutes.get(target);
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
target = RandomUtils.nextJob(lastRemoved, random);
|
||||
Iterator<Job> neighborIterator = jobNeighborhoods.getNearestNeighborsIterator(nOfJobs2BeRemoved,target);
|
||||
while(neighborIterator.hasNext()){
|
||||
Iterator<Job> neighborIterator = jobNeighborhoods.getNearestNeighborsIterator(nOfJobs2BeRemoved, target);
|
||||
while (neighborIterator.hasNext()) {
|
||||
Job j = neighborIterator.next();
|
||||
if(!removed.contains(j) && !ruined.contains(mappedRoutes.get(j))){
|
||||
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)) {
|
||||
if (targetRoute == null) break;
|
||||
if (cycleCandidates.contains(targetRoute)) break;
|
||||
if (ruined.contains(targetRoute)) {
|
||||
cycleCandidates.add(targetRoute);
|
||||
break;
|
||||
}
|
||||
|
|
@ -154,9 +152,9 @@ public final class RuinClusters extends AbstractRuinStrategy implements Iteratio
|
|||
dbscan.setMinPts(minPts);
|
||||
dbscan.setEpsFactor(epsFactor);
|
||||
List<Job> cluster = dbscan.getRandomCluster(targetRoute);
|
||||
for(Job j : cluster){
|
||||
if(toRemove == 0) break;
|
||||
if(removeJob(j, vehicleRoutes)) {
|
||||
for (Job j : cluster) {
|
||||
if (toRemove == 0) break;
|
||||
if (removeJob(j, vehicleRoutes)) {
|
||||
lastRemoved.add(j);
|
||||
unassignedJobs.add(j);
|
||||
}
|
||||
|
|
@ -168,17 +166,17 @@ public final class RuinClusters extends AbstractRuinStrategy implements Iteratio
|
|||
|
||||
private List<JobActivityWrapper> wrap(List<TourActivity> activities) {
|
||||
List<JobActivityWrapper> wl = new ArrayList<JobActivityWrapper>();
|
||||
for(TourActivity act : activities){
|
||||
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);
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -25,12 +25,10 @@ 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 {
|
||||
|
||||
|
|
@ -48,26 +46,25 @@ public class ServiceLoadActivityLevelConstraint implements HardActivityConstrain
|
|||
public ConstraintsStatus fulfilled(JobInsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double prevActDepTime) {
|
||||
Capacity futureMaxLoad;
|
||||
Capacity prevMaxLoad;
|
||||
if(prevAct instanceof Start){
|
||||
if (prevAct instanceof Start) {
|
||||
futureMaxLoad = stateManager.getRouteState(iFacts.getRoute(), InternalStates.MAXLOAD, Capacity.class);
|
||||
if(futureMaxLoad == null) futureMaxLoad = defaultValue;
|
||||
if (futureMaxLoad == null) futureMaxLoad = defaultValue;
|
||||
prevMaxLoad = stateManager.getRouteState(iFacts.getRoute(), InternalStates.LOAD_AT_BEGINNING, Capacity.class);
|
||||
if(prevMaxLoad == null) prevMaxLoad = defaultValue;
|
||||
}
|
||||
else{
|
||||
if (prevMaxLoad == null) prevMaxLoad = defaultValue;
|
||||
} else {
|
||||
futureMaxLoad = stateManager.getActivityState(prevAct, InternalStates.FUTURE_MAXLOAD, Capacity.class);
|
||||
if(futureMaxLoad == null) futureMaxLoad = defaultValue;
|
||||
if (futureMaxLoad == null) futureMaxLoad = defaultValue;
|
||||
prevMaxLoad = stateManager.getActivityState(prevAct, InternalStates.PAST_MAXLOAD, Capacity.class);
|
||||
if(prevMaxLoad == null) prevMaxLoad = defaultValue;
|
||||
if (prevMaxLoad == null) prevMaxLoad = defaultValue;
|
||||
|
||||
}
|
||||
if(newAct instanceof PickupService || newAct instanceof ServiceActivity){
|
||||
if(!Capacity.addup(newAct.getSize(), futureMaxLoad).isLessOrEqual(iFacts.getNewVehicle().getType().getCapacityDimensions())){
|
||||
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())){
|
||||
if (newAct instanceof DeliverService) {
|
||||
if (!Capacity.addup(Capacity.invert(newAct.getSize()), prevMaxLoad).isLessOrEqual(iFacts.getNewVehicle().getType().getCapacityDimensions())) {
|
||||
return ConstraintsStatus.NOT_FULFILLED_BREAK;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,13 +18,6 @@
|
|||
package jsprit.core.algorithm;
|
||||
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import jsprit.core.algorithm.box.GreedySchrimpfFactory;
|
||||
import jsprit.core.algorithm.box.Jsprit;
|
||||
import jsprit.core.algorithm.box.Jsprit.Builder;
|
||||
|
|
@ -53,9 +46,13 @@ import jsprit.core.problem.vehicle.VehicleTypeImpl;
|
|||
import jsprit.core.reporting.SolutionPrinter;
|
||||
import jsprit.core.util.Coordinate;
|
||||
import jsprit.core.util.Solutions;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class InitialRoutesTest {
|
||||
|
||||
@Test
|
||||
|
|
@ -396,9 +393,9 @@ public class InitialRoutesTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void whenAllJobsInInitialRoute_itShouldWork(){
|
||||
Service s = Service.Builder.newInstance("s").setLocation(Location.newInstance(0,10)).build();
|
||||
VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance(0,0)).build();
|
||||
public void whenAllJobsInInitialRoute_itShouldWork() {
|
||||
Service s = Service.Builder.newInstance("s").setLocation(Location.newInstance(0, 10)).build();
|
||||
VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance(0, 0)).build();
|
||||
VehicleRoute iniRoute = VehicleRoute.Builder.newInstance(v).addService(s).build();
|
||||
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addInitialVehicleRoute(iniRoute).build();
|
||||
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
|
||||
|
|
@ -409,11 +406,11 @@ public class InitialRoutesTest {
|
|||
|
||||
@Test
|
||||
public void buildWithoutTimeConstraints() {
|
||||
Service s1 = Service.Builder.newInstance("s1").setLocation(Location.newInstance(0,10)).addSizeDimension(0, 10).build();
|
||||
Service s2 = Service.Builder.newInstance("s2").setLocation(Location.newInstance(10,20)).addSizeDimension(0, 12).build();
|
||||
Service s1 = Service.Builder.newInstance("s1").setLocation(Location.newInstance(0, 10)).addSizeDimension(0, 10).build();
|
||||
Service s2 = Service.Builder.newInstance("s2").setLocation(Location.newInstance(10, 20)).addSizeDimension(0, 12).build();
|
||||
|
||||
VehicleTypeImpl vt = VehicleTypeImpl.Builder.newInstance("vt").addCapacityDimension(0, 15).build();
|
||||
VehicleImpl v = VehicleImpl.Builder.newInstance("v").setType(vt).setStartLocation(Location.newInstance(0,0)).build();
|
||||
VehicleImpl v = VehicleImpl.Builder.newInstance("v").setType(vt).setStartLocation(Location.newInstance(0, 0)).build();
|
||||
|
||||
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(s1).addJob(s2).addVehicle(v).build();
|
||||
Builder algBuilder = Jsprit.Builder.newInstance(vrp).addCoreStateAndConstraintStuff(false);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problem xmlns="http://www.w3schools.com"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.w3schools.com vrp_xml_schema.xsd">
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com vrp_xml_schema.xsd">
|
||||
<problemType>
|
||||
<fleetSize>FINITE</fleetSize>
|
||||
</problemType>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problem xmlns="http://www.w3schools.com"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.w3schools.com vrp_xml_schema.xsd">
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com vrp_xml_schema.xsd">
|
||||
<problemType>
|
||||
<fleetSize>INFINITE</fleetSize>
|
||||
</problemType>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue