Non-professional students recently some times a little specification hope everybody to help!
# include & lt; Iostream>
#include
#include
#include
#include
using namespace std;
# define NP 10
2 * # define NP2 NP
# define x_min 0.1
# define x_max 1
# define y_min 0
# define y_max 5
//generate a random number, random number is (0.0, 1.0)
Double Rand_Double (void)
{
Return static_cast & lt; Double> (rand ())/static_cast & lt; Double> (RAND_MAX);
}
//test functions f1 and f2
//number of parameters to 2
Double f1 (double x_1, double x_2)
{
Return x_1;
}
Double f2 (double x_3, double x_4)
{
The return (1 + x_4)/x_3;
}
//define individual classes, including feature values
The class individual
{
Public:
Double x, y;
Double fit1 fit2;
Int irank;
Double idist;
Void copy (individual b)
{
X=b.x;
Y=b.y;
Fit1=b. it1;
Fit2=b. it2;
Irank=b.i rank;
Idist=b.i dist.
}
};
//definition exchange function
Void swap (individual & amp; Eg_1, individual & amp; Eg_2)
{
Individual temp_1;
Temp_1. X=eg_1. X;
Eg_1. X=eg_2. X;
Eg_2. X=temp_1. X;
Temp_1. Y=eg_1. Y;
Eg_1. Y=eg_2. Y;
Eg_2. Y=temp_1. Y;
Temp_1. Fit1=eg_1. Fit1;
Eg_1. Fit1=eg_2. Fit1;
Eg_2. Fit1=temp_1. Fit1;
Temp_1. Fit2=eg_1. Fit2;
Eg_1. Fit2=eg_2. Fit2;
Eg_2. Fit2=temp_1. Fit2;
Temp_1. Irank=eg_1. Irank;
Eg_1. Irank=eg_2. Irank;
Eg_2. Irank=temp_1. Irank;
Temp_1. Idist=eg_1. Idist;
Eg_1. Idist=eg_2. Idist;
Eg_2. Idist=temp_1. Idist;
}
//step 2 to calculate irank, adopt the mode of circular contrast statistics here number itself will not be dominant
Void compute_irank (individual eg_3 [], int n)
{
//for (int I=0; i//eg_3 [I] irank=0;
for (int i=0; iFor (int j=I; J {
If (eg_3 [I] fit1 & lt;=eg_3 [j] fit1 & amp; & Eg_3 [I] fit2 & lt; Fit2) eg_3=eg_3 [j]. [I] irank=eg_3 [I] irank + 1;
Else if (eg_3 [I] fit1 & gt;=eg_3 [j] fit1 & amp; & Eg_3 [I] fit2 & gt;=eg_3 [j] fit2) eg_3 [j]. J irank=eg_3 [j]. J irank + 1;
}
}
//carried out in accordance with the formula one ascending order
Void sort (individual eg_4 [], int n)
{
Int I, j, p;
For (I=0; i{
P=I;
For (j=I + 1; JIf (eg_4 [j] fit1 & lt; Eg_4 [p]. Fit1) p=j;
If (p!=I)
Swap (eg_4 [p], eg_4 [I]);
}
}
//screening of the next generation of population
Void sort2 (individual eg_8 [], int n)
{
Int I, j, p;
For (I=0; i{
P=I;
For (j=I + 1; J{
If (eg_8 [j] irank==eg_8 [p]. Irank)
{
If (eg_8 [j] idist> Eg_8 [p]. Idist) p=j;
}
Else if (eg_8 [j]. Journal of irank> Eg_8 [p]. Irank) p=j;
}
If (p!=I)
Swap (eg_8 [p], eg_8 [I]);
}
}
//find idist constitute a rectangular length calculation two general, choose the nearest point and its adjacent points
Void compute_idist (individual eg_5 [], int n)
{
Double temp_2 temp_3;
for (int i=0; i{
If (I==1) eg_5 [I] idist=SQRT ((eg_5 [I + 1] fit1 - eg_5 [I] fit1) * (eg_5 [I + 1] fit1 - eg_5 [I] fit1)) + SQRT ((eg_5 [I + 1] fit2 - eg_5 [I] fit2) * (eg_5 [I + 1] fit2 - eg_5 [I] fit2));
Else if (I==n - 1) eg_5 [n - 1]. Idist=SQRT ((eg_5 [I + 1] fit1 - eg_5 [I] fit1) * (eg_5 [I + 1] fit1 - eg_5 [I] fit1)) + SQRT ((eg_5 [I + 1] fit2 - eg_5 [I + 1] fit2) * (eg_5 [I + 1] fit2 - eg_5 [I + 1] fit2));
The else
{
Temp_2=SQRT ((eg_5 [I] fit1 - eg_5 [I - 1]. Fit1) * (eg_5 [I] fit1 - eg_5 [I - 1]. Fit1) + (eg_5 [I] fit2 - eg_5 [I - 1]. Fit2) * (eg_5 [I] fit2 - eg_5 [I - 1]. Fit2));
Temp_3=SQRT ((eg_5 [I + 1] fit1 - eg_5 [I] fit1) * (eg_5 [I + 1] fit1 - eg_5 [I] fit1) + (eg_5 [I + 1] fit2 - eg_5 [I + 1] fit2) * (eg_5 [I + 1] fit2 - eg_5 [I + 1] fit2));
If (temp_2 & gt;=temp_3) eg_5 [I] idist=SQRT ((eg_5 [I + 1] fit1 - eg_5 [I] fit1) * (eg_5 [I + 1] fit1 - eg_5 [I] fit1)) + SQRT ((eg_5 [I + 1] fit2 - eg_5 [I] fit2) * (eg_5 [I + 1] fit2 - eg_5 [I] fit2));
The else eg_5 [I] idist=SQRT ((eg_5 [I] fit1 - eg_5 [I - 1]. Fit1) * (eg_5 [I] fit1 - eg_5 [I - 1]. Fit1)) + SQRT ((eg_5 [I] fit2 - eg_5 [I - 1]. Fit2) * (eg_5 [I] fit2 - eg_5 [I - 1]. Fit2));
}
}
}
Void main ()
{
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- set of random Numbers -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Srand ((unsigned int) (time (NULL)));
//get parameter
Int Gen=10;
Double F=0.5, cr=0.5;
int i;
P1, individual * * trials, * (P2);
P1=new individual (NP),
Trials=new individual (NP),
The P2=new individual [NP2];
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the initial random population -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
For (I=0; i{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull