?? functionguotao.txt
字號(hào):
演化計(jì)算是基于隨即搜索的新算法;它的技術(shù)模型源于自然的演化。下面是一個(gè)例子,該函數(shù)是典型的多峰(震動(dòng)劇烈)的函數(shù)。用的算法是郭濤算法。
問題:
求函數(shù)的最大值 :
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
目前最好結(jié)果:f(11.6255448,5.7250441)=38.8502944790207
程序在VC++.NET上調(diào)試,原代碼如下(僅供參考):
/*
* 類_Point表示二維空間的向量,即目標(biāo)函數(shù)的自變量點(diǎn)
*
***************************************************************/
#pragma once
class _Point
{
public:
double 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;
}
};
/*
* 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;//種群規(guī)模
const int M=8; //子空間V的維度
const double MIN=0.000000009;//停機(jī)精確度
const double 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; //迭代次數(shù)
int i_worst=0; //種群P中最差所在位置
double 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)
{//迭代計(jì)算
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);
//如果有提高,打印中間結(jié)果
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;
}
/*
* 隨機(jī)數(shù)產(chǎn)生函數(shù)(重載兩個(gè))
**/
double Random(double min,double max)
{
double 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;
}
/*
* 求最值的目標(biāo)函數(shù)
**/
double f(const _Point &point)
{
double 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[i].x=Random(-3,12.1);
P[i].y=Random(4.1,5.8);
}
}
/*
* 從種群p中獲得最好與最壞個(gè)體
**/
_Point GetBestX()
{
_Point point=P[0];
double z=f(P[0]),zz;
for(int i=0;i<N;i++){
zz=f(P[i]);
if(z<zz){
z=zz;
point=P[i];
}
}
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[i]);
if(z>zz){
z=zz;
point=P[i];
i_worst=i;
}
}
return point;
}
/*
* 從種群P中隨機(jī)選擇M個(gè)個(gè)體,按照a[](隨機(jī)向量)要滿足的條件:
* Sum(a[i])=1,i=1,2,...,M , & -0.5<=a[i]<=1.5
* 從子空間V中生成一個(gè)新個(gè)體
**/
_Point SelectX()
{
_Point point;
double a[M];
int i_rand=0;
double sum=0.0;
double MAX=1.5;
for(int i=0;i<M;i++){
i_rand=Random(N);
Select[i]=P[i_rand];
}
for(int i=0;i<M-1;i++){
a[i]=Random(-0.5,MAX);
MAX=1-a[i];
sum=sum+a[i];
}
for(int i=0;i<M;i++)
point=point+Select[i]*a[i];
return point;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -