Playing back results

Once the swarm optimization is complete, the job of playing back the results comes to the forefront. Our main function in replaying plots is called PlaybackPlot. Let's discuss the function in detail:

private void PlaybackPlot()
{

Get our current iteration, as follows:

int iterN = GetCurrentIteration();
_Closing = false;

If we have played back all our points, then leave, as follows:

if (iterN >= maxIter)
return;
PlotStatusN = PlotStatus.Play;

Update the progress bar, as follows:

progressBarX1.Visible = true;
progressBarX1.Minimum = 0;
progressBarX1.Maximum = maxIter;

Go through all iterations, as follows:

for (int iter = iterN; iter < maxIter; iter++)
{

Update the progress bar value, as follows:

progressBarX1.Value = iter;
if (_Closing)
{
_Closing = false;
progressBarX1.Visible = false;
return;
}

Plot a single swarm iteration point, as follows:

PlotSwarmIterationPoint();

Briefly pause to allow the UI to remain responsive, as follows:

PauseForMilliSeconds(1);
ShowTitle();
}
PlotStatusN = PlotStatus.Pause;
progressBarX1.Visible = false;
}

You will notice in the preceding function a call to PlotSwarmIterationPoint. This function call (or method, if you prefer) is responsible for plotting a single movement of the particles. One step, if you will. Let's take you through that function and describe what is happening, as follows:

private void PlotSwarmIterationPoint()
{

If we have reached our final iteration, then leave, as follows:

intiterN = GetCurrentIteration();
if (iterN >= maxIter)
return;
NChart chart = nChartControl2.Charts[0];
chart.Axis(StandardAxis.PrimaryX).ScaleConfigurator = new NLinearScaleConfigurator();

We need to plot a single point for each particle in the swarm, as follows:

for (int Swarm = 0; Swarm < maxSwarm; Swarm++)
{

Add a series for each point, as follows:

chartPSO.Series[Swarm].Points.AddXY(plotSwarmPositions[iterN, Swarm, 0], plotSwarmPositions[iterN, Swarm, 1]);

Add a data point for the series we just created, as follows:

NLineSeries m_Line1 = (NLineSeries)nChartControl2.Charts[0].Series[Swarm];
m_Line1.AddDataPoint(new NDataPoint(plotSwarmPositions[iterN, Swarm, 0], plotSwarmPositions[iterN, Swarm, 1]));

Dynamically handle the colors based upon the range values each particle is in, as follows:

ApplyLineColorRanges(new NRange1DD[] { new NRange1DD(-10, -5), new NRange1DD(5, 10) },
new Color[] { Color.Red, Color.Yellow }, m_Line1);
}

Now, add a point for the optimal global position, as follows:

chartPSO.Series[maxSwarm].Points.Clear();
chartPSO.Series[maxSwarm].Points.AddXY(plotGlobalPositions[iterN, 0], plotGlobalPositions[iterN, 1]);

Get the next iteration in line, paint the control, and show the text for what is going on:

iterN = Math.Min(iterN + 1, maxIter - 1);
nChartControl2.Refresh();
pictureBox1.Invalidate();
ShowTitle();
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset