⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 probabilityagent.java

📁 P2P模拟器P2Psim的程序源码
💻 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;

/*
 * ProbabilityAgent:
 * 	the agent starts in a vertex, and has status = MOVING
 *	in the next step, the agent chooses a next node. The probability that a neigbouring
 *	node is selected is dependent on the strength of the pheromone pointing to the
 *	resource the agent is looking for.
 *	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 ProbabilityAgent extends Agent implements Serializable
{
	public ProbabilityAgent(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 ?
				
				int neighbours = GraphEventManager.getReference().getConnectedNodes(currentGraphCell).size();
				Iterator pheromoneIterator = GraphEventManager.getReference().getProperties(currentGraphCell).getInformation("Pheromones");
				Pheromone tempPheromone = null;
				//Pheromone tempPheromone2 = null;
				//boolean followpath = false;
				
				int totaal = neighbours-1;
				
				if (pheromoneIterator != null)
				{
					while (pheromoneIterator.hasNext())
					{
						tempPheromone = (Pheromone)pheromoneIterator.next();
						//System.out.println("Gradient met source: " + tempGradient.getSource() + " =?= " + origin );
						if (tempPheromone.getDirection() != previousNode && tempPheromone.getResource().equals(searchFor.getSearchFor()))
						{
							if (tempPheromone.getDirection() != null)
							{
								totaal += tempPheromone.getStrength() * 5;	
							}
						}
					}
				}
				
				
				boolean chosen = false;
				boolean beenThereAlready;
				int pointer = 0;
				
				if (totaal == 0)
				{
					nextNode = (DefaultGraphCell)GraphEventManager.getReference().getConnectedNodes(currentGraphCell).get(0);
				}
				else
				{
					do
					{
						beenThereAlready = false;
						int choice = (int) ((Math.random() * totaal) - 0.001);
						
						System.out.println("ProbabilityAgent :: run : " + choice + " - " + totaal);
						
						DefaultGraphCell tempNextNode;
						Iterator neighbourIterator = GraphEventManager.getReference().getConnectedNodes(currentGraphCell).iterator();
						
						while (!chosen && neighbourIterator.hasNext())
						{
							tempNextNode = (DefaultGraphCell)neighbourIterator.next();
							
							// bevat hoeveel getallen ?  maw. bepaal de range
							// 1 + sterkte van pheromoon dat wijst naar deze knoop.
							int range = 0;
							if (tempNextNode != previousNode)
							{
								range = 1; 
							}
							 
							pheromoneIterator = GraphEventManager.getReference().getProperties(currentGraphCell).getInformation("Pheromones");
							tempPheromone = null;
		
							if (pheromoneIterator != null)
							{
								while (pheromoneIterator.hasNext())
								{
									tempPheromone = (Pheromone)pheromoneIterator.next();
									if (tempPheromone.getDirection() != previousNode && tempPheromone.getDirection() == tempNextNode)
									{
										if (tempPheromone.getDirection() != null)
										{
											range += tempPheromone.getStrength() * 5;	
										}
									}
								}
							}
							
							// valt het gekozen getal in deze range ?
							if (pointer <= choice && choice < pointer + range)
							{
								// Zoja, je hebt de volgende node gevonden
								// indien deze node niet de vorige was.
								System.out.println("" + pointer + " <= " + choice + " < " + (pointer+range));
								chosen = true;
								
								if (tempNextNode != previousNode)
								{
									nextNode = tempNextNode;
								}
								// anders, doe de while-lus nog eens opnieuw, dus kies een nieuw getal.
								else
								{
									beenThereAlready = true;
								}
							}
							else
							{
								pointer += range;
							}
						}
					} while (beenThereAlready);
				}
				
				
				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);
				}
				else
				{
					GraphEventManager.getReference().getProperties(GraphEventManager.getReference().getConnectingEdge(previousNode, nextNode)).addInformation("Agents", this);
				}
			}
			
			if (status == RETURNING)
			{		
				improvePheromone(currentGraphCell);
						
				GraphEventManager.getReference().getProperties(currentGraphCell).removeInformation("Agents", this);
				
				if (currentGraphCell == origin)
				{
					// Agent has returned.
					status = DEAD;
					getOriginalQuery().finished();
					
				}
				else
				{
					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	
	}

public void improvePheromone(DefaultGraphCell currentGraphCell)
{
	Iterator pheromoneIterator = GraphEventManager.getReference().getProperties(currentGraphCell).getInformation("Pheromones");
	Pheromone foundPheromone = null;
	
	boolean certain = true;
	
	if (pheromoneIterator != null)
	{
		Pheromone tempPheromone;
		
		while (foundPheromone == null && pheromoneIterator.hasNext())
		{
			tempPheromone = (Pheromone)pheromoneIterator.next();
			//System.out.println(tempPheromone.getResource().getName() + " <> " + searchFor.getName());
			if (tempPheromone.getResource().equals(searchFor.getSearchFor()))
			{
				if (tempPheromone.getDirection() == previousNode)
				{
					//System.out.println(tempPheromone);
					foundPheromone = tempPheromone;
					//System.out.println(foundPheromone);
				}
				else
				{
					// Enkel verbeteren als we terugkeren gebaseerd op een gradientveld
					Iterator gradientFieldIterator = GraphEventManager.getReference().getProperties(currentGraphCell).getInformation("Gradient");
					Gradient tempGradient;
					int maxGradient = 0;
					
					if (gradientFieldIterator != null)
					{
						while (gradientFieldIterator.hasNext())
						{
							tempGradient = (Gradient)gradientFieldIterator.next();
							if (tempGradient.getSource() == origin)
							{
								maxGradient = tempGradient.getDistance();	
							}
						}
					}

					if (maxGradient != 0) 
					{
						// we zijn dus aan het terugkeren gebaseerd op een gradient, en
						// tempPheromone.getDirection() != previousNode
						// maak een nieuw feromoon aan.
						certain = true;
					}
					else
					{
						// we zijn aan het terugkeren at random, we weten dus niet zeker
						// of we beter zijn.
						// de filosofie:  iets is beter dan niets, en zekerheid is beter dan meer.
						certain = false;
					}
				}
			}
		}
		if (foundPheromone != null)
		{
			//System.out.println("PerhomoneAgent :: improvePheromone");
			foundPheromone.increase();
		}
		else if (certain)
		{
			GraphEventManager.getReference().getProperties(currentGraphCell).addInformation("Pheromones", new Pheromone(searchFor.getSearchFor(), previousNode));
		}
	}
	else
	{
		GraphEventManager.getReference().getProperties(currentGraphCell).addInformation("Pheromones", new Pheromone(searchFor.getSearchFor(), previousNode));
	}
}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -