mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
simplify to get strategies and their weight
This commit is contained in:
parent
73021253de
commit
349364dce6
5 changed files with 27 additions and 17 deletions
|
|
@ -33,15 +33,18 @@ import java.util.Collections;
|
||||||
public class SearchStrategy {
|
public class SearchStrategy {
|
||||||
|
|
||||||
public static class DiscoveredSolution {
|
public static class DiscoveredSolution {
|
||||||
|
|
||||||
private VehicleRoutingProblemSolution solution;
|
private VehicleRoutingProblemSolution solution;
|
||||||
|
|
||||||
private boolean accepted;
|
private boolean accepted;
|
||||||
private String strategyName;
|
|
||||||
|
private String strategyId;
|
||||||
|
|
||||||
public DiscoveredSolution(VehicleRoutingProblemSolution solution,boolean accepted, String strategyName) {
|
public DiscoveredSolution(VehicleRoutingProblemSolution solution, boolean accepted, String strategyId) {
|
||||||
super();
|
super();
|
||||||
this.solution = solution;
|
this.solution = solution;
|
||||||
this.accepted = accepted;
|
this.accepted = accepted;
|
||||||
this.strategyName = strategyName;
|
this.strategyId = strategyId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public VehicleRoutingProblemSolution getSolution() {
|
public VehicleRoutingProblemSolution getSolution() {
|
||||||
|
|
@ -52,10 +55,12 @@ public class SearchStrategy {
|
||||||
return accepted;
|
return accepted;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("UnusedDeclaration")
|
@Deprecated
|
||||||
public String getStrategyName() {
|
public String getStrategyName() {
|
||||||
return strategyName;
|
return strategyId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getStrategyId() { return strategyId; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -137,7 +142,7 @@ public class SearchStrategy {
|
||||||
double costs = solutionCostCalculator.getCosts(lastSolution);
|
double costs = solutionCostCalculator.getCosts(lastSolution);
|
||||||
lastSolution.setCost(costs);
|
lastSolution.setCost(costs);
|
||||||
boolean solutionAccepted = solutionAcceptor.acceptSolution(solutions, lastSolution);
|
boolean solutionAccepted = solutionAcceptor.acceptSolution(solutions, lastSolution);
|
||||||
return new DiscoveredSolution(lastSolution, solutionAccepted, getName());
|
return new DiscoveredSolution(lastSolution, solutionAccepted, getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getErrMsg() {
|
private String getErrMsg() {
|
||||||
|
|
|
||||||
|
|
@ -43,22 +43,27 @@ public class SearchStrategyManager {
|
||||||
this.random = random;
|
this.random = random;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public List<SearchStrategy> getStrategies() {
|
public List<SearchStrategy> getStrategies() {
|
||||||
return Collections.unmodifiableList(strategies);
|
return Collections.unmodifiableList(strategies);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the probabilities.
|
* Returns the probabilities.
|
||||||
* [schroeder (2014.11.21): Now they are actually no propabilities anymore but weights. The resulting probabilities
|
* [schroeder (2014.11.21): Now they are actually no propabilities anymore but weights. The resulting probabilities
|
||||||
* are calculated here with the sum of weights]
|
* are calculated here with the sum of weights]
|
||||||
* @return list of probabilities
|
* @return list of probabilities
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public List<Double> getProbabilities() {
|
public List<Double> getProbabilities() {
|
||||||
return Collections.unmodifiableList(weights);
|
return Collections.unmodifiableList(weights);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Double> getWeights(){ return Collections.unmodifiableList(weights); }
|
||||||
|
|
||||||
|
public double getWeight(String strategyId){
|
||||||
|
return weights.get(id2index.get(strategyId));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* adds a new search strategy with a certain weight.
|
* adds a new search strategy with a certain weight.
|
||||||
* @param strategy strategy to be added
|
* @param strategy strategy to be added
|
||||||
|
|
|
||||||
|
|
@ -205,7 +205,7 @@ public class VehicleRoutingAlgorithm {
|
||||||
SearchStrategy strategy = searchStrategyManager.getRandomStrategy();
|
SearchStrategy strategy = searchStrategyManager.getRandomStrategy();
|
||||||
DiscoveredSolution discoveredSolution = strategy.run(problem, solutions);
|
DiscoveredSolution discoveredSolution = strategy.run(problem, solutions);
|
||||||
memorizeIfBestEver(discoveredSolution);
|
memorizeIfBestEver(discoveredSolution);
|
||||||
selectedStrategy(strategy.getId(),searchStrategyManager,problem, solutions);
|
selectedStrategy(discoveredSolution,problem,solutions);
|
||||||
if(terminationManager.isPrematureBreak(discoveredSolution)){
|
if(terminationManager.isPrematureBreak(discoveredSolution)){
|
||||||
logger.info("premature algorithm termination at iteration "+ (i+1));
|
logger.info("premature algorithm termination at iteration "+ (i+1));
|
||||||
noIterationsThisAlgoIsRunning = (i+1);
|
noIterationsThisAlgoIsRunning = (i+1);
|
||||||
|
|
@ -233,8 +233,8 @@ public class VehicleRoutingAlgorithm {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void selectedStrategy(String name, SearchStrategyManager searchStrategyManager, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
private void selectedStrategy(DiscoveredSolution discoveredSolution, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||||
algoListeners.selectedStrategy(name, searchStrategyManager, problem, solutions);
|
algoListeners.selectedStrategy(discoveredSolution,problem,solutions);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package jsprit.core.algorithm.listener;
|
package jsprit.core.algorithm.listener;
|
||||||
|
|
||||||
import jsprit.core.algorithm.SearchStrategyManager;
|
import jsprit.core.algorithm.SearchStrategy;
|
||||||
import jsprit.core.problem.VehicleRoutingProblem;
|
import jsprit.core.problem.VehicleRoutingProblem;
|
||||||
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
||||||
|
|
||||||
|
|
@ -27,6 +27,6 @@ import java.util.Collection;
|
||||||
|
|
||||||
public interface StrategySelectedListener extends VehicleRoutingAlgorithmListener{
|
public interface StrategySelectedListener extends VehicleRoutingAlgorithmListener{
|
||||||
|
|
||||||
void informSelectedStrategy(String strategyId, SearchStrategyManager searchStrategyManager, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions);
|
void informSelectedStrategy(SearchStrategy.DiscoveredSolution discoveredSolution, VehicleRoutingProblem vehicleRoutingProblem, Collection<VehicleRoutingProblemSolution> vehicleRoutingProblemSolutions);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package jsprit.core.algorithm.listener;
|
package jsprit.core.algorithm.listener;
|
||||||
|
|
||||||
import jsprit.core.algorithm.SearchStrategyManager;
|
import jsprit.core.algorithm.SearchStrategy;
|
||||||
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
|
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
|
||||||
import jsprit.core.problem.VehicleRoutingProblem;
|
import jsprit.core.problem.VehicleRoutingProblem;
|
||||||
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
||||||
|
|
@ -172,10 +172,10 @@ public class VehicleRoutingAlgorithmListeners {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void selectedStrategy(String name, SearchStrategyManager searchStrategyManager, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
public void selectedStrategy(SearchStrategy.DiscoveredSolution discoveredSolution, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||||
for(PrioritizedVRAListener l : algorithmListeners){
|
for(PrioritizedVRAListener l : algorithmListeners){
|
||||||
if(l.getListener() instanceof StrategySelectedListener){
|
if(l.getListener() instanceof StrategySelectedListener){
|
||||||
((StrategySelectedListener)l.getListener()).informSelectedStrategy(name, searchStrategyManager, problem, solutions);
|
((StrategySelectedListener)l.getListener()).informSelectedStrategy(discoveredSolution, problem, solutions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue