//this file is comentropy and TOPSIS to decide the final decsion solution
#include"declaration.h"
void GA::TOPSIS()
{
//cout<<"hellow"<<endl;
int i,j,k,colum;
double sum=0.0;
double M[maxpop][maxfun];
double R_standard[maxpop][maxfun];
double V_weight_standard[maxpop][maxfun];
double ideal_solution[maxfun];
double negative_ideal_solution[maxfun];
double min,max;
double s_ideal_solution[maxpop];
double s_negative_ideal_solution[maxpop];
double C_relative_approach_degree[maxpop];
const double K=1/log(popsize);
double p_i_j[maxpop][maxfun];
double E_j[maxfun];
double d_j[maxfun];
double w_j[maxfun];
for(i=0;i<popsize;i++)
for(j=0;j<nfunc;j++)
M[i][j]=oldpop.ind[i].fitness[j];
/*--------------------------*信息熵法确定权重*------------------------------*/
for(j=0;j<nfunc;j++)
{
for(i=0;i<popsize;i++)
{
for(k=0;k<popsize;k++)
{
sum+=M[k][j];
}
p_i_j[i][j]=M[i][j]/sum;
sum=0;
}
}
sum=0;
for(j=0;j<nfunc;j++)
{
for(i=0;i<popsize;i++)
{
sum+=p_i_j[i][j]*log(p_i_j[i][j]);
E_j[j]=-K*sum;
}
sum=0;
}
for(j=0;j<nfunc;j++)
d_j[j]=1-E_j[j];
sum=0;
for(j=0;j<nfunc;j++)
sum+=d_j[j];
for(j=0;j<nfunc;j++)
w_j[j]=d_j[j]/sum;
sum=0;
if(answer==1)
{
for(j=0;j<nfunc;j++)
sum+=subjective_weight[j]*w_j[j];
for(j=0;j<nfunc;j++)
w_j[j]=w_j[j]*subjective_weight[j]/sum;
}
cout<<"信息熵法权重:"<<" ";
for(int ac=0;ac<nfunc;ac++)
cout<<w_j[ac]<<" ";
cout<<endl;
sum=0;
/*for(i=0;i<popsize;i++)
{
for(j=0;j<nfunc;j++)
cout<<p_i_j[i][j]<<" ";
cout<<endl;
}
cout<<endl;
for(j=0;j<nfunc;j++)
cout<<E_j[j]<<" ";
cout<<endl;
for(j=0;j<nfunc;j++)
cout<<d_j[j]<<" ";
cout<<endl;
for(j=0;j<nfunc;j++)
cout<<w_j[j]<<" ";
cout<<endl;*/
/*--------------------------*TOPSIS法*------------------------------*/
for(j=0;j<nfunc;j++)
{
for(i=0;i<popsize;i++)
{
for(k=0;k<popsize;k++)
{
sum+=pow(M[k][j],2);
}
R_standard[i][j]=M[i][j]/sqrt(sum);
sum=0;
}
}
for(j=0;j<nfunc;j++)
for(i=0;i<popsize;i++)
V_weight_standard[i][j]=w_j[j]*R_standard[i][j];
for(j=0;j<nfunc;j++)
{
min=V_weight_standard[0][j];
for(i=0;i<popsize;i++)
if(V_weight_standard[i][j]<min)
min=V_weight_standard[i][j];
ideal_solution[j]=min;
}
for(j=0;j<nfunc;j++)
{
max=V_weight_standard[0][j];
for(i=0;i<4;i++)
if(V_weight_standard[i][j]>max)
max=V_weight_standard[i][j];
negative_ideal_solution[j]=max;
}
sum=0;
for(i=0;i<popsize;i++)
{
for(j=0;j<nfunc;j++)
sum+=pow(V_weight_standard[i][j]-ideal_solution[j],2);
s_ideal_solution[i]=sqrt(sum);
sum=0;
}
for(i=0;i<popsize;i++)
{
for(j=0;j<nfunc;j++)
sum+=pow(V_weight_standard[i][j]-negative_ideal_solution[j],2);
s_negative_ideal_solution[i]=sqrt(sum);
sum=0;
}
for(i=0;i<popsize;i++)
C_relative_approach_degree[i]=s_negative_ideal_solution[i]/(s_negative_ideal_solution[i]+s_ideal_solution[i]);
max=C_relative_approach_degree[0];
colum=0;
for(i=0;i<popsize;i++)
{
if(C_relative_approach_degree[i]>max)
{
max=C_relative_approach_degree[i];
colum=i;
}
}
ofstream outf("TOPSIS.out");
outf<<"the final decsion solution according to TOPSIS approcach is:"<<colum+1<<"th solution"<<endl;
outf<<"#variable(first "<<nvar<<')';
outf<<" #Fitness_vector(next "<<nfunc<<')'<<" #Constraint_violation(next "<<ncons<<')'<<" #Overall_penalty"<<endl;
for(j=0;j<nvar;j++)
outf<<setiosflags(ios::left)<<setiosflags(ios::fixed)<<setprecision(6)<<oldpop.ind[colum].xreal[j]<<" ";
for(j=0;j<nfunc;j++)
outf<<setiosflags(ios::left)<<setiosflags(ios::fixed)<<setprecision(6)<<oldpop.ind[colum].fitness[j]<<" ";
for(j=0;j<ncons;j++)
outf<<setiosflags(ios::left)<<setiosflags(ios::fixed)<<setprecision(6)<<oldpop.ind[colum].constr[j]<<" ";
outf<<setiosflags(ios::left)<<setiosflags(ios::fixed)<<setprecision(6)<<oldpop.ind[colum].error;
outf.close();
/*cout<<endl;
for(i=0;i<popsize;i++)
{
for(j=0;j<nfunc;j++)
cout<<R_standard[i][j]<<" ";
cout<<endl;
}
cout<<endl;
for(i=0;i<popsize;i++)
{
for(j=0;j<nfunc;j++)
cout<<V_weight_standard[i][j]<<" ";
cout<<endl;
}
cout<<endl;
for(j=0;j<nfunc;j++)
cout<<ideal_solution[j]<<" ";
cout<<endl;
cout<<endl;
for(j=0;j<nfunc;j++)
cout<<negative_ideal_solution[j]<<" ";
cout<<endl;
for(i=0;i<popsize;i++)
cout<<s_ideal_solution[i]<<" ";
cout<<endl;
for(i=0;i<popsize;i++)
cout<<s_negative_ideal_solution[i]<<" ";
cout<<endl;
for(i=0;i<popsize;i++)
cout<<C_relative_approach_degree[i]<<" ";
cout<<endl;
// return 0;*/
}