Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Различные алгоритмы минимизации, реализованные в С++ / all / LAB6_1
.CPP#include <math.h>
#include <conio.h>
#include <iostream.h>
class MyVector
{
public:
double x1,x2;
MyVector(double x=0.0,double y=0.0)
{
x1=x;
x2=y;
}
friend double mv1(MyVector& x)
{
return x.x1;
}
friend double mv2(MyVector& x)
{
return x.x2;
}
friend MyVector operator+ (MyVector a,MyVector b)
{
MyVector sum;
sum.x1=a.x1+b.x1;
sum.x2=a.x2+b.x2;
return sum;
}
friend MyVector operator- (MyVector a,MyVector b)
{
MyVector vich;
vich.x1=a.x1-b.x1;
vich.x2=a.x2-b.x2;
return vich;
}
friend MyVector operator* (double k,MyVector a)
{
MyVector umn;
umn.x1=k*a.x1;
umn.x2=k*a.x2;
return umn;
}
friend MyVector operator/ (MyVector a,double k)
{
return ((a.x1/k),(a.x2/k));
}
friend ostream& operator<<(ostream& s,MyVector x)
{
return s<<"["<<x.x1<<" , "<<x.x2<<"]";
}
friend double func(MyVector x)
{
//return ((x.x1*x.x1+x.x2-11)*(x.x1*x.x1+x.x2-11)+(x.x1+x.x2*x.x2-7)*(x.x1+x.x2*x.x2-7));
return (-12*x.x1+4*x.x1*x.x1+4*x.x2*x.x2-4*x.x1*x.x2);
}
};
void main()
{
clrscr();
MyVector x1(0,0),x2,x3,e1(1,0),e2(0,1),help1,help2,help3,p;
double eps=0.0000001,alfa=1,beta=0.5,gamma=2,h=1,ysr,sigma;
int k=0;
x2=x1+h*e1;
x3=x1+h*e2;
do
{
/************Rangirovanie***************/
if(func(x1)<=func(x2))
{
if(func(x2)<=func(x3))
{
help1=x1;
help2=x2;
help3=x3;
}
else
{
if(func(x1)<=func(x3))
{
help1=x1;
help2=x3;
help3=x2;
}
else
{
help1=x3;
help2=x1;
help3=x2;
}
}
}
else
{
if(func(x2)>=func(x3))
{
help1=x3;
help2=x2;
help3=x1;
}
else
{
if(func(x1)<=func(x3))
{
help1=x2;
help2=x1;
help3=x3;
}
else
{
help1=x2;
help2=x3;
help3=x1;
}
}
}
x1=help1;
x2=help2;
x3=help3;
/***********************************************/
/************Poisk centroida********************/
MyVector x0;
x0=(x1+x2+x3)/3;
/************Otragenie**************************/
MyVector xr;
xr=2*x0-x3;
if((func(xr)>func(x1))&&(func(xr)<=func(x2)))
{
x3=xr;
}
/************Rastyagenie***********************/
MyVector xL;
if(func(xr)<=func(x1))
{
p=x0-x3;
xL=x0+gamma*p;
if(func(xr)<func(xL))
{
x3=xr;
}
else
{
x3=xL;
}
}
MyVector xS;
if((func(xr)>func(x2))&&(func(xr)<=func(x3)))
{
p=xr-x0;
xS=x0+beta*p;
if(func(xr)<func(xS))
{
x3=xr;
}
else
{
x3=xS;
}
}
MyVector xT;
if(func(xr)>func(x3))
{
xT=x0-0.5*(x0-x3);
if(func(xT)<=func(x3))
{
x3=xT;
}
else
{
x2=0.5*(x1+x2);
x3=0.5*(x1+x3);
}
}
ysr=(func(x1)+func(x2)+func(x3))/3;
sigma=((func(x1)-ysr)*(func(x1)-ysr)+(func(x2)-ysr)*(func(x2)-ysr)+(func(x3)-ysr)*(func(x3)-ysr))/3;
k++;
}
while(sigma>=eps);
cout<<"Minimum = "<<x1<<endl;
cout<<"Kol-vo iteraci = "<<k;
getch();
}
Соседние файлы в папке all