#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
const double b = 0.9;
double u = 0;
///.....计算函数的值......
double Ifunc(double x, double y, double z)
{
double hsz = 0.0;
hsz = 50 - pow(5 * x - 2 * y , 2) - pow(x - 7 * y * y + 2 * z * z , 4);
return hsz;
}
/..........概率计算.........//
double glfunc(double w1, double w2, double T)
{
double p;
double u = 0;
double w3 = - 1.00 * abs(w1 - w2);
u = exp(w3 / (b * T));
if (u < 1)
p = u;
else
p = 1;
return p;
}
void main()
{
double x = 0 , y = 0 , z = 0 , x_next = 0 , y_next = 0 , z_next = 0 , hsz = 0 , hsz_next = 0 , hsz_max = 0;
double x_max , y_max , z_max;
cout << "请输入 x y z 值 : " ;
cin >> x >> y >> z;
cout << endl;
double T = 200;
hsz_max = Ifunc(x,y,z);
do
{
hsz = Ifunc(x,y,z);
cout << "hsz = " << hsz << endl;//计算初始值
srand(static_cast<unsigned>(time(static_cast<time_t *>(NULL))));
//进行 y = x + z 变换
x_next = x + -x * (rand() % 10000)/ 10100.00 ;
y_next = y + -y * (rand() % 10000)/ 10100.00 ;
z_next = z + -z * (rand() % 10000)/ 10100.00 ;
cout << "x_next = " << x_next << endl;
cout << "y_next = " << y_next << endl;
cout << "z_next = " << z_next << endl;
hsz_next = Ifunc(x_next,y_next,z_next),u = 0.9 * u;
cout << "hsz_next = " << hsz_next << endl;
//随机选取数r_rand
double r_rand = 0.01 * (rand() + 500) / 33000.00;
cout << "r_rand = " << r_rand << endl;
double p = glfunc(hsz,hsz_next,T);
///概率接受检验
if (r_rand <= 1)
{
x = x_next;
y = y_next;
z = z_next;
hsz = hsz_next;
}
else
{
x_next = x;
y_next = y;
z_next = z;
hsz_next = hsz;
}
if(hsz_next > hsz_max)
{
x_max = x_next;
y_max = y_next;
z_max = z_next;
hsz_max = hsz_next;
T = T * b;
}
cout << endl << endl;
cout << "最大值为 :" << hsz_max << endl;
cout << "当取得最大值时" << endl;
cout << "x_max = " << x_max << endl;
cout << "y_max = " << y_max << endl;
cout << "z_max = " << z_max << endl;
cout << "T ="<<T<<endl;
cout << "***************************" << endl;
//getchar();
}while(T >= 0.5);
}
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
const double b = 0.9;
double u = 0;
///.....计算函数的值......
double Ifunc(double x, double y, double z)
{
double hsz = 0.0;
hsz = 50 - pow(5 * x - 2 * y , 2) - pow(x - 7 * y * y + 2 * z * z , 4);
return hsz;
}
/..........概率计算.........//
double glfunc(double w1, double w2, double T)
{
double p;
double u = 0;
double w3 = - 1.00 * abs(w1 - w2);
u = exp(w3 / (b * T));
if (u < 1)
p = u;
else
p = 1;
return p;
}
void main()
{
double x = 0 , y = 0 , z = 0 , x_next = 0 , y_next = 0 , z_next = 0 , hsz = 0 , hsz_next = 0 , hsz_max = 0;
double x_max , y_max , z_max;
cout << "请输入 x y z 值 : " ;
cin >> x >> y >> z;
cout << endl;
double T = 200;
hsz_max = Ifunc(x,y,z);
do
{
hsz = Ifunc(x,y,z);
cout << "hsz = " << hsz << endl;//计算初始值
srand(static_cast<unsigned>(time(static_cast<time_t *>(NULL))));
//进行 y = x + z 变换
x_next = x + -x * (rand() % 10000)/ 10100.00 ;
y_next = y + -y * (rand() % 10000)/ 10100.00 ;
z_next = z + -z * (rand() % 10000)/ 10100.00 ;
cout << "x_next = " << x_next << endl;
cout << "y_next = " << y_next << endl;
cout << "z_next = " << z_next << endl;
hsz_next = Ifunc(x_next,y_next,z_next),u = 0.9 * u;
cout << "hsz_next = " << hsz_next << endl;
//随机选取数r_rand
double r_rand = 0.01 * (rand() + 500) / 33000.00;
cout << "r_rand = " << r_rand << endl;
double p = glfunc(hsz,hsz_next,T);
///概率接受检验
if (r_rand <= 1)
{
x = x_next;
y = y_next;
z = z_next;
hsz = hsz_next;
}
else
{
x_next = x;
y_next = y;
z_next = z;
hsz_next = hsz;
}
if(hsz_next > hsz_max)
{
x_max = x_next;
y_max = y_next;
z_max = z_next;
hsz_max = hsz_next;
T = T * b;
}
cout << endl << endl;
cout << "最大值为 :" << hsz_max << endl;
cout << "当取得最大值时" << endl;
cout << "x_max = " << x_max << endl;
cout << "y_max = " << y_max << endl;
cout << "z_max = " << z_max << endl;
cout << "T ="<<T<<endl;
cout << "***************************" << endl;
//getchar();
}while(T >= 0.5);
}