-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdemo.cpp
67 lines (53 loc) · 1.61 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
/*
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"
#include "pretty_print.h"
using namespace std;
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{1.23, 10.96};
//initial trial simplex
vector<vector<double> > simplex{{-1.5,-1}, {-2, -2}, {2.5, 1.5}};
//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;
}