-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.cpp
72 lines (59 loc) · 1.97 KB
/
demo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Copyright (C) 2010 Botao Jia
This file is an testing of the implementation of the
downhill simplex optimization algorithm using C++.
One function object Rosenbrock and two function rosenbrock, polynomial_fun are used.
You can redistribute it and/or modify it at will.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.
*/
#include <iostream>
#include <vector>
#include <ctime>
#include <algorithm>
#include <iterator>
#include <cmath>
#include "simplex.h"
using namespace std;
template<class Con>
void printcon(const Con& c){
std::cout.precision(12);
copy( c.begin(),
c.end(),
ostream_iterator<typename Con::value_type>(cout, " ") );
cout<<endl;
}
class Rosenbrock{
public:
double operator()(vector<double> x){
return 100*pow(x[1]-pow(x[0],2),2)+pow(1-x[0],2);
}
};
double rosenbrock(vector<double> x){
return 100*pow(x[1]-pow(x[0],2),2)+pow(1-x[0],2);
}
double polynomial_fun(vector<double> x){
return pow( pow(x[0],2) + pow(x[1],2), 2 ) - pow(x[0]-3*x[1],2);
}
int main(){
//initial guessing value for Rosenbrock function
vector<double> init;
init.push_back(1.23);
init.push_back(10.96);
//initial trial simplex
double a0[]={-1.5, -1}, a1[]={-2, -2}, a2[]={ 2.5, 1.5};
vector<vector<double> > simplex;
simplex.push_back( vector<double>(a0,a0+2) );
simplex.push_back( vector<double>(a1,a1+2) );
simplex.push_back( vector<double>(a2,a2+2) );
//optimizating...
//printcon is a function printing container values
using BT::Simplex;
cout<<"Rosenbrock function achieves minimum at:"<<endl;
printcon( Simplex(Rosenbrock(), init) ); cout<<endl;
cout<<"Rosenbrock function achieves minimum at:"<<endl;
printcon( Simplex(rosenbrock, init, 1e-7, simplex, 1E5) ); cout<<endl;
cout<<"Polynomial function achieves minimum at:"<<endl;
printcon( Simplex(polynomial_fun, init, 1e-7, simplex) ); cout<<endl;
}