- #include "iostream.h"
- #include "math.h"
- #include "Opti.h"
-
-
- int main()
- {
- double bas, haut, mid;
- double * Tab;
- Tab = new double[3];
- *Tab=1;
- *(Tab+1)=2;
- *(Tab+2)=-3;
-
- //l'optimisation nécessite 3 "first guess" , la fourchette moyenne est ici la moyenne des deux autres...
- bas=-4;
- haut=2;
- mid=(bas+haut)/2;
-
- Opti K(bas, haut, mid, Tab);
-
- cout << endl
- << K.Gsearch();
- cout << endl;
-
- delete [] Tab;
-
- return 0;
- }
-
- //voici le Opti.h
-
- #ifndef OPTI_H
- #define OPTI_H
-
- #include "math.h"
- #define Tol 0.000000001
- #define R 0.6180339 //golden section
- #define C (1-R)
- #define N 100 //max iter
-
- class Opti
- {
- public: double a;
- double b;
- double c; //valeur initiale d'optimisation
- double * para; //paramêtre additionnelle de la fonction à optimiser
-
- Opti(double A, double B, double M, double * Para)
- {
- a=A;
- b=B;
- c=M;
- para=Para;
- }
-
- //fonction à optimiser et à editer (ici f(x)=1/(x^k) + g
- //on intégre par rapport à x, k et g sont des paramêtres additionnelles
-
- double ff(double i){return (*para)*pow(i,2)+ *(para+1) * i + *(para+2);}
-
- //fin de la fonction
-
- double Gsearch()
- {
- double f1, f2, x0, x1, x2, x3;
- int n;
-
- n=0;
- x0=a;
- x3=c;
-
- if( fabs(c-b)>fabs(b-a) )
- {
- x1=b;
- x2 = b + C * (c - b);
- }
- else
- {
- x2 = b;
- x1 = b - C * (b - a);
- }
-
- f1 = ff(x1);
- f2 = ff(x2);
- while( fabs(x3-x0) > Tol*(fabs(x1)+fabs(x2)))
- {
- if(f2 < f1)
- {
- x0 = x1;
- x1 = x2;
- x2 = R * x1 + C * x3;
- f1 = f2;
- f2 = ff(x2);
- }
- else
- {
- x3 = x2;
- x2 = x1;
- x1 = R * x2 + C * x0;
- f2 = f1;
- f1 = ff(x1);
- }
- if(n>=N){x3=x0;}
- n++;
- }
-
- if(f1 < f2){return x1;}
- else{return x2;}
- }
- };
-
- #endif
#include "iostream.h"
#include "math.h"
#include "Opti.h"
int main()
{
double bas, haut, mid;
double * Tab;
Tab = new double[3];
*Tab=1;
*(Tab+1)=2;
*(Tab+2)=-3;
//l'optimisation nécessite 3 "first guess" , la fourchette moyenne est ici la moyenne des deux autres...
bas=-4;
haut=2;
mid=(bas+haut)/2;
Opti K(bas, haut, mid, Tab);
cout << endl
<< K.Gsearch();
cout << endl;
delete [] Tab;
return 0;
}
//voici le Opti.h
#ifndef OPTI_H
#define OPTI_H
#include "math.h"
#define Tol 0.000000001
#define R 0.6180339 //golden section
#define C (1-R)
#define N 100 //max iter
class Opti
{
public: double a;
double b;
double c; //valeur initiale d'optimisation
double * para; //paramêtre additionnelle de la fonction à optimiser
Opti(double A, double B, double M, double * Para)
{
a=A;
b=B;
c=M;
para=Para;
}
//fonction à optimiser et à editer (ici f(x)=1/(x^k) + g
//on intégre par rapport à x, k et g sont des paramêtres additionnelles
double ff(double i){return (*para)*pow(i,2)+ *(para+1) * i + *(para+2);}
//fin de la fonction
double Gsearch()
{
double f1, f2, x0, x1, x2, x3;
int n;
n=0;
x0=a;
x3=c;
if( fabs(c-b)>fabs(b-a) )
{
x1=b;
x2 = b + C * (c - b);
}
else
{
x2 = b;
x1 = b - C * (b - a);
}
f1 = ff(x1);
f2 = ff(x2);
while( fabs(x3-x0) > Tol*(fabs(x1)+fabs(x2)))
{
if(f2 < f1)
{
x0 = x1;
x1 = x2;
x2 = R * x1 + C * x3;
f1 = f2;
f2 = ff(x2);
}
else
{
x3 = x2;
x2 = x1;
x1 = R * x2 + C * x0;
f2 = f1;
f1 = ff(x1);
}
if(n>=N){x3=x0;}
n++;
}
if(f1 < f2){return x1;}
else{return x2;}
}
};
#endif