Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WF agent shared memory graph implementation #269

Merged
merged 6 commits into from
Apr 25, 2022
Merged
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
4 changes: 1 addition & 3 deletions src/main/java/app/model/agents/AgentImp.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import app.controller.linAlg.Intersection;
import app.controller.linAlg.Vector;
import app.controller.linAlg.VectorSet;
import app.controller.soundEngine.SoundRay;
import app.controller.soundEngine.SoundVector;
import app.model.Move;
import app.model.agents.Cells.GraphCell;
Expand All @@ -14,7 +13,6 @@
import javafx.scene.paint.Color;
import lombok.Getter;
import lombok.Setter;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.DefaultWeightedEdge;

import java.util.ArrayList;
Expand All @@ -35,7 +33,7 @@ public class AgentImp implements Agent
@Getter protected VectorSet seen;
@Getter protected AgentView agentViewWindow;

@Getter protected static MemoryGraph<GraphCell, DefaultWeightedEdge> world;
@Getter @Setter protected static MemoryGraph<GraphCell, DefaultWeightedEdge> world;

public AgentImp(Vector position, Vector direction, double radius, Team team)
{
Expand Down
55 changes: 0 additions & 55 deletions src/main/java/app/model/agents/Cells/BooleanCell.java

This file was deleted.

5 changes: 0 additions & 5 deletions src/main/java/app/model/agents/Cells/Cell.java

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/java/app/model/agents/Cells/CellFactory.java

This file was deleted.

7 changes: 0 additions & 7 deletions src/main/java/app/model/agents/Cells/CellType.java

This file was deleted.

6 changes: 6 additions & 0 deletions src/main/java/app/model/agents/Cells/GraphCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,10 @@ public boolean equals(Object obj)
}
return false;
}

@Override
public String toString()
{
return XY;
}
}
78 changes: 70 additions & 8 deletions src/main/java/app/model/agents/MemoryGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

import app.controller.linAlg.Vector;
import app.model.agents.Cells.GraphCell;

import lombok.Getter;
import org.jgrapht.Graph;
import lombok.Setter;
import org.jgrapht.graph.SimpleWeightedGraph;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MemoryGraph<Object, DefaultWeightedEdge> extends SimpleWeightedGraph
{
@Getter private HashMap<String, GraphCell> vertices = new HashMap<>();
@Getter private HashMap<String, Vector> cardinalDirections = new HashMap<>();
private int travelDistance;
@Getter private int travelDistance;

@Getter @Setter private GraphCell initialWallFollowPos;
@Getter private double obstaclePheromoneValue = 1000.0;

public MemoryGraph(int distance)
Expand All @@ -38,7 +41,7 @@ public void add_or_adjust_Vertex(Vector position)
}
}

private GraphCell addNewVertex(Vector position)
protected GraphCell addNewVertex(Vector position)
{
Vector vertexCentre = determineVertexCentre(position);
GraphCell cell = new GraphCell(vertexCentre);
Expand All @@ -65,12 +68,12 @@ public void setVertexAsObstacle(Vector currentPosition, Vector movement)
obstacleVertex.setPheromone(obstaclePheromoneValue);
}

private void modifyVertex(GraphCell cell)
protected void modifyVertex(GraphCell cell)
{
cell.setOccupied(true);
}

private void connectNeighbouringVertices(GraphCell currentCell)
protected void connectNeighbouringVertices(GraphCell currentCell)
{
Vector currentPosition = currentCell.getPosition();
for(Vector cardinal: cardinalDirections.values())
Expand Down Expand Up @@ -121,18 +124,24 @@ public void leaveVertex(Vector position, double pheromoneValue)
cell.updatePheromone(pheromoneValue);
}

public void leaveVertex(Vector position)
{
GraphCell cell = getVertexAt(position);
cell.setOccupied(false);
}

public GraphCell getVertexAt(Vector position)
{
return vertices.get(keyGenerator(position));
}

private String keyGenerator(Vector position)
protected String keyGenerator(Vector position)
{
Vector centrePosition = determineVertexCentre(position);
return centrePosition.getX() + " " + centrePosition.getY();
}

private Vector determineVertexCentre(Vector position)
protected Vector determineVertexCentre(Vector position)
{
int x_centre = calculateDimensionCentre(position.getX());
int y_centre = calculateDimensionCentre(position.getY());
Expand Down Expand Up @@ -170,4 +179,57 @@ public void evaporateWorld()
cell.evaporate();
}
}

public ArrayList<GraphCell> getVerticesWithUnexploredNeighbours()
{
// TODO currently checking if vertex has less than 4 neighbours
// but should also check if they're direct neighbours or neighbours through portals?
ArrayList<GraphCell> unexploredFrontier = new ArrayList<>();
for (String v : vertices.keySet())
{
GraphCell vertex = vertices.get(v);
if (!vertex.getObstacle() && edgesOf(vertex).size() < 4)
{
unexploredFrontier.add(vertex);
}
}
return unexploredFrontier;
}

public Vector getNeighbourDir(GraphCell agentCell, GraphCell neighbour)
{
if (agentCell.getX() == neighbour.getX() && neighbour.getY() == agentCell.getY())
{
return new Vector(0,0);
}
else if (agentCell.getX() == neighbour.getX() && neighbour.getY() < agentCell.getY())
{
return new Vector(0,-1); // north of agent
}
else if (agentCell.getX() == neighbour.getX() && neighbour.getY() > agentCell.getY())
{
return new Vector(0,1); // south of agent
}
else if (agentCell.getY() == neighbour.getY() && neighbour.getX() < agentCell.getX())
{
return new Vector(-1,0); // west of agent
}
else if (agentCell.getY() == neighbour.getY() && neighbour.getX() > agentCell.getX())
{
return new Vector(1,0); // east of agent
}
return new Vector();
}

public String getDirectionStr(double directionAngle)
{
for (Map.Entry<String,Vector> dir : cardinalDirections.entrySet())
{
if (dir.getValue().getAngle() == directionAngle)
{
return dir.getKey();
}
}
throw new RuntimeException("No cardinal direction matches given angle.");
}
}
Loading