mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
merged skills
This commit is contained in:
commit
9c4bd498c4
17 changed files with 676 additions and 32 deletions
|
|
@ -58,8 +58,8 @@ public class VehicleRoutingAlgorithmBuilder {
|
|||
/**
|
||||
* Constructs the builder with the problem and an algorithmConfigFile. Latter is to configure and specify the ruin-and-recreate meta-heuristic.
|
||||
*
|
||||
* @param problem
|
||||
* @param algorithmConfig
|
||||
* @param problem to solve
|
||||
* @param algorithmConfig config file of VehicleRoutingAlgorithm
|
||||
*/
|
||||
public VehicleRoutingAlgorithmBuilder(VehicleRoutingProblem problem, String algorithmConfig) {
|
||||
this.vrp=problem;
|
||||
|
|
@ -69,9 +69,9 @@ public class VehicleRoutingAlgorithmBuilder {
|
|||
|
||||
/**
|
||||
* Constructs the builder with the problem and an algorithmConfig. Latter is to configure and specify the ruin-and-recreate meta-heuristic.
|
||||
*
|
||||
* @param problem
|
||||
* @param algorithmConfig
|
||||
*
|
||||
* @param problem to solve
|
||||
* @param algorithmConfig config file of VehicleRoutingAlgorithm
|
||||
*/
|
||||
public VehicleRoutingAlgorithmBuilder(VehicleRoutingProblem problem, AlgorithmConfig algorithmConfig) {
|
||||
this.vrp=problem;
|
||||
|
|
@ -85,7 +85,7 @@ public class VehicleRoutingAlgorithmBuilder {
|
|||
* <p>If objective function is not set, a default function is applied (which basically minimizes
|
||||
* fixed and variable transportation costs ({@link VariablePlusFixedSolutionCostCalculatorFactory}).
|
||||
*
|
||||
* @param objectiveFunction
|
||||
* @param objectiveFunction to be minimized
|
||||
* @see VariablePlusFixedSolutionCostCalculatorFactory
|
||||
*/
|
||||
public void setObjectiveFunction(SolutionCostCalculator objectiveFunction) {
|
||||
|
|
@ -95,7 +95,7 @@ public class VehicleRoutingAlgorithmBuilder {
|
|||
/**
|
||||
* Sets stateManager to memorize states.
|
||||
*
|
||||
* @param stateManager
|
||||
* @param stateManager that memorizes your states
|
||||
* @see StateManager
|
||||
*/
|
||||
public void setStateManager(StateManager stateManager) {
|
||||
|
|
@ -105,7 +105,7 @@ public class VehicleRoutingAlgorithmBuilder {
|
|||
/**
|
||||
* Adds core constraints.
|
||||
*
|
||||
* <p>Thus, it adds vehicle-capacity and time-window constraints and their
|
||||
* <p>Thus, it adds vehicle-capacity, time-window and skills constraints and their
|
||||
* required stateUpdater.
|
||||
*
|
||||
*/
|
||||
|
|
@ -130,8 +130,8 @@ public class VehicleRoutingAlgorithmBuilder {
|
|||
/**
|
||||
* Sets state- and constraintManager.
|
||||
*
|
||||
* @param stateManager
|
||||
* @param constraintManager
|
||||
* @param stateManager that memorizes your states
|
||||
* @param constraintManager that manages your constraints
|
||||
* @see StateManager
|
||||
* @see ConstraintManager
|
||||
*/
|
||||
|
|
@ -143,7 +143,7 @@ public class VehicleRoutingAlgorithmBuilder {
|
|||
/**
|
||||
* Sets nuOfThreads.
|
||||
*
|
||||
* @param nuOfThreads
|
||||
* @param nuOfThreads to be operated
|
||||
*/
|
||||
public void setNuOfThreads(int nuOfThreads){
|
||||
this.nuOfThreads=nuOfThreads;
|
||||
|
|
@ -154,7 +154,7 @@ public class VehicleRoutingAlgorithmBuilder {
|
|||
*
|
||||
* <p>If algorithmConfigFile is set, it reads the configuration.
|
||||
*
|
||||
* @return
|
||||
* @return the algorithm
|
||||
*/
|
||||
public VehicleRoutingAlgorithm build() {
|
||||
if(stateManager == null) stateManager = new StateManager(vrp);
|
||||
|
|
@ -167,8 +167,10 @@ public class VehicleRoutingAlgorithmBuilder {
|
|||
if(addCoreConstraints){
|
||||
constraintManager.addLoadConstraint();
|
||||
constraintManager.addTimeWindowConstraint();
|
||||
stateManager.updateLoadStates();
|
||||
constraintManager.addSkillsConstraint();
|
||||
stateManager.updateLoadStates();
|
||||
stateManager.updateTimeWindowStates();
|
||||
stateManager.updateSkillStates();
|
||||
}
|
||||
if(algorithmConfig==null){
|
||||
algorithmConfig = new AlgorithmConfig();
|
||||
|
|
|
|||
|
|
@ -461,6 +461,7 @@ public class VehicleRoutingAlgorithms {
|
|||
}
|
||||
stateManager.updateLoadStates();
|
||||
stateManager.updateTimeWindowStates();
|
||||
stateManager.updateSkillStates();
|
||||
stateManager.addStateUpdater(new UpdateEndLocationIfRouteIsOpen());
|
||||
stateManager.addStateUpdater(new OpenRouteStateVerifier());
|
||||
stateManager.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts()));
|
||||
|
|
@ -473,6 +474,7 @@ public class VehicleRoutingAlgorithms {
|
|||
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager,vrp.getConstraints());
|
||||
constraintManager.addTimeWindowConstraint();
|
||||
constraintManager.addLoadConstraint();
|
||||
constraintManager.addSkillsConstraint();
|
||||
|
||||
return readAndCreateAlgorithm(vrp, config, nuOfThreads, null, stateManager, constraintManager, true);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
package jsprit.core.algorithm.state;
|
||||
|
||||
import jsprit.core.problem.Skills;
|
||||
import jsprit.core.problem.solution.route.VehicleRoute;
|
||||
import jsprit.core.problem.solution.route.activity.ActivityVisitor;
|
||||
import jsprit.core.problem.solution.route.activity.TourActivity;
|
||||
import jsprit.core.problem.solution.route.state.StateFactory;
|
||||
|
||||
/**
|
||||
* Created by schroeder on 01.07.14.
|
||||
*/
|
||||
public class UpdateSkills implements StateUpdater, ActivityVisitor{
|
||||
|
||||
private Skills.Builder skillBuilder;
|
||||
|
||||
private StateManager statesManager;
|
||||
|
||||
private VehicleRoute route;
|
||||
|
||||
public UpdateSkills(StateManager statesManager) {
|
||||
this.statesManager = statesManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void begin(VehicleRoute route) {
|
||||
this.route = route;
|
||||
skillBuilder = Skills.Builder.newInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TourActivity activity) {
|
||||
if(activity instanceof TourActivity.JobActivity){
|
||||
Skills skills = ((TourActivity.JobActivity) activity).getJob().getRequiredSkills();
|
||||
skillBuilder.addAllSkills(skills.values());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
Skills skills = skillBuilder.build();
|
||||
statesManager.putTypedInternalRouteState(route, StateFactory.SKILLS, Skills.class, skills);
|
||||
}
|
||||
}
|
||||
|
|
@ -71,7 +71,7 @@ public class Capacity {
|
|||
/**
|
||||
* Returns the inverted capacity, i.e. it multiplies all capacity dimensions with -1.
|
||||
*
|
||||
* @param cap
|
||||
* @param cap2invert
|
||||
* @return inverted capacity
|
||||
* @throws NullPointerException if one of the args is null
|
||||
*/
|
||||
|
|
|
|||
76
jsprit-core/src/main/java/jsprit/core/problem/Skills.java
Normal file
76
jsprit-core/src/main/java/jsprit/core/problem/Skills.java
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
package jsprit.core.problem;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Created by schroeder on 01.07.14.
|
||||
*/
|
||||
public class Skills {
|
||||
|
||||
public static class Builder {
|
||||
|
||||
/**
|
||||
* Returns new instance of skill-builder.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static Builder newInstance(){
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
private Set<String> skills = new HashSet<String>();
|
||||
|
||||
/**
|
||||
* Adds skill. Skill is transformed into lowerCase.
|
||||
*
|
||||
* @param skill
|
||||
* @return
|
||||
*/
|
||||
public Builder addSkill(String skill){
|
||||
skills.add(skill.toLowerCase());
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addAllSkills(Collection<String> skills){
|
||||
for(String skill : skills) this.skills.add(skill);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Skills build(){
|
||||
return new Skills(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Set<String> skills = new HashSet<String>();
|
||||
|
||||
private Skills(Builder builder){
|
||||
skills.addAll(builder.skills);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an unmodifiable set of skills. All skills are inLowerCase.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Set<String> values(){
|
||||
return Collections.unmodifiableSet(skills);
|
||||
}
|
||||
|
||||
/**
|
||||
* Not case sensitive.
|
||||
*
|
||||
* @param skill
|
||||
* @return
|
||||
*/
|
||||
public boolean containsSkill(String skill){
|
||||
return skills.contains(skill.toLowerCase());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -38,7 +38,7 @@ import java.util.List;
|
|||
*/
|
||||
public class ConstraintManager implements HardActivityStateLevelConstraint, HardRouteStateLevelConstraint, SoftActivityConstraint, SoftRouteConstraint{
|
||||
|
||||
public static enum Priority {
|
||||
public static enum Priority {
|
||||
CRITICAL, HIGH, LOW
|
||||
}
|
||||
|
||||
|
|
@ -59,6 +59,8 @@ public class ConstraintManager implements HardActivityStateLevelConstraint, Hard
|
|||
private boolean loadConstraintsSet = false;
|
||||
|
||||
private boolean timeWindowConstraintsSet = false;
|
||||
|
||||
private boolean skillconstraintSet = false;
|
||||
|
||||
public ConstraintManager(VehicleRoutingProblem vrp, RouteAndActivityStateGetter stateManager) {
|
||||
this.vrp = vrp;
|
||||
|
|
@ -113,6 +115,13 @@ public class ConstraintManager implements HardActivityStateLevelConstraint, Hard
|
|||
loadConstraintsSet=true;
|
||||
}
|
||||
}
|
||||
|
||||
public void addSkillsConstraint() {
|
||||
if (!skillconstraintSet){
|
||||
addConstraint(new HardSkillConstraint(stateManager));
|
||||
skillconstraintSet=true;
|
||||
}
|
||||
}
|
||||
|
||||
// public void add
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
package jsprit.core.problem.constraint;
|
||||
|
||||
import jsprit.core.problem.Skills;
|
||||
import jsprit.core.problem.misc.JobInsertionContext;
|
||||
import jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
|
||||
import jsprit.core.problem.solution.route.state.StateFactory;
|
||||
|
||||
/**
|
||||
* SkillConstraint that ensures that only vehicles with according skills can serve route and job to be inserted.
|
||||
*
|
||||
*/
|
||||
public class HardSkillConstraint implements HardRouteStateLevelConstraint{
|
||||
|
||||
private RouteAndActivityStateGetter states;
|
||||
|
||||
public HardSkillConstraint(RouteAndActivityStateGetter states) {
|
||||
this.states = states;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fulfilled(JobInsertionContext insertionContext) {
|
||||
for(String skill : insertionContext.getJob().getRequiredSkills().values()){
|
||||
if(!insertionContext.getNewVehicle().getSkills().containsSkill(skill)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Skills requiredSkillsForRoute = states.getRouteState(insertionContext.getRoute(), StateFactory.SKILLS, Skills.class);
|
||||
for(String skill : requiredSkillsForRoute.values()){
|
||||
if(!insertionContext.getNewVehicle().getSkills().containsSkill(skill)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue