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

📄 node.cs

📁 ACO.zip是一个蚁群优化算法
💻 CS
字号:
using System;
using System.Collections;
using System.Drawing;

namespace ACO
{
	/// <summary>
	/// The Node class represents node on the network and holds phermone tables
	/// which can be called to determine the best route to a destination node.
	/// </summary>
	public class Node
	{
		#region Properties

		// the id of the node
		public int ID;
		// the ids of all the connected nodes
		public int[] Connections;	
		// the call capacity of the node
		public int Capacity=Global.NodeCapacity;
		// the ids of all the calls currently placed on the node
		public ArrayList CallIDS=new ArrayList();
		// a pheromone table for every other node in the network
		public PheromoneTable[] PheromoneTable;
		// when set to false the node will only accept traffic as a final desination node
		// and will refuse to route any traffic
		public bool DontRouteTraffic=false;
		// the point on screen in which to render the node
		private Point coordinates;

		/// <summary>
		/// The centre pixels of the node (Actual coordinates * Scale)
		/// </summary>
		public Point Centroid
		{
			get	{return new Point(Convert.ToInt32(coordinates.X*Global.Scale), Convert.ToInt32(coordinates.Y*Global.Scale) );}
		}

		/// <summary>
		/// Actual coordinates of the Node
		/// </summary>
		public Point Coordinates
		{
			get	{return new Point(Convert.ToInt32(coordinates.X*Global.Scale), Convert.ToInt32(coordinates.Y*Global.Scale) );}
			set{this.coordinates=value;}
		}

		/// <summary>
		/// returns true if the network is full or Traffic Routing has been disabled
		/// </summary>
		public bool FullCapacity
		{
			get
			{
				// if DontRouteTraffic is set to true then pretend it is full capacity and
				// refuse connection
				if(DontRouteTraffic)
					return false;
				else // oherwise make sure capacity hasnt been reached
					return (bool)(Global.NodeCapacity<=CallIDS.Count);
			}
		}

		#endregion

		// constructor takes an id
		public Node(int id)
		{
			this.ID = id;
		}

		/// gets a PheromoneTable for a Node
		public PheromoneTable GetPheromoneTableForNode(int nodeid)
		{
			// Loop through all the phermomone tables to find the matching nodeid
			for(int i=0;i<PheromoneTable.Length;i++)
			{
				// return the node with matching id to the parameter nodeid
				if(PheromoneTable[i].NodeID==nodeid)
					return PheromoneTable[i];
			}
			
			// return null if no matching id was found
			return null;
		}


		///  Creates the default phermone tables with equal weights for all paths
		public void GenerateDefaultPheromoneTables()
		{
			// create an array for all the nodes apart from itself
			this.PheromoneTable = new PheromoneTable[Global.Nodes.Length-1];

			int index=0;

			// create a table for each of the other nodes on the network
			for(int i=0;i<Global.Nodes.Length;i++)
			{
				// dont create a table for itself
				if(Global.Nodes[i].ID!=this.ID)
				{
					this.PheromoneTable[index++] = new PheromoneTable(Global.Nodes[i], this.Connections);
				}
			}
		}


		/// Connects a call to a this Node by adding the call id to itself
		public static bool Connect(int nodeid, int callid, int DestinationNodeID)
		{
			// check if the node is has capacity
			Node n = Global.Nodes[nodeid];

			if( !n.FullCapacity && (!n.DontRouteTraffic || DestinationNodeID==n.ID))
			{
				// add the call id to the node
				n.CallIDS.Add(callid);

				// the call connection was successful
				return true;
			}

			// the connection failed
			return false;
		}


		// returns the node id for automatic labelling in the listbox
		public override string ToString()
		{
			return "Node "+this.ID.ToString();
		}

	}
}

⌨️ 快捷键说明

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