Автор работы: Пользователь скрыл имя, 19 Марта 2012 в 19:29, лабораторная работа
Цель работы: знакомство с методами многомерной безусловной оптимизации первого и нулевого порядка и их освоение, сравнение эффективности применения этих методов конкретных целевых функций.
Цель работы….………………………………..……………………………3
Задание….…………………………………...…………………............…..3
Постановка задачи………………………………………………………….4
График функции……………………………………………………………4
Блок-схемы………………………………………………………………….5
Поиск по образцу……………………………………….............................5
Метод деформируемого симплекса …………………………………….6-8
Метод симплекса………………………………………………………..9-10
Градиентный метод с дроблением шага…………………..………....11-12
Метод наискорейшего спуска……………………..............................13-14
Подпрограмма Золотое сечение………………………………………….15
Метод Гаусса-Зейделя.………………………………………………..16-17
Эвристический алгоритм……………………………………………...18-19
Листинг программы…………………………………………………...20-33
Графики траекторий промежуточных приближений……………….34-36
Результирующая таблица и вывод……………………………………...37
{
double *px_1,*px_2,*py;
double eps,min,l1,max,l2,r,c1,c2,u1,
int i,N,N1,L1,L2;
N1=1;
px_1=new double[3];
px_2=new double[3];
py=new double[3];
cout<<"Vvedite bazovuyu tochku:/n"<<endl;
cout<<"x0(x1,x2) ";cout<<endl;
cout<<"x1 = "; cin>>px_1[0];
cout<<"x2 = "; cin>>px_2[0];
cout<<"eps = "; cin>>eps;
cout<<"r = "; cin>>r;
px_1[1]=px_1[0]+r;
px_2[1]=px_2[0];
px_1[2]=px_1[0];
px_2[2]=px_2[0]+r;
py[0]=25*px_1[0]+0.9*px_2[0]+
py[1]=25*px_1[1]+0.9*px_2[1]+
py[2]=25*px_1[2]+0.9*px_2[2]+
N=3;
point1:
min=py[0]; l1=0;
for(i=1;i<=2;i++)
{if(py[i]<min)
{min=py[i];
l1=i;
}
}
max=py[0]; l2=0;
for(i=1;i<=2;i++)
{if(py[i]>max)
{max=py[i];
l2=i;
}
}
if(r>eps)
{c1=0; c2=0;
for(i=0;i<=2;i++)
{if(i==l2) continue;
c1=c1+px_1[i];
c2=c2+px_2[i];
}
L2=l2;
L1=l1;
u1=c1-px_1[L2];
u2=c2-px_2[L2];
y=25*u1+0.9*u2+exp(0.35*(u1*
N++;
if(y<py[L2])
{px_1[L2]=u1;
px_2[L2]=u2;
py[L2]=y;
}
else
{for(i=0;i<=2;i++)
{if(i==L1) continue;
px_1[i]=(px_1[i]+px_1[L1])*0.
px_2[i]=(px_2[i]+px_2[L1])*0.
py[i]=25*px_1[i]+0.9*px_2[i]+
N++;
}
r=r*0.5;
}
cout<<px_1[L1]<<" "<<px_2[L1]<<endl;
N1++;
goto point1;
}
X1=px_1[L1];
X2=px_2[L1];
Y=25*px_1[L1]+0.9*px_2[L1]+
cout<<"x_(x1,x2) = x_( "<<X1<<","<<X2<<" )"<<endl;
cout<<"y_ = "<<Y<<endl;
cout<<"Kol-vo icpitanii: N = "<<N<<endl;
cout<<"Kol-vo iteracii N1="<<N1<<endl;
}
void grad_s_drobl()
{const int L=1500;
znach_x x[L];
znach_x x_;
double a,eps,delta,z1,z2,y,y1,d,y_;
int k,N,N1,N0;
cout<<"Vvedite bazovuyu tochku:/n"<<endl;
cout<<"x0(x1,x2) ";cout<<endl;
cout<<"x1 = ";
cin>>x[0].x1;
cout<<"x2 = ";
cin>>x[0].x2;
cout<<"Vvedite a:\n";
cout<<"a = ";cin>>a;
cout<<"Vvedite tochnost':\n";
cout<<"eps = "; cin>>eps;
cout<<"Vvedite delta:\n";
cout<<"delta = "; cin>>delta;
k=0;
z1=25+0.7*x[0].x1*exp(0.35*(
z2=0.9+0.7*x[0].x2*exp(0.35*(
N1=2;N0=0;
d=sqrt(pow(z1,2)+pow(z2,2));
m2:
y=25*x[k].x1+0.9*x[k].x2+exp(
N0++;
m1:
y1=25*(x[k].x1-a*z1)+0.9*(x[k]
if ((y1-y)>(-a*delta*d)) {a=a/2.0;
goto m1;}
x[k+1].x1=x[k].x1-a*z1;
x[k+1].x2=x[k].x2-a*z2;
cout<<k<<" "<<x[k+1].x1<<" "<<x[k+1].x2<<endl;
cout<<endl;
z1=25+0.7*x[k+1].x1*exp(0.35*(
z2=0.9+0.7*x[k+1].x2*exp(0.35*
N1=N1+2;
d=pow(z1,2)+pow(z2,2);
if (sqrt(d)>eps) {k=k+1;goto m2;}
else{ x_=x[k+1];
y_=25*x_.x1+0.9*x_.x2+exp(0.
N=N1+N0;}
cout<<"Result:\n";
cout<<"x_(x1,x2) = x_( "<<x_.x1<<","<<x_.x2<<" )"<<endl;
cout<<"y_ = "<<y_<<endl;
cout<<"Kol-vo icpitanii: N1 = "<<N1<<endl;
cout<<"Kol-vo icpitanii: N0 = "<<N0<<endl;
cout<<"Kol-vo icpitanii: N = "<<N<<endl;
cout<<"Kol-vo iteracii: k = "<<k<<endl;
}
void naisk_spusk()
{const int L=1500;
znach_x x[L];
znach_x x_;
double a,a1,a2,f1,f2,amin,eps,z1,z2,
int k,i,N1,N,N0;
cout<<"Vvedite bazovuyu tochku:/n"<<endl;
cout<<"x0(x1,x2) ";cout<<endl;
cout<<"x1 = ";
cin>>x[0].x1;
cout<<"x2 = ";
cin>>x[0].x2;
cout<<"Vvedite tochnost':\n";
cout<<"eps = "; cin>>eps;
cout<<"Vvedite da:\n";
cout<<"da = "; cin>>da;
k=0;
N0=0;
z1=25+0.7*x[0].x1*exp(0.35*(
z2=0.9+0.7*x[0].x2*exp(0.35*(
N1=2;
m2:
y1=25*x[k].x1+0.9*x[k].x2+exp(
//cout<<"y1= "<<y1<<endl;
i=0;
m1:
y2=25*(x[k].x1-((i+1)*da)*z1)+
//cout<<"y2= "<<y2<<endl;
N0++;
if (y2<y1) {i++;y1=y2;goto m1;}
else {B=(1+i)*da;
if (i>0) A=(i-1)*da;
else A=0;
}
//cout<<"A="<<A<<" B="<<B<<endl;
//dich
d=eps/100;
do
{a=(A+B)/2.0;
a1=a-d;
a2=a+d;
f1=25*(x[k].x1-a1*z1)+0.9*(x[
f2=25*(x[k].x1-a2*z1)+0.9*(x[
N=0;
N=N+2;
cout<<"a1 = "<<a1<<" f1 = "<<f1<<endl;
cout<<"a2 = "<<a2<<" f2 = "<<f2<<endl;
if (f1<f2) B=a2;
else A=a1;
}
while (B-A>2*eps);
amin=(A+B)/2.0;
// cout<<endl;
//cout<<"Result: "<<"amin= "<<amin<<endl;
x[k+1].x1=x[k].x1-amin*z1;
x[k+1].x2=x[k].x2-amin*z2;
z1=25+0.7*x[k+1].x1*exp(0.35*(
z2=0.9+0.7*x[k+1].x2*exp(0.35*
N1=N1+2;
if ((sqrt(z1*z1+z2*z2))>eps) {k=k+1; goto m2;}
else {x_=x[k+1];
y_=25*x_.x1+0.9*x_.x2+exp(0.
N=N1+N0;
}
cout<<"Result:\n";
cout<<"x_(x1,x2) = x_( "<<x_.x1<<","<<x_.x2<<" )"<<endl;
cout<<"y_ = "<<y_<<endl;
cout<<"Kol-vo icpitanii: N1 = "<<N1<<endl;
cout<<"Kol-vo icpitanii: N0 = "<<N0<<endl;
cout<<"Kol-vo icpitanii: N = "<<N<<endl;
cout<<"Kol-vo iteracii: k = "<<k<<endl;
}
void evristich()
{
const int L=1500;
znach_x x[L];
znach_x x_;
double a1,a2,d1,d2,g1,g2,b1,b2,s1,s2;
double eps,y_,p1,p1_1,p2,p2_2,norma;
int k;
int N0=0,N1=0,N;
cout<<"Vvedite bazovuyu tochku:"<<endl;
cout<<"x0(x1,x2) ";cout<<endl;
cout<<"x1 = ";
cin>>x[0].x1;
cout<<"x2 = ";
cin>>x[0].x2;
cout<<"Vvedite tochnost':\n";
cout<<"eps = ";
cin>>eps;
a1=0.5;
a2=2;
d1=0.01;
d2=3;
k=0;
//N=0;N0=0;N1=0;
m1:
p1=dfx1(x[k].x1,x[k].x2);
p2=dfx2(x[k].x1,x[k].x2);
N1=N1+2; //N1-kol-vo vichislenii proizv.
if (abs(p1)>d1) g1=p1;
if (p1<=d1) g1=0;
if (abs(p2)>d1) g2=p2;
if (p2<=d1) g2=0;
x[k+1].x1=x[k].x1-a1*g1;
x[k+1].x2=x[k].x2-a1*g2;
N0=N0+2; //N0 kol-vo vichislenii f
if (f(x[k+1].x1,x[k+1].x2)<f(x[k]
m2:
p1_1=dfx1(x[k].x1,x[k].x2);
p2_2=dfx2(x[k].x1,x[k].x2);
N1=N1+2;
if (abs(p1)>d2) b1=0;
if (p1<=d2) b1=p1_1;
if (abs(p2)>d2) b2=0;
if (p2<=d2) b2=p2_2;
x[k+1].x1=x[k].x1-a2*b1;
x[k+1].x2=x[k].x2-a2*b2;
N0=N0+2;
if (f(x[k+1].x1,x[k+1].x2)<f(x[k]
s1=dfx1(x[k+1].x1,x[k+1].x2);
s2=dfx2(x[k+1].x1,x[k+1].x2);
N1=N1+2;
norma=sqrt(pow(s1,2)+pow(s2,2)
if (norma>eps) {a1=0.5*a1;
a2=0.5*a2;
goto m1;}
N=N0+N1;
x_=x[k+1];
y_=f(x_.x1,x_.x2);
cout<<"Result:\n";
cout<<"x_(x1,x2) = x_( "<<x_.x1<<","<<x_.x2<<" )"<<endl;
cout<<"y_ = "<<y_<<endl;
cout<<"Kol-vo icpitanii: N1 = "<<N1<<endl;
cout<<"Kol-vo icpitanii: N0 = "<<N0<<endl;
cout<<"Itogo kol-vo icpitanii: N = "<<N<<endl;
cout<<"kol-vo iteracii k="<<k<<endl;
}
double ZS(double a,double b,double x1k,double x2k,double z1,int n)
{ double e0,t,x1,x2,fi1,fi2;
e0=0.0001;
t=(1+sqrt(5))/2;
x1=a+(b-a)/(t*t);
x2=a+(b-a)/t;
if (n==1)
{ fi1=f(x1k-x1*z1,x2k);
fi2=f(x1k-x2*z1,x2k);
do
{ if (fi1<fi2) { b=x2; x2=x1; fi2=fi1;
x1=a+b-x2;fi1=f(x1k-x1*z1,x2k)
else { a=x1; x1=x2; fi1=fi2;
x2=a+b-x1; fi2=f(x1k-x2*z1,x2k); }
}
while((b-a)/2>e0);
if (fi1<fi2) b=x2;
else a=x1; }
else
{ fi1=f(x1k,x2k-x1*z1);
fi2=f(x1k,x2k-x2*z1);
do
{ if (fi1<fi2) { b=x2; x2=x1; fi2=fi1;
x1=a+b-x2; fi1=f(x1k,x2k-x1*z1); }
else { a=x1; x1=x2;fi1=fi2;
x2=a+b-x1; fi2=f(x1k,x2k-x2*z1); }
}
while((b-a)/2>e0);
if (fi1<fi2) b=x2;
else a=x1; }
return ((a+b)/2); }
void gauss_zeidel()
{
int k,m;
double x1[100],x2[100];
double e2,dalpha;
double alphamin1,alphamin2,z1,z2,y1,
k=0;
x1[0]=1;
x2[0]=0;
e2=0.0004;
dalpha=0.9;
cout<<setw(5)<<setiosflags(
metka:
z1=dfx1(x1[2*k],x2[2*k]);
m=0;
y1=f(x1[2*k]-m*dalpha*z1,x2[2*
metka1:
y2=f(x1[2*k]-(m+1)*dalpha*z1,
if (y2<y1)
{m++;y1=y2;goto metka1;}
else
{b1=(m+1)*dalpha;
if (m==0) a1=0;
else a1=(m-1)*dalpha; }
alphamin1=ZS(a1,b1,x1[2*k],x2[
x1[2*k+1]=x1[2*k]-alphamin1*
x2[2*k+1]=x2[2*k];
cout<<setw(5)<<setiosflags(
z2=dfx2(x1[2*k+1],x2[2*k+1]);
m=0;
y1=f(x1[2*k+1],x2[2*k+1]-m*
metka2:
y2=f(x1[2*k+1],x2[2*k+1]-(m+1)
if (y2<y1)
{m++;y1=y2;goto metka2;}
else
{b2=(m+1)*dalpha;
if (m==0) a2=0;
else a2=(m-1)*dalpha; }
alphamin2=ZS(a2,b2,x1[2*k+1],
x1[2*k+2]=x1[2*k+1];
x2[2*k+2]=x2[2*k+1]-alphamin2*
cout<<setw(5)<<setiosflags(
d=sqrt(pow((x1[2*k+2]-x1[2*k])
if (d>e2)
{k++;goto metka;}
else
{ xmin1=x1[2*k+2];
xmin2=x2[2*k+2];
ymin=f(xmin1,xmin2); }
cout<<endl<<endl;
cout<<"x1="<<xmin1<<", x2="<<xmin2<<"; y="<<ymin<<endl;
cout<<"N="<<(N0+N1)<<endl;
cout<<"k="<<(k+1)<<endl;
}
void main()
{int j;
while (true)
{clrscr();
cout<<endl;
cout<<"1.Poisk po obrazcu\n";
cout<<"2.Diformir. simpleks\n";