PaGMO  1.1.5
worhp.cpp
1 /*****************************************************************************
2  * Copyright (C) 2015 The PaGMO development team, *
3  * Advanced Concepts Team (ACT), European Space Agency (ESA) *
4  * http://apps.sourceforge.net/mediawiki/pagmo *
5  * http://apps.sourceforge.net/mediawiki/pagmo/index.php?title=Developers *
6  * http://apps.sourceforge.net/mediawiki/pagmo/index.php?title=Credits *
7  * act@esa.int *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the *
21  * Free Software Foundation, Inc., *
22  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23  *****************************************************************************/
24 #include "worhp.h"
25 
26 namespace pagmo { namespace algorithm {
27 
28 // Dummy print function used to suppress all screen output
29 void no_screen_output (int, const char []) {}
30 void default_output(int mode, const char *message)
31 {
32  if (mode & WORHP_PRINT_MESSAGE) {
33  printf(" %s\n",message);
34  }
35  if (mode & WORHP_PRINT_WARNING) {
36  printf(" %s\n",message);
37  }
38  if (mode & WORHP_PRINT_ERROR) {
39  fprintf(stderr," %s\n",message);
40  }
41 }
42 
44 
47 worhp::worhp(const int iter, const double feas, const double opt, const bool screen_output)
48 {
49  // We construct the map between parameters and integers used to set and get them
50  define_param_map();
51 
52  // We set the screen output member from algorithm::base
53  set_screen_output(screen_output);
54 
55  // We deactivate WORHP keyboard handler as it does introduce funny problems
56  setenv("WORHP_DISABLE_KEYBOARD_HANDLER", "1", 0);
57 
58  // We deal with screen output (this is buggy in lworhp 1.8.0, hopefully future releases can fix the problem
59  // and we will be able to restore the screen output upon request
60  if (m_screen_output) {
61  SetWorhpPrint(default_output);
62  } else {
63  SetWorhpPrint(no_screen_output);
64  }
65 
66  // We read the algorithm parameters from the xml file. If this is not found
67  // we set default values and ignore the issue.
68  int status;
69  ReadParams(&status, const_cast<char*>("param.xml"), &m_params);
70  m_params.MatrixCC = false; // Not sure what this does exactly
71 
72  // We set some of the parameters exposed in the constructor
73  set_param("TolFeas", feas);
74  set_param("TolOpti", opt);
75  set_param("MaxIter", iter);
76 }
77 
79 
84 void worhp::evolve(pagmo::population& pop) const {
85  if (pop.size() == 0) {
86  return;
87  }
88 
89  const auto& prob = pop.problem();
90 
91  if (prob.get_i_dimension() != 0) {
92  pagmo_throw(value_error,
93  "The problem has an integer part and WORHP is not suitable to solve it.");
94  }
95 
96  if (prob.get_f_dimension() != 1) {
97  pagmo_throw(value_error,
98  "The problem is not single objective and WORHP is not suitable to solve it");
99  }
100 
101  // We check the screen output again as the user may have changed it
102  if (m_screen_output) {
103  SetWorhpPrint(default_output);
104  } else {
105  SetWorhpPrint(no_screen_output);
106  }
107 
108  OptVar opt;
109  Control control;
110  Params params;
111  Workspace workspace;
112  WorhpPreInit(&opt, &workspace, &params, &control);
113 
114  opt.n = prob.get_dimension(); // number of variables
115  opt.m = prob.get_c_dimension(); // number of constraints
116  auto n_eq = prob.get_c_dimension() - prob.get_ic_dimension(); // number of equality constraints
117 
118  // specify nonzeros of derivative matrixes
119  workspace.DF.nnz = opt.n; // dense
120  workspace.DG.nnz = opt.n * opt.m; // dense
121  workspace.HM.nnz = opt.n;
122 
123  WorhpInit(&opt, &workspace, &params, &control);
124  assert(control.status == FirstCall);
125  params = m_params;
126 
127  // Specify a derivative free case
128  params.UserDF = false;
129  params.UserDG = false;
130  params.UserHM = false;
131  params.UserHMstructure = false;
132 
133  // Initialization of variables
134  const auto best_idx = pop.get_best_idx();
135  pagmo::decision_vector x = pop.get_individual(best_idx).cur_x;
136 
137  for (int i = 0; i < opt.n; ++i) {
138  opt.X[i] = x[i];
139  opt.Lambda[i] = 0;
140  opt.XL[i] = prob.get_lb()[i];
141  opt.XU[i] = prob.get_ub()[i];
142  }
143 
144  // Equality constraints
145  for (auto i = 0u; i < n_eq; ++i) {
146  opt.Mu[i] = 0;
147  opt.GL[i] = 0;
148  opt.GU[i] = 0;
149  }
150 
151  // Inequality constraints
152  for (auto i = n_eq; i < unsigned(opt.m); ++i) {
153  opt.Mu[i] = 0;
154  opt.GL[i] = -params.Infty;
155  opt.GU[i] = 0;
156  }
157 
158  while (control.status < TerminateSuccess && control.status > TerminateError) {
159  if (GetUserAction(&control, callWorhp)) {
160  Worhp(&opt, &workspace, &params, &control);
161  }
162 
163  if (GetUserAction(&control, iterOutput))
164  {
165  IterationOutput(&opt, &workspace, &params, &control);
166  DoneUserAction(&control, iterOutput);
167  }
168 
169  if (GetUserAction(&control, evalF)) {
170  for (int i = 0; i < opt.n; ++i) {
171  x[i] = opt.X[i];
172  }
173  auto f = prob.objfun(x);
174  opt.F = workspace.ScaleObj * f[0];
175  DoneUserAction(&control, evalF);
176  }
177 
178  if (GetUserAction(&control, evalG)) {
179  for (int i = 0; i < opt.n; ++i) {
180  x[i] = opt.X[i];
181  }
182  auto g = prob.compute_constraints(x);
183  for (int i = 0; i < opt.m; ++i) {
184  opt.G[i] = g[i];
185  }
186  DoneUserAction(&control, evalG);
187  }
188 
189  if (GetUserAction(&control, fidif)) {
190  WorhpFidif(&opt, &workspace, &params, &control);
191  }
192  }
193 
194  for (int i = 0; i < opt.n; ++i) {
195  x[i] = opt.X[i];
196  }
197  pop.set_x(best_idx, x);
198 
199  StatusMsg(&opt, &workspace, &params, &control);
200  WorhpFree(&opt, &workspace, &params, &control);
201 }
202 
204 base_ptr worhp::clone() const
205 {
206  return pagmo::algorithm::base_ptr(new worhp(*this));
207 }
208 
210 std::string worhp::get_name() const
211 {
212  return "WORHP";
213 }
214 
216 
219 std::string worhp::human_readable_extra() const
220 {
221  std::ostringstream s;
222  s << "MaxIter:" << get_param("MaxIter") << " TolFeas:"<<get_param("TolFeas")<< " TolOpti:"<< get_param("TolOpti") << std::endl;
223  return s.str();
224 }
225 
226 }} // namespaces
227 
228 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::algorithm::worhp)
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
Root PaGMO namespace.
std::vector< double > decision_vector
Decision vector type.
Definition: types.h:40
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
Definition: population.cpp:277
Population class.
Definition: population.h:70
decision_vector cur_x
Current decision vector.
Definition: population.h:83