西安电子科技大学
——电子工程学院
最优化方法及应用(1)
上机报告
题 目: 最速下降法求最优解 任课老师:_____ ___xxxxxx__ _______________ 姓 名:_________ xxxxxx___ _______________
学 号:_________ _xxxxxxxx_______ _________ 班 级:________ 02951_____ _ ___________
一、问题叙述: 编程求解函数:
f(X)(x110x2)25(x3x4)2(x22x3)410(x1x4)4
的最小值点及最小值。
二、问题分析
实现方法:最速下降法(C语言) 其中一维线性搜索用黄金分割法。
三、代码
//
//用最速下降法求解
minf(X)=minf(x)=(x1+10*x2)^2+5(x3-x4)^2+(x2-2*x3)^4+10(x1-x4)^4,e=0.001。
#include "stdafx.h" #include #include using namespace std; #define _accuracy 0.001
//plusx() /*
void plusx(float x[4],float g[4],float t){ x[0] += t*g[0];
x[1] += t*g[1]; x[2] += t*g[2]; x[3] += t*g[3];
// 1_Steep.cpp : Defines the entry point for the console application.
}*/
/********** f() *********/ float f(float x[4]){ float sum = 0.0; } //
void plusx(float x1[4],float x0[4],float g[4],float t){ x1[0] = x0[0]+t*g[0]; x1[1] = x0[1]+t*g[1];
sum += (x[0]+10*x[1])*(x[0]+10*x[1]);
sum += 5*(x[2]-x[3])*(x[2]-x[3]);
sum += (x[1]-2*x[2])*(x[1]-2*x[2])*(x[1]-2*x[2])*(x[1]-2*x[2]); sum += 10*(x[0]-x[3])*(x[0]-x[3])*(x[0]-x[3])*(x[0]-x[3]); return sum;
x1[2] = x0[2]+t*g[2];
x1[3] = x0[3]+t*g[3]; }
//searchRange()
void searchRange(float &a,float &b,float x[4],float g[4]){ }
float t0=10,t1=10,t2=2; float h=1; float xi=2; float f0,f1; float x1[4]; int k=0;
while (1)
{ f0 = f(x); }
t2 = t1+h;
plusx(x1,x,g,t2); f1 = f(x1); if (f1
h = xi*h; t0 = t1; t1 = t2; f0 = f1; k++;
continue;
}
else if(0==k) { }
h = -h; t0 = t2; continue;
else { a=(t0
b=(t0
//gold()
void gold(float &a,float &b,float x[4],float g[4]){
float t1,t2;
float x1[4],x2[4]; t2=a+0.382*(b-a);
}
plusx(x2,x,g,t2); t1=a+b-t2;
plusx(x1,x,g,t1); float f1,f2; f1=f(x1); f2=f(x2);
if(f1f2) b=t1; else {a=t2;b=t1;}
/*********************** grad() *****************************/ int grad(float X[4],float g[4]){ g[0]=2*(X[0]+10*X[1])+40*(X[0]-X[3])*(X[0]-X[3])*(X[0]-X[3]); }
/******************** linesearch() *********************/ int linesearch(float Xk1[4],float Xk0[4],float g[4]){ float mina,maxb,Lanbuda; /* searchRange(mina,maxb,Xk0,g);*/ mina = -100;
maxb =100;
while (maxb-mina>_accuracy) { }
Lanbuda = (mina+maxb)/2; plusx(Xk1,Xk0,g,Lanbuda);
gold(mina,maxb,Xk0,g);
g[1]=20*(X[0]+10*X[1])+4*(X[1]-2*X[2])*(X[1]-2*X[2])*(X[1]-2*X[2]); g[2]=10*(X[2]-X[3])-8*(X[1]-2*X[2])*(X[1]-2*X[2])*(X[1]-2*X[2]); g[3]=0-10*(X[2]-X[3])-40*(X[0]-X[3])*(X[0]-X[3])*(X[0]-X[3]); /*float sum = g[0]+g[1]+g[2]+g[3]; g[0] = g[0]/sum; g[1] = g[1]/sum; g[2] = g[2]/sum; g[3] = g[3]/sum;*/ return 0;
return 0; }
/************************main()*****************************/
int main(int argc, char* argv[]) { }
float X0[4]={10,10,10,10},X1[4]={10,10,10,10}; float f0,f1,g[4]; f0=f(X0); grad(X0,g); int k=0;
while(1){ if(0==g[0]*g[0]+g[1]*g[1]+g[2]*g[2]+g[3]*g[3]) break;
linesearch(X1,X0,g); f1=f(X1); grad(X1,g);
if (_accuracy>=g[0]*g[0]+g[1]*g[1]+g[2]*g[2]+g[3]*g[3]) break; X0[0] = X1[0]; X0[1] = X1[1]; X0[2] = X1[2]; X0[3] = X1[3]; /*if(k
printf("minf(X)=%.3f\n",f1);
数%d:\tX=[%.3f,%.3f,%.3f,%.3f]\t",++k,X0[0],X0[1],X0[2],X0[3]);
}*/
printf("迭代次数%d:\tX=[%.3f,%.3f,%.3f,%.3f]\t",++k,X0[0],X0[1],X0[2],X0[3]); printf("minf(X)=%.3f\n",f1); f0=f1; }
printf("target:X=[%.3f,%.3f,%.3f,%.3f]\n",X0[0],X0[1],X0[2],X0[3]); printf("minf(X)=%.3f\n",f1); // cout.unsetf(ios::scientific); // //
cout
return 0;
四、运行结果及分析
运行结果:
函数f(X)(x110x2)25(x3x4)2(x22x3)410(x1x4)4 精度:0.001
起始X=[10,10,10,10] 前20次迭代:
共迭代493次:
可见最速下降法迭代次数很多,收敛速度较慢。
西安电子科技大学
——电子工程学院
最优化方法及应用(1)
上机报告
题 目: 最速下降法求最优解 任课老师:_____ ___xxxxxx__ _______________ 姓 名:_________ xxxxxx___ _______________
学 号:_________ _xxxxxxxx_______ _________ 班 级:________ 02951_____ _ ___________
一、问题叙述: 编程求解函数:
f(X)(x110x2)25(x3x4)2(x22x3)410(x1x4)4
的最小值点及最小值。
二、问题分析
实现方法:最速下降法(C语言) 其中一维线性搜索用黄金分割法。
三、代码
//
//用最速下降法求解
minf(X)=minf(x)=(x1+10*x2)^2+5(x3-x4)^2+(x2-2*x3)^4+10(x1-x4)^4,e=0.001。
#include "stdafx.h" #include #include using namespace std; #define _accuracy 0.001
//plusx() /*
void plusx(float x[4],float g[4],float t){ x[0] += t*g[0];
x[1] += t*g[1]; x[2] += t*g[2]; x[3] += t*g[3];
// 1_Steep.cpp : Defines the entry point for the console application.
}*/
/********** f() *********/ float f(float x[4]){ float sum = 0.0; } //
void plusx(float x1[4],float x0[4],float g[4],float t){ x1[0] = x0[0]+t*g[0]; x1[1] = x0[1]+t*g[1];
sum += (x[0]+10*x[1])*(x[0]+10*x[1]);
sum += 5*(x[2]-x[3])*(x[2]-x[3]);
sum += (x[1]-2*x[2])*(x[1]-2*x[2])*(x[1]-2*x[2])*(x[1]-2*x[2]); sum += 10*(x[0]-x[3])*(x[0]-x[3])*(x[0]-x[3])*(x[0]-x[3]); return sum;
x1[2] = x0[2]+t*g[2];
x1[3] = x0[3]+t*g[3]; }
//searchRange()
void searchRange(float &a,float &b,float x[4],float g[4]){ }
float t0=10,t1=10,t2=2; float h=1; float xi=2; float f0,f1; float x1[4]; int k=0;
while (1)
{ f0 = f(x); }
t2 = t1+h;
plusx(x1,x,g,t2); f1 = f(x1); if (f1
h = xi*h; t0 = t1; t1 = t2; f0 = f1; k++;
continue;
}
else if(0==k) { }
h = -h; t0 = t2; continue;
else { a=(t0
b=(t0
//gold()
void gold(float &a,float &b,float x[4],float g[4]){
float t1,t2;
float x1[4],x2[4]; t2=a+0.382*(b-a);
}
plusx(x2,x,g,t2); t1=a+b-t2;
plusx(x1,x,g,t1); float f1,f2; f1=f(x1); f2=f(x2);
if(f1f2) b=t1; else {a=t2;b=t1;}
/*********************** grad() *****************************/ int grad(float X[4],float g[4]){ g[0]=2*(X[0]+10*X[1])+40*(X[0]-X[3])*(X[0]-X[3])*(X[0]-X[3]); }
/******************** linesearch() *********************/ int linesearch(float Xk1[4],float Xk0[4],float g[4]){ float mina,maxb,Lanbuda; /* searchRange(mina,maxb,Xk0,g);*/ mina = -100;
maxb =100;
while (maxb-mina>_accuracy) { }
Lanbuda = (mina+maxb)/2; plusx(Xk1,Xk0,g,Lanbuda);
gold(mina,maxb,Xk0,g);
g[1]=20*(X[0]+10*X[1])+4*(X[1]-2*X[2])*(X[1]-2*X[2])*(X[1]-2*X[2]); g[2]=10*(X[2]-X[3])-8*(X[1]-2*X[2])*(X[1]-2*X[2])*(X[1]-2*X[2]); g[3]=0-10*(X[2]-X[3])-40*(X[0]-X[3])*(X[0]-X[3])*(X[0]-X[3]); /*float sum = g[0]+g[1]+g[2]+g[3]; g[0] = g[0]/sum; g[1] = g[1]/sum; g[2] = g[2]/sum; g[3] = g[3]/sum;*/ return 0;
return 0; }
/************************main()*****************************/
int main(int argc, char* argv[]) { }
float X0[4]={10,10,10,10},X1[4]={10,10,10,10}; float f0,f1,g[4]; f0=f(X0); grad(X0,g); int k=0;
while(1){ if(0==g[0]*g[0]+g[1]*g[1]+g[2]*g[2]+g[3]*g[3]) break;
linesearch(X1,X0,g); f1=f(X1); grad(X1,g);
if (_accuracy>=g[0]*g[0]+g[1]*g[1]+g[2]*g[2]+g[3]*g[3]) break; X0[0] = X1[0]; X0[1] = X1[1]; X0[2] = X1[2]; X0[3] = X1[3]; /*if(k
printf("minf(X)=%.3f\n",f1);
数%d:\tX=[%.3f,%.3f,%.3f,%.3f]\t",++k,X0[0],X0[1],X0[2],X0[3]);
}*/
printf("迭代次数%d:\tX=[%.3f,%.3f,%.3f,%.3f]\t",++k,X0[0],X0[1],X0[2],X0[3]); printf("minf(X)=%.3f\n",f1); f0=f1; }
printf("target:X=[%.3f,%.3f,%.3f,%.3f]\n",X0[0],X0[1],X0[2],X0[3]); printf("minf(X)=%.3f\n",f1); // cout.unsetf(ios::scientific); // //
cout
return 0;
四、运行结果及分析
运行结果:
函数f(X)(x110x2)25(x3x4)2(x22x3)410(x1x4)4 精度:0.001
起始X=[10,10,10,10] 前20次迭代:
共迭代493次:
可见最速下降法迭代次数很多,收敛速度较慢。