6 #include "../../problem/base.h"
7 #include "../../algorithm/snopt.h"
8 #include "snopt_PAGMO.h"
9 #include "snfilewrapper_PAGMO.h"
10 #include "snoptProblem_PAGMO.h"
14 snoptProblem_PAGMO::snoptProblem_PAGMO(
const pagmo::problem::base& problema, pagmo::algorithm::snopt::preallocated_memory *di_comodo):
15 iSpecs(0), iSumm(0), iPrint(0), initCalled(0),cu((char *)&problema),lencu(500),ru((doublereal *)di_comodo), lenru(500)
32 this->alloc( 500, 500, 500 );
38 snoptProblem_PAGMO::~snoptProblem_PAGMO()
54 void snoptProblem_PAGMO::init2zero()
63 iAfun = 0; jAvar = 0; A = 0;
66 x = 0; xlow = 0; xupp = 0; xmul = 0;
67 F = 0; Flow = 0; Fupp = 0; Fmul = 0;
69 xstate = 0; Fstate = 0;
70 nxnames = 0; nFnames = 0;
80 void snoptProblem_PAGMO::userDataSet()
82 if ( n == 0) errMsgExit(
"n" );
83 if ( neF == 0) errMsgExit(
"neF");
85 if ( x == 0 ) errMsgExit(
"x" );
86 if ( xlow == 0 ) errMsgExit(
"xlow" );
87 if ( xupp == 0 ) errMsgExit(
"xupp" );
88 if ( xmul == 0 ) errMsgExit(
"xmul" );
90 if ( F == 0 ) errMsgExit(
"F" );
91 if ( Flow == 0 ) errMsgExit(
"Flow" );
92 if ( Fupp == 0 ) errMsgExit(
"Fupp" );
93 if ( Fmul == 0 ) errMsgExit(
"Fmul" );
95 if ( xnames == 0 ) errMsgExit(
"xnames" );
96 if ( Fnames == 0 ) errMsgExit(
"Fnames" );
97 if ( nxnames == 0 ) errMsgExit(
"nxnames" );
98 if ( nFnames == 0 ) errMsgExit(
"nFnames" );
100 if ( usrfun == 0 ) errMsgExit(
"usrfun" );
101 if ( lenA == -1 ) errMsgExit(
"lenA" );
102 if ( lenG == -1 ) errMsgExit(
"lenG" );
104 if ( (neA > 0) && (iAfun == 0) ) errMsgExit(
"iAfun" );
105 if ( (neA > 0) && (jAvar == 0) ) errMsgExit(
"jAvar" );
106 if ( neA > 0 && A == 0 ) errMsgExit(
"A" );
108 if ( neG > 0 && iGfun == 0 ) errMsgExit(
"iGfun" );
109 if ( neG > 0 && jGvar == 0 ) errMsgExit(
"jGvar" );
112 void snoptProblem_PAGMO::errMsgExit(
const char *var )
114 cerr <<
"****************************************************\n";
115 cerr <<
"Error: " << var <<
" must be set prior to call to " << endl
116 <<
"snoptProblem_PAGMO::solve() or snoptProblem_PAGMO::computeJac()!\n";
117 cerr <<
"****************************************************\n";
121 void snoptProblem_PAGMO::setMemory()
124 memoryGuess = this->snmema(mincw, miniw, minrw);
125 if ( mincw > lencw || miniw > leniw || minrw > lenrw ) {
127 this->realloc( mincw, miniw, minrw );
129 this->setIntParameter(
"Total real workspace ", lenrw );
130 this->setIntParameter(
"Total integer workspace", leniw );
133 if ( memoryGuess == 1 ) {
135 memoryGuess = this->snmema(mincw, miniw, minrw);
136 assert( memoryGuess == 0 );
137 this->realloc( mincw, miniw, minrw );
138 this->setIntParameter(
"Total real workspace ", lenrw );
139 this->setIntParameter(
"Total integer workspace", leniw );
144 void snoptProblem_PAGMO::alloc( integer alencw, integer aleniw, integer alenrw )
152 cw =
new char[8*lencw];
153 iw =
new integer[leniw];
154 rw =
new doublereal[lenrw];
157 void snoptProblem_PAGMO::realloc( integer alencw, integer aleniw, integer alenrw )
160 integer tlencw = lencw;
161 integer tleniw = leniw;
162 integer tlenrw = lenrw;
167 doublereal *trw = rw;
170 this->alloc ( alencw, aleniw, alenrw );
172 this->memcpyIn( tcw, tiw, trw, tlencw, tleniw, tlenrw );
180 void snoptProblem_PAGMO::memcpyIn(
char *tcw, integer *tiw, doublereal *trw,
181 integer tlencw, integer tleniw, integer tlenrw )
183 integer mlencw = lencw < tlencw ? lencw : tlencw;
184 integer mleniw = leniw < tleniw ? leniw : tleniw;
185 integer mlenrw = lenrw < tlenrw ? lenrw : tlenrw;
187 memcpy( cw, tcw, 8*mlencw*
sizeof(
char ) );
188 memcpy( iw, tiw, mleniw*
sizeof(integer));
189 memcpy( rw, trw, mlenrw*
sizeof(doublereal));
192 void snoptProblem_PAGMO::memcpyOut(
char *tcw, integer *tiw, doublereal *trw,
193 integer tlencw, integer tleniw, integer tlenrw )
195 integer mlencw = lencw < tlencw ? lencw : tlencw;
196 integer mleniw = leniw < tleniw ? leniw : tleniw;
197 integer mlenrw = lenrw < tlenrw ? lenrw : tlenrw;
199 memcpy( tcw, cw, 8*mlencw*
sizeof(
char ) );
200 memcpy( tiw, iw, mleniw*
sizeof(integer));
201 memcpy( trw, rw, mlenrw*
sizeof(doublereal));
204 void snoptProblem_PAGMO::increment()
206 if( !fortranStyleObj ) {
212 if( !fortranStyleAG ) {
214 for(
int k = 0; k < neA; k++ ) {
215 iAfun[k]++; jAvar[k]++;
218 for(
int k = 0; k < neG; k++ ) {
219 iGfun[k]++; jGvar[k]++;
225 void snoptProblem_PAGMO::decrement()
227 if( fortranStyleObj ) {
233 if (fortranStyleAG) {
235 for(
int k = 0; k < neA; k++ ) {
236 iAfun[k]--; jAvar[k]--;
239 for(
int k = 0; k < neG; k++ ) {
240 iGfun[k]--; jGvar[k]--;
246 void snoptProblem_PAGMO::computeJac()
250 this->snmema( mincw, miniw, minrw );
251 if ( mincw > lencw || miniw > leniw || minrw > lenrw ) {
253 this->realloc( mincw, miniw, minrw );
255 this->setIntParameter(
"Total real workspace ", lenrw );
256 this->setIntParameter(
"Total integer workspace", leniw );
258 snjac_( &inform, &neF, &n, usrfun,
259 iAfun, jAvar, &lenA, &neA, A,
260 iGfun, jGvar, &lenG, &neG,
261 x, xlow, xupp, &mincw, &miniw, &minrw,
262 cu, &lencu, iw, &leniw, ru, &lenru,
263 cw, &lencw, iw, &leniw, rw, &lenrw,
269 int snoptProblem_PAGMO::snmema
270 ( integer &amincw, integer &aminiw, integer &aminrw)
273 integer nxname = 1; integer nfname = 1;
282 snmema_( &inform, &neF, &n, &nxname, &nfname, &neA, &neG,
283 &amincw, &aminiw, &aminrw, cw, &lencw, iw, &leniw,
288 void snoptProblem_PAGMO::init()
291 sninit_( &iPrint, &iSumm, cw, &lencw, iw, &leniw, rw, &lenrw, 8*500 );
294 void snoptProblem_PAGMO::setParameter(
char *stropt )
296 assert( initCalled == 1 );
300 integer stropt_len = strlen(stropt);
301 snset_( stropt, &iPrt, &iSum, &inform, cw, &lencw, iw, &leniw,
302 rw, &lenrw, stropt_len, 8*500 );
305 void snoptProblem_PAGMO::getParameter(
char *stroptin,
char *stroptout )
307 assert( initCalled == 1 );
308 integer stroptin_len = strlen(stroptin);
309 integer stroptout_len = strlen(stroptout);
310 sngetc_( stroptin, stroptout, &inform, cw, &lencw, iw, &leniw,
311 rw, &lenrw, stroptin_len, stroptout_len, 8*500 );
314 void snoptProblem_PAGMO::setIntParameter(
const char *stropt, integer opt )
316 assert( initCalled == 1 );
320 integer stropt_len = strlen(stropt);
321 snseti_( (
char*)(
void*)stropt, &opt, &iPrt, &iSum, &inform,
322 cw, &lencw, iw, &leniw, rw, &lenrw, stropt_len, 8*500 );
325 void snoptProblem_PAGMO::getIntParameter(
const char *stropt, integer &opt )
327 assert( initCalled == 1 );
328 integer stropt_len = strlen(stropt);
329 sngeti_( (
char*)(
void*)stropt, &opt, &inform, cw, &lencw, iw, &leniw,
330 rw, &lenrw, stropt_len, 8*500 );
333 void snoptProblem_PAGMO::setRealParameter(
const char *stropt, doublereal opt )
335 assert( initCalled == 1 );
339 integer stropt_len = strlen(stropt);
340 snsetr_( (
char*)(
void*)stropt, &opt, &iPrt, &iSum, &inform,
341 cw, &lencw, iw, &leniw, rw, &lenrw, stropt_len, 8*500 );
344 void snoptProblem_PAGMO::getRealParameter(
const char *stropt, doublereal &opt )
346 assert( initCalled == 1 );
347 integer stropt_len = strlen(stropt);
348 sngetr_( (
char*)(
void*)stropt, &opt, &inform, cw, &lencw, iw, &leniw,
349 rw, &lenrw, stropt_len, 8*500 );
352 void snoptProblem_PAGMO::solve( integer starttype )
354 assert( initCalled == 1 );
358 if ( neA == -1 || neG == -1 ) {
359 cerr <<
"Warning: neA and neG must be set before calling"
360 <<
"snoptProblem_PAGMO::solve()\n";
363 integer npname = strlen(Prob);
368 snopta_( &starttype, &neF, &n, &nxnames,
370 &ObjAdd, &ObjRow, Prob,
371 usrfun, iAfun, jAvar, &lenA, &neA, A,
372 iGfun, jGvar, &lenG, &neG,
373 xlow, xupp, xnames, Flow,
374 Fupp, Fnames, x, xstate,
375 xmul, F, Fstate, Fmul,
376 &inform, &mincw, &miniw, &minrw, &nS, &nInf, &sInf,
377 cu, &lencu, iw, &leniw, ru, &lenru,
378 cw, &lencw, iw, &leniw, rw, &lenrw,
379 npname, 8*(nxnames), 8*(nFnames), 8*500, 8*500);
383 void snoptProblem_PAGMO::setPrintFile(
const char aprintname[] )
385 assert( initCalled = 1 );
390 strcpy( printname, aprintname ); prnt_len = strlen(printname);
391 snopenappend_( &iPrint, printname, &inform, prnt_len );
392 this->setIntParameter(
"Print file", iPrint);
395 void snoptProblem_PAGMO::setSpecFile(
char aspecname[] )
397 assert( initCalled == 1 );
402 strcpy( specname, aspecname ); spec_len = strlen(specname);
403 snfilewrapper_( specname, &iSpecs, &inform, cw, &lencw,
404 iw, &leniw, rw, &lenrw, spec_len, 8*lencw);
406 printf(
"Warning: unable to find specs file %s \n", specname);
410 void snoptProblem_PAGMO::setProblemSize( integer an, integer aneF )
417 void snoptProblem_PAGMO::setObjective( integer aObjRow, doublereal aObjAdd )
424 void snoptProblem_PAGMO::setA( integer alenA, integer *aiAfun,
425 integer *ajAvar, doublereal *aA )
434 void snoptProblem_PAGMO::setNeA( integer aneA )
439 void snoptProblem_PAGMO::setG( integer alenG, integer *aiGfun,
448 void snoptProblem_PAGMO::setNeG( integer aneG )
454 void snoptProblem_PAGMO::setX( doublereal *ax, doublereal *axlow, doublereal *axupp,
455 doublereal *axmul, integer *axstate )
465 void snoptProblem_PAGMO::setF( doublereal *aF, doublereal *aFlow, doublereal *aFupp,
466 doublereal *aFmul, integer *aFstate )
476 void snoptProblem_PAGMO::setXNames(
char *axnames, integer anxnames )
483 void snoptProblem_PAGMO::setFNames(
char *aFnames, integer anFnames )
490 void snoptProblem_PAGMO::setProbName(
const char *aProb )
493 sprintf(Prob,
"%8s", aProb );
496 void snoptProblem_PAGMO::setUserFun( My_fp ausrfun )