📄 mainform.cs
字号:
using System;
using System.Threading;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using dotnetCHARTING.WinForms;
namespace ACO
{
/// <summary>
/// The Main form of the application
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
#region properties
// windows controls
private System.Windows.Forms.Timer Ticker;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.TextBox txtCallDuration;
private System.Windows.Forms.CheckBox chbIO;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txtNodeCapacity;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtConcurrentCalls;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtTotalConnections;
private System.Windows.Forms.Label lblTotalCalls;
private System.Windows.Forms.Label lblAverage;
private System.Windows.Forms.ComboBox cmbSpeed;
private System.Windows.Forms.ComboBox cmbAlgorithm;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button btnGenerate;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.Label FailedCalls;
private System.Windows.Forms.CheckBox chbReturnUponConnection;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.CheckedListBox lbNodes;
private System.Windows.Forms.Label label12;
private System.Windows.Forms.Button btnResetPHTables;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.CheckBox chbLoopRemoval;
// the charting object
private dotnetCHARTING.WinForms.Chart TheChart;
// the custom draw panel
private ACO.DrawPanel pnlGraph;
// total time for this simulation
private double totaltime=0;
// the current simultation
private Simulation sim;
// number of completed calls
private double complete=0;
// number of failed calls
private double failed=0;
// if antnet was actuavated on the last iteration (used for labelling charts)
private bool AntNetOnLastIteration=true;
#endregion
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
// constructor for main form
public MainForm()
{
// initialise the windows components
InitializeComponent();
// set the speed to 5 (1000 ticks per second)
cmbSpeed.SelectedIndex=5;
// set the ANTNet algorithm to activated
cmbAlgorithm.SelectedIndex=0;
// add all the nodes to the listbox
lbNodes.Items.AddRange(Global.Nodes);
//check all the nodes to signal activated
for(int i=0;i<lbNodes.Items.Count;i++)
lbNodes.SetItemChecked(i,true);
// add the onclick handler
this.lbNodes.ItemCheck += new ItemCheckEventHandler(this.lbNodes_OnClick);
}
// start the simulation
private void btnStart_Click(object sender, System.EventArgs e)
{
// stop the ticker if if was previously started
Ticker.Stop();
#region textbox input validation
// set the timer speed
if(cmbSpeed.SelectedIndex==0)
Ticker.Interval = 1000;
else if(cmbSpeed.SelectedIndex==1)
Ticker.Interval = 200;
else if(cmbSpeed.SelectedIndex==2)
Ticker.Interval = 200;
else if(cmbSpeed.SelectedIndex==3)
Ticker.Interval = 50;
else if(cmbSpeed.SelectedIndex==4)
Ticker.Interval = 10;
else if(cmbSpeed.SelectedIndex==5)
Ticker.Interval = 1;
try
{
Global.MaxConcurrentCalls = Int32.Parse(txtConcurrentCalls.Text);
}
catch
{
MessageBox.Show("Maximum Concurrent Calls textbox has an invalid input");
txtConcurrentCalls.Focus();
txtConcurrentCalls.Text = "";
return;
}
try
{
Global.TotalCalls = Int32.Parse(txtTotalConnections.Text);
}
catch
{
MessageBox.Show("Total number of Calls textbox has an invalid input", "invalid input" ,MessageBoxButtons.OK,MessageBoxIcon.Warning);
txtTotalConnections.Focus();
txtTotalConnections.Text = "";
return;
}
try
{
Global.NodeCapacity = Int32.Parse(txtNodeCapacity.Text);
}
catch
{
MessageBox.Show("Total number of Calls textbox has an invalid input", "invalid input", MessageBoxButtons.OK,MessageBoxIcon.Warning);
txtNodeCapacity.Focus();
txtNodeCapacity.Text = "";
return;
}
try
{
Global.CallDuration = Int32.Parse(txtCallDuration.Text);
}
catch
{
MessageBox.Show("Call duration textbox has an invalid input", "invalid input", MessageBoxButtons.OK,MessageBoxIcon.Warning);
txtCallDuration.Focus();
txtCallDuration.Text = "";
return;
}
#endregion
// Create a new simulation
sim = new Simulation();
// set simulation results to 0
complete=0;
failed=0;
totaltime=0;
// add the simulation to the global array
Global.Simulations.Add(sim);
// clear all existing calls
Global.Calls.Clear();
// reset the number of calls made to 0
Global.TotalCallsMade=0;
// reset all labels
lblAverage.Text = "";
lblTotalCalls.Text = "";
// release all node resources from the network
Global.TerminateAllCalls();
// start the ticker
Ticker.Start();
// disable the button while the simulation
btnStart.Enabled=false;
}
// this method is called on every tick of the timer
private void Ticker_Tick(object sender, System.EventArgs e)
{
Call c;
// if the simulation is about to exceed the total number of calls, stop the ticker
if(Global.TotalCallsMade>=Global.TotalCalls)
{
// write the tables to disk
//Global.WriteTablesToDisk();
// enable the button again
btnStart.Enabled=true;
// stop the ticker
Ticker.Stop();
}
// create a new call
if(Global.MaxConcurrentCalls>Global.Calls.Count)
{
// create a random generator
Random r1 = new Random(Global.Seed);
Random r2 = new Random(Global.Seed);
// add a new call with a randon source and destination node
Global.Calls.Add(new Call(r1.Next(0,29), r2.Next(0,29)));
}
// One tick for each call
for(int i=0;i<Global.Calls.Count;i++)
{
// get the ith call
c = (Call)Global.Calls[i];
// if the call duration is zero then the call has finished
if(c.Duration!=0 && !c.HasFailed )
{
// check if the ant is working its way backwards
if(c.AntDirection == eAntDirection.BackwardAnt)
{
// retrace one step for the ant whist updating the table
// on the way
if(c.NewValue==-1)
{
// 1. calculate new value
double total = Global.CallDuration;
//dont use self for calc
double vnodes = c.VisitedNodes.Count-1;
c.NewValue = ((1/(vnodes/total))/total)*100;
}
if(c.VisitedNodes.Count>0)
{
if(c.finalVisitedNodeCount==0)
c.finalVisitedNodeCount=c.VisitedNodes.Count;
// Pop top visited node from top
Node endNode = Global.Nodes[(int)c.VisitedNodes[c.VisitedNodes.Count-1]];
c.FinalVisitedNodes.Add(endNode);
// remove it from the visited array
c.VisitedNodes.RemoveAt(c.VisitedNodes.Count-1);
Node tNode;
int LinkedConnID=-1;
if(c.VisitedNodes.Count>0)
LinkedConnID = (int)c.VisitedNodes[c.VisitedNodes.Count-1];
// get pheromene table for each of visited
for(int j=c.VisitedNodes.Count-1;j>=0;j--)
{
// get second, third, etc visited node
tNode = Global.Nodes[((int)c.VisitedNodes[j])];
// get the pheromone table for visitednode
PheromoneTable p = endNode.GetPheromoneTableForNode(tNode.ID);
if(p!=null)
{
// update the new values to all the Table Entries
if(cmbAlgorithm.SelectedIndex==0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -