Skip to content

Commit

Permalink
Cleaned up DistanceTableAStarHeuristic and its subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
J-morag committed Oct 14, 2021
1 parent cf25221 commit 9550e36
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package BasicCBS.Solvers.AStar;

import BasicCBS.Instances.Agent;
import BasicCBS.Instances.Maps.*;
import BasicCBS.Instances.Maps.Coordinates.I_Coordinate;
import BasicCBS.Instances.Maps.I_Location;
import BasicCBS.Instances.Maps.I_Map;

import java.util.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
* A {@link AStarHeuristic} that uses a pre-calculated dictionary of distances from possible goal locations to every
Expand All @@ -13,32 +16,37 @@
public class DistanceTableAStarHeuristic implements AStarHeuristic {
// nicetohave avoid duplicates (when two agents have the same goal)

private Map<Agent, Map<I_Location, Integer>> distanceDictionaries;
/**
* Dictionary from target location, to its distance from any location on the map.
*/
private Map<I_Location, Map<I_Location, Integer>> distanceDictionaries;
private I_Map map;

public Map<Agent, Map<I_Location, Integer>> getDistanceDictionaries() {
public Map<I_Location, Map<I_Location, Integer>> getDistanceDictionaries() {
return distanceDictionaries;
}

public DistanceTableAStarHeuristic(List<? extends Agent> agents, I_Map map) {

this.map = map;
this.distanceDictionaries = new HashMap<>();
for (int i = 0; i < agents.size(); i++) {
for (Agent agent : agents) {
addAgentToHeuristic(agent);
}
}

//this map will entered to distanceDictionaries for every agent
public void addAgentToHeuristic(Agent agent) {
I_Location target = map.getMapCell(agent.target);
if (!distanceDictionaries.containsKey(target)){
//this map will be added to distanceDictionaries for every agent
Map<I_Location, Integer> mapForAgent = new HashMap<>();
this.distanceDictionaries.put(agents.get(i), mapForAgent);
LinkedList<I_Location> queue = new LinkedList<>();
I_Coordinate i_coordinate = agents.get(i).target;
I_Location mapCell = map.getMapCell(i_coordinate);
this.distanceDictionaries.put(target, mapForAgent);

//distance of a graphMapCell from itself
this.distanceDictionaries.get(agents.get(i)).put(mapCell, 0);
this.distanceDictionaries.get(target).put(target, 0);

//all the neighbors of a graphMapCell
List<I_Location> neighbors = mapCell.getNeighbors();
for (int j = 0; j < neighbors.size(); j++) {
queue.add(neighbors.get(j));
}
List<I_Location> neighbors = target.getNeighbors();
LinkedList<I_Location> queue = new LinkedList<>(neighbors);

int distance = 1;
int count = queue.size();
Expand All @@ -47,8 +55,8 @@ public DistanceTableAStarHeuristic(List<? extends Agent> agents, I_Map map) {
I_Location i_location = queue.remove(0);

//if a graphMapCell didn't got a distance yet
if (!(this.distanceDictionaries.get(agents.get(i)).containsKey(i_location))) {
this.distanceDictionaries.get(agents.get(i)).put(i_location, distance);
if (!(this.distanceDictionaries.get(target).containsKey(i_location))) {
this.distanceDictionaries.get(target).put(i_location, distance);

//add all the neighbors of the current graphMapCell to the queue
List<I_Location> neighborsCell = i_location.getNeighbors();
Expand All @@ -66,8 +74,13 @@ public DistanceTableAStarHeuristic(List<? extends Agent> agents, I_Map map) {

@Override
public float getH(SingleAgentAStar_Solver.AStarState state) {
Map<I_Location, Integer> relevantDictionary = this.distanceDictionaries.get(state.getMove().agent);
return relevantDictionary.get(state.getMove().currLocation);
return getHForAgentAndCurrentLocation(state.getMove().agent, state.getMove().currLocation);
}

// todo work only with locations
public float getHForAgentAndCurrentLocation(Agent agent, I_Location currLocation){
Map<I_Location, Integer> relevantDictionary = this.distanceDictionaries.get(map.getMapCell(agent.target));
return relevantDictionary.get(currLocation);
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package BasicCBS.Solvers.ICTS.MDDs;

import BasicCBS.Instances.Agent;
import BasicCBS.Instances.Maps.I_Location;
import BasicCBS.Instances.Maps.I_Map;
import BasicCBS.Solvers.AStar.DistanceTableAStarHeuristic;

import java.util.List;
import java.util.Map;

public class DistanceTableAStarHeuristicICTS extends DistanceTableAStarHeuristic {
public DistanceTableAStarHeuristicICTS(List<? extends Agent> agents, I_Map map) {
super(agents, map);
}

public void setH(MDDSearchNode node) {
Map<I_Location, Integer> relevantDictionary = getDistanceDictionaries().get(node.getAgent());
node.setH(relevantDictionary.get(node.getLocation()));
node.setH(getHForAgentAndCurrentLocation(node.getAgent(), node.getLocation()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private Solution mergeSolutions(Set<AgentsGroup> agentGroups) {
}

protected I_ConflictManager getConflictManager(Set<AgentsGroup> agentGroups) {
I_ConflictManager conflictManager = new NaiveConflictDetection(true);
I_ConflictManager conflictManager = new NaiveConflictDetection();
for (AgentsGroup ag : agentGroups){
for (SingleAgentPlan plan : ag.getSolution()){
conflictManager.addPlan(plan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public class DistanceTableAStarHeuristicTest {
I_Map map= MapFactory.newSimple4Connected2D_GraphMap(map_2D_H);

/* = Equals Maps = */
private boolean equalsAllAgentMap(Map<Agent, Map<I_Location, Integer>> expectedValues, Map<Agent, Map<I_Location, Integer>> actualValues){
private boolean equalsAllAgentMap(Map<I_Location, Map<I_Location, Integer>> expectedValues, Map<I_Location, Map<I_Location, Integer>> actualValues){

if( expectedValues.size() != actualValues.size() ){
return false;
}
for (Map.Entry<Agent, Map<I_Location, Integer>> agentMapEntry: expectedValues.entrySet()){
for (Map.Entry<I_Location, Map<I_Location, Integer>> agentMapEntry: expectedValues.entrySet()){

Agent agent = agentMapEntry.getKey();
Map<I_Location, Integer> expectedCellMap = expectedValues.get(agent);
Map<I_Location, Integer> actualCellMap = actualValues.get(agent);
I_Location target = agentMapEntry.getKey();
Map<I_Location, Integer> expectedCellMap = expectedValues.get(target);
Map<I_Location, Integer> actualCellMap = actualValues.get(target);

if (! this.equalsAllCellMap(expectedCellMap,actualCellMap)){
return false;
Expand Down Expand Up @@ -87,9 +87,9 @@ public void test(){

/* = Expected values = */

Map<Agent, Map<I_Location, Integer>> expected=new HashMap<>();
Map<I_Location, Integer> insideMap=new HashMap<>();
Map<I_Location, Integer> insideMap2=new HashMap<>();
Map<I_Location, Map<I_Location, Integer>> expected = new HashMap<>();
Map<I_Location, Integer> insideMap = new HashMap<>();
Map<I_Location, Integer> insideMap2 = new HashMap<>();
insideMap.put(map.getMapCell(new Coordinate_2D(1,3)),1);
insideMap.put(map.getMapCell(new Coordinate_2D(2,3)),2);
insideMap.put(map.getMapCell(new Coordinate_2D(1,2)),2);
Expand All @@ -108,11 +108,11 @@ public void test(){
insideMap2.put(map.getMapCell(new Coordinate_2D(2,0)),5);
insideMap2.put(map.getMapCell(new Coordinate_2D(2,3)),0);

expected.put(agent_1,insideMap);
expected.put(agent_2,insideMap2);
expected.put(map.getMapCell(agent_1.target), insideMap);
expected.put(map.getMapCell(agent_2.target), insideMap2);

/* = Test actual values = */
DistanceTableAStarHeuristic distanceTableAStarHeuristic=new DistanceTableAStarHeuristic(list,map);
DistanceTableAStarHeuristic distanceTableAStarHeuristic = new DistanceTableAStarHeuristic(list, map);

Assert.assertTrue(equalsAllAgentMap(expected, distanceTableAStarHeuristic.getDistanceDictionaries()));
}
Expand Down

0 comments on commit 9550e36

Please sign in to comment.