aboutsummaryrefslogtreecommitdiffstats
path: root/test/many_dims.cpp
blob: a1426281926630b86b2b410363390a78355d3081 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#define private public
#include <cassert>
#include <core/optimizer.hpp>
#include <methods/powell/powell_method.hpp>
//#include <methods/gsl_simplex/gsl_simplex.hpp>
#include <methods/bfgs/bfgs.hpp>
#include <methods/lbfgs/lbfgs_method.hpp>
#include <methods/aga/aga.hpp>
#include <vector>
#include <iostream>
using namespace opt_utilities;
using namespace std;


class foo1
  :public func_obj<double,vector<double> >
{
  long long n_eval;
public:
  foo1()
    :n_eval(0)
  {
  }

  ~foo1()
  {
    cerr<<n_eval<<" evaluations done"<<endl;
  }

  foo1* do_clone()const
  {
    return new foo1(*this);
  }
  
  

  double do_eval(const vector<double>& p)
  {
    ++n_eval;
    double result=0;
    for(int i=0;i!=p.size();++i)
      {
	result+=p[i]*p[i];
      }
    return result;
  }
};

class foo2
  :public func_obj<double,vector<double> >
{
  foo2* do_clone()const
  {
    return new foo2(*this);
  }

  double do_eval(const vector<double>& p)
  {
    double result=0;
    for(int i=0;i!=p.size();++i)
      {
	result+=(i+1)*p[i]*p[i];
      }
    return result;
  }
};

class foo3
  :public func_obj<double,vector<double> >
{
  foo3* do_clone()const
  {
    return new foo3(*this);
  }

  double do_eval(const vector<double>& p)
  {
    double result=0;
    for(int i=0;i<p.size();++i)
      {
	double temp=0;
	for(int j=0;j<=i;++j)
	  {
	    temp+=p[j];
	  }
	result+=temp*temp;
      }

    return result;
  }
};


class foo4
  :public func_obj<double,vector<double> >
{
  foo4* do_clone()const
  {
    return new foo4(*this);
  }

  double do_eval(const vector<double>& p)
  {
    double result=0;
    for(int i=0;i<p.size()-1;++i)
      {
	result+=100*(p[i+1]-p[i])*(p[i+1]-p[i])
	  +(1-p[i])*(1-p[i]);
      }

    return result;
  }
};

class foo5
  :public func_obj<double,vector<double> >
{
  foo5* do_clone()const
  {
    return new foo5(*this);
  }

  double do_eval(const vector<double>& p)
  {
    const double pi=3.14159265358979323846;
    double result=0;
    result+=10*p.size();
    for(int i=0;i<p.size();++i)
      {
	result+=(p[i]*p[i]-10*cos(2*pi*p[i]));
      }

    return result;
  }
};


void test_opt(const func_obj<double,vector<double> >& fo,
	      const opt_method<double,vector<double> >& optm)
{
  const int problem_size=100000;
  optimizer<double,vector<double> > opt;
  
  opt.set_func_obj(fo);
  opt.set_opt_method(optm);
  
  vector<double> lim(problem_size);
  for(int i=0;i<lim.size();++i)
    {
      lim[i]=10;
    }

  opt.set_upper_limit(lim);
  //  vector<double> lim(problem_size);
  for(int i=0;i<lim.size();++i)
    {
      lim[i]=-10;
    }
  opt.set_lower_limit(lim);
  vector<double> p(problem_size);
  for(int i=0;i<p.size();++i)
    {
      p[i]=1;
    }
  opt.set_start_point(p);
  opt.set_precision(1E-7);
  p=opt.optimize();


  cout<<"the result is:\n";
  for(int i=0;i<p.size();++i)
    {
      //      cout<<p[i]<<endl;
    }
  cout<<"The result of the object function is:\n"
      <<opt.eval(p)<<endl;

}


int main()
{
  //gsl_simplex<double,vector<double> > agam;
  lbfgs_method<double,vector<double> > agam;
  //powell_method<double,vector<double> > agam;
  //aga_method<double,vector<double> > agam(100,50);
  test_opt(foo1(),agam);
  //  test_opt(foo1(),agam);
  //  test_opt(foo2(),agam);
  //test_opt(foo3(),agam);
  //test_opt(foo4(),agam);
  //test_opt(foo5(),agam);
}