演化计算实例(0分)

  • 主题发起人 主题发起人 CDlee
  • 开始时间 开始时间
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 &amp;
operator =(const _Point &amp;point)
{
this->x=point.x;
this->y=point.y;
return *this;
}
_Point &amp;
operator +(const _Point &amp;point)
{
this->x+=point.x;
this->y+=point.y;
return *this;
}
_Point &amp;
operator *(double k)
{
this->x*=k;
this->y*=k;
return *this;
}
};
/*
* name:percy lee
* e-mail:percylee@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 &amp;point);
void Initialize_P();
_Point GetBestX();
_Point GetWorstX(int &amp;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 &amp;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 &amp;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 , &amp;
-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联系,万分感激。
 
先求二阶偏微分对X,对y,然后再搜索!
给分!!
 
To:秀
没有这样要分的吧?
不是日本的MM? 是GG?
 
thx1180:????
没看到:
>z=point.x*sin(4*PI*point.x)+point.y*sin(20*PI*point.y)+21.5;
是直接求的吗?
????????
我提出更好的方法
不可以要分吗??!
 
惨了!!
问题:演化计算实例 ( 积分:0, 回复:3, 阅读:13 )
 
To:秀
别太在意,跟你开个玩笑。
我看你只“给介绍介绍各位大虾咯!”,小虾米不用理它。
说实话,楼主的代码看得我晕!!!
 
嘻嘻,我会介意吗? ^_^
if 你比我大
then
s:='大哥'
else
s:='弟弟';
以后还请+s+多关照啊! : )
 
Result := '大哥';
 
关键地方写错了:
do
uble sum=0;
//应该是double类型的:)
To:秀:
呵呵,演化计算可不是直接求值,遗传算法便是它的一个例子。这个函数用传统计算
方法是很难求出最大值的。不信你用数学工具画出它的图形,哈哈,你就发现它多峰的
“壮观”!
如果大家有好的结果,我当然愿意送分!
 
对了,算法是“郭涛算法”---武大的一个博士写的。这个程序是他的导师(现在在我们
学校做系主任)给我们研讨班布置的练习题(我凑数听的:)。
 
CDlee啊,你以为把博士啊导师啊搬出来,我就怕了?!
看看你PI的定义:
>constdo
uble PI=3.14159265;
呵呵,我又定义了一下:
const
PI:Double=3.14159265358979324;
这个时候,看看你“最好”的解:
f(11.625544534797,5.7250437340051)=38.85029449622
就变成了:
f(11.625544534797,5.7250437340051)=38.8502944753644
嘻嘻!而人家的:
f(11.6255448,5.7250441)=38.8502944790207
可是比你的好喔!!
我用微分的办法求出来:
f(11.6255447016806,5.72504424420114)=38.8502944794474
更加好吧?
时间:38876 ms
 
To:秀
[:)][:D][8D][:(][:(!][^][?]
 
thx1180:
[:)][:D][8D][:(][:(!][^][?]
[:)] [:D] [8D] [:I] [:P] [}:)] [;)] [:o)]
[B)] [8] [:(] [8)] [:O] [:(!] [xx(] [|)] [:X] [^] [V] [?]
 
To 秀
男的女的?
 
To:秀
这么多, Result := '大哥' + '小虾米'吃不消了。
To:阿西喊佛
这么可爱的一个小妹妹,你还问?
 
thx1180: : )
>男的女的?
有什么企图?快说! [:(!]
 
To:秀
[:D][:D][:D][:D][:D][:D]
//有什么企图?
这不是...?
 
好快呀,什么算法来的,我试着写了一个演化算法,简直惨不忍睹,贴出来,
各位指点一下。
Unit2.h
//---------------------------------------------------------------------------
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
using namespace std;
class _Point
{
public:
do
uble x,y,fxy;
_Point(void):x(0),y(0),fxy(0)
{
}
_Point(constdo
uble xx,constdo
uble yy):x(xx),y(yy),fxy(0)
{

}
~_Point(void)
{
}
inline _Point &amp;
operator =(const _Point &amp;point)
{
this->x=point.x;
this->y=point.y;
this->fxy = point.fxy;
return *this;
}
inline _Point &amp;
operator =(constdo
uble &amp;afxy)
{
this->fxy = afxy;
return *this;
}
inline _Point operator +(const _Point &amp;point)
{
_Point p;
p.x= this->x+point.x;
p.y=this->y+point.y;
return p;
}
inline _Point operator -(const _Point &amp;point)
{
_Point p;
p.x= this->x-point.x;
p.y=this->y-point.y;
return p;
}
inline _Point operator *(constdo
uble k)
{
_Point p;
p.x= this->x*k;
p.y=this->y*k;
return p;
}
inline bool operator <(const _Point &amp;point)
{
return this->fxy < point.fxy;
}
inline bool operator >(const _Point &amp;point)
{
return this->fxy > point.fxy;
}
};
#endif
/******************************************************************************/
test2.cpp
/******************************************************************************/
#include "Unit2.h"
const int N=20;//种群规模
constdo
uble MIN=0.000000009;//停机精确度
constdo
uble PI=3.14159265;
const _Point vmax(15.1,1.7);
constdo
uble C1=2;
constdo
uble C2=2;
_Point present[N],v[N], pBest[N] ,gBest,oldgBest;

double Random(double min,double max);
inlinedo
uble Random();
inlinedo
uble f(const _Point &amp;point);
void Initialize_P();
inline _Point GetBestX();

//---------------------------------------------------------------------------
void Test2()
{ clock_t start, finish;
int t=0;
srand((unsigned)time(NULL));
start=clock();
Initialize_P();
do
{
for(int j=0;j<N;j++){
present[j]=f(present[j]);
if ( present[j]>pBest[j] )
pBest[j]=present[j];
}
gBest=GetBestX();
for(int i=0;i<N;i++){
v=v+(pBest-present)*Random()*C1 +
(gBest-present)* Random()* C2 ;
v.x=v.x>vmax.x ?vmax.x:v.x ;
v.y=v.y>vmax.y ?vmax.y:v.y ;
v.x=v.x<(-vmax.x) ?vmax.x:-v.x ;
v.y=v.y<(-vmax.y) ?vmax.y:-v.y ;
_Point px;
px=present+v;
if(px.x<-3||px.x>12.1||px.y<4.1||px.y>5.8)
continue;
present=px;
}

if (gBest>oldgBest){
finish=clock();
cout<<finish-start<<endl;
cout<<gBest.x<<" "<<gBest.y << " " << gBest.fxy <<endl;
oldgBest=gBest;
t++;
if (t>100)
cin>>t;
}
}
while((gBest.fxy -oldgBest.fxy)<= MIN);
finish=clock();
cout<<finish-start<<endl;
cout<<"赶快跑去买彩票"<<endl;
cout<<gBest.x<<" "<<gBest.y << " " << gBest.fxy <<endl;
cin>>t;
};
double Random(double min,double max)
{
do
uble randNum=0.0;
randNum=(double)rand()/RAND_MAX;
randNum*=(max-min);
randNum+=min;
return randNum;
}
inlinedo
uble Random()
{
return (double)rand()/(double)RAND_MAX;
}
/*
* 求最值的目标函数
**/
inlinedo
uble f(const _Point &amp;point)
{
return point.x*sin(4*PI*point.x)+point.y*sin(20*PI*point.y)+21.5;
}
/*
* 初始化种群
**/
void Initialize_P()
{
for(int i=0;i<N;i++){
present.x=Random(-3,12.1);
present.y=Random(4.1,5.8);
}
}

_Point GetBestX()
{
_Point point=pBest[0];
for(int i=1;i<N;i++){
if(point<pBest){
point=pBest;
}
}
return point;
}
/****************************************************************************/
/****************************************************************************/
//---------------------------------------------------------------------------
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "Unit2.h"
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
void Test2();
int main(int argc, char* argv[])
{
cout.precision(14);
randomize();
Test2();
return 0;
}
//---------------------------------------------------------------------------
 
55555555555
有 delphi的代码么
 
后退
顶部