📄 funs.cpp
字号:
// Funs.cpp: implementation of the Funs class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MyGA.h"
#include "Funs.h"
#include "stdlib.h"
#include "math.h"
#include "XX1.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#include"Funs.h"
extern int VarNum;
extern char FirstFlag;
double SchafferF6(double x[]) //Schaffer F6,此函数使用全基因变异效果好。
{
double xx1,xx2;
if(FirstFlag)
{
// printf("\n\n Schaffer F6 function n=2 [-100,100]");
VarNum=2; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
xx1=sin(sqrt(x[1]*x[1]+x[2]*x[2]));
xx1=xx1*xx1-0.5;
xx2=1.0+0.001*(x[1]*x[1]+x[2]*x[2]);
xx2=xx2*xx2;
xx1=0.5+xx1/xx2;
return -xx1;
}
double SchafferF7(double x[]) //Schaffer F7
{
double xx1,xx2;
if(FirstFlag)
{
// printf("\n\n Schaffer F7 function n=2 [-100,100]");
VarNum=2; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
xx1=x[1]*x[1]+x[2]*x[2];
xx2=sin(50*pow(xx1,0.1));
xx1=-pow(xx1,0.25)*(xx2*xx2+1.0);
return xx1;
}
double ShubertFun(double x[]) //Shubert function
{
double xx1,xx2;
int i;
if(FirstFlag)
{
// printf("\n\n Shubert function n=2 [-10,10]");
VarNum=2; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
xx1=xx2=0;
for(i=1;i<=5;i++)
{
xx1+=i*cos((i+1)*x[1]+i);
xx2+=i*cos((i+1)*x[2]+i);
}
return -xx1*xx2;
}
double WxzFun1(double x[]) // Designed by wxz
{
double xx,xx1;
if(FirstFlag)
{
// VarNum=2;
FirstFlag=0;
}
// xx=0.001/((x[1]-0.5)*(x[1]-0.5)+0.001);
// xx=xx/((x[1]-0.55)*(x[1]-0.55)+0.0005);
/* xx=sin(Pi*x[1]);//*exp(-x[1]*x[1]*0.1);*/
xx1=x[1]*x[1]+x[2]-11;
xx=660-xx1*xx1;
xx1=x[1]+x[2]*x[2]-7;
xx+=-xx1*xx1;
return xx;
}
double BackStep(double x[]) //Back Step Function1
{
int i;
double xx=0,xz;
if(FirstFlag)
{
// FuncHead("\n\n BackStep function [-100,100]",
// "\nInput dimension(1--100)");
FirstFlag=0; //numbers of the variable(s) in the function.
}
for(i=1;i<=VarNum;i++)
{
// if(i&1)xz=x[i]+cos(i)*i/10;
// else xz=x[i]-cos(i)*i/10;
xz=x[i];//-sqrt(i);
xx+=(xz)*(xz);
}
return -xx;
}
double DeJongF2(double x[]) //De Jong F2 [-2.048,2.048]
{
double xx1,xx2;
if(FirstFlag)
{
// printf("\n\n DeJong F2 function [-2.048,2.048]");
VarNum=2; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
xx1=x[1]*x[1]-x[2];
xx1=xx1*xx1;
xx2=1-x[1];xx2=xx2*xx2;
xx1=100*xx1+xx2;
return -xx1;
}
double DeJongF3(double x[]) //De Jong F3 [-5.12,5.12]
{
int i;
double xx=0;
if(FirstFlag)
{
// printf("\n\n DeJong F3 function [-5.12,5.12]");
VarNum=5; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
for(i=1;i<=VarNum;i++)
xx+=Integer(x[i]);
return -xx;
}
double DeJongF4(double x[]) //De Jong F4 [-1.28,1.28]
{
double xx1;
int i;
if(FirstFlag)
{
// FuncHead("DeJongF4,[-1.28,1.28] ","N(1--100):");
FirstFlag=0;
}
for(xx1=0,i=1;i<=VarNum;i++)
xx1+=i*pow(x[1],4);
xx1=xx1+GaussX(0,1);
return -xx1;
}
double DeJongF5(double x[]) //De Jong F5
{
double xx1=0;
// int i;
if(FirstFlag)
{
// FuncHead("DeJongF5(Foxhole),[-63.536,63.536]");
VarNum=2;
FirstFlag=0;
}
/* for(i=0;i<=24;i++)
{
aj=
xx1=*/
xx1=-x[1]+xx1+Randval(0,1);
return -xx1;
}
double MultiGoal1(double x[]) //Multi-goal [-2,2]
{
double xx1,xx2;
if(FirstFlag)
{
// printf("\n\n Multi Goal1 function n=2 [-2,2]");
VarNum=2; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
xx1=-(x[1]+0.5)*(x[1]+0.5);
xx2=-(x[2]+1.0)*(x[2]+1.0);
return xx1>xx2?xx1:xx2;
}
int Integer(double x)
{
return (int)x;/*
if(x>=0)return (int)(x+0.5);
else return (int)(x-0.5);*/
}
double Zhanglin1(double x[])
{
double xx;
if(FirstFlag)
{
// printf("\n\n Zhanglin function1 [0,1]");
VarNum=1; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
xx=(1-x[1])*x[1]*x[1];
xx=xx*sin(200*Pi*x[1]);
xx=fabs(xx);
return xx; //to find maximum.
}
double Zhanglin2(double x[])
{
double xx;
if(FirstFlag)
{
//printf("\n\n Zhanglin function2, [0,1]");
VarNum=1; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
xx=1-pow(sin(3*Pi*x[1]),20)+pow(sin(20*Pi*x[1]),20);
return pow(xx,20); //to find maximum.
}
double AckleyFun(double x[]) //Ackley 100
{
int i;
double xx1,xx2;
if(FirstFlag)
{
// printf("\n\nAckley function ,[-20,30]");
// printf("\nInput the dimension(1--100): ");
// scanf("%d",&VarNum);
FirstFlag=0;
// if(VarNum>100)VarNum=100;
// if(VarNum<0)VarNum=1;
}
xx1=xx2=0;
for(i=1;i<=VarNum;i++)
{
xx1+=x[i]*x[i];xx2+=cos(2*Pi*x[i]);
}
xx1=-20*exp(-0.2*sqrt(xx1/VarNum))-exp(xx2/VarNum)+20+exp(1);
return -xx1;
}
double Rosenbrock(double x[])
{
int i;
double xx1,xx2;
if(FirstFlag)
{
// printf("\n\nThis is Rosenbrock function, [-5.12,5.12]");
// printf("\nInput the dimension(1--100): ");
// scanf("%d",&VarNum);
FirstFlag=0;
// if(VarNum>100)VarNum=100;
// if(VarNum<0)VarNum=1;
}
xx1=0;
for(i=1;i<=VarNum;i++)
{
if(i==VarNum)xx2=0;
else xx2=x[i+1]-x[i]*x[i];
xx1=xx1+100.0*xx2*xx2+(x[i]-1)*(x[i]-1);
}
return -xx1;
}
double Rastrigin(double x[])
{
int i;
double xx1,xx2;
if(FirstFlag)
{
// printf("\n\nThis is Rasrrign function, [-5.12,5.12]");
// printf("\nInput the dimension(1--100): ");
// scanf("%d",&VarNum);
FirstFlag=0;
}
xx1=0;
for(i=1;i<=VarNum;i++)
{
xx2=x[i]*x[i]-10*cos(2*Pi*x[i]);
xx1=xx1+xx2;
}
return -(VarNum*10+xx1);
}
double Gauss(double x[])
{
int i;
double xx1,xx2=0;
if(FirstFlag)
{
// printf("\n\n Gauss function, [-5.12,5.12]");
VarNum=100; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
for(i=1;i<=VarNum;i++)
{
xx1=i*pow(x[i],4);
xx2+=xx1;//+(500-rand()%1000)/10000.0;
}
return -xx2;
}
double Wxz2(double x[]) //by wxz, [-5.12,5.12]
{
int i;
double xx1,xx2;
xx1=1/(x[1]*x[1]+x[2]*x[2]+0.1);
xx1+=1/((x[1]-1)*(x[1]-1)+(x[2]-1)*(x[2]-1)+0.05);
return xx1;
}
double Goldstein_Price(double x[]) //Goldstein_Price, [-2,2],Min=3
{
double xx1,xx2,xx3,xx4;
if(FirstFlag)
{
// printf("\n\n Goldstein_Price function [-2,2]");
VarNum=2; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
xx1=x[1]+x[2]+1; xx1=xx1*xx1;
xx2=19-14*x[1]+3*x[1]*x[1]-14*x[2]+6*x[1]*x[2]+3*x[2]*x[2];
xx1=1+xx1*xx2;
xx3=2*x[1]-3*x[2];xx3=xx3*xx3;
xx4=18-32*x[1]+12*x[1]*x[1]+48*x[2]-36*x[1]*x[2]+27*x[2]*x[2];
xx3=30+xx3*xx4;
return -xx1*xx3;
}
double Branin_RCOS(double x[]) //x1:[-5,10],x2:[0,15],Min=0.397887
{
static double a,b,c,d,e,f;
if(FirstFlag)
{
// printf("\n\n Branin_RCOS function [-5,15],Min=0.397887");
VarNum=2; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
a=1;b=5.1/(4*Pi*Pi);c=5/Pi;d=6;e=10;f=1/(8*Pi);
}
double xx1,xx2;
xx1=x[2]-b*x[1]*x[1]+c*x[1]-d;xx1=a*xx1*xx1;
xx2=e*(1-f)*cos(x[1])+e;
return -(xx1+xx2);
}
double Colville(double x[]) //Coliville [-10,10],Min=0;
{
double xx1,xx2;
if(FirstFlag)
{
// printf("\n\n Colville function [-10,10],Min=0");
VarNum=4; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
xx1=x[2]-x[1]*x[1];xx1=100*xx1*xx1;
xx2=1-x[1];xx1=xx1+xx2*xx2;
xx2=x[4]-x[3]*x[3];xx1=xx1+90*xx2*xx2;
xx2=1-x[3];xx1=xx1+xx2*xx2;
xx1=xx1+10.1*((x[2]-1)*(x[2]-1)+(x[4]-1)*(x[4]-1));
xx2=19.8*(x[2]-1)*(x[4]-1);
xx1=xx1+xx2;
return -xx1;
}
double BohachevskyF3(double x[]) //[-50,50]
{
double xx1,xx2;
if(FirstFlag)
{
// printf("\n\n BohachevskyF3 function [-50,50]");
VarNum=2; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
xx1=x[1]*x[1]+2*x[2]*x[2]+0.3;
xx2=0.3*cos(3*Pi*x[1]+4*Pi*x[2]);
return -(xx1-xx2);
}
double SolveEquation1(double x[]) //by wxz,[-Pi,Pi]
{
double xx1,xx2,xx3;
if(FirstFlag)
{
// printf("\n\n Solve Equations1 function by Wang Xiangzhong [-Pi,Pi]");
VarNum=3; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
xx1=log(fabs(x[1]))+sin(x[1]*x[2])+cos(x[1]*x[2])-0.5*x[3]*x[3];
xx2=sin((x[1]+x[2])*(x[1]+x[2]))-sin(cos(x[1])*sin(x[2]))-x[3];
xx3=x[1]*x[1]+x[2]*x[2]+x[3]*x[3]-1;
return -(xx1*xx1+xx2*xx2+xx3*xx3);
}
double SolveEquation2(double x[]) //by wxz,[-Pi,Pi]
{
/* original equation
double xx1,xx2;
int i;
/* if(FirstFlag)
{
printf("\n\n Solve Equations2 function by Wang Xiangzhong [-Pi,Pi]");
VarNum=5; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}*/
/*xx1=0;
for(i=1;i<=VarNum;i++)
{
xx2=sin(x[i]*x[i])+exp(x[i])-i-1;
xx1=xx1+xx2*xx2;
}
return -xx1;*/
/* from HuXiaobing
double xx1,xx2;
int i;
if(FirstFlag)
{
printf("\n\n Solve Equations2 function by Wang Xiangzhong [-Pi,Pi]");
VarNum=4; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
xx1=0;
xx2=2*x[1]+6*x[2]-6*x[3]*x[4]-5;
xx1=xx2*xx2;
xx2=(x[1]+x[2])*(x[1]+x[2])-4*x[3]-10*x[4];
xx1+=xx2*xx2;
xx2=2.5*x[1]+pow(2,x[2])-2*x[3]-3*x[4]-4;
xx1+=xx2*xx2;
xx2=x[1]*x[2]+x[3]+x[4]+0.5;
xx1+=xx2*xx2; */
/*form above
double xx1,xx2;
int i;
if(FirstFlag)
{
printf("\n\n Solve Equations2 function by Wang Xiangzhong [-Pi,Pi]");
VarNum=2; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
xx1=0;
xx2=(x[1]+99.7091)*(x[1]+99.7091)+x[2]*x[2]-10000;
xx1=xx2*xx2;
xx2=sin(5*x[1])+cos(5*x[2])-1.9932452;
xx1+=xx2*xx2;
return -xx1;*/
double xx1;
if(FirstFlag)
{
// printf("\n\n Solve Equations2 function by Wang Xiangzhong [-Pi,Pi]");
VarNum=1; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
xx1=fabs(sin(30*x[1]))*(1-fabs(x[1])/2)-0.9739626;
xx1=xx1*xx1;
return -xx1;
}
double ThemoPowerPlant(double x[])
{
static float Pd;
if(FirstFlag)
{
// printf("\n\n ThemoPowerPlant Load Optimization. n=8");
// printf("\nInput the Load Pd (1900,2000,2200,2400,2600,2900): ");
// scanf("%f",&Pd);
/* Upper[0]=600;Lower[0]=100;
Upper[1]=400;Lower[1]=100;
Upper[2]=200;Lower[2]=50;
Upper[3]=440;Lower[3]=110;
Upper[4]=440;Lower[4]=110;
Upper[5]=440;Lower[5]=110;
Upper[6]=300;Lower[6]=70;
Upper[7]=450;Lower[7]=50;*/
VarNum=8; //This is necessory,VarNum indicates the
FirstFlag=0; //numbers of the variable(s) in the function.
}
double xx1,xx2;
int i;
xx2=xx1=0;
for(i=1;i<=VarNum-1;i++)
xx2+=x[i];
x[VarNum]=Pd-xx2;
//xx1=fabs(xx2-Pd)*15.0;
xx1+=0.001562*x[1]*x[1]+7.92*x[1]+561;
xx1+=0.00194*x[2]*x[2]+7.85*x[2]+310;
xx1+=0.00482*x[3]*x[3]+7.97*x[3]+78;
xx1+=0.00139*x[4]*x[4]+7.06*x[4]+500;
xx1+=0.00184*x[5]*x[5]+7.46*x[5]+295;
xx1+=0.00184*x[6]*x[6]+7.46*x[6]+295;
xx1+=0.00230*x[7]*x[7]+8.67*x[7]+173.6;
xx1+=0.00150*x[8]*x[8]+8.14*x[8]+300.8;
return -xx1;
}
double FletchPowellFun(double x[])
{
int i,j;
double xx1,Ai,Bi;
#define MAX_NUM 60
static float A[MAX_NUM][MAX_NUM],B[MAX_NUM][MAX_NUM],Alph[MAX_NUM];
if(FirstFlag)
{
// printf("\n\nThis is Flatcher and Powell function, [-Pi,Pi]");
// printf("\nInput the dimension(1--100): ");
// scanf("%d",&VarNum);
FirstFlag=0;
if(VarNum>MAX_NUM)VarNum=MAX_NUM-2;
if(VarNum<0)VarNum=1;
FILE *fp;
if((fp=fopen("fletch.dat","r"))==NULL)
{
fp=fopen("fletch.dat","w");
for(i=1;i<=VarNum;i++)
{
for(j=1;j<=VarNum;j++)
{A[i][j]=(float)(int)Randval(-100,100);
B[i][j]=(float)(int)Randval(-100,100);
} //for(j=1;...)
//注意此句需要加的 Alph[i]=(float)Randval(Lower[0],Upper[0]);//-.0142,.0142);//Pi,Pi);
printf("%12.10f ",Alph[i]);
if(i%5==0)printf("\n");
} //for(i=1;... */
A[1][1]=-41;A[1][2]=73;A[2][1]=47;A[2][2]=-79;
B[1][1]=47;B[1][2]=90;B[2][1]=-42;B[2][2]=-96;
// Alph[1]=2.356;Alph[2]=-1.0788;
fprintf(fp,"A[%02d][%02d]\n",VarNum,VarNum);
for(i=1;i<=VarNum;i++)
{
for(j=1;j<=VarNum;j++)
{
fprintf(fp,"%6.1f ",A[i][j]);
if((j)%10==0)fprintf(fp,"\n");
}
fprintf(fp,"\n");
}
fprintf(fp,"\nB[%02d][%02d]\n",VarNum,VarNum);
for(i=1;i<=VarNum;i++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -