Click here to Skip to main content
15,897,891 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralTraversing the views Pin
Poorniselva14-Jun-04 18:43
Poorniselva14-Jun-04 18:43 
GeneralRe: Traversing the views Pin
Vadim Tabakman14-Jun-04 19:00
Vadim Tabakman14-Jun-04 19:00 
GeneralRe: Traversing the views Pin
Poorniselva14-Jun-04 19:40
Poorniselva14-Jun-04 19:40 
GeneralRe: Traversing the views Pin
V.15-Jun-04 2:23
professionalV.15-Jun-04 2:23 
QuestionHow to get memory info of a process not using PSAPI, thanks Pin
xingworld14-Jun-04 18:40
xingworld14-Jun-04 18:40 
AnswerRe: How to get memory info of a process not using PSAPI, thanks Pin
David Crow15-Jun-04 2:46
David Crow15-Jun-04 2:46 
AnswerRe: How to get memory info of a process not using PSAPI, thanks Pin
ThatsAlok16-Jun-04 1:46
ThatsAlok16-Jun-04 1:46 
GeneralSOS [about Genetic Algorithms] Pin
onetwone14-Jun-04 17:56
onetwone14-Jun-04 17:56 
How to improve on Genetic Algorithms by using Fusion operator?
Change the codes please, thank you!
My English is poorFrown | :(

Codes:

#include<iostream.h>
#include<time.h>
#include<math.h>
#include<stdlib.h>

#define maxgen 500
#define POPSIZE 500
#define PopSize 100
#define kind -2
#define NVARS 9
#define Umin -1
#define Umax 1
#define Cmax 100
#define Cmin 0
double pxover= 0.75;
double pmutation = 0.2;
int gen,j;
int best_val = 0;
double m_A0,m_B0,m_A1,m_B1,m_A2,m_B2;
double A0,A1,A2,A3,A4,B1,B2,B3,B4;
long double x[100],y[100],z[100],a[100];


struct Individual{
double chrom[NVARS];
long double value;
long double fitness;
};
struct Individual population[POPSIZE];
struct Individual bestindividual;
struct Individual currentbest;


#define rd(i) (rand()%(int)(i))
#define rdf() ((float)rd(1000)/(1000.0))

void produce()
{
srand((unsigned)time(NULL));

for(int i=0;i<PopSize;i++)
{

population[i].chrom[0] =m_A0 +rdf()*(m_B0-m_A0);
population[i].chrom[1] =m_A0 +rdf()*(m_B0-m_A0);
population[i].chrom[2] =m_A0 +rdf()*(m_B0-m_A0);
population[i].chrom[3] =m_A0 +rdf()*(m_B0-m_A0);
population[i].chrom[4] =m_A0 +rdf()*(m_B0-m_A0);
population[i].chrom[5] =m_A0 +rdf()*(m_B0-m_A0);
population[i].chrom[6] =m_A0 +rdf()*(m_B0-m_A0);
population[i].chrom[7] =m_A0 +rdf()*(m_B0-m_A0);
population[i].chrom[8] =m_A0 +rdf()*(m_B0-m_A0);

}
for(int j=0;j<100;j++)
{
x[j]=m_A0+rdf()*(m_B0-m_A0);
y[j]=x[j];

}

}


void evaluation()
{
long double l;
l=(m_B0-m_A0)/2;
for(int i=0;i<PopSize;i++){
A0=population[i].chrom[0];
A1=population[i].chrom[1];
B1=population[i].chrom[2];
A2=population[i].chrom[3];
B2=population[i].chrom[4];
A3=population[i].chrom[5];
B3=population[i].chrom[6];
A4=population[i].chrom[7];
B4=population[i].chrom[8];

for(j=0;j<100;j++)
{
z[j]=A0/2+A1*cos(atan(1)*4*x[j]/l)+B1*sin(atan(1)*4*x[j]/l)+A2*cos(atan(1)*8*x[j]/l)+B2*sin(atan(1)*8*x[j]/l)+A3*cos(atan(1)*12*x[j]/l)+B3*sin(atan(1)*12*x[j]/l)+A4*cos(atan(1)*16*x[j]/l)+B4*sin(atan(1)*16*x[j]/l);
//z[j]=A0/2+A1*cos((atan(1)*4*x[j])/l)+B1*sin((atan(1)*4*x[j])/l)+A2*cos((atan(1)*8*x[j])/l)+B2*sin((atan(1)*8*x[j])/l)+A3*cos((atan(1)*12*x[j])/l)+B3*sin((atan(1)*12*x[j])/l)+A4*cos((atan(1)*16*x[j])/l)+B4*sin((atan(1)*16*x[j])/l);
}
for(j=0;j<100;j++)
{
a[j]=0.5*(z[j]-y[j])*(z[j]-y[j]);
population[i].value+=a[j];
}
}
}


void fitness()
{
for(int i=0;i<PopSize;i++)
{
if(kind>0)
{
population[i].fitness = population[i].value-Cmin;
}
else
{
population[i].fitness = Cmax - population[i].value;
}
}
}

void findbest()
{
bestindividual=population[0];
for(int i=1;i<PopSize;i++)
{
if(population[i].fitness > bestindividual.fitness)
{
bestindividual = population[i];
best_val = i;
}

}
if(gen==0)
{
currentbest = bestindividual;
}
else if( currentbest.fitness<bestindividual.fitness)
{
currentbest = bestindividual;
}
}

void select()
{
double cfitness[POPSIZE];
double sum = 0;
double p;
int index=0;
struct Individual newpopulation[POPSIZE];
for(int i=0;i<PopSize;i++)
{
sum +=population[i].fitness;
}
for(i =0;i<PopSize;i++)
{
cfitness[i] = population[i].fitness / sum;
}
for(i=1;i<PopSize;i++)
{
cfitness[i] = cfitness[i-1] + cfitness[i];
}
srand((unsigned)time(NULL));
for(i=0;i<PopSize;i++)
{
p = rdf();
while(p > cfitness[index])
{
index++;
}
newpopulation[i] = population[index];
}
for(i =0;i<PopSize;i++)
{
population[i] = newpopulation[i];
}
}


void crossover()
{
int index[POPSIZE];
int point,temp,i=0;
long double p;
struct Individual newpopulation[POPSIZE];
for(i=0;i<PopSize;i++)
{
index[i] = i;
newpopulation[i] = population[i];
}
srand((unsigned)time(NULL));
for(i=0;i<PopSize;i++)
{
point=(int)rand()/1000;
point+=i;
while(point>=PopSize)
{
point/=2;
}
temp = index[i];
index[i] = index[point];
index[point] = temp;
}

while(i<PopSize-1)
{
for(int j=0;j<NVARS;j++)
{
p = rdf();
if(p<pxover)
{
newpopulation[index[i]].chrom[j]= p*population[index[i+1]].chrom[j] + (1-p)*population[index[i]].chrom[j];
newpopulation[index[i+1]].chrom[j]= p*population[index[i]].chrom[j] + (1-p)*population[index[i+1]].chrom[j];
}
}
i=i+2;
}
for(i=0;i<PopSize;i++)
{
population[i] = newpopulation[i];
}
}


void mutate()
{
int p = 2;
double r;
double ytemp;
double temp;
double T;
double a[NVARS],b[NVARS];
srand((unsigned)time(NULL));
for(int i=0;i<POPSIZE;i++)
{
for(int j=0;j<NVARS;j++)
{
a[0]=m_A0;
b[0]=m_B0;
a[1]=m_A1;
b[1]=m_B1;
a[2]=m_A2;
b[2]=m_B2;
r = rdf()-0.5;
if(r<pmutation)
{
if(r<0)
r = -r;
T = 1-population[i].fitness/100;
if(rand()%2==0)
{
ytemp = b[j]- population[i].chrom[j];
temp = ytemp*( 1-pow(r,pow( T,p)));
population[i].chrom[j] = population[i].chrom[j] + temp;
}
else if(rand()%2==1)
{
ytemp = population[i].chrom[j] - a[j];
temp = ytemp*( 1-pow(r,pow( T,p)));
population[i].chrom[j] = population[i].chrom[j] - temp;
}

}
}
}
}


void replace()
{
if(bestindividual.fitness>currentbest.fitness)
{
currentbest = population[best_val];
}
}

void main()
{
m_A0=-atan(1)*4,m_B0=atan(1)*4;
produce();

cout<<"A0="<<A0<<'\n';
cout<<"A1="<<A1<<'\t'<<"B1="<<B1<<'\n';
cout<<"A2="<<A2<<'\t'<<"B2="<<B2<<'\n';
cout<<"A3="<<A3<<'\t'<<"B3="<<B3<<'\n';
cout<<"A4="<<A4<<'\t'<<"B4="<<B4<<'\n';
for(int i=0;i<maxgen;i++)
{
evaluation();
fitness();
findbest();
select();
crossover();
mutate();
replace();
}
cout<<"m_A0="<<m_A0<<'\t'<<"m_B0="<<m_B0<<'\n';
cout<<bestindividual.fitness<<'\n';
cout<<currentbest.value<<'\n';
cout<<"kind="<<kind<<'\n';
//cout<<"A0="<<population[PopSize-1].chrom[0]<<'\n';
//cout<<"A1="<<population[PopSize-1].chrom[1]<<'\t'<<"B1="<<population[PopSize-1].chrom[2]<<'\n';
//cout<<"A2="<<population[PopSize-1].chrom[3]<<'\t'<<"B2="<<population[PopSize-1].chrom[4]<<'\n';
//cout<<"A3="<<population[PopSize-1].chrom[5]<<'\t'<<"B3="<<population[PopSize-1].chrom[6]<<'\n';
//cout<<"A4="<<population[PopSize-1].chrom[7]<<'\t'<<"B4="<<population[PopSize-1].chrom[8]<<'\n';
cout<<"A0="<<A0<<'\n';
cout<<"A1="<<A1<<'\t'<<"B1="<<B1<<'\n';
cout<<"A2="<<A2<<'\t'<<"B2="<<B2<<'\n';
cout<<"A3="<<A3<<'\t'<<"B3="<<B3<<'\n';
cout<<"A4="<<A4<<'\t'<<"B4="<<B4<<'\n';
//for(int j=0;j<100;j++){
// cout<<x[j]<<'\t'<<y[j]<<'\t'<<z[j]<<'\t';
//}
}
GeneralRe: SOS [about Genetic Algorithms] Pin
Vadim Tabakman14-Jun-04 18:10
Vadim Tabakman14-Jun-04 18:10 
GeneralRe: SOS [about Genetic Algorithms] Pin
onetwone14-Jun-04 22:19
onetwone14-Jun-04 22:19 
Questionhow to get these Rect Area Pin
yingkou14-Jun-04 16:50
yingkou14-Jun-04 16:50 
AnswerRe: how to get these Rect Area Pin
Johan Rosengren14-Jun-04 20:53
Johan Rosengren14-Jun-04 20:53 
GeneralException handling - catch(...) Pin
Tom Archer14-Jun-04 16:40
Tom Archer14-Jun-04 16:40 
GeneralRe: Exception handling - catch(...) Pin
Johnny ²14-Jun-04 21:19
Johnny ²14-Jun-04 21:19 
GeneralRe: Exception handling - catch(...) Pin
V.14-Jun-04 21:45
professionalV.14-Jun-04 21:45 
GeneralRe: Exception handling - catch(...) Pin
toxcct14-Jun-04 22:11
toxcct14-Jun-04 22:11 
GeneralSending messages with ::PostMessage Pin
kfaday14-Jun-04 16:32
kfaday14-Jun-04 16:32 
GeneralRe: Sending messages with ::PostMessage Pin
Vadim Tabakman14-Jun-04 16:38
Vadim Tabakman14-Jun-04 16:38 
GeneralRe: Sending messages with ::PostMessage Pin
kfaday14-Jun-04 17:19
kfaday14-Jun-04 17:19 
GeneralRe: Sending messages with ::PostMessage Pin
Vadim Tabakman14-Jun-04 17:42
Vadim Tabakman14-Jun-04 17:42 
GeneralRe: Sending messages with ::PostMessage Pin
V.14-Jun-04 21:47
professionalV.14-Jun-04 21:47 
GeneralRe: Sending messages with ::PostMessage Pin
kfaday15-Jun-04 8:13
kfaday15-Jun-04 8:13 
GeneralRe: Sending messages with ::PostMessage Pin
V.15-Jun-04 21:01
professionalV.15-Jun-04 21:01 
GeneralRe: Sending messages with ::PostMessage Pin
kfaday16-Jun-04 2:28
kfaday16-Jun-04 2:28 
GeneralChanging the Mouse cursor Pin
kfaday14-Jun-04 16:19
kfaday14-Jun-04 16:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.