|
The following sample may be downloaded from the download section.
Function minimization (Program screen shot - 156KB)
This sample shows how to use optiGA to minimize a function. The function being minimized is:
f(x,y)=x*sin(4*x)+1.1*y*sin(2*y)
0 < X < 10 and 0 < Y < 10
You can use binary parameters coding or real parameters coding
Just follow these simple steps and let optiGA's defaults do the job for you:
1) Define the variables:
Dim vMin As Variant, vMax As As Variant
'set the minimum and maximum values of the parameters
vMin = Array(0, 0)
vMax = Array(10, 10)
'reset the control
optiGA1.ResetOptiGA
'define the two real genes.
'just pass to the method the number of genes and their boundaries.
i = optiGA1.DefineRealGenes(2, vMin, vMax)
'set the report interval so you can see the results as the run progresses
optiGA1.ReportEveryGeneration = 1
2) Define the FitnessFunction event:
(This event is fired every time optiGA need to evaluate the fitness function)
Private Sub optiGA1_FitnessFunction(BinaryGenes As Variant,
RealGenes As Variant, IntegerGenes As Variant,
GenerationNumber As Long, Fitness As Single)
Dim X As Single, Y As Single
X = RealGenes(0)
Y = RealGenes(1)
Fitness = X * Sin(4 * X) + 1.1 * Y * Sin(2 * Y)
End Sub
3) Define the GenerationReport event (you can skip this section and wait for the final results):
(This event is fired evrey generation as set by the optiGA1.ReportEveryGeneration property)
Private Sub optiGA1_GenerationReport(BestFitness As Single,
GenerationNumber As Long, BinaryGenes As Variant,
RealGenes As Variant, IntegerGenes As Variant,
ElapsedTime As Long, GenerationMeanFitness As Single)
'show the current generation report
Label7 = GenerationNumber
Label8 = BestFitness
Label9 = ElapsedTime
Label10 = GenerationMeanFitness
Label11 = RealGenes(0)
Label12 = RealGenes(1)
'use the DoEvents method to make sure the report is updated on the form
DoEvents
End Sub
3) Now just run the control:
i = optiGA1.RunOptiGA
THAT IS IT !!!
|