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

📄 mainform.cs

📁 ACO.zip是一个蚁群优化算法
💻 CS
📖 第 1 页 / 共 4 页
字号:
									{
										p.UpdateProbabilities(c.NewValue, LinkedConnID);

										// if it has just been switched on then add a label
										if(!AntNetOnLastIteration)
										{
											sim.AnnoText[sim.AnnoText.Count-1]="AntNET Activated";
											AntNetOnLastIteration=true;
										}
									}
									else
									{
										// if it has just been switched on then add a label
										if(AntNetOnLastIteration)
										{
											sim.AnnoText[sim.AnnoText.Count-1]="AntNET Deactivated";
											AntNetOnLastIteration=false;
										}
									}
								}

								LinkedConnID = tNode.ID;
							}

							if(c.VisitedNodes.Count==0 && chbReturnUponConnection.Checked)
								c.Finished=true;
						}
					}

					// if the node is not connected
					if(c.AntDirection == eAntDirection.ForwardAnt)
					{
						// reques the next node
						int nodeid = c.ConnectNextNode();

						//if no node is available then increment the wait time and continue
						if(nodeid==-1)
						{				
							c.waitTime++;
						}// otherwise connect to the node
						else if(Node.Connect(nodeid, c.CallID, c.DestinationNodeID))
						{
							// set the current node id
							c.CurrentNodeID = nodeid;
							// add the node as a visited node
							c.VisitedNodes.Add(nodeid);
						}

						// if the call has reached it destination
						if(nodeid==c.DestinationNodeID)
						{
							c.Successful=true;
							// remove loops if option is selected
							if(chbLoopRemoval.Checked)
								c.LoopRemoval();

							// now Change the direction of the ant
							c.AntDirection = eAntDirection.BackwardAnt;
						}
					}

					// Shorten duration by one tick
					c.Duration--;	
				}
			
				// if the node has finished or has timed out
				if(c.Duration==0 || c.Finished)
				{
					// was the call successful
					if (c.Successful)
					{
						// increment the completed counter
						complete++;
						// add the node count to the total
						totaltime += c.finalVisitedNodeCount;
						// calculate the average and render the label
						double ave = totaltime / complete;
						this.lblAverage.Text = ave.ToString();
					}
					else
					{
						//otherwise the calls failed
						// update the failed call label
						FailedCalls.Text = ((int)++failed).ToString();
					}

					// update the total calls label
					this.lblTotalCalls.Text = (++Global.TotalCallsMade).ToString();

					// teminate the calls and release network resourses
					c.TerminateCall();

					// add the call to the simulation output
					sim.Calls.Add(c);

					// add the default blank label
					sim.AnnoText.Add("");
			
					int util=0;
					// Add the total network utilisation as a total number of node connections
					for(int k=0;k<Global.Nodes.Length;k++)
						util+=Global.Nodes[k].CallIDS.Count;

					sim.NetworkUtilisation.Add(util);

					// nullify the call
					Global.Calls.Remove(c);	

					// The array has bee reduced so decrease the indexer
					// so not to skip the next call in this loop
					i--;												
				}
			}

			// refresh the drawing panel
			if(!chbIO.Checked)
				pnlGraph.Refresh();
		}


		// call for a graph to be created
		private void btnGenerate_Click(object sender, System.EventArgs e)
		{
			GenerateGraph();
		}


		// generates a graph from the simulations
		public void GenerateGraph()
		{
			// nullify the chart data
			TheChart.SeriesCollection.Clear();

			//set the  formatting properties
			TheChart.Title = "Average connection time";
			TheChart.Type = ChartType.Scatter;
			TheChart.Use3D = false;	
			TheChart.DefaultSeries.DefaultElement.Transparency = 20;
			TheChart.DefaultSeries.DefaultElement.Marker.Type = ElementMarkerType.None;
			TheChart.ChartArea.LegendBox.Template = "%Name%Icon";
			TheChart.DefaultSeries.Line.Width = 2;

			// generate the graph data from the simulations
			TheChart.SeriesCollection.Add(GenerateChartData());		
			
			// refresh the chat
			TheChart.RefreshChart();
		}


		// generate the chart data
		public SeriesCollection GenerateChartData()
		{
			// create a new series collection
			SeriesCollection SC = new SeriesCollection();

			// create a new simulation
			Simulation s;

			// Loop through all the simulations
			for(int i=0;i<Global.Simulations.Count;i++)
			{
				double ave=0;
				double TotalHops=0;

				// get the ith simulations
				s = (Simulation)Global.Simulations[i];

				// create a new plotting series
				Series series = new Series();

				// label the series
				series.Name = "Simulation "+((int)i+1).ToString();

				Call c;
				// loop through all the calls
				for(int j=0;j<s.Calls.Count;j++)
				{
					// calulate the running average number of hops for the simulation
					c = (Call)s.Calls[j];
					TotalHops += c.FinalVisitedNodes.Count;
					ave = Convert.ToDouble(TotalHops) / Convert.ToDouble(j+1);

					Element e = new Element();
					
					// if annotation text was stored
					if(s.AnnoText[j].ToString()!="")
					{
						// render it as a label
						Annotation anno = new Annotation(s.AnnoText[j].ToString()); 
						anno.DefaultCorner = BoxCorner.Round;
						anno.Orientation = dotnetCHARTING.WinForms.Orientation.TopRight;
						e.Annotation = anno;
					}
					
					// set the X & Y coordinates for the point
					e.YValue = ave;
					e.XValue = j+1;
					
					series.Elements.Add(e);
				}

				// add the series to the collection
				SC.Add(series);
			}

			// Set Different Colors for the new Series
			for(int i=0;i<SC.Count;i++)
			{
				if(i==0)
					SC[0].DefaultElement.Color = Color.FromArgb(255,99,49);
				if(i==1)
					SC[1].DefaultElement.Color = Color.FromArgb(0,156,255);
				if(i==2)
					SC[2].DefaultElement.Color = Color.FromArgb(49,255,49);
				if(i==3)
					SC[3].DefaultElement.Color = Color.FromArgb(255,255,0);
			}

			// return the newly created series collection
			return SC;
		}


		// toggles a nodes ability to route traffic
		private void lbNodes_OnClick(object sender, ItemCheckEventArgs e)
		{
			// if the last state was checked then the item is now checked
			if(e.CurrentValue==CheckState.Checked)
			{
				// set the nodes DontRouteTraffic property to true
				Global.Nodes[lbNodes.SelectedIndex].DontRouteTraffic=true;
				// add the event to the simulator class
				sim.AnnoText[sim.AnnoText.Count-1] = "Node "+lbNodes.SelectedIndex.ToString()+" Routing Off";
			}
			else if(e.CurrentValue==CheckState.Unchecked)
			{
				// the item was once again checked
				// set the nodes DontRouteTraffic property to true
				Global.Nodes[lbNodes.SelectedIndex].DontRouteTraffic=false;
				// add the event to the simulator class
				sim.AnnoText[sim.AnnoText.Count-1] = "Node "+lbNodes.SelectedIndex.ToString()+" Routing On";
			}
		}


		/// Resets the pheromone tables for all Nodes
		private void btnResetPHTables_Click(object sender, System.EventArgs e)
		{
			Node n;

			// loop through all the nodes
			for(int i=0;i<Global.Nodes.Length;i++)
			{
				n = Global.Nodes[i];
				// call the default Pheromone Tables to be generated
				n.GenerateDefaultPheromoneTables();
			}
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
			dotnetCHARTING.WinForms.ChartArea chartArea1 = new dotnetCHARTING.WinForms.ChartArea();
			dotnetCHARTING.WinForms.Element element1 = new dotnetCHARTING.WinForms.Element();
			dotnetCHARTING.WinForms.LegendEntry legendEntry1 = new dotnetCHARTING.WinForms.LegendEntry();
			dotnetCHARTING.WinForms.LegendBox legendBox1 = new dotnetCHARTING.WinForms.LegendBox();
			dotnetCHARTING.WinForms.LegendEntry legendEntry2 = new dotnetCHARTING.WinForms.LegendEntry();

⌨️ 快捷键说明

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