| 1 |
// (C) Copyright International Business Machines Corporation, 2006, 2007 |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// Authors : |
| 6 |
// Pierre Bonami, International Business Machines Corporation |
| 7 |
// |
| 8 |
// Date : 10/02/2006 |
| 9 |
|
| 10 |
#ifndef FilterSolver_H |
| 11 |
#define FilterSolver_H |
| 12 |
|
| 13 |
#include "BonTNLPSolver.hpp" |
| 14 |
#include "BonFilterTypes.hpp" |
| 15 |
#include "BonBqpdSolver.hpp" |
| 16 |
#include "BonFilterWarmStart.hpp" |
| 17 |
|
| 18 |
namespace Bonmin |
| 19 |
{ |
| 20 |
class FilterSolver : public TNLPSolver |
| 21 |
{ |
| 22 |
public: |
| 23 |
|
| 24 |
friend struct BqpdSolver::cachedInfo; |
| 25 |
|
| 26 |
class UnsolvedFilterError: public TNLPSolver::UnsolvedError |
| 27 |
{ |
| 28 |
public: |
| 29 |
UnsolvedFilterError(int errorNum, |
| 30 |
Ipopt::SmartPtr<TMINLP2TNLP> model, |
| 31 |
const std::string &name): |
| 32 |
TNLPSolver::UnsolvedError(errorNum, model, name) |
| 33 |
{} |
| 34 |
virtual const std::string& errorName() const; |
| 35 |
|
| 36 |
virtual const std::string& solverName() const; |
| 37 |
virtual ~UnsolvedFilterError() |
| 38 |
{} |
| 39 |
|
| 40 |
private: |
| 41 |
static std::string errorNames_[1]; |
| 42 |
static std::string solverName_; |
| 43 |
}; |
| 44 |
|
| 45 |
/** Fortran type for integer used in filter. */ |
| 46 |
typedef FilterTypes::fint fint; |
| 47 |
/** Fortran type for double.used in filter */ |
| 48 |
typedef FilterTypes::real real; |
| 49 |
|
| 50 |
|
| 51 |
virtual UnsolvedError * newUnsolvedError(int num, |
| 52 |
Ipopt::SmartPtr<TMINLP2TNLP> problem, |
| 53 |
std::string name) |
| 54 |
{ |
| 55 |
return new UnsolvedFilterError(num, problem, name); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
///Default constructor |
| 60 |
FilterSolver(bool createEmpty = false); |
| 61 |
|
| 62 |
|
| 63 |
/// Constructor with passed journalist, roptions, options. |
| 64 |
FilterSolver(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions, |
| 65 |
Ipopt::SmartPtr<Ipopt::OptionsList> options, |
| 66 |
Ipopt::SmartPtr<Ipopt::Journalist> journalist, |
| 67 |
const std::string & prefix |
| 68 |
); |
| 69 |
|
| 70 |
/// Constructor with passed journalist, roptions, options. |
| 71 |
FilterSolver(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions, |
| 72 |
Ipopt::SmartPtr<Ipopt::OptionsList> options, |
| 73 |
Ipopt::SmartPtr<Ipopt::Journalist> journalist |
| 74 |
); |
| 75 |
|
| 76 |
/// Copy constructor |
| 77 |
FilterSolver(const FilterSolver & other); |
| 78 |
///destructor |
| 79 |
virtual ~FilterSolver(); |
| 80 |
|
| 81 |
/** Initialize the TNLPSolver (read options from params_file) |
| 82 |
*/ |
| 83 |
virtual bool Initialize(std::string params_file); |
| 84 |
|
| 85 |
/** Initialize the TNLPSolver (read options from istream is) |
| 86 |
*/ |
| 87 |
virtual bool Initialize(std::istream& is); |
| 88 |
|
| 89 |
/** @name Solve methods */ |
| 90 |
//@{ |
| 91 |
/// Solves a problem expresses as a TNLP |
| 92 |
virtual ReturnStatus OptimizeTNLP(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp); |
| 93 |
|
| 94 |
/// Resolves a problem expresses as a TNLP |
| 95 |
virtual ReturnStatus ReOptimizeTNLP(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp); |
| 96 |
|
| 97 |
/// Set the warm start in the solver |
| 98 |
virtual bool setWarmStart(const CoinWarmStart * warm, |
| 99 |
Ipopt::SmartPtr<TMINLP2TNLP> tnlp); |
| 100 |
|
| 101 |
/// Get warm start used in last optimization |
| 102 |
virtual CoinWarmStart * getUsedWarmStart(Ipopt::SmartPtr<TMINLP2TNLP> tnlp) const{ |
| 103 |
if(warmF_.IsValid()) |
| 104 |
return new FilterWarmStart(*warmF_); |
| 105 |
else return NULL; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
/// Get the warm start form the solver |
| 110 |
virtual CoinWarmStart * getWarmStart(Ipopt::SmartPtr<TMINLP2TNLP> tnlp) const; |
| 111 |
|
| 112 |
virtual CoinWarmStart * getEmptyWarmStart() const; |
| 113 |
|
| 114 |
/** Check that warm start object is valid.*/ |
| 115 |
virtual bool warmStartIsValid(const CoinWarmStart * ws) const; |
| 116 |
|
| 117 |
|
| 118 |
virtual void enableWarmStart() |
| 119 |
{//No options to be set |
| 120 |
} |
| 121 |
virtual void disableWarmStart() |
| 122 |
{ |
| 123 |
warmF_ = NULL; |
| 124 |
//No options to be set |
| 125 |
} |
| 126 |
//@} |
| 127 |
|
| 128 |
/// Virtual copy constructor |
| 129 |
virtual Ipopt::SmartPtr<TNLPSolver> clone(); |
| 130 |
|
| 131 |
/// Get the CpuTime of the last optimization. |
| 132 |
virtual double CPUTime() |
| 133 |
{ |
| 134 |
return (cached_.IsValid()) ? cached_->cpuTime_: 0.; |
| 135 |
} |
| 136 |
|
| 137 |
/// Get the iteration count of the last optimization. |
| 138 |
virtual int IterationCount() |
| 139 |
{ |
| 140 |
return (cached_.IsValid()) ? cached_->istat[1]:0; |
| 141 |
} |
| 142 |
|
| 143 |
/// turn off all output from the solver |
| 144 |
virtual void setOutputToDefault() |
| 145 |
{ |
| 146 |
if (cached_.IsValid()) cached_->iprint = default_log_level_; |
| 147 |
} |
| 148 |
/// turn on all output from the solver |
| 149 |
virtual void forceSolverOutput(int log_level) |
| 150 |
{ |
| 151 |
if (cached_.IsValid()) cached_->iprint = log_level; |
| 152 |
} |
| 153 |
|
| 154 |
/// Get the solver name |
| 155 |
virtual std::string & solverName() |
| 156 |
{ |
| 157 |
return solverName_; |
| 158 |
} |
| 159 |
|
| 160 |
/// Register this solver options into passed roptions |
| 161 |
void registerOptions() |
| 162 |
{ |
| 163 |
registerOptions(roptions_); |
| 164 |
} |
| 165 |
|
| 166 |
/** Error code (solver specific).*/ |
| 167 |
virtual int errorCode() const |
| 168 |
{ |
| 169 |
return -1; |
| 170 |
} |
| 171 |
/// Register this solver options into passed roptions |
| 172 |
static void registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions); |
| 173 |
private: |
| 174 |
/** @name Private function members. */ |
| 175 |
/** @{ */ |
| 176 |
/** Perform optimization using data structure in cache. */ |
| 177 |
TNLPSolver::ReturnStatus callOptimizer(); |
| 178 |
/** @} */ |
| 179 |
Coin::SmartPtr<FilterWarmStart> warmF_; |
| 180 |
|
| 181 |
/** Cached information for reoptimizing. */ |
| 182 |
struct cachedInfo : public Coin::ReferencedObject |
| 183 |
{ |
| 184 |
fint n; |
| 185 |
fint m; |
| 186 |
fint nnz_h_; |
| 187 |
fint kmax; |
| 188 |
fint maxa; |
| 189 |
fint maxf; |
| 190 |
fint mlp; |
| 191 |
fint maxWk; |
| 192 |
fint maxiWk; |
| 193 |
fint iprint; |
| 194 |
fint nout; |
| 195 |
fint ifail; |
| 196 |
real rho; |
| 197 |
real * x; |
| 198 |
real * c; |
| 199 |
real f; |
| 200 |
real fmin; |
| 201 |
real * bounds; |
| 202 |
real * s; |
| 203 |
real * a; |
| 204 |
fint * la; |
| 205 |
real * ws; |
| 206 |
fint * lws; |
| 207 |
real * lam; |
| 208 |
char * cstype; |
| 209 |
fint maxiter; |
| 210 |
fint * istat; |
| 211 |
real * rstat; |
| 212 |
Ipopt::TNLP * tnlp_; |
| 213 |
fint * hStruct_; |
| 214 |
int * permutationJac_; |
| 215 |
int * permutationHess_; |
| 216 |
/** Elapsed CPU time in last optimization. */ |
| 217 |
double cpuTime_; |
| 218 |
/** flag remembering if warm start information has been put into |
| 219 |
cache */ |
| 220 |
bool use_warm_start_in_cache_; |
| 221 |
|
| 222 |
|
| 223 |
/** Constructor.*/ |
| 224 |
cachedInfo(): |
| 225 |
n(-1), |
| 226 |
m(-1), |
| 227 |
nnz_h_(-1), |
| 228 |
kmax(-1), |
| 229 |
maxa(-1), |
| 230 |
maxf(-1), |
| 231 |
mlp(-1), |
| 232 |
maxWk(-1), |
| 233 |
maxiWk(-1), |
| 234 |
iprint(-1), |
| 235 |
nout(6), |
| 236 |
ifail(-100), |
| 237 |
rho(0), |
| 238 |
x(NULL), |
| 239 |
c(NULL), |
| 240 |
f(1e100), |
| 241 |
fmin(-1e100), |
| 242 |
bounds(NULL), |
| 243 |
s(NULL), |
| 244 |
a(NULL), |
| 245 |
la(NULL), |
| 246 |
ws(NULL), |
| 247 |
lws(NULL), |
| 248 |
lam(NULL), |
| 249 |
cstype(NULL), |
| 250 |
maxiter(1000), |
| 251 |
istat(NULL), |
| 252 |
rstat(NULL), |
| 253 |
tnlp_(NULL), |
| 254 |
hStruct_(NULL), |
| 255 |
permutationJac_(NULL), |
| 256 |
permutationHess_(NULL), |
| 257 |
cpuTime_(0), |
| 258 |
use_warm_start_in_cache_(false) |
| 259 |
{} |
| 260 |
|
| 261 |
cachedInfo(const Ipopt::SmartPtr<Ipopt::TNLP> &tnlp, |
| 262 |
Ipopt::SmartPtr<Ipopt::OptionsList>& options): |
| 263 |
n(-1), |
| 264 |
m(-1), |
| 265 |
nnz_h_(-1), |
| 266 |
kmax(-1), |
| 267 |
maxa(-1), |
| 268 |
maxf(-1), |
| 269 |
mlp(-1), |
| 270 |
maxWk(-1), |
| 271 |
maxiWk(-1), |
| 272 |
iprint(-1), |
| 273 |
nout(6), |
| 274 |
ifail(0), |
| 275 |
rho(0), |
| 276 |
x(NULL), |
| 277 |
c(NULL), |
| 278 |
f(1e100), |
| 279 |
fmin(-1e100), |
| 280 |
bounds(NULL), |
| 281 |
s(NULL), |
| 282 |
a(NULL), |
| 283 |
la(NULL), |
| 284 |
ws(NULL), |
| 285 |
lws(NULL), |
| 286 |
lam(NULL), |
| 287 |
cstype(NULL), |
| 288 |
maxiter(1000), |
| 289 |
istat(NULL), |
| 290 |
rstat(NULL), |
| 291 |
tnlp_(NULL), |
| 292 |
hStruct_(NULL), |
| 293 |
permutationJac_(NULL), |
| 294 |
permutationHess_(NULL), |
| 295 |
cpuTime_(0), |
| 296 |
use_warm_start_in_cache_(false) |
| 297 |
{ |
| 298 |
initialize(tnlp, options); |
| 299 |
} |
| 300 |
|
| 301 |
/** Fill data structures for filter with info from tnlp. */ |
| 302 |
void initialize(const Ipopt::SmartPtr<Ipopt::TNLP> &tnlp, |
| 303 |
Ipopt::SmartPtr<Ipopt::OptionsList>& options); |
| 304 |
|
| 305 |
/** Optimize problem described by cache with filter.*/ |
| 306 |
void optimize(); |
| 307 |
|
| 308 |
/** Destructor. */ |
| 309 |
~cachedInfo() |
| 310 |
{ |
| 311 |
delete [] x; |
| 312 |
delete [] c; |
| 313 |
delete [] bounds; |
| 314 |
delete [] s; |
| 315 |
delete [] a; |
| 316 |
delete [] la; |
| 317 |
delete [] ws; |
| 318 |
delete [] lws; |
| 319 |
delete [] lam; |
| 320 |
delete [] cstype; |
| 321 |
delete [] istat; |
| 322 |
delete [] rstat; |
| 323 |
delete [] permutationJac_; |
| 324 |
delete [] permutationHess_; |
| 325 |
delete [] hStruct_; |
| 326 |
tnlp_ = NULL; |
| 327 |
} |
| 328 |
|
| 329 |
void load_ws(Coin::SmartPtr<FilterWarmStart>); |
| 330 |
}; |
| 331 |
|
| 332 |
/** Cached information on last problem optimized for reoptimization. */ |
| 333 |
Coin::SmartPtr<cachedInfo> cached_; |
| 334 |
|
| 335 |
//name of solver (Filter) |
| 336 |
static std::string solverName_; |
| 337 |
|
| 338 |
/** To record default log level.*/ |
| 339 |
int default_log_level_; |
| 340 |
/** Converting TMatrices into row-ordered matrices */ |
| 341 |
static void TMat2RowPMat(bool symmetric, fint n, fint m, int nnz, const Ipopt::Index* iRow, |
| 342 |
const Ipopt::Index* iCol, int * permutation2, |
| 343 |
fint * lws, int nnz_offset, int n_offset, |
| 344 |
Ipopt::TNLP::IndexStyleEnum index_style); |
| 345 |
}; |
| 346 |
|
| 347 |
}// end namespace Bonmin |
| 348 |
#endif |