mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
add helper
This commit is contained in:
parent
dfc8068eef
commit
d8f7af7fb5
2 changed files with 53 additions and 1 deletions
|
|
@ -25,7 +25,9 @@ import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolutio
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -78,7 +80,6 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList
|
|||
|
||||
private final int solutionMemory;
|
||||
|
||||
|
||||
public SchrimpfAcceptance(int solutionMemory, double alpha) {
|
||||
this.alpha = alpha;
|
||||
this.solutionMemory = solutionMemory;
|
||||
|
|
@ -110,6 +111,32 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList
|
|||
return solutionAccepted;
|
||||
}
|
||||
|
||||
public boolean acceptSolution(VehicleRoutingProblemSolution solution, VehicleRoutingProblemSolution newSolution) {
|
||||
List<VehicleRoutingProblemSolution> solutions = new ArrayList<>();
|
||||
solutions.add(solution);
|
||||
boolean solutionAccepted = false;
|
||||
if (solutions.size() < solutionMemory) {
|
||||
solutions.add(newSolution);
|
||||
solutionAccepted = true;
|
||||
} else {
|
||||
VehicleRoutingProblemSolution worst = null;
|
||||
double threshold = getThreshold(currentIteration);
|
||||
for (VehicleRoutingProblemSolution solutionInMemory : solutions) {
|
||||
if (worst == null) worst = solutionInMemory;
|
||||
else if (solutionInMemory.getCost() > worst.getCost()) worst = solutionInMemory;
|
||||
}
|
||||
if (worst == null) {
|
||||
solutions.add(newSolution);
|
||||
solutionAccepted = true;
|
||||
} else if (newSolution.getCost() < worst.getCost() + threshold) {
|
||||
solutions.remove(worst);
|
||||
solutions.add(newSolution);
|
||||
solutionAccepted = true;
|
||||
}
|
||||
}
|
||||
return solutionAccepted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[name=SchrimpfAcceptance][alpha=" + alpha + "]";
|
||||
|
|
@ -136,6 +163,16 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList
|
|||
this.initialThreshold = initialThreshold;
|
||||
}
|
||||
|
||||
public void setMaxIterations(int maxIteration) {
|
||||
this.maxIterations = maxIteration;
|
||||
}
|
||||
|
||||
public void incIteration() {
|
||||
currentIteration++;
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
@Override
|
||||
public void informAlgorithmStarts(VehicleRoutingProblem problem, VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
reset();
|
||||
|
|
|
|||
|
|
@ -30,7 +30,9 @@ import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -119,9 +121,22 @@ public class VariationCoefficientTermination implements PrematureAlgorithmTermin
|
|||
}
|
||||
}
|
||||
|
||||
public void informIterationEnds(int i, VehicleRoutingProblem problem, VehicleRoutingProblemSolution solution) {
|
||||
informIterationEnds(i, problem, toList(solution));
|
||||
}
|
||||
|
||||
private List<VehicleRoutingProblemSolution> toList(VehicleRoutingProblemSolution solution) {
|
||||
List<VehicleRoutingProblemSolution> solutions = new ArrayList<>();
|
||||
solutions.add(solution);
|
||||
return solutions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void informIterationStarts(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
if (lastAccepted == null) lastAccepted = Solutions.bestOf(solutions);
|
||||
}
|
||||
|
||||
public void informIterationStarts(int i, VehicleRoutingProblem problem, VehicleRoutingProblemSolution solution) {
|
||||
informIterationStarts(i, problem, toList(solution));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue