Skip to content

Commit 82e9651

Browse files
authored
refactor TransientMAPFBehaviour (#86)
* Remove SST from TransientMAPFSettings, use only I_SolutionCostFunction. In PrP, properly initialize subproblem GAndH when using SST cost function * fix TransientMAPFExampleMain
1 parent dc6e4a3 commit 82e9651

File tree

11 files changed

+112
-92
lines changed

11 files changed

+112
-92
lines changed

Diff for: src/main/java/BasicMAPF/Solvers/CBS/CBS_Solver.java

+37-22
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import BasicMAPF.CostFunctions.I_SolutionCostFunction;
44
import BasicMAPF.CostFunctions.SumOfCosts;
5+
import BasicMAPF.CostFunctions.SumServiceTimes;
56
import BasicMAPF.DataTypesAndStructures.*;
67
import BasicMAPF.Instances.Agent;
78
import BasicMAPF.Instances.MAPF_Instance;
89
import BasicMAPF.Instances.Maps.Coordinates.I_Coordinate;
10+
import BasicMAPF.Solvers.AStar.CostsAndHeuristics.CachingDistanceTableHeuristic;
911
import BasicMAPF.Solvers.AStar.CostsAndHeuristics.ServiceTimeGAndH;
1012
import BasicMAPF.Solvers.AStar.GoalConditions.VisitedTargetAStarGoalCondition;
1113
import BasicMAPF.Solvers.AStar.GoalConditions.VisitedTargetAndBlacklistAStarGoalCondition;
@@ -24,7 +26,7 @@
2426
import BasicMAPF.Solvers.AStar.RunParameters_SAAStar;
2527
import BasicMAPF.Solvers.AStar.SingleAgentAStar_Solver;
2628
import BasicMAPF.Solvers.ConstraintsAndConflicts.*;
27-
import TransientMAPF.TransientMAPFBehaviour;
29+
import TransientMAPF.TransientMAPFSettings;
2830
import TransientMAPF.TransientMAPFSolution;
2931

3032
import java.util.*;
@@ -100,7 +102,7 @@ public class CBS_Solver extends A_Solver {
100102
*/
101103
private final boolean sharedSources;
102104

103-
private final TransientMAPFBehaviour transientMAPFBehaviour;
105+
private final TransientMAPFSettings transientMAPFSettings;
104106

105107
/* = Constructors = */
106108

@@ -112,11 +114,11 @@ public class CBS_Solver extends A_Solver {
112114
* @param openListManagementMode @see {@link OpenListManagementMode}. @Nullable
113115
* @param costFunction a cost function for solutions. @Nullable
114116
* @param cbsNodeComparator determines how to sort {@link #openList OPEN}. @Nullable
115-
* @param useCorridorReasoning whether or not to use corridor reasoning.
117+
* @param useCorridorReasoning whether to use corridor reasoning.
116118
*/
117119
public CBS_Solver(I_Solver lowLevelSolver, I_OpenList<CBS_Node> openList, OpenListManagementMode openListManagementMode,
118120
I_SolutionCostFunction costFunction, Comparator<? super CBS_Node> cbsNodeComparator, Boolean useCorridorReasoning,
119-
Boolean sharedGoals, Boolean sharedSources, TransientMAPFBehaviour transientMAPFBehaviour) {
121+
Boolean sharedGoals, Boolean sharedSources, TransientMAPFSettings transientMAPFSettings) {
120122
this.lowLevelSolver = Objects.requireNonNullElseGet(lowLevelSolver, SingleAgentAStar_Solver::new);
121123
this.openList = Objects.requireNonNullElseGet(openList, OpenListHeap::new);
122124
this.openListManagementMode = openListManagementMode != null ? openListManagementMode : OpenListManagementMode.AUTOMATIC;
@@ -127,9 +129,9 @@ public CBS_Solver(I_Solver lowLevelSolver, I_OpenList<CBS_Node> openList, OpenLi
127129
this.CBSNodeComparator = cbsNodeComparator != null ? cbsNodeComparator : new CBSNodeComparatorForcedTotalOrdering();
128130
this.sharedGoals = Objects.requireNonNullElse(sharedGoals, false);
129131
this.sharedSources = Objects.requireNonNullElse(sharedSources, false);
130-
this.transientMAPFBehaviour = Objects.requireNonNullElse(transientMAPFBehaviour, TransientMAPFBehaviour.regularMAPF);
132+
this.transientMAPFSettings = Objects.requireNonNullElse(transientMAPFSettings, TransientMAPFSettings.defaultRegularMAPF);
131133

132-
super.name = "CBS" + (this.transientMAPFBehaviour.isTransientMAPF() ? "t" : "");
134+
super.name = "CBS" + (this.transientMAPFSettings.isTransientMAPF() ? "t" : "");
133135
}
134136

135137
/**
@@ -149,12 +151,22 @@ protected void init(MAPF_Instance instance, RunParameters runParameters) {
149151
this.generatedNodes = 0;
150152
this.expandedNodes = 0;
151153
this.instance = instance;
152-
this.singleAgentGAndH = runParameters.singleAgentGAndH != null ? runParameters.singleAgentGAndH :
153-
this.lowLevelSolver instanceof SingleAgentAStar_Solver ?
154-
this.transientMAPFBehaviour == TransientMAPFBehaviour.transientMAPFsstWithBlacklist ?
155-
new ServiceTimeGAndH(new DistanceTableSingleAgentHeuristic(new ArrayList<>(this.instance.agents), this.instance.map)) :
156-
new DistanceTableSingleAgentHeuristic(new ArrayList<>(this.instance.agents), this.instance.map) :
157-
null;
154+
155+
// heuristic
156+
if (runParameters.singleAgentGAndH != null){
157+
this.singleAgentGAndH = runParameters.singleAgentGAndH;
158+
}
159+
else {
160+
if (this.lowLevelSolver instanceof SingleAgentAStar_Solver){
161+
this.singleAgentGAndH = new DistanceTableSingleAgentHeuristic(new ArrayList<>(instance.agents), instance.map);
162+
}
163+
if (this.singleAgentGAndH instanceof CachingDistanceTableHeuristic){
164+
((CachingDistanceTableHeuristic)this.singleAgentGAndH).setCurrentMap(instance.map);
165+
}
166+
if (this.singleAgentGAndH != null && this.costFunction instanceof SumServiceTimes){
167+
this.singleAgentGAndH = new ServiceTimeGAndH(this.singleAgentGAndH);
168+
}
169+
}
158170
}
159171

160172
/* = algorithm = */
@@ -189,7 +201,7 @@ private void initOpen(I_ConstraintSet initialConstraints) {
189201
*/
190202
private CBS_Node generateRoot(I_ConstraintSet initialConstraints) {
191203
// init an empty solution
192-
Solution solution = transientMAPFBehaviour.isTransientMAPF() ? new TransientMAPFSolution() : new Solution();
204+
Solution solution = transientMAPFSettings.isTransientMAPF() ? new TransientMAPFSolution() : new Solution();
193205
// for every agent, add its plan to the solution
194206
for (Agent agent :
195207
this.instance.agents) {
@@ -292,7 +304,7 @@ private CBS_Node generateNode(CBS_Node parent, Constraint constraint, boolean co
292304

293305
// replace with copies if required
294306
if(copyDatastructures) {
295-
solution = transientMAPFBehaviour.isTransientMAPF() ? new TransientMAPFSolution(solution) : new Solution(solution);
307+
solution = transientMAPFSettings.isTransientMAPF() ? new TransientMAPFSolution(solution) : new Solution(solution);
296308
}
297309

298310
// modify for this node
@@ -367,16 +379,19 @@ private RunParameters getSubproblemParameters(Solution currentSolution, I_Constr
367379
RunParameters_SAAStar astarSubproblemParameters = new RunParameters_SAAStar(subproblemParametes);
368380

369381
// TMAPF goal condition
370-
if (transientMAPFBehaviour == TransientMAPFBehaviour.transientMAPF || transientMAPFBehaviour == TransientMAPFBehaviour.transientMAPFsstWithBlacklist){
371-
astarSubproblemParameters.goalCondition = new VisitedTargetAStarGoalCondition();
372-
} else if (transientMAPFBehaviour == TransientMAPFBehaviour.transientMAPFWithBlacklist) {
373-
Set<I_Coordinate> targetsOfAgentsThatHaventPlannedYet = new HashSet<>();
374-
for (Agent agentToBlack: this.instance.agents) {
375-
if (!agent.equals(agentToBlack)){
376-
targetsOfAgentsThatHaventPlannedYet.add(agentToBlack.target);
382+
if (transientMAPFSettings.isTransientMAPF()){
383+
if (transientMAPFSettings.useBlacklist()) {
384+
Set<I_Coordinate> targetsOfAgentsThatHaventPlannedYet = new HashSet<>();
385+
for (Agent agentToBlack : this.instance.agents) {
386+
if (!agent.equals(agentToBlack)) {
387+
targetsOfAgentsThatHaventPlannedYet.add(agentToBlack.target);
388+
}
377389
}
390+
astarSubproblemParameters.goalCondition = new VisitedTargetAndBlacklistAStarGoalCondition(targetsOfAgentsThatHaventPlannedYet);
391+
}
392+
else {
393+
astarSubproblemParameters.goalCondition = new VisitedTargetAStarGoalCondition();
378394
}
379-
astarSubproblemParameters.goalCondition = new VisitedTargetAndBlacklistAStarGoalCondition(targetsOfAgentsThatHaventPlannedYet);
380395
}
381396

382397
SingleUseConflictAvoidanceTable cat = new SingleUseConflictAvoidanceTable(currentSolution, agent);

Diff for: src/main/java/BasicMAPF/Solvers/LargeNeighborhoodSearch/LNSBuilder.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import BasicMAPF.CostFunctions.I_SolutionCostFunction;
44
import BasicMAPF.Solvers.I_Solver;
5-
import TransientMAPF.TransientMAPFBehaviour;
5+
import TransientMAPF.TransientMAPFSettings;
66

77
import java.util.List;
88

@@ -15,7 +15,7 @@ public class LNSBuilder {
1515
private Integer neighborhoodSize = null;
1616
private I_Solver initialSolver;
1717
private I_Solver iterationsSolver;
18-
private TransientMAPFBehaviour transientMAPFBehaviour = null;
18+
private TransientMAPFSettings transientMAPFSettings = null;
1919

2020
public LNSBuilder setSolutionCostFunction(I_SolutionCostFunction solutionCostFunction) {
2121
this.solutionCostFunction = solutionCostFunction;
@@ -47,8 +47,8 @@ public LNSBuilder setNeighborhoodSize(Integer neighborhoodSize) {
4747
return this;
4848
}
4949

50-
public LNSBuilder setTransientMAPFBehaviour(TransientMAPFBehaviour transientMAPFBehaviour) {
51-
this.transientMAPFBehaviour = transientMAPFBehaviour;
50+
public LNSBuilder setTransientMAPFBehaviour(TransientMAPFSettings transientMAPFSettings) {
51+
this.transientMAPFSettings = transientMAPFSettings;
5252
return this;
5353
}
5454

@@ -63,6 +63,6 @@ public LNSBuilder setIterationsSolver(I_Solver iterationsSolver) {
6363
}
6464

6565
public LargeNeighborhoodSearch_Solver createLNS() {
66-
return new LargeNeighborhoodSearch_Solver(solutionCostFunction, destroyHeuristics, sharedGoals, sharedSources, reactionFactor, neighborhoodSize, initialSolver, iterationsSolver, transientMAPFBehaviour);
66+
return new LargeNeighborhoodSearch_Solver(solutionCostFunction, destroyHeuristics, sharedGoals, sharedSources, reactionFactor, neighborhoodSize, initialSolver, iterationsSolver, transientMAPFSettings);
6767
}
6868
}

Diff for: src/main/java/BasicMAPF/Solvers/LargeNeighborhoodSearch/LargeNeighborhoodSearch_Solver.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import BasicMAPF.Solvers.PrioritisedPlanning.PrioritisedPlanning_Solver;
1515
import BasicMAPF.Solvers.PrioritisedPlanning.RestartsStrategy;
1616
import Environment.Metrics.InstanceReport;
17-
import TransientMAPF.TransientMAPFBehaviour;
17+
import TransientMAPF.TransientMAPFSettings;
1818
import TransientMAPF.TransientMAPFSolution;
1919

2020
import java.util.*;
@@ -55,7 +55,7 @@ public class LargeNeighborhoodSearch_Solver extends A_Solver {
5555
* If true, agents staying at their source (since the start) will not conflict
5656
*/
5757
private final boolean sharedSources;
58-
private final TransientMAPFBehaviour transientMAPFBehaviour;
58+
private final TransientMAPFSettings transientMAPFSettings;
5959

6060
/* = Fields related to the run = */
6161

@@ -78,7 +78,7 @@ public class LargeNeighborhoodSearch_Solver extends A_Solver {
7878
* @param sharedSources if agents share goals, they will not conflict at their source until they move.
7979
* @param reactionFactor how quickly ALNS adapts to which heuristic is more successful. default = 0.01 .
8080
* @param neighborhoodSize What size neighborhoods to select.
81-
* @param transientMAPFBehaviour indicates whether to solve transient-MAPF instead of regular MAPF.
81+
* @param transientMAPFSettings indicates whether to solve transient-MAPF instead of regular MAPF.
8282
* @param initialSolver a solver to use for the initial solution.
8383
* If null, use {@link PrioritisedPlanning_Solver} with random restarts until an initial solution is found.
8484
* @param iterationsSolver a solver to use for solving sub-problems for a subset of agents while avoiding other agents.
@@ -87,21 +87,21 @@ public class LargeNeighborhoodSearch_Solver extends A_Solver {
8787
*/
8888
LargeNeighborhoodSearch_Solver(I_SolutionCostFunction solutionCostFunction, List<I_DestroyHeuristic> destroyHeuristics,
8989
Boolean sharedGoals, Boolean sharedSources, Double reactionFactor, Integer neighborhoodSize,
90-
I_Solver initialSolver, I_Solver iterationsSolver, TransientMAPFBehaviour transientMAPFBehaviour) {
90+
I_Solver initialSolver, I_Solver iterationsSolver, TransientMAPFSettings transientMAPFSettings) {
9191

92-
this.transientMAPFBehaviour = Objects.requireNonNullElse(transientMAPFBehaviour, TransientMAPFBehaviour.regularMAPF);
92+
this.transientMAPFSettings = Objects.requireNonNullElse(transientMAPFSettings, TransientMAPFSettings.defaultRegularMAPF);
9393
this.solutionCostFunction = Objects.requireNonNullElseGet(solutionCostFunction, SumOfCosts::new);
9494

9595
this.initialSolver = Objects.requireNonNullElseGet(initialSolver,
9696
// PP with random restarts until an initial solution is found
9797
() -> new PrioritisedPlanning_Solver(null, null, this.solutionCostFunction,
9898
new RestartsStrategy(RestartsStrategy.RestartsKind.none, 0, RestartsStrategy.RestartsKind.randomRestarts),
99-
sharedGoals, sharedSources, this.transientMAPFBehaviour));
99+
sharedGoals, sharedSources, this.transientMAPFSettings));
100100
this.iterationsSolver = Objects.requireNonNullElseGet(iterationsSolver,
101101
// PP with just one attempt
102102
() -> new PrioritisedPlanning_Solver(null, null, this.solutionCostFunction,
103103
new RestartsStrategy(RestartsStrategy.RestartsKind.none, 0, RestartsStrategy.RestartsKind.none),
104-
sharedGoals, sharedSources, this.transientMAPFBehaviour));
104+
sharedGoals, sharedSources, this.transientMAPFSettings));
105105

106106
this.destroyHeuristics = destroyHeuristics == null || destroyHeuristics.isEmpty() ?
107107
List.of(new RandomDestroyHeuristic(), new MapBasedDestroyHeuristic())
@@ -112,7 +112,7 @@ public class LargeNeighborhoodSearch_Solver extends A_Solver {
112112
this.reactionFactor = Objects.requireNonNullElse(reactionFactor, 0.01);
113113
this.neighborhoodSize = Objects.requireNonNullElse(neighborhoodSize, 5);
114114

115-
super.name = (this.destroyHeuristics.size() > 1 ? "A" : "") + "LNS" + (this.transientMAPFBehaviour.isTransientMAPF() ? "t" : "") + (this.destroyHeuristics.size() == 1 ? "-" + destroyHeuristics.get(0).getClass().getSimpleName() : "");
115+
super.name = (this.destroyHeuristics.size() > 1 ? "A" : "") + "LNS" + (this.transientMAPFSettings.isTransientMAPF() ? "t" : "") + (this.destroyHeuristics.size() == 1 ? "-" + destroyHeuristics.get(0).getClass().getSimpleName() : "");
116116
}
117117

118118
/* = initialization = */
@@ -205,7 +205,7 @@ protected Solution solveLNS(MAPF_Instance instance, I_ConstraintSet initialConst
205205
}
206206

207207
private Solution finalizeSolution(Solution bestSolution) {
208-
return (transientMAPFBehaviour.isTransientMAPF() && bestSolution != null) ? new TransientMAPFSolution(bestSolution) : bestSolution;
208+
return (transientMAPFSettings.isTransientMAPF() && bestSolution != null) ? new TransientMAPFSolution(bestSolution) : bestSolution;
209209
}
210210

211211
private void updateDestroyHeuristicWeight(Solution newSubsetSolution, Solution oldSubsetSolution, int destroyHeuristicIndex) {

0 commit comments

Comments
 (0)