Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
268 changes: 127 additions & 141 deletions A_Star.cs

Large diffs are not rendered by default.

73 changes: 28 additions & 45 deletions A_Star_MDDs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,22 @@ namespace mapf;
class A_Star_MDDs : IConflictReporting
{
MDD[] problem;
Dictionary<A_Star_MDDs_Node, A_Star_MDDs_Node> closedList;
Run runner;
BinaryHeap<A_Star_MDDs_Node> openList;
readonly Dictionary<A_Star_MDDs_Node, A_Star_MDDs_Node> closedList = [];
readonly Stopwatch stopwatch;
readonly BinaryHeap<A_Star_MDDs_Node> openList = new();
public int expanded;
public int generated;
public int conflictCount;
ConflictAvoidanceTable CAT;
readonly ConflictAvoidanceTable CAT;

public A_Star_MDDs(MDD[] problem, Run runner, ConflictAvoidanceTable CAT)
public A_Star_MDDs(MDD[] problem, Stopwatch stopwatch, ConflictAvoidanceTable CAT)
{
this.expanded = 0;
this.generated = 0;
A_Star_MDDs_Node root;
this.problem = problem;
this.runner = runner;
this.stopwatch = stopwatch;
this.CAT = CAT;
this.closedList = new Dictionary<A_Star_MDDs_Node, A_Star_MDDs_Node>();
this.openList = new BinaryHeap<A_Star_MDDs_Node>();
MDDNode[] sRoot = new MDDNode[problem.Length];
for (int i = 0; i < problem.Length; i++)
{
Expand All @@ -48,7 +46,7 @@ public SinglePlan[] Solve()

while (openList.Count > 0)
{
if (runner.ElapsedMilliseconds() > Constants.MAX_TIME)
if (stopwatch.ElapsedMilliseconds > Constants.MAX_TIME)
{
return null;
}
Expand Down Expand Up @@ -95,7 +93,7 @@ public void Expand(A_Star_MDDs_Node node)

for (int mddIndex = 0; mddIndex < this.problem.Length && intermediateNodes.Count != 0; ++mddIndex)
{
if (runner.ElapsedMilliseconds() > Constants.MAX_TIME)
if (stopwatch.ElapsedMilliseconds > Constants.MAX_TIME)
return;

intermediateNodes = ExpandOneAgent(intermediateNodes, mddIndex);
Expand All @@ -110,9 +108,9 @@ public void Expand(A_Star_MDDs_Node node)
// Accumulating the conflicts count from parent to child
// We're counting conflicts along the entire path, so the parent's conflicts count is added to the child's:
child.conflictCounts = new Dictionary<int, int>(child.prev.conflictCounts);
child.conflictTimes = new Dictionary<int, List<int>>();
child.conflictTimes = [];
foreach (var kvp in child.prev.conflictTimes)
child.conflictTimes[kvp.Key] = new List<int>(kvp.Value);
child.conflictTimes[kvp.Key] = [.. kvp.Value];
child.IncrementConflictCounts(this.CAT); // We're counting conflicts along the entire path, so the parent's conflicts count
// is added to the child's.

Expand Down Expand Up @@ -164,7 +162,7 @@ public void Expand(A_Star_MDDs_Node node)

protected List<A_Star_MDDs_Node> ExpandOneAgent(List<A_Star_MDDs_Node> intermediateNodes, int mddIndex)
{
var generated = new List<A_Star_MDDs_Node>();
List<A_Star_MDDs_Node> generated = [];

// Expand the mdd node
foreach (A_Star_MDDs_Node node in intermediateNodes)
Expand All @@ -175,7 +173,7 @@ protected List<A_Star_MDDs_Node> ExpandOneAgent(List<A_Star_MDDs_Node> intermedi
if (node.currentMoves != null && childMddNode.move.IsColliding(node.currentMoves)) // Can happen. We only prune partially, we don't build the full k-agent MDD.
continue;

var childNode = new A_Star_MDDs_Node(node, mddIndex != node.allSteps.Length - 1);
A_Star_MDDs_Node childNode = new(node, mddIndex != node.allSteps.Length - 1);
childNode.allSteps[mddIndex] = childMddNode;

// Update target conflict count and prune nodes that can't get to the target conflict count
Expand Down Expand Up @@ -208,19 +206,13 @@ protected List<A_Star_MDDs_Node> ExpandOneAgent(List<A_Star_MDDs_Node> intermedi
/// <summary>
/// </summary>
/// <returns>Map each external agent to the number of conflicts with their path the solution has</returns>
public Dictionary<int, int> GetExternalConflictCounts()
{
return this.conflictCounts;
}
public Dictionary<int, int> GetExternalConflictCounts() => this.conflictCounts;

/// <summary>
/// </summary>
/// <returns>Map each external agent to a list of times the solution has a conflict with theirs</returns>
public Dictionary<int, List<int>> GetConflictTimes()
{
return this.conflictTimes;
}

public Dictionary<int, List<int>> GetConflictTimes() => this.conflictTimes;

public void Expand(A_Star_MDDs_Expander currentNode)
{
while (true)
Expand Down Expand Up @@ -256,9 +248,9 @@ public void Expand(A_Star_MDDs_Expander currentNode)
}
}

public int GetGenerated() { return this.generated; }
public int GetGenerated() => this.generated;

public int GetExpanded() { return this.expanded; }
public int GetExpanded() => this.expanded;

private bool GoalTest(A_Star_MDDs_Node toCheck)
{
Expand All @@ -270,30 +262,30 @@ private bool GoalTest(A_Star_MDDs_Node toCheck)
private SinglePlan[] GetAnswer(A_Star_MDDs_Node finish)
{
// TODO: Move the construction of the SinglePlans to a static method in SinglePlan
var routes = new LinkedList<Move>[problem.Length];
List<Move>[] routes = new List<Move>[problem.Length];
for (int i = 0; i < routes.Length; i++)
routes[i] = new LinkedList<Move>();
routes[i] = [];

A_Star_MDDs_Node current = finish;
while (current != null)
{
for (int i = 0; i < problem.Length; i++)
{
routes[i].AddFirst(new Move(current.allSteps[i].move));
routes[i].Add(new Move(current.allSteps[i].move));
}
current = current.prev;
}

var ans = new SinglePlan[problem.Length];
for (int i = 0; i < ans.Length; i++)
{
routes[i].Reverse();
ans[i] = new SinglePlan(routes[i], i);
}
return ans;
}

private bool CheckIfLegal(MDDNode to1, MDDNode to2)
{
return to1.move.IsColliding(to2.move) == false;
}
private bool CheckIfLegal(MDDNode to1, MDDNode to2) => to1.move.IsColliding(to2.move) == false;

private bool IsLegalMove(A_Star_MDDs_Node to)
{
Expand Down Expand Up @@ -380,20 +372,11 @@ public virtual void IncrementConflictCounts(ConflictAvoidanceTable CAT)
/// Returns whether all possible f values were generated from this node already
/// </summary>
/// <returns></returns>
public bool hasMoreChildren()
{
return this.targetDeltaConflictCount <= this.maxDeltaConflictCount;
}
public bool hasMoreChildren() => this.targetDeltaConflictCount <= this.maxDeltaConflictCount;

public bool IsAlreadyExpanded()
{
return alreadyExpanded;
}
public bool IsAlreadyExpanded() => alreadyExpanded;

public bool hasChildrenForCurrentDeltaConflictCount(int agentNum = 0)
{
return existsChildForConflictCount(agentNum, this.remainingDeltaConflictCount);
}
public bool hasChildrenForCurrentDeltaConflictCount(int agentNum = 0) => existsChildForConflictCount(agentNum, this.remainingDeltaConflictCount);

/// <summary>
/// An MDD child node was chosen between calculating the singleAgentDeltaConflictCounts and this call.
Expand Down Expand Up @@ -591,7 +574,7 @@ public override int GetHashCode()
}
}

public int GetDepth() { return allSteps[0].move.time; }
public int GetDepth() { return allSteps[0].move.Time; }

/// <summary>
/// Updates the conflictCount member according to given CATs. Table may be null.
Expand Down
19 changes: 7 additions & 12 deletions A_Star_WithOD.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace mapf;
Expand All @@ -20,7 +21,7 @@ public A_Star_WithOD(IHeuristicCalculator<WorldState> heuristic = null, bool mSt

override protected WorldState CreateSearchRoot(int minDepth = -1, int minCost = -1, MDDNode mddNode = null)
{
return new WorldStateWithOD(this.instance.agents, minDepth, minCost, mddNode);
return new WorldStateWithOD(this.instance.Agents, minDepth, minCost, mddNode);
}

protected override WorldState CreateSearchNode(WorldState from)
Expand All @@ -30,12 +31,12 @@ protected override WorldState CreateSearchNode(WorldState from)

public override string GetName() { return base.GetName() + "+OD"; }

public override void Setup(ProblemInstance problemInstance, int minDepth, Run runner,
public override void Setup(ProblemInstance problemInstance, int minDepth, Stopwatch stopwatch,
ConflictAvoidanceTable CAT = null,
ISet<CbsConstraint> constraints = null, ISet<CbsConstraint> positiveConstraints = null,
int minCost = -1, int maxCost = int.MaxValue, MDD mdd = null)
{
base.Setup(problemInstance, minDepth, runner, CAT, constraints, positiveConstraints,
base.Setup(problemInstance, minDepth, stopwatch, CAT, constraints, positiveConstraints,
minCost, maxCost, mdd);
this.expandedFullStates = 0;
this.generatedFullStates = 0;
Expand Down Expand Up @@ -74,7 +75,7 @@ protected override List<WorldState> ExpandOneAgent(List<WorldState> intermediate

var generated = base.ExpandOneAgent(intermediateNodes, agentIndex);

int childAgentTurn = ((parent.agentTurn + 1) % (this.instance.agents.Length));
int childAgentTurn = ((parent.agentTurn + 1) % (this.instance.Agents.Length));
foreach (var node in generated)
{
WorldStateWithOD childNode = (WorldStateWithOD)node;
Expand All @@ -84,7 +85,7 @@ protected override List<WorldState> ExpandOneAgent(List<WorldState> intermediate
// Makespan increases only if this is the move of the first agent. This makes sure that under a makespan
// cost function, partial nodes have a correct cost and can even serve as goal nodes.
if (parent.agentTurn != 0)
childNode.makespan--; // Cancel the increment in base
childNode.Makespan--; // Cancel the increment in base
}

this.alreadyExpanded = true;
Expand Down Expand Up @@ -121,13 +122,7 @@ public override void OutputStatistics(TextWriter output)
output.Write(this.generatedFullStates + Run.RESULTS_DELIMITER);
}

public override int NumStatsColumns
{
get
{
return 2 + base.NumStatsColumns;
}
}
public override int NumStatsColumns => 2 + base.NumStatsColumns;

public override void ClearAccumulatedStatistics()
{
Expand Down
10 changes: 5 additions & 5 deletions AdditivePDBs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public void build(ProblemInstance pi, WorldState s)
// with the first two, then the second two, etc.

PDBs = new List<PDB>();
if (s.allAgentsState.Length > 1)
if (s.AllAgentsState.Length > 1)
{
for (uint i = 0; i < s.allAgentsState.Length - 1; i += 2)
for (uint i = 0; i < s.AllAgentsState.Length - 1; i += 2)
{
// Make a list of agents we want to include together in the
// next additive pattern database. We specify agents by
Expand All @@ -50,7 +50,7 @@ public void build(ProblemInstance pi, WorldState s)
// node. This is done by passing into the state copy
// constructor our list of important agents.

WorldState tws = new WorldState(s.allAgentsState, agentsToConsider);
WorldState tws = new WorldState(s.AllAgentsState, agentsToConsider);

// Initialize, build, and save the new pattern database.

Expand All @@ -65,11 +65,11 @@ public void build(ProblemInstance pi, WorldState s)
// Create single shortest path pattern database heuristics for the
// remaining agents if we have any left over.

if (s.allAgentsState.Length % 2 == 1)
if (s.AllAgentsState.Length % 2 == 1)
{
SumIndividualCosts pdb = new SumIndividualCosts();
List<uint> agentsToConsider = new List<uint>(1);
agentsToConsider.Add((uint) s.allAgentsState.Length - 1);
agentsToConsider.Add((uint) s.AllAgentsState.Length - 1);
pdb.Init(pi, agentsToConsider);
pdb.build();
PDBs.Add(pdb);
Expand Down
2 changes: 1 addition & 1 deletion Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public Agent(Agent a)
public Agent(int Goal_X, int Goal_Y, int agentNum)
{
this.agentNum = agentNum;
Goal = new Move(Goal_X, Goal_Y, Move.Direction.NO_DIRECTION);
Goal = new Move(Goal_X, Goal_Y, Direction.NO_DIRECTION);
}
public override string ToString()
{
Expand Down
Loading