C
CDlee
Unregistered / Unconfirmed
GUEST, unregistred user!
问题:
求函数的最大值 :
f(x,y)=21.5+x*sin(4*PI*x)+y*sin(20*PI*y)
定义域 D: -3<=x<=12.1 , 4.1<=y<=5.8
目前最好结果:f(11.6255448,5.7250441)=38.8502944790207
使用演化计算,下面是我的两次特选的计算过程(结果都好于上述结果)---
第一次:
The time is : 94 ms...
f(10.613780655192,5.4270725677725)=37.389590453122
The time is : 828 ms...
f(11.648138627057,5.6314757672118)=37.830907986378
The time is : 859 ms...
f(11.623185595273,5.5174844157187)=38.03382295962
The time is : 859 ms...
f(11.634316347843,5.6190768060714)=38.289083748513
The time is : 906 ms...
f(11.620488075365,5.6297732837676)=38.480285785697
The time is : 1469 ms...
f(11.624052756192,5.5211617593343)=38.484611826977
The time is : 1906 ms...
f(11.629479334248,5.7245406683673)=38.833217277123
The time is : 7547 ms...
f(11.628501769182,5.7256210877389)=38.838506980212
The time is : 11015 ms...
f(11.628990553932,5.725245725128)=38.83893540685
The time is : 11969 ms...
f(11.627319059268,5.7247037616953)=38.846094206515
The time is : 12734 ms...
f(11.626627654967,5.72470024015)=38.847880597523
The time is : 16531 ms...
f(11.626284272148,5.7248784052774)=38.849481578093
The time is : 21828 ms...
f(11.62546600176,5.725088372424)=38.85026681199
The time is : 35437 ms...
f(11.625691375017,5.7250492572474)=38.850274471389
The time is : 37422 ms...
f(11.625424432767,5.7250520077432)=38.850280537875
The time is : 57969 ms...
f(11.625544924148,5.7250393798973)=38.850294231074
The time is : 300734 ms...
f(11.625537295811,5.7250458543146)=38.850294419674
The time is : 320734 ms...
f(11.625551305625,5.7250456271188)=38.850294438025
The time is : 524906 ms...
f(11.625546222444,5.725042960308)=38.850294478362
The time is : 622531 ms...
f(11.625541563766,5.7250436853337)=38.850294486529
The time is : 887437 ms...
f(11.625543834178,5.7250447922435)=38.850294495249
...(强行结束,时间太长了,我忍无可忍:)
第二次:
The time is : 62 ms...
f(11.15327680912,4.7177078534651)=36.187644880164
The time is : 547 ms...
f(10.114454845179,5.1190655310813)=36.293084212477
The time is : 609 ms...
f(11.132918545622,5.2277957888025)=37.725190221753
The time is : 640 ms...
f(11.602294240881,5.6246903015475)=38.256827946179
The time is : 844 ms...
f(11.628592228779,5.3231210514507)=38.402814315623
The time is : 1297 ms...
f(11.629693704912,5.6207093957877)=38.527164800413
The time is : 1375 ms...
f(11.618814053708,5.7228828559478)=38.756049789327
The time is : 3312 ms...
f(11.630681377929,5.725831044228)=38.819079619686
The time is : 9562 ms...
f(11.624684717387,5.725069716947)=38.849608272997
The time is : 16797 ms...
f(11.626123696597,5.7251320898199)=38.849899572729
The time is : 21687 ms...
f(11.625976613617,5.7249107707789)=38.849921915857
The time is : 22000 ms...
f(11.625608911871,5.7251796919998)=38.850083407698
The time is : 31547 ms...
f(11.625827954136,5.7251238770665)=38.850149203063
The time is : 32656 ms...
f(11.625531350969,5.7251428369372)=38.850184498982
The time is : 37703 ms...
f(11.625600864559,5.7250710504427)=38.850283488897
The time is : 41281 ms...
f(11.625610878351,5.7250375333562)=38.850289970939
The time is : 55172 ms...
f(11.625512817363,5.7250396718597)=38.850293328183
The time is : 122812 ms...
f(11.625550504992,5.7250390354831)=38.850294161098
The time is : 186172 ms...
f(11.625544534797,5.7250437340051)=38.85029449622
The time is : 186219 ms...
Now the answer(max of f) is :
f(11.625544534797,5.7250437340051)=38.85029449622
Press any key to continue
程序在VC++.NET上调试,原代码如下(仅供有关同学参考):
/*
* 类_Point表示二维空间的向量,即目标函数的自变量点
*
***************************************************************/
#pragma once
class _Point
{
public:
do
uble x,y;
_Point(void):x(0),y(0)
{
}
_Point(double xx,double yy):x(xx),y(yy)
{
}
~_Point(void)
{
}
_Point &
operator =(const _Point &point)
{
this->x=point.x;
this->y=point.y;
return *this;
}
_Point &
operator +(const _Point &point)
{
this->x+=point.x;
this->y+=point.y;
return *this;
}
_Point &
operator *(double k)
{
this->x*=k;
this->y*=k;
return *this;
}
};
/*
* nameercy lee
* e-mailercylee@eyou.com
* time:2003-3-16
*
* compute the max_number of :
* f(x,y)=21.5+x*sin(4*PI*x)+y*sin(20*PI*y)
* D: -3<=x<=12.1 , 4.1<=y<=5.8
***************************************************************************/
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "_point.h"
using namespace std;
const int N=20;//种群规模
const int M=8;
//子空间V的维度
constdo
uble MIN=0.000000009;//停机精确度
constdo
uble PI=3.14159265;
double Random(double min,double max);
int Random(int max);
double f(const _Point &point);
void Initialize_P();
_Point GetBestX();
_Point GetWorstX(int &i_worst);
_Point SelectX();
_Point P[N],x_best,x_worst,x_oldbest;
_Point Select[M],x;
void main()
{
clock_t start, finish;
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand((unsigned)time(NULL));
start=clock();
Initialize_P();
long int t=0;
//迭代次数
int i_worst=0;
//种群P中最差所在位置
do
uble z_best=0.0,z_worst=0.0;
x_best=GetBestX();
x_oldbest=x_best;
x_worst=GetWorstX(i_worst);
z_best=f(x_best);
z_worst=f(x_worst);
while(z_best-z_worst>MIN)
{//迭代计算
x=SelectX();
if(x.x<-3||x.x>12.1||x.y<4.1||x.y>5.8)
continue;
if(f(x)>z_worst)
P[i_worst]=x;
else
{//!
//
//
}
t++;
x_oldbest=x_best;
x_best=GetBestX();
x_worst=GetWorstX(i_worst);
z_best=f(x_best);
z_worst=f(x_worst);
//如果有提高,打印中间结果
if(z_best>f(x_oldbest)){
finish=clock();
cout<<"/nThe time is : "<<(finish-start)<<" ms..."<<endl;
cout.precision(14);
cout<<"f("<<x_best.x<<","<<x_best.y<<")="<<z_best<<endl;
}
}
finish=clock();
cout<<"/nThe time is : "<<(finish-start)<<" ms...";
cout<<"/nNow the answer(max of f) is :"<<endl;
cout.precision(14);
cout<<"f("<<x_best.x<<","<<x_best.y<<")="<<z_best<<endl<<endl;
}
/*
* 随机数产生函数(重载两个)
**/
double Random(double min,double max)
{
do
uble randNum=0.0;
randNum=(double)rand()/RAND_MAX;
randNum*=(max-min);
randNum+=min;
return randNum;
}
int Random(int max)
{
int randNum=1;
randNum=(int)(max*((double)rand()/RAND_MAX));
return randNum;
}
/*
* 求最值的目标函数
**/
double f(const _Point &point)
{
do
uble z=0.0;
z=point.x*sin(4*PI*point.x)+point.y*sin(20*PI*point.y)+21.5;
return z;
}
/*
* 初始化种群
**/
void Initialize_P()
{
for(int i=0;i<N;i++){
P.x=Random(-3,12.1);
P.y=Random(4.1,5.8);
}
}
/*
* 从种群p中获得最好与最坏个体
**/
_Point GetBestX()
{
_Point point=P[0];
double z=f(P[0]),zz;
for(int i=0;i<N;i++){
zz=f(P);
if(z<zz){
z=zz;
point=P;
}
}
return point;
}
_Point GetWorstX(int &i_worst)
{
_Point point=P[0];
i_worst=0;
double z=f(P[0]),zz;
for(int i=0;i<N;i++){
zz=f(P);
if(z>zz){
z=zz;
point=P;
i_worst=i;
}
}
return point;
}
/*
* 从种群P中随机选择M个个体,按照a[](随机向量)要满足的条件:
* Sum(a)=1,i=1,2,...,M , &
-0.5<=a<=1.5
* 从子空间V中生成一个新个体
**/
_Point SelectX()
{
_Point point;
double a[M];
int i_rand=0,sum=0;
double MAX=1.5;
for(int i=0;i<M;i++){
i_rand=Random(N);
Select=P[i_rand];
}
for(int i=0;i<M-1;i++){
a=Random(-0.5,MAX);
MAX=1-a;
sum=sum+a;
}
a[M-1]=1-sum;
for(int i=0;i<M;i++)
point=point+Select*a;
return point;
}
希望大家有兴趣的可共同研究。如果您能获得更好的结果,且您愿意与我分享,请与
我e-mail联系,万分感激。
求函数的最大值 :
f(x,y)=21.5+x*sin(4*PI*x)+y*sin(20*PI*y)
定义域 D: -3<=x<=12.1 , 4.1<=y<=5.8
目前最好结果:f(11.6255448,5.7250441)=38.8502944790207
使用演化计算,下面是我的两次特选的计算过程(结果都好于上述结果)---
第一次:
The time is : 94 ms...
f(10.613780655192,5.4270725677725)=37.389590453122
The time is : 828 ms...
f(11.648138627057,5.6314757672118)=37.830907986378
The time is : 859 ms...
f(11.623185595273,5.5174844157187)=38.03382295962
The time is : 859 ms...
f(11.634316347843,5.6190768060714)=38.289083748513
The time is : 906 ms...
f(11.620488075365,5.6297732837676)=38.480285785697
The time is : 1469 ms...
f(11.624052756192,5.5211617593343)=38.484611826977
The time is : 1906 ms...
f(11.629479334248,5.7245406683673)=38.833217277123
The time is : 7547 ms...
f(11.628501769182,5.7256210877389)=38.838506980212
The time is : 11015 ms...
f(11.628990553932,5.725245725128)=38.83893540685
The time is : 11969 ms...
f(11.627319059268,5.7247037616953)=38.846094206515
The time is : 12734 ms...
f(11.626627654967,5.72470024015)=38.847880597523
The time is : 16531 ms...
f(11.626284272148,5.7248784052774)=38.849481578093
The time is : 21828 ms...
f(11.62546600176,5.725088372424)=38.85026681199
The time is : 35437 ms...
f(11.625691375017,5.7250492572474)=38.850274471389
The time is : 37422 ms...
f(11.625424432767,5.7250520077432)=38.850280537875
The time is : 57969 ms...
f(11.625544924148,5.7250393798973)=38.850294231074
The time is : 300734 ms...
f(11.625537295811,5.7250458543146)=38.850294419674
The time is : 320734 ms...
f(11.625551305625,5.7250456271188)=38.850294438025
The time is : 524906 ms...
f(11.625546222444,5.725042960308)=38.850294478362
The time is : 622531 ms...
f(11.625541563766,5.7250436853337)=38.850294486529
The time is : 887437 ms...
f(11.625543834178,5.7250447922435)=38.850294495249
...(强行结束,时间太长了,我忍无可忍:)
第二次:
The time is : 62 ms...
f(11.15327680912,4.7177078534651)=36.187644880164
The time is : 547 ms...
f(10.114454845179,5.1190655310813)=36.293084212477
The time is : 609 ms...
f(11.132918545622,5.2277957888025)=37.725190221753
The time is : 640 ms...
f(11.602294240881,5.6246903015475)=38.256827946179
The time is : 844 ms...
f(11.628592228779,5.3231210514507)=38.402814315623
The time is : 1297 ms...
f(11.629693704912,5.6207093957877)=38.527164800413
The time is : 1375 ms...
f(11.618814053708,5.7228828559478)=38.756049789327
The time is : 3312 ms...
f(11.630681377929,5.725831044228)=38.819079619686
The time is : 9562 ms...
f(11.624684717387,5.725069716947)=38.849608272997
The time is : 16797 ms...
f(11.626123696597,5.7251320898199)=38.849899572729
The time is : 21687 ms...
f(11.625976613617,5.7249107707789)=38.849921915857
The time is : 22000 ms...
f(11.625608911871,5.7251796919998)=38.850083407698
The time is : 31547 ms...
f(11.625827954136,5.7251238770665)=38.850149203063
The time is : 32656 ms...
f(11.625531350969,5.7251428369372)=38.850184498982
The time is : 37703 ms...
f(11.625600864559,5.7250710504427)=38.850283488897
The time is : 41281 ms...
f(11.625610878351,5.7250375333562)=38.850289970939
The time is : 55172 ms...
f(11.625512817363,5.7250396718597)=38.850293328183
The time is : 122812 ms...
f(11.625550504992,5.7250390354831)=38.850294161098
The time is : 186172 ms...
f(11.625544534797,5.7250437340051)=38.85029449622
The time is : 186219 ms...
Now the answer(max of f) is :
f(11.625544534797,5.7250437340051)=38.85029449622
Press any key to continue
程序在VC++.NET上调试,原代码如下(仅供有关同学参考):
/*
* 类_Point表示二维空间的向量,即目标函数的自变量点
*
***************************************************************/
#pragma once
class _Point
{
public:
do
uble x,y;
_Point(void):x(0),y(0)
{
}
_Point(double xx,double yy):x(xx),y(yy)
{
}
~_Point(void)
{
}
_Point &
operator =(const _Point &point)
{
this->x=point.x;
this->y=point.y;
return *this;
}
_Point &
operator +(const _Point &point)
{
this->x+=point.x;
this->y+=point.y;
return *this;
}
_Point &
operator *(double k)
{
this->x*=k;
this->y*=k;
return *this;
}
};
/*
* nameercy lee
* e-mailercylee@eyou.com
* time:2003-3-16
*
* compute the max_number of :
* f(x,y)=21.5+x*sin(4*PI*x)+y*sin(20*PI*y)
* D: -3<=x<=12.1 , 4.1<=y<=5.8
***************************************************************************/
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "_point.h"
using namespace std;
const int N=20;//种群规模
const int M=8;
//子空间V的维度
constdo
uble MIN=0.000000009;//停机精确度
constdo
uble PI=3.14159265;
double Random(double min,double max);
int Random(int max);
double f(const _Point &point);
void Initialize_P();
_Point GetBestX();
_Point GetWorstX(int &i_worst);
_Point SelectX();
_Point P[N],x_best,x_worst,x_oldbest;
_Point Select[M],x;
void main()
{
clock_t start, finish;
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand((unsigned)time(NULL));
start=clock();
Initialize_P();
long int t=0;
//迭代次数
int i_worst=0;
//种群P中最差所在位置
do
uble z_best=0.0,z_worst=0.0;
x_best=GetBestX();
x_oldbest=x_best;
x_worst=GetWorstX(i_worst);
z_best=f(x_best);
z_worst=f(x_worst);
while(z_best-z_worst>MIN)
{//迭代计算
x=SelectX();
if(x.x<-3||x.x>12.1||x.y<4.1||x.y>5.8)
continue;
if(f(x)>z_worst)
P[i_worst]=x;
else
{//!
//
//
}
t++;
x_oldbest=x_best;
x_best=GetBestX();
x_worst=GetWorstX(i_worst);
z_best=f(x_best);
z_worst=f(x_worst);
//如果有提高,打印中间结果
if(z_best>f(x_oldbest)){
finish=clock();
cout<<"/nThe time is : "<<(finish-start)<<" ms..."<<endl;
cout.precision(14);
cout<<"f("<<x_best.x<<","<<x_best.y<<")="<<z_best<<endl;
}
}
finish=clock();
cout<<"/nThe time is : "<<(finish-start)<<" ms...";
cout<<"/nNow the answer(max of f) is :"<<endl;
cout.precision(14);
cout<<"f("<<x_best.x<<","<<x_best.y<<")="<<z_best<<endl<<endl;
}
/*
* 随机数产生函数(重载两个)
**/
double Random(double min,double max)
{
do
uble randNum=0.0;
randNum=(double)rand()/RAND_MAX;
randNum*=(max-min);
randNum+=min;
return randNum;
}
int Random(int max)
{
int randNum=1;
randNum=(int)(max*((double)rand()/RAND_MAX));
return randNum;
}
/*
* 求最值的目标函数
**/
double f(const _Point &point)
{
do
uble z=0.0;
z=point.x*sin(4*PI*point.x)+point.y*sin(20*PI*point.y)+21.5;
return z;
}
/*
* 初始化种群
**/
void Initialize_P()
{
for(int i=0;i<N;i++){
P.x=Random(-3,12.1);
P.y=Random(4.1,5.8);
}
}
/*
* 从种群p中获得最好与最坏个体
**/
_Point GetBestX()
{
_Point point=P[0];
double z=f(P[0]),zz;
for(int i=0;i<N;i++){
zz=f(P);
if(z<zz){
z=zz;
point=P;
}
}
return point;
}
_Point GetWorstX(int &i_worst)
{
_Point point=P[0];
i_worst=0;
double z=f(P[0]),zz;
for(int i=0;i<N;i++){
zz=f(P);
if(z>zz){
z=zz;
point=P;
i_worst=i;
}
}
return point;
}
/*
* 从种群P中随机选择M个个体,按照a[](随机向量)要满足的条件:
* Sum(a)=1,i=1,2,...,M , &
-0.5<=a<=1.5
* 从子空间V中生成一个新个体
**/
_Point SelectX()
{
_Point point;
double a[M];
int i_rand=0,sum=0;
double MAX=1.5;
for(int i=0;i<M;i++){
i_rand=Random(N);
Select=P[i_rand];
}
for(int i=0;i<M-1;i++){
a=Random(-0.5,MAX);
MAX=1-a;
sum=sum+a;
}
a[M-1]=1-sum;
for(int i=0;i<M;i++)
point=point+Select*a;
return point;
}
希望大家有兴趣的可共同研究。如果您能获得更好的结果,且您愿意与我分享,请与
我e-mail联系,万分感激。