source: colin/branches/colin-appresponse-dev/test/test7.cpp @ 5690

Revision 5690, 5.3 KB checked in by jdsiiro, 5 years ago (diff)

More work on the overhaul of the OptSolver? hierarchy. The COLIN
regression tests build again, and more than half actually pass.

Adding the first draft of a general AmplApplication? and simplified
AmplDriver? (both derived from the original LoadAMPL.cpp)

  • Property svn:executable set to *
Line 
1/*  _________________________________________________________________________
2 *
3 *  COLIN: A Common Optimization Library INterface
4 *  Copyright (c) 2003, Sandia National Laboratories.
5 *  This software is distributed under the GNU Lesser General Public License.
6 *  For more information, see the README.html file in the top COLIN directory.
7 *  _________________________________________________________________________
8 */
9
10//
11// OptSolver tests - with gradients
12//
13
14#include <acro_config.h>
15#include <colin/colin.h>
16#include <colin/OptApplications.h>
17#include <utilib/BasicArray.h>
18#include <TestOptSolver.h>
19
20using namespace colin;
21using namespace std;
22
23namespace
24{
25
26void func(const std::vector<double>& vec, int asv, colin::real& ans,
27          std::vector<double>& cvec, std::vector<double>& grad)
28{
29   if (asv & 1)
30   {
31      ans = 0.0;
32      for (size_t i = 0; i < vec.size(); i++)
33      { ans += pow(vec[i], (int)i + 1); }
34   }
35
36   if (asv & 2)
37   {
38      cvec << 0.0;
39   }
40
41   if (asv & 4)
42   {
43      grad.resize(vec.size());
44
45      for (size_t i = 0; i < vec.size(); i++)
46      {
47         if (i == 0)
48         { grad[i] = (i + 1.0); }
49         else
50         { grad[i] = (i + 1.0) * pow(vec[i], (int)i); }
51      }
52   }
53}
54
55
56//
57// Define a generic datatype that contains real parameters
58//
59class A
60{
61public:
62   vector<double> vec;
63
64   operator const vector<double>&() const
65      {return vec;}
66
67   bool operator==(const A& rhs) const
68   { return vec == rhs.vec; }
69   bool operator<(const A& rhs) const
70   { return vec < rhs.vec; }
71};
72
73int lexical_cast_A_vector(const utilib::Any& from_, utilib::Any& to_)
74{
75   to_.set<vector<double> > () << utilib::anyref_cast<A>(from_).vec;
76   return OK;
77}
78
79int lexical_cast_vector_A(const utilib::Any& from_, utilib::Any& to_)
80{
81   to_.set<A>().vec << utilib::anyref_cast<vector<double> > (from_);
82   return OK;
83}
84
85//
86// Define an objective function over A.
87//
88void funcA(const A& point, int asv, colin::real& ans,
89           std::vector<double>& cvec, std::vector<double>& grad)
90{ func(point.vec, asv, ans, cvec, grad); }
91
92
93}
94
95//
96// Define necessary operator functions
97//
98ostream& operator<<(ostream& os, const A& a)
99{ os << a.vec; return os; }
100
101A& operator&=(A& a1, A& a2)
102{ a1.vec &= a2.vec; return a1; }
103
104istream& operator>>(istream& is, A& a)
105{ is >> a.vec; return is; }
106
107
108int test_test7a(int, char**)
109{
110   std::cout << std::endl;
111   std::cout << "Test7a - a simple minimization example (Test7a+gradient)"
112   << std::endl;
113
114   ConfigurableApplication<NLP1_problem> *app
115      = new_application("test7a", &func);
116   app->set_num_real_vars(4);
117   app->set_bounds("[-10.0,10.0]^4");
118
119   OptProblem<NLP1_problem> prob;
120   prob.set_application(app);
121
122   TestOptSolver_g opt;
123
124   vector<double> point(4);
125   point << 10.0;
126
127   opt.set_initial_point(point);
128   opt.set_problem(prob);
129   opt.set_parameter("accuracy", 29.0);
130
131   opt.reset();
132   opt.optimize();
133
134   ApplicationMngr().clear();
135   return 0;
136}
137
138int test_test7b(int, char**)
139{
140   std::cout << std::endl;
141   std::cout << "Test7b - a simple minimization example with a different "
142   "domain (Test7b+gradient)" << std::endl;
143
144
145   ContextMngr().register_lexical_cast(typeid(A), typeid(vector<double>),
146                                       &lexical_cast_A_vector, true);
147   ContextMngr().register_lexical_cast(typeid(vector<double>), typeid(A),
148                                       &lexical_cast_vector_A, true);
149
150   ConfigurableApplication<NLP1_problem> *app
151      = new_application("test7b", &funcA);
152   app->set_num_real_vars(4);
153   app->set_bounds("[-10.0,10.0]^4");
154
155   OptProblem<NLP1_problem> prob;
156   prob.set_application(app);
157
158   TestOptSolver_g opt;
159
160   vector<double> point(4);
161   point << 10.0;
162
163   opt.set_initial_point(point);
164   opt.set_problem(prob);
165   opt.set_parameter("accuracy", 29.0);
166
167   opt.reset();
168   opt.optimize();
169
170   ApplicationMngr().clear();
171   return 0;
172}
173
174
175int test_test7c(int, char**)
176{
177   ColinGlobals::output_level = "quiet";
178   std::cout << std::endl;
179   std::cout << "Test7c - a simple minimization example with a shell command "
180   "(Test7c+gradient)" << std::endl;
181
182   ConfigurableApplication<NLP1_problem> *app =
183      new_application<std::vector<double>, NLP1_problem >
184      ( "test7c", "shell_func7", "shell_func7.in", "shell_func7.out",
185        true, true );
186   app->set_num_real_vars(4);
187   app->set_bounds("[-10.0,10.0]^4");
188
189   OptProblem<NLP1_problem> prob;
190   prob.set_application(app);
191
192   TestOptSolver_g opt;
193
194   vector<double> point(4);
195   point << 10.0;
196
197   opt.set_initial_point(point);
198   opt.set_problem(prob);
199   opt.set_parameter("accuracy", 29.0);
200
201   opt.reset();
202   opt.optimize();
203
204   ColinGlobals::reset();
205   ApplicationMngr().clear();
206   return 0;
207}
208
209
210int test_test7d(int, char**)
211{
212   std::cout << std::endl;
213   std::cout << "Test7d - a simple minimization example converting vectors to "
214   "BasicArrays (Test7d+gradient)" << std::endl;
215
216   ConfigurableApplication<NLP1_problem> *app
217      = new_application("test7d", &func);
218   app->set_num_real_vars(4);
219   app->set_bounds("[-10.0,10.0]^4");
220
221   OptProblem<NLP1_problem> prob;
222   prob.set_application(app);
223
224   TestOptSolver_g opt;
225
226   utilib::BasicArray<colin::real> point(4);
227   point << 10.0;
228
229   opt.set_initial_point(point);
230   opt.set_problem(prob);
231   opt.set_parameter("accuracy", 29.0);
232
233   opt.reset();
234   opt.optimize();
235
236   ApplicationMngr().clear();
237   return 0;
238}
Note: See TracBrowser for help on using the repository browser.