?? 演化計算(實例:多峰函數最值).txt
字號:
演化計算(實例:多峰函數最值)
演化計算是基于隨即搜索的新算法;它的技術模型源于自然的演化。下面是一個例子,該函數是典型的多峰(震動劇烈)的函數。用的算法是郭濤算法。
問題:
求函數的最大值 :
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
程序在VC++.NET上調試,原代碼如下(僅供參考):
/*
* 類_Point表示二維空間的向量,即目標函數的自變量點
*
***************************************************************/
#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;//種群規模
const int M=8; //子空間V的維度
const double MIN=0.000000009;//停機精確度
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; //迭代次數
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)
{//迭代計算
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)
{
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;
}
/*
* 求最值的目標函數
**/
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中獲得最好與最壞個體
**/
_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中隨機選擇M個個體,按照a[](隨機向量)要滿足的條件:
* Sum(a[i])=1,i=1,2,...,M , & -0.5<=a[i]<=1.5
* 從子空間V中生成一個新個體
**/
_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];
}
a[M-1]=1-sum;
for(int i=0;i<M;i++)
point=point+Select[i]*a[i];
return point;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -