Home > Back-end >  For c/c great god grant instruction!
For c/c great god grant instruction!

Time:10-26

My program is as follows, why don't you display the results after running???????
#include
#include
#include
#include
# include "iostream. H"

# define CLOCKS_PER_SEC (1000) (clock_t)
80//# define P_num particle number
8//# define dim particle dimension
1//# define low range search domain
# define high 8
# define iter_num 200
2//# define V_max speed range
# define c1 2
# define c2 2
# define w 0.5
# define alp 1
Int Deb=0;
Int Fin=8192;
Int iter.
Int the available;
Int particle [P_num] [dim];//individual collection
Int particle_loc_best [P_num] [dim];//each individual local optimal vector
Double particle_loc_fit [P_num];//local optimum of individual fitness, there are local optimal vector calculation to
Int particle_glo_best (dim);//the global optimal vector
Double gfit.//global optimal fitness, a global optimal vector calculation to
Int particle_v [P_num] [dim];//record every individual of the current generation of the velocity vector
Double particle_fit [P_num];
Double particle_value [P_num];

Void GenerateInitial (void);
Void CalculateFitnessValue (void);
Void OutputTextReport (void);
Void FindPbestAndGbest (void);
Void renew_particle ();
Void renew_var ();


Void main ()
{
Clock_t start and end;
Start=clock ();
The available=rand () % 2;
If (the available==1)
{
Iter=0;
GenerateInitial ();
CalculateFitnessValue ();
FindPbestAndGbest ();
Cout<& lt;" Gfit="& lt; While (iter{
Iter++;
Renew_var ();
Renew_particle ();
CalculateFitnessValue ();
FindPbestAndGbest ();
OutputTextReport ();
Cout<& lt;" Gfit="& lt; }
}
End=clock ();
Cout<& lt;" The Run time: "& lt; <(double) (end - start)/CLOCKS_PER_SEC & lt; & lt;" S "& lt; }

Void GenerateInitial (void)
{
Int I, j, k, x, sum;
for(i=0; i{
for(j=0; J{
X=rand () % + 1;
Particle [I] [j]=x;
for(k=0; k{
If (particle [I] [k]==particle [I] [j])
{
J -;
break;
}
}
}
Sum=particle [I] [0] (2.0, 3) + particle * pow [I] [1] (2.0, 2) + particle * pow [I] [2] * 2 + particle [I] [3] + particle [I], [4] * pow (2.0, 3) + particle [I], [5] * pow (2.0, 2) + particle [I] [6] * 2 + particle [I] [7].
While (sum> Fin)
{
for(j=0; J{
X=rand () % + 1;
Particle [I] [j]=x;
for(k=0; k{
If (particle [I] [k]==particle [I] [j])
{
J -;
break;
}
}
}
Sum=particle [I] [0] (2.0, 3) + particle * pow [I] [1] (2.0, 2) + particle * pow [I] [2] * 2 + particle [I] [3] + particle [I], [4] * pow (2.0, 3) + particle [I], [5] * pow (2, 2) + particle [I] [6] * 2 + particle [I] [7].
}


}
for(i=0; i{
for(j=0; J{
Particle_v [I] [j]=rand () % 5-2;
}
}

}

Void CalculateFitnessValue (void)
{
Double temp1, temp2, x1, x2,
int i;
for(i=0; i{
If (particle_fit [I] & gt; Fin)
break;
The else
{
Temp1=particle [I] [0] + particle (2, 3) * pow [I] [1] * 4 + particle [I] [2] * 2 + 1;
Temp2=particle [I], [4] * pow (2, 3) + particle [I] [5] * 4 + particle [I] [6] * 2 + 1;
X1=4.096 * temp1/1023.0 0.048;
X2=4.096 * temp2/1023.0-0.048;
Particle_value [I] + temp2=temp1;
Particle_fit [I]=1/(x1 + x2);
}
}
}

Void FindPbestAndGbest ()
{
Int I, j, a, b, p, q);
If (iter==0)
{
for(i=0; i{
for(j=0; J{
Particle_loc_best [I] [j]=particle [I] [j];
}
Particle_loc_fit [I]=particle_fit [I];
}
Gfit=particle_loc_fit [0];
j=0;
for(i=1; i{
If (particle_loc_fit [I] & gt; Gfit)
{
Gfit=particle_loc_fit [I];
j=i;
}
}
for(i=0; i{
Particle_glo_best [I]=particle_loc_best [j] [I];
}
}
The else
{
For (a=0; A{
If (particle_fit [a] & gt; Particle_loc_fit [a])
{
Particle_loc_fit [a]=particle_fit [a];
For (b=0; B{
Particle_loc_best [a] [b]=particle [a] [b];
}
}
}
For (p=0, q=1; P{
If (particle_loc_fit [p] & gt; Gfit)
{
Gfit=particle_loc_fit [p].
Q=p;
}
}
If (q!=1)
{
For (p=0; P{
Particle_glo_best [p]=particle_loc_best [q] [p];
}
}
}
}

Void renew_particle ()
{
Int I, j, k;
for(i=0; i{
for(j=0; J{

Particle [I] [j] +=alp * particle_v [I] [j];
If (particle [I] [j] & gt; High)
{
Particle [I] [j]=high;
}
If (particle [I] [j] {
Particle [I] [j]=low;
}
for(k=0; k{
If (particle [I] [k]==particle [I] [j])
{
J -;
break;
}
}

}
}
}

Void renew_var ()
{
Int t, I, j;
T=rand () % 11;
Double l;
L=10.0 t/;
for(i=0; i
  • Related