25 #include "../exceptions.h"
26 #include "../population.h"
27 #include "../problem/base.h"
32 #include "snopt_cpp_wrapper/snopt_PAGMO.h"
33 #include "snopt_cpp_wrapper/snfilewrapper_PAGMO.h"
34 #include "snopt_cpp_wrapper/snoptProblem_PAGMO.h"
46 static int snopt_function_(integer *Status, integer *n, doublereal *x,
47 integer *needF, integer *neF, doublereal *F,
48 integer *needG, integer *neG, doublereal *G,
49 char *cu, integer *lencu,
50 integer *iu, integer *leniu,
51 doublereal *ru, integer *lenru )
69 pagmo::algorithm::snopt::preallocated_memory* preallocated = (pagmo::algorithm::snopt::preallocated_memory*)ru;
74 prob->
objfun(preallocated->f,preallocated->x);
75 F[0] = preallocated->f[0];
92 namespace pagmo {
namespace algorithm {
103 snopt::snopt(
const int major,
const double feas,
const double opt) : m_major(major),m_feas(feas),m_opt(opt), m_file_out(false)
106 pagmo_throw(value_error,
"number of major iterations cannot be negative");
108 if (feas < 0 || feas > 1) {
109 pagmo_throw(value_error,
"feasibility cireria ");
111 if (opt < 0 || opt > 1) {
112 pagmo_throw(value_error,
"number of major iterations cannot be negative");
140 const std::vector<double>::size_type D_eqc = prob_c_dimension - D_ineqc;
141 const std::string name = prob.
get_name();
144 if ( prob_i_dimension != 0 ) {
145 pagmo_throw(value_error,
"No integer part allowed yet....");
149 pagmo_throw(value_error,
"No continuous part....");
152 if ( prob_f_dimension != 1 ) {
153 pagmo_throw(value_error,
"The problem is not single objective and SNOPT is not suitable to solve it");
157 if (NP == 0 || m_major == 0) {
162 di_comodo.x.resize(Dc);
163 di_comodo.c.resize(prob_c_dimension);
164 di_comodo.f.resize(prob_f_dimension);
169 snoptProblem_PAGMO SnoptProblem(prob, &di_comodo);
175 integer neF = 1 + prob_c_dimension;
178 integer lenA = Dc * (1 + prob_c_dimension);
179 integer *iAfun =
new integer[lenA];
180 integer *jAvar =
new integer[lenA];
181 doublereal *A =
new doublereal[lenA];
185 int lenG =Dc * (1 + prob_c_dimension);
186 integer *iGfun =
new integer[lenG];
187 integer *jGvar =
new integer[lenG];
192 doublereal *x =
new doublereal[n];
193 doublereal *xlow =
new doublereal[n];
194 doublereal *xupp =
new doublereal[n];
195 doublereal *xmul =
new doublereal[n];
196 integer *xstate =
new integer[n];
199 doublereal *F =
new doublereal[neF];
200 doublereal *Flow =
new doublereal[neF];
201 doublereal *Fupp =
new doublereal[neF];
202 doublereal *Fmul =
new doublereal[neF];
203 integer *Fstate =
new integer[neF];
207 char *xnames =
new char[nxnames*8];
208 char *Fnames =
new char[nFnames*8];
211 doublereal ObjAdd = 0;
214 int bestidx = pop.get_best_idx();
224 Flow[0] = -std::numeric_limits<double>::max();
225 Fupp[0] = std::numeric_limits<double>::max();
234 Flow[i+1+D_eqc] = -std::numeric_limits<double>::max();
239 SnoptProblem.setProblemSize( n, neF );
240 SnoptProblem.setNeG( lenG );
241 SnoptProblem.setNeA( lenA );
242 SnoptProblem.setA ( lenA, iAfun, jAvar, A );
243 SnoptProblem.setG ( lenG, iGfun, jGvar );
244 SnoptProblem.setObjective ( ObjRow, ObjAdd );
245 SnoptProblem.setX ( x, xlow, xupp, xmul, xstate );
246 SnoptProblem.setF ( F, Flow, Fupp, Fmul, Fstate );
247 SnoptProblem.setXNames ( xnames, nxnames );
248 SnoptProblem.setFNames ( Fnames, nFnames );
249 SnoptProblem.setProbName ( name.c_str() );
250 SnoptProblem.setUserFun ( snopt_function_ );
254 if (m_file_out) SnoptProblem.setPrintFile ( name.c_str() );
255 SnoptProblem.setIntParameter (
"Derivative option", 0 );
256 SnoptProblem.setIntParameter (
"Major iterations limit", m_major);
257 SnoptProblem.setIntParameter (
"Iterations limit",100000);
258 SnoptProblem.setRealParameter(
"Major feasibility tolerance", m_feas);
259 SnoptProblem.setRealParameter(
"Major optimality tolerance", m_opt);
266 std::vector<int> iGfun_vect, jGvar_vect;
268 for (
int i=0;i < neG;i++)
270 iGfun[i] = iGfun_vect[i];
271 jGvar[i] = jGvar_vect[i];
273 SnoptProblem.setNeG( neG );
274 SnoptProblem.setNeA( 0 );
275 SnoptProblem.setG( lenG, iGfun, jGvar );
278 catch (not_implemented_error)
280 SnoptProblem.computeJac();
281 neG = SnoptProblem.getNeG();
287 std::cout <<
"PaGMO 4 SNOPT:" << std::endl << std::endl;
288 std::cout <<
"Sparsity pattern set, NeG = " << neG << std::endl;
289 std::cout <<
"iGfun: [";
290 for (
int i=0; i<neG-1; ++i) std::cout << iGfun[i] <<
",";
291 std::cout << iGfun[neG-1] <<
"]" << std::endl;
292 std::cout <<
"jGvar: [";
293 for (
int i=0; i<neG-1; ++i) std::cout << jGvar[i] <<
",";
294 std::cout << jGvar[neG-1] <<
"]" << std::endl;
300 SnoptProblem.solve( Cold );
303 std::copy(x,x+n,di_comodo.x.begin());
305 std::transform(di_comodo.x.begin(), di_comodo.x.end(), pop.
get_individual(bestidx).
cur_x.begin(), di_comodo.x.begin(),std::minus<double>());
306 for (integer i=0;i<n;i++)
308 newx[i] = std::min(std::max(lb[i],newx[i]),ub[i]);
311 pop.set_x(bestidx,newx);
312 pop.set_v(bestidx,di_comodo.x);
315 delete []iAfun;
delete []jAvar;
delete []A;
316 delete []iGfun;
delete []jGvar;
318 delete []x;
delete []xlow;
delete []xupp;
319 delete []xmul;
delete []xstate;
321 delete []F;
delete []Flow;
delete []Fupp;
322 delete []Fmul;
delete []Fstate;
324 delete []xnames;
delete []Fnames;
350 std::ostringstream s;
351 s <<
"major_iter:" << m_major <<
" feas_tol: "<<m_feas<<
" opt_tol: "<<m_opt << std::endl;
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
std::vector< double > decision_vector
Decision vector type.
snopt(const int major=100, const double feas=1e-10, const double opt=1e-4)
Constructor.
fitness_vector cur_f
Current fitness vector.
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
virtual void set_sparsity(int &lenG, std::vector< int > &iGfun, std::vector< int > &jGvar) const
Sets the sparsity pattern of the gradient.
c_size_type get_ic_dimension() const
Return inequality constraints dimension.
size_type get_dimension() const
Return global dimension.
std::string get_name() const
Algorithm name.
fitness_vector objfun(const decision_vector &) const
Return fitness of pagmo::decision_vector.
bool m_screen_output
Indicates to the derived class whether to print stuff on screen.
Wrapper for the SNOPT solver.
void file_output(const bool)
Activate file output.
size_type get_i_dimension() const
Return integer dimension.
constraint_vector compute_constraints(const decision_vector &) const
Compute constraints and return constraint vector.
std::string human_readable_extra() const
Extra human readable algorithm info.
c_size_type get_c_dimension() const
Return global constraints dimension.
const decision_vector & get_ub() const
Upper bounds getter.
container_type::size_type size_type
Population size type.
decision_vector cur_x
Current decision vector.
f_size_type get_f_dimension() const
Return fitness dimension.
void evolve(population &) const
Evolve implementation.
virtual std::string get_name() const
Get problem's name.
base_ptr clone() const
Clone method.
const decision_vector & get_lb() const
Lower bounds getter.
decision_vector::size_type size_type
Problem's size type: the same as pagmo::decision_vector's size type.