📄 simpleagent.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.LinkedList;
import java.util.Iterator;
/*
* SimpleAgent:
* the agent starts in a vertex, and has status = MOVING
* in the next step, the agent constructs new agents, for each edge one agent.
* then he deletes himself. The agents are put on an edge, and they get the
* status = MOVING. When the agent reaches another node, status is changed to
* SEARCHING. When the resource is not found, status is chaned to EXPLORING, and
* new agents are constructed, 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.
*/
public class SimpleAgent extends Agent implements Serializable
{
public SimpleAgent(DefaultGraphCell origin, ResourceQuery searchFor)
{
super(origin, searchFor);
}
public void run(DefaultGraphCell currentGraphCell)
{
age++;
//Properties properties = (Properties)GraphConstants.getValue(currentGraphCell.getAttributes());
Properties properties = GraphEventManager.getReference().getProperties(currentGraphCell);
//System.out.println("Ant searching for " + searchFor + " - next destination: " + nextNode + " - progress: " + progress);
//progress++;
// Ant bevindt zich op een edge
if (currentGraphCell instanceof DefaultEdge)
{
if (status == MOVING)
{
int capacity = GraphEventManager.getReference().getEdgeProperties((DefaultEdge)currentGraphCell).getCapacity();
//if (progress >= ((14 - capacity)/2))
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("Agent :: 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");
LinkedList connectedNodes = getNextNodes(currentGraphCell);
if (connectedNodes != null)
{
Iterator newNextNodeIterator = connectedNodes.iterator();
DefaultGraphCell newNextNode;
while(newNextNodeIterator.hasNext())
{
newNextNode = (DefaultGraphCell) newNextNodeIterator.next();
if (newNextNode != previousNode)
{
DefaultEdge newEdge = GraphEventManager.getReference().eventGetConnectingEdge(currentGraphCell, newNextNode);
Agent newAgent = new SimpleAgent(origin, searchFor);
newAgent.setNextNode(newNextNode);
newAgent.setStatus(Agent.MOVING);
newAgent.setPreviousNode(currentGraphCell);
GraphEventManager.getReference().getProperties(newEdge).addInformation("Agents", newAgent);
}
}
properties.removeInformation("Agents", this);
status = DEAD;
}
else
{
System.out.println("Leaf-node");
properties.removeInformation("Agents", this);
status = DEAD;
}
}
if (status == RETURNING)
{
GraphEventManager.getReference().getProperties(currentGraphCell).removeInformation("Agents", this);
if (currentGraphCell == origin)
{
// Agent has returned.
status = DEAD;
getOriginalQuery().finished();
}
else
{
//previousNode = currentGraphCell;
//Iterator connectedNodesIterator = GraphEventManager.getReference().getConnectedNodes(nextNode).iterator();
int maxGradient = 0;
Iterator gradientFieldIterator = GraphEventManager.getReference().getProperties(currentGraphCell).getInformation("Gradient");
Gradient tempGradient;
if (gradientFieldIterator != null)
{
while (gradientFieldIterator.hasNext())
{
tempGradient = (Gradient)gradientFieldIterator.next();
if (tempGradient.getSource() == origin)
{
nextNode = tempGradient.getBackPointer();
maxGradient = tempGradient.getDistance();
}
}
}
if (maxGradient == 0)
{
//Neem willekeurige node.
nextNode = getRandomGraphCellFromExcept(currentGraphCell, previousNode);
//System.out.println("properties edge van " + currentGraphCell + " to " + nextNode);
}
progress = 0;
previousNode = currentGraphCell;
GraphEventManager.getReference().getProperties(GraphEventManager.getReference().getConnectingEdge(previousNode, nextNode)).addInformation("Agents", this);
}
} // EIND STATUS RETURNING
} // EIND VERTEX
} // EINDe
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -