Let's add a new function

Now it's time for us to demonstrate how to add a new function. The function we are going to deal with is a modified version of the original Levy function and is the 13th version known to exist. This function is a minimization function.

The function itself, which you can find in the visual workbench source code, looks like this:

public double LevyFunction13(double[] data)
{
double x1 = data[0];
double x2 = data[1];
double term1 = Math.Pow(Math.Sin(3 * Math.PI * x1), 2);
double term2 = Math.Pow((x1 - 1), 2) * (1 + (Math.Pow(Math.Sin(3 * Math.PI * x2),
2)));
double term3 = Math.Pow((x2 - 1), 2) * (1 + (Math.Pow(Math.Sin(2 * Math.PI * x2), 2)));
return term1 + term2 + term3;
}

Definitely a lot of math there, right? Many times, functions such as this would be created in editors that make the math much easier to view. For instance, if I were to represent this code mathematically, it would look like this:

f(x,y)=sin2(3πx)+(x−1)2(1+sin2(3πy))+(y−1)2(1+sin2(2πy))

If we were to take that and plot it with a tool such as MATLAB, here's what it would look like graphically:

Our view under MATLAB

Why did I say and show all of that? Because, as you use this tool to validate your testing, you need to be able to relay, and sometimes justify, this information to others. No doubt just showing the C# code will not be enough, and the math and visualizations are many times what others expect to see. Don't let that put you off; you'll see how easy it is to create these functions, and our application makes it very easy to produce the kind of information you need.

Let's get back on track with adding that function.

Once we have added our new function, we now need to add it to the GetFitnessValue function so that what is selected in the user interface can relate to our specific function:

else if (fitnessFunction == "Shubert")
return Swarm_Round(ShubertFunction(data));
else if (fitnessFunction == "Levy13")
return Swarm_Round(LevyFunction13(data));

Once this is complete, we need to add it to our dropdown function list box on the user interface. Just select the combo box on the user interface, go to the Items property, and click on the button:

Next, simply add the text you want displayed, as follows:

String Collection Editor

Once this is complete, build the project, run it, and you should see the function displayed in the dropdown:

New Function

After you select the Levy13 function, click on the Run button and voilà, you've successfully added a new function and tested its execution. You can view the two and three-dimensional plots as a validation of your success. Before you reach your maximum number of iterations (100 in this case), you should have reached a global optimum of 0 (the flat tape segments on the right-hand side of the three-dimensional plot):

3D View
..................Content has been hidden....................

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