📄 pheromoneagent.java
字号:
package graph;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.GraphConstants;
import java.io.Serializable;
import java.util.Iterator;
/*
* PheromoneAgent:
* the agent starts in a vertex, and has status = MOVING
* in the next step, the agent finds out if he has a pheromone pointing to
* the right resource. If this is the case, he uses it and follows the path. Otherwise, he finds the
* best node, based on the pheromones found in neighbouring nodes, only one hop away.
* When a pheromone is used, its strength is
* decreased. The agent jumps on the connecting edge, and gets the
* status MOVING. When the agent reaches another node, status is changed to
* SEARCHING. When the resource is not found, status is changed to EXPLORING, and
* the same procedure is followed, as explained above.
* When the resource is found, status is changed to RETURNING
* and the next node is chosen, based on the gradient-field, constructed from
* the origin. When there is no gradient-field, a random node is chosen as the
* successor. While returning, a pheromone is dropped, or an existing pheromone is
* strenghtend. The existing pheromone is only strenghtened when the previousNode of
* the agent equals the node the pheromone is pointing to.
*/
public class PheromoneAgent extends Agent implements Serializable
{
public PheromoneAgent(DefaultGraphCell origin, ResourceQuery searchFor)
{
super(origin, searchFor);
}
public void run(DefaultGraphCell currentGraphCell)
{
age++;
Properties properties = GraphEventManager.getReference().getProperties(currentGraphCell);
// Ant bevindt zich op een edge
if (currentGraphCell instanceof DefaultEdge)
{
if (status == MOVING)
{
int capacity = GraphEventManager.getReference().getEdgeProperties((DefaultEdge)currentGraphCell).getCapacity();
if (progress >= 1)
{
// We zijn aan de volgende knoop
//Spring van de edge
properties.removeInformation("Agents", this);
//Kruip in de volgende vertex
GraphEventManager.getReference().getProperties(nextNode).addInformation("Agents", this);
status = SEARCHING;
}
else
{
progress = Math.min (1, progress + (0.05 * capacity));
}
}
else if (status == RETURNING)
{
int capacity = GraphEventManager.getReference().getEdgeProperties((DefaultEdge)currentGraphCell).getCapacity();
if (progress >= 1)
{
// We zijn aan de volgende knoop
//Spring van de edge
properties.removeInformation("Agents", this);
//Kruip in de volgende vertex
GraphEventManager.getReference().getProperties(nextNode).addInformation("Agents", this);
status = RETURNING;
}
else
{
progress = Math.min (1, progress + (0.05 * capacity));
}
}
}
// Ant bevindt zich in een vertex
else
{
if (status == MOVING)
{
//System.out.println("PheromoneAgent :: run : MOVING");
properties.removeInformation("Agents", this);
DefaultEdge newEdge = GraphEventManager.getReference().eventGetConnectingEdge(previousNode, nextNode);
//System.out.println(newEdge);
//System.out.print("van " + ((DefaultGraphCell)((DefaultPort)newEdge.getSource()).getParent()) + " naar ");
//System.out.println(((DefaultGraphCell)((DefaultPort)newEdge.getTarget()).getParent()));
Properties edgeProperties = (Properties)GraphConstants.getValue(newEdge.getAttributes());
edgeProperties.addInformation("Agents", this);
}
if (status == SEARCHING)
{
//System.out.println("Search for resource");
Properties currentNodeProperties = (Properties)GraphConstants.getValue(nextNode.getAttributes());
if (currentNodeProperties.getListInformation("Resources") != null)
{
Iterator li = currentNodeProperties.getListInformation("Resources").iterator();
while (li.hasNext())
{
if (((Resource)li.next()).getName().equals(searchFor.getSearchFor()))
{
System.out.println("Found ! (in " + currentNodeProperties.getName() + ")");
previousNode = currentGraphCell;
status = RETURNING;
/*
DefaultEdge returningEdge = GraphEventManager.getReference().getConnectingEdge(previousNode, nextNode);
DefaultGraphCell tempCell = nextNode;
nextNode = previousNode;
previousNode = tempCell;
GraphEventManager.getReference().getProperties(returningEdge).addInformation("Agents", this);
GraphEventManager.getReference().getProperties(previousNode).removeInformation("Agents", this);
*/
}
}
if (status != RETURNING)
{
// dus resource is niet gevonden
//System.out.println("Not found :-(");
status = EXPLORING;
}
}
else
{
//System.out.println("No resources ...");
status = EXPLORING;
}
}
if (status == EXPLORING)
{
// we zitten in next_node, wat is de volgende stap ?
//System.out.println("Exploring: looking for next node");
// Is er lokaal een pheromoon aanwezig dat aangeeft langs waar het is ?
Iterator pheromoneIterator = GraphEventManager.getReference().getProperties(currentGraphCell).getInformation("Pheromones");
Pheromone tempPheromone = null;
Pheromone tempPheromone2 = null;
boolean followpath = false;
int strength = 0;
if (pheromoneIterator != null)
{
while (pheromoneIterator.hasNext())
{
tempPheromone = (Pheromone)pheromoneIterator.next();
//System.out.println("Gradient met source: " + tempGradient.getSource() + " =?= " + origin );
if (tempPheromone.getResource().equals(searchFor.getSearchFor()))
{
if (tempPheromone.getDirection() != null)
{
if (tempPheromone.getStrength() > strength)
{
strength = tempPheromone.getStrength();
nextNode = tempPheromone.getDirection();
tempPheromone2 = tempPheromone;
followpath = true;
}
}
}
}
if (tempPheromone2 != null)
{
tempPheromone2.decrease();
}
}
if (tempPheromone2 != null && tempPheromone2.getStrength() == 0)
{
GraphEventManager.getReference().getProperties(currentGraphCell).removeInformation("Pheromones", tempPheromone2);
}
// geen lokaal pheromoon, zoek er een in een nabijliggende knoop.
if (! followpath)
{
Iterator connectedNodesIterator = GraphEventManager.getReference().getConnectedNodes(currentGraphCell).iterator();
int maxPheromone = 0;
DefaultGraphCell tempCell;
while (connectedNodesIterator.hasNext())
{
tempCell = (DefaultGraphCell)connectedNodesIterator.next();
pheromoneIterator = GraphEventManager.getReference().getProperties(tempCell).getInformation("Pheromones");
if (pheromoneIterator != null)
{
while (pheromoneIterator.hasNext())
{
tempPheromone = (Pheromone)pheromoneIterator.next();
//System.out.println("Gradient met source: " + tempGradient.getSource() + " =?= " + origin );
if (tempPheromone.getResource().equals(searchFor.getSearchFor())
&& tempPheromone.getDirection() == previousNode)
{
if (tempPheromone.getStrength() > maxPheromone)
{
maxPheromone = tempPheromone.getStrength();
nextNode = tempCell;
}
}
}
}
}
if (maxPheromone == 0)
{
//Neem willekeurige node.
nextNode = getRandomGraphCellFromExcept(currentGraphCell, previousNode);
//System.out.println("properties edge van " + currentGraphCell + " to " + nextNode);
}
}
progress = 0;
previousNode = currentGraphCell;
//System.out.println("properties edge van " + previousNode + " to " + nextNode);
GraphEventManager.getReference().getProperties(currentGraphCell).removeInformation("Agents", this);
setStatus(Agent.MOVING);
if (GraphEventManager.getReference().getConnectingEdge(previousNode, nextNode) == null)
{
setStatus(Agent.DEAD);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -