| 1 |
// (C) Copyright International Business Machines Corporation and Carnegie Mellon University 2004, 2006 |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// Authors : |
| 6 |
// Carl D. Laird, Carnegie Mellon University, |
| 7 |
// Andreas Waechter, International Business Machines Corporation |
| 8 |
// Pierre Bonami, Carnegie Mellon University, |
| 9 |
// |
| 10 |
// Date : 12/01/2004 |
| 11 |
|
| 12 |
|
| 13 |
#include "BonTMINLP2TNLP.hpp" |
| 14 |
#include "IpBlas.hpp" |
| 15 |
#include "IpAlgTypes.hpp" |
| 16 |
#include "IpIpoptCalculatedQuantities.hpp" |
| 17 |
#include <climits> |
| 18 |
#include <string> |
| 19 |
#include <fstream> |
| 20 |
#include <sstream> |
| 21 |
#include "Ipopt/BonIpoptInteriorWarmStarter.hpp" |
| 22 |
#include "OsiBranchingObject.hpp" |
| 23 |
|
| 24 |
using namespace Ipopt; |
| 25 |
|
| 26 |
extern bool BonminAbortAll; |
| 27 |
class OsiObject; |
| 28 |
namespace Bonmin |
| 29 |
{ |
| 30 |
|
| 31 |
TMINLP2TNLP::TMINLP2TNLP(const SmartPtr<TMINLP> tminlp |
| 32 |
#ifdef WARM_STARTER |
| 33 |
, |
| 34 |
const OptionsList& options |
| 35 |
#endif |
| 36 |
) |
| 37 |
: |
| 38 |
var_types_(), |
| 39 |
x_l_(), |
| 40 |
x_u_(), |
| 41 |
orig_x_l_(), |
| 42 |
orig_x_u_(), |
| 43 |
g_l_(), |
| 44 |
g_u_(), |
| 45 |
x_init_(), |
| 46 |
duals_init_(NULL), |
| 47 |
x_init_user_(), |
| 48 |
x_sol_(), |
| 49 |
g_sol_(), |
| 50 |
duals_sol_(), |
| 51 |
tminlp_(tminlp), |
| 52 |
nnz_jac_g_(0), |
| 53 |
nnz_h_lag_(0), |
| 54 |
index_style_(TNLP::FORTRAN_STYLE), |
| 55 |
return_status_(Ipopt::UNASSIGNED), |
| 56 |
obj_value_(1e100), |
| 57 |
curr_warm_starter_(), |
| 58 |
nlp_lower_bound_inf_(-DBL_MAX), |
| 59 |
nlp_upper_bound_inf_(DBL_MAX), |
| 60 |
warm_start_entire_iterate_(true), |
| 61 |
need_new_warm_starter_(true) |
| 62 |
{ |
| 63 |
// read the nlp size and bounds information from |
| 64 |
// the TMINLP and keep an internal copy. This way the |
| 65 |
// caller can modify the bounds that are sent to Ipopt; |
| 66 |
assert(IsValid(tminlp_)); |
| 67 |
Index n,m; |
| 68 |
bool retval = |
| 69 |
tminlp_->get_nlp_info(n, m, nnz_jac_g_, nnz_h_lag_, index_style_); |
| 70 |
|
| 71 |
ASSERT_EXCEPTION(retval, TMINLP_INVALID, |
| 72 |
"get_nlp_info of TMINLP returns false."); |
| 73 |
|
| 74 |
// Allocate space for the variable types vector |
| 75 |
var_types_.resize(n); |
| 76 |
|
| 77 |
// retrieve the variable types |
| 78 |
tminlp_->get_variables_types(n, var_types_()); |
| 79 |
|
| 80 |
// Allocate space for the internal copy of the variable bounds |
| 81 |
x_l_.resize(n); |
| 82 |
x_u_.resize(n); |
| 83 |
orig_x_l_.resize(n); |
| 84 |
orig_x_u_.resize(n); |
| 85 |
|
| 86 |
g_l_.resize(m); |
| 87 |
g_u_.resize(m); |
| 88 |
|
| 89 |
// retrieve the variable bounds |
| 90 |
if(m){ |
| 91 |
tminlp_->get_bounds_info(n, x_l_(), x_u_(), m, g_l_(), g_u_()); |
| 92 |
} |
| 93 |
else { |
| 94 |
tminlp_->get_bounds_info(n, x_l_(), x_u_(), m, NULL, NULL); |
| 95 |
} |
| 96 |
IpBlasDcopy(n, x_l_(), 1, orig_x_l_(), 1); |
| 97 |
IpBlasDcopy(n, x_u_(), 1, orig_x_u_(), 1); |
| 98 |
|
| 99 |
|
| 100 |
// Allocate space for the initial point |
| 101 |
x_init_user_.resize(n); |
| 102 |
tminlp_->get_starting_point(n, true, x_init_user_(), false, NULL, NULL, |
| 103 |
m, false, NULL); |
| 104 |
|
| 105 |
#ifdef WARM_STARTER |
| 106 |
// Get values for parameters |
| 107 |
options.GetNumericValue("nlp_lower_bound_inf", nlp_lower_bound_inf_, ""); |
| 108 |
options.GetNumericValue("nlp_upper_bound_inf", nlp_upper_bound_inf_, ""); |
| 109 |
options.GetBoolValue("warm_start_entire_iterate", |
| 110 |
warm_start_entire_iterate_, ""); |
| 111 |
#endif |
| 112 |
} |
| 113 |
|
| 114 |
TMINLP2TNLP::TMINLP2TNLP(const TMINLP2TNLP& other) |
| 115 |
: |
| 116 |
var_types_(), |
| 117 |
x_l_(), |
| 118 |
x_u_(), |
| 119 |
orig_x_l_(), |
| 120 |
orig_x_u_(), |
| 121 |
g_l_(), |
| 122 |
g_u_(), |
| 123 |
x_init_(), |
| 124 |
duals_init_(NULL), |
| 125 |
x_init_user_(), |
| 126 |
x_sol_(), |
| 127 |
g_sol_(), |
| 128 |
duals_sol_(), |
| 129 |
tminlp_(other.tminlp_), |
| 130 |
nnz_jac_g_(other.nnz_jac_g_), |
| 131 |
nnz_h_lag_(other.nnz_h_lag_), |
| 132 |
index_style_(other.index_style_), |
| 133 |
return_status_(other.return_status_), |
| 134 |
obj_value_(other.obj_value_), |
| 135 |
curr_warm_starter_(other.curr_warm_starter_), |
| 136 |
nlp_lower_bound_inf_(other.nlp_lower_bound_inf_), |
| 137 |
nlp_upper_bound_inf_(other.nlp_upper_bound_inf_), |
| 138 |
warm_start_entire_iterate_(other.warm_start_entire_iterate_), |
| 139 |
need_new_warm_starter_(other.need_new_warm_starter_) |
| 140 |
{ |
| 141 |
gutsOfCopy(other); |
| 142 |
} |
| 143 |
|
| 144 |
/** Overloaded Equals Operator */ |
| 145 |
TMINLP2TNLP & |
| 146 |
TMINLP2TNLP::operator=(const TMINLP2TNLP& rhs){ |
| 147 |
if(this != &rhs){ |
| 148 |
tminlp_ = rhs.tminlp_; |
| 149 |
nnz_jac_g_ = rhs.nnz_jac_g_; |
| 150 |
nnz_h_lag_ = rhs.nnz_h_lag_; |
| 151 |
index_style_ = rhs.index_style_; |
| 152 |
return_status_ = rhs.return_status_; |
| 153 |
obj_value_ = rhs.obj_value_; |
| 154 |
curr_warm_starter_ = rhs.curr_warm_starter_; |
| 155 |
nlp_lower_bound_inf_ = rhs.nlp_lower_bound_inf_; |
| 156 |
nlp_upper_bound_inf_ = rhs.nlp_upper_bound_inf_; |
| 157 |
warm_start_entire_iterate_ = rhs.warm_start_entire_iterate_; |
| 158 |
need_new_warm_starter_ = rhs.need_new_warm_starter_; |
| 159 |
|
| 160 |
gutsOfDelete(); |
| 161 |
gutsOfCopy(rhs); |
| 162 |
|
| 163 |
} |
| 164 |
return (*this); |
| 165 |
} |
| 166 |
|
| 167 |
TMINLP2TNLP::~TMINLP2TNLP() |
| 168 |
{ |
| 169 |
gutsOfDelete(); |
| 170 |
} |
| 171 |
|
| 172 |
void |
| 173 |
TMINLP2TNLP::gutsOfDelete(){ |
| 174 |
} |
| 175 |
|
| 176 |
/** Copies all the arrays. |
| 177 |
\warning this and other should be two instances of the same problem |
| 178 |
\warning AW: I am trying to mimic a copy construction for Cbc |
| 179 |
use with great care not safe. |
| 180 |
*/ |
| 181 |
void |
| 182 |
TMINLP2TNLP::gutsOfCopy(const TMINLP2TNLP& other) |
| 183 |
{ |
| 184 |
Index n = other.num_variables(); |
| 185 |
Index m = other.num_constraints(); |
| 186 |
|
| 187 |
if(n > 0){//Copies all the arrays in n_ |
| 188 |
var_types_ = other.var_types_; |
| 189 |
|
| 190 |
x_l_.resize(n); |
| 191 |
x_u_.resize(n); // Those are copied in copyUserModification |
| 192 |
IpBlasDcopy(n, other.x_l_(), 1, x_l_(), 1); |
| 193 |
IpBlasDcopy(n, other.x_u_(), 1, x_u_(), 1); |
| 194 |
|
| 195 |
orig_x_l_.resize(n); |
| 196 |
orig_x_u_.resize(n); |
| 197 |
IpBlasDcopy(n, other.orig_x_l_(), 1, orig_x_l_(), 1); |
| 198 |
IpBlasDcopy(n, other.orig_x_u_(), 1, orig_x_u_(), 1); |
| 199 |
x_init_user_.resize(n); |
| 200 |
IpBlasDcopy(n, other.x_init_user_(), 1, x_init_user_(), 1); |
| 201 |
if(!other.x_sol_.empty()) { |
| 202 |
Set_x_sol(n,other.x_sol_()); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
if(!other.g_l_.empty()){ |
| 207 |
const size_t& size = other.g_l_.size(); |
| 208 |
g_l_.resize(size); |
| 209 |
g_u_.resize(size); |
| 210 |
} |
| 211 |
|
| 212 |
if(m > 0){//Copies all the arrays in m_ |
| 213 |
IpBlasDcopy(m, other.g_l_(), 1, g_l_(), 1); |
| 214 |
IpBlasDcopy(m, other.g_u_(), 1, g_u_(), 1); |
| 215 |
if(!other.g_sol_.empty()) { |
| 216 |
g_sol_.resize(m); |
| 217 |
IpBlasDcopy(m, other.g_sol_(), 1, g_sol_(), 1); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
x_init_ = other.x_init_; |
| 223 |
|
| 224 |
if(other.duals_init_) { |
| 225 |
duals_init_ = x_init_() + n; |
| 226 |
} |
| 227 |
else |
| 228 |
duals_init_ = NULL; |
| 229 |
|
| 230 |
|
| 231 |
if(!other.duals_sol_.empty()) { |
| 232 |
duals_sol_.resize(m + 2*n); |
| 233 |
IpBlasDcopy((int) duals_sol_.size(), other.duals_sol_(), 1, duals_sol_(), 1); |
| 234 |
} |
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
void TMINLP2TNLP::SetVariablesBounds(Index n, |
| 239 |
const Number * x_l, |
| 240 |
const Number * x_u) |
| 241 |
{ |
| 242 |
assert(n==num_variables()); |
| 243 |
IpBlasDcopy(n, x_l, 1, x_l_(), 1); |
| 244 |
IpBlasDcopy(n, x_u, 1, x_u_(), 1); |
| 245 |
} |
| 246 |
|
| 247 |
void TMINLP2TNLP::SetVariablesLowerBounds(Index n, |
| 248 |
const Number * x_l) |
| 249 |
{ |
| 250 |
assert(n==num_variables()); |
| 251 |
IpBlasDcopy(n, x_l, 1, x_l_(), 1); |
| 252 |
} |
| 253 |
|
| 254 |
void TMINLP2TNLP::SetVariablesUpperBounds(Index n, |
| 255 |
const Number * x_u) |
| 256 |
{ |
| 257 |
assert(n==num_variables()); |
| 258 |
IpBlasDcopy(n, x_u, 1, x_u_(), 1); |
| 259 |
} |
| 260 |
|
| 261 |
void TMINLP2TNLP::SetVariableBounds(Index var_no, Number x_l, Number x_u) |
| 262 |
{ |
| 263 |
assert(var_no >= 0 && var_no < num_variables()); |
| 264 |
x_l_[var_no] = x_l; |
| 265 |
x_u_[var_no] = x_u; |
| 266 |
} |
| 267 |
|
| 268 |
void TMINLP2TNLP::SetVariableLowerBound(Index var_no, Number x_l) |
| 269 |
{ |
| 270 |
assert(var_no >= 0 && var_no < num_variables()); |
| 271 |
x_l_[var_no] = x_l; |
| 272 |
} |
| 273 |
|
| 274 |
void TMINLP2TNLP::SetVariableUpperBound(Index var_no, Number x_u) |
| 275 |
{ |
| 276 |
assert(var_no >= 0 && var_no < num_variables()); |
| 277 |
x_u_[var_no] = x_u; |
| 278 |
} |
| 279 |
|
| 280 |
void TMINLP2TNLP::resetStartingPoint() |
| 281 |
{ |
| 282 |
curr_warm_starter_ = NULL; |
| 283 |
x_init_.clear(); |
| 284 |
} |
| 285 |
|
| 286 |
void TMINLP2TNLP::setxInit(Index n,const Number* x_init) |
| 287 |
{ |
| 288 |
assert(n == num_variables()); |
| 289 |
if((int)x_init_.size() < n) |
| 290 |
x_init_.resize(n); |
| 291 |
IpBlasDcopy(n, x_init, 1, x_init_(), 1); |
| 292 |
} |
| 293 |
|
| 294 |
void TMINLP2TNLP::setDualsInit(Index m, const Number* duals_init) |
| 295 |
{ |
| 296 |
assert(m == num_variables() * 2 + num_constraints() ); |
| 297 |
x_init_.resize(num_variables() * 3 + num_constraints(), 0.); |
| 298 |
duals_init_ = x_init_() + num_variables(); |
| 299 |
|
| 300 |
if(m >0) |
| 301 |
IpBlasDcopy(m, duals_init, 1, duals_init_, 1); |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
/** Set the contiuous solution */ |
| 306 |
void TMINLP2TNLP::Set_x_sol(Index n, const Number* x_sol) |
| 307 |
{ |
| 308 |
assert(n == num_variables()); |
| 309 |
if (x_sol_.empty()) { |
| 310 |
x_sol_.resize(n); |
| 311 |
} |
| 312 |
assert(n == (int) x_sol_.size()); |
| 313 |
IpBlasDcopy(n, x_sol, 1, x_sol_(), 1); |
| 314 |
} |
| 315 |
|
| 316 |
/** Set the contiuous dual solution */ |
| 317 |
void TMINLP2TNLP::Set_dual_sol(Index n, const Number* dual_sol) |
| 318 |
{ |
| 319 |
assert(n == num_variables() *2 + num_constraints()); |
| 320 |
if (duals_sol_.empty()) { |
| 321 |
duals_sol_.resize(n); |
| 322 |
} |
| 323 |
assert(n == (int) duals_sol_.size()); |
| 324 |
IpBlasDcopy(n, dual_sol, 1, duals_sol_(), 1); |
| 325 |
} |
| 326 |
|
| 327 |
/** Change the type of the variable */ |
| 328 |
void TMINLP2TNLP::SetVariableType(Index n, TMINLP::VariableType type) |
| 329 |
{ |
| 330 |
assert(n >= 0 && n < num_variables()); |
| 331 |
var_types_[n] = type; |
| 332 |
} |
| 333 |
|
| 334 |
bool TMINLP2TNLP::get_nlp_info(Index& n, Index& m, Index& nnz_jac_g, |
| 335 |
Index& nnz_h_lag, TNLP::IndexStyleEnum& index_style) |
| 336 |
{ |
| 337 |
n = num_variables(); |
| 338 |
m = num_constraints(); |
| 339 |
nnz_jac_g = nnz_jac_g_; |
| 340 |
nnz_h_lag = nnz_h_lag_; |
| 341 |
index_style = index_style_; |
| 342 |
//printf("Been there and said %i\n", nnz_jac_g_); |
| 343 |
return true; |
| 344 |
} |
| 345 |
|
| 346 |
bool TMINLP2TNLP::get_bounds_info(Index n, Number* x_l, Number* x_u, |
| 347 |
Index m, Number* g_l, Number* g_u) |
| 348 |
{ |
| 349 |
assert(n==num_variables()); |
| 350 |
assert(m==num_constraints()); |
| 351 |
IpBlasDcopy(n, x_l_(), 1, x_l, 1); |
| 352 |
IpBlasDcopy(n, x_u_(), 1, x_u, 1); |
| 353 |
if (m > 0){ |
| 354 |
IpBlasDcopy(m, g_l_(), 1, g_l, 1); |
| 355 |
IpBlasDcopy(m, g_u_(), 1, g_u, 1); |
| 356 |
} |
| 357 |
return true; |
| 358 |
} |
| 359 |
|
| 360 |
bool TMINLP2TNLP::get_starting_point(Index n, bool init_x, Number* x, |
| 361 |
bool init_z, Number* z_L, Number* z_U, |
| 362 |
Index m, bool init_lambda, |
| 363 |
Number* lambda) |
| 364 |
{ |
| 365 |
assert(m==num_constraints()); |
| 366 |
assert(n==num_variables()); |
| 367 |
#if 0 |
| 368 |
x_init_.resize(3*n + m, 0.); |
| 369 |
duals_init_ = x_init_() + n; |
| 370 |
#endif |
| 371 |
if (init_x == true) { |
| 372 |
if(x_init_.empty()){ |
| 373 |
assert(x_init_user_.size() >= n); |
| 374 |
IpBlasDcopy(n, x_init_user_(), 1, x, 1); |
| 375 |
} |
| 376 |
else |
| 377 |
IpBlasDcopy(n, x_init_(), 1, x, 1); |
| 378 |
} |
| 379 |
if (init_z == true) { |
| 380 |
if(duals_init_ == NULL) |
| 381 |
return false; |
| 382 |
assert(x_init_.size() == 3*n + m && duals_init_ == x_init_() + n); |
| 383 |
IpBlasDcopy(n, duals_init_, 1, z_L, 1); |
| 384 |
IpBlasDcopy(n, duals_init_ + n, 1, z_U, 1); |
| 385 |
|
| 386 |
} |
| 387 |
if(init_lambda == true) { |
| 388 |
if(duals_init_ == NULL) |
| 389 |
return false; |
| 390 |
assert(x_init_.size() == 3*n + m && duals_init_ == x_init_() + n); |
| 391 |
if(m > 0) |
| 392 |
IpBlasDcopy(m, duals_init_ + 2*n , 1, lambda, 1); |
| 393 |
} |
| 394 |
|
| 395 |
need_new_warm_starter_ = true; |
| 396 |
return true; |
| 397 |
} |
| 398 |
|
| 399 |
bool TMINLP2TNLP::get_warm_start_iterate(IteratesVector& warm_start_iterate) |
| 400 |
{ |
| 401 |
if (IsNull(curr_warm_starter_)) { |
| 402 |
return false; |
| 403 |
} |
| 404 |
|
| 405 |
bool retval = curr_warm_starter_->WarmStartIterate(num_variables(), x_l_(), x_u_(), |
| 406 |
warm_start_iterate); |
| 407 |
|
| 408 |
need_new_warm_starter_ = true; |
| 409 |
return retval; |
| 410 |
} |
| 411 |
|
| 412 |
bool TMINLP2TNLP::eval_f(Index n, const Number* x, bool new_x, |
| 413 |
Number& obj_value) |
| 414 |
{ |
| 415 |
return tminlp_->eval_f(n, x, new_x, obj_value); |
| 416 |
} |
| 417 |
|
| 418 |
bool TMINLP2TNLP::eval_grad_f(Index n, const Number* x, bool new_x, |
| 419 |
Number* grad_f) |
| 420 |
{ |
| 421 |
grad_f[n-1] = 0; |
| 422 |
return tminlp_->eval_grad_f(n, x, new_x, grad_f); |
| 423 |
} |
| 424 |
|
| 425 |
bool TMINLP2TNLP::eval_g(Index n, const Number* x, bool new_x, |
| 426 |
Index m, Number* g) |
| 427 |
{ |
| 428 |
int return_code = tminlp_->eval_g(n, x, new_x, m, g); |
| 429 |
return return_code; |
| 430 |
} |
| 431 |
|
| 432 |
bool TMINLP2TNLP::eval_jac_g(Index n, const Number* x, bool new_x, |
| 433 |
Index m, Index nele_jac, Index* iRow, |
| 434 |
Index *jCol, Number* values) |
| 435 |
{ |
| 436 |
bool return_code = |
| 437 |
tminlp_->eval_jac_g(n, x, new_x, m, nele_jac, |
| 438 |
iRow, jCol, values); |
| 439 |
if(iRow != NULL){ |
| 440 |
Index buf; |
| 441 |
for(Index k = 0; k < nele_jac ; k++){ |
| 442 |
buf = iRow[k]; |
| 443 |
iRow[k] = -1; |
| 444 |
iRow[k] = buf; |
| 445 |
} |
| 446 |
} |
| 447 |
return return_code; |
| 448 |
} |
| 449 |
|
| 450 |
bool TMINLP2TNLP::eval_h(Index n, const Number* x, bool new_x, |
| 451 |
Number obj_factor, Index m, const Number* lambda, |
| 452 |
bool new_lambda, Index nele_hess, |
| 453 |
Index* iRow, Index* jCol, Number* values) |
| 454 |
{ |
| 455 |
return tminlp_->eval_h(n, x, new_x, obj_factor, m, lambda, |
| 456 |
new_lambda, nele_hess, |
| 457 |
iRow, jCol, values); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
bool TMINLP2TNLP::eval_gi(Index n, const Number* x, bool new_x, |
| 462 |
Index i, Number& gi) |
| 463 |
{ |
| 464 |
return tminlp_->eval_gi(n, x, new_x, i, gi); |
| 465 |
} |
| 466 |
|
| 467 |
bool TMINLP2TNLP::eval_grad_gi(Index n, const Number* x, bool new_x, |
| 468 |
Index i, Index& nele_grad_gi, Index* jCol, |
| 469 |
Number* values) |
| 470 |
{ |
| 471 |
return tminlp_->eval_grad_gi(n, x, new_x, i, nele_grad_gi, jCol, values); |
| 472 |
} |
| 473 |
|
| 474 |
void TMINLP2TNLP::finalize_solution(SolverReturn status, |
| 475 |
Index n, const Number* x, const Number* z_L, const Number* z_U, |
| 476 |
Index m, const Number* g, const Number* lambda, |
| 477 |
Number obj_value, |
| 478 |
const IpoptData* ip_data, |
| 479 |
IpoptCalculatedQuantities* ip_cq) |
| 480 |
{ |
| 481 |
assert(n == (Index) num_variables()); |
| 482 |
assert(m == (Index) num_constraints()); |
| 483 |
x_sol_.resize(n); |
| 484 |
|
| 485 |
IpBlasDcopy(n, x, 1, x_sol_(), 1); |
| 486 |
|
| 487 |
if(m > 0){ |
| 488 |
g_sol_.resize(m); |
| 489 |
IpBlasDcopy(m, g, 1, g_sol_(), 1); |
| 490 |
} |
| 491 |
duals_sol_.resize(m + 2*n); |
| 492 |
if(lambda){ |
| 493 |
if(m > 0) |
| 494 |
IpBlasDcopy(m, lambda, 1, duals_sol_() + 2*n, 1); |
| 495 |
|
| 496 |
IpBlasDcopy(n, z_L, 1 , duals_sol_() , 1); |
| 497 |
IpBlasDcopy(n, z_U, 1 , duals_sol_() + n, 1); |
| 498 |
} |
| 499 |
|
| 500 |
return_status_ = status; |
| 501 |
obj_value_ = obj_value; |
| 502 |
|
| 503 |
if(status == Ipopt::LOCAL_INFEASIBILITY && ip_cq != NULL){ |
| 504 |
obj_value_ = ip_cq->curr_nlp_constraint_violation(NORM_MAX); |
| 505 |
} |
| 506 |
if (IsValid(curr_warm_starter_)) { |
| 507 |
curr_warm_starter_->Finalize(); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
bool TMINLP2TNLP::intermediate_callback(AlgorithmMode mode, |
| 513 |
Index iter, Number obj_value, |
| 514 |
Number inf_pr, Number inf_du, |
| 515 |
Number mu, Number d_norm, |
| 516 |
Number regularization_size, |
| 517 |
Number alpha_du, Number alpha_pr, |
| 518 |
Index ls_trials, |
| 519 |
const IpoptData* ip_data, |
| 520 |
IpoptCalculatedQuantities* ip_cq) |
| 521 |
{ |
| 522 |
if (BonminAbortAll) return false; |
| 523 |
#if WARM_STARTER |
| 524 |
// If we don't have this swtiched on, we assume that also the |
| 525 |
// "warm_start" option for bonmin is set not to refer to the |
| 526 |
// interior warm start object |
| 527 |
if (!warm_start_entire_iterate_) { |
| 528 |
return true; |
| 529 |
} |
| 530 |
if (need_new_warm_starter_) { |
| 531 |
// Create a new object for later warm start information |
| 532 |
curr_warm_starter_ = new IpoptInteriorWarmStarter(n_, x_l_, x_u_, |
| 533 |
nlp_lower_bound_inf_, |
| 534 |
nlp_upper_bound_inf_, |
| 535 |
warm_start_entire_iterate_); |
| 536 |
need_new_warm_starter_ = false; |
| 537 |
} |
| 538 |
|
| 539 |
return curr_warm_starter_->UpdateStoredIterates(mode, *ip_data, *ip_cq); |
| 540 |
#else |
| 541 |
return true; |
| 542 |
#endif |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
/** Procedure to ouptut relevant informations to reproduce a sub-problem. |
| 547 |
Compare the current problem to the problem to solve |
| 548 |
and writes files with bounds which have changed and current starting point. |
| 549 |
|
| 550 |
*/ |
| 551 |
void |
| 552 |
TMINLP2TNLP::outputDiffs(const std::string& probName, const std::string * varNames) |
| 553 |
{ |
| 554 |
const int &numcols = num_variables(); |
| 555 |
const int &numrows = num_constraints(); |
| 556 |
|
| 557 |
const double * currentLower = x_l(); |
| 558 |
const double * currentUpper = x_u(); |
| 559 |
|
| 560 |
const double * originalLower = orig_x_l(); |
| 561 |
const double * originalUpper = orig_x_u(); |
| 562 |
CoinRelFltEq eq; |
| 563 |
std::string fBoundsName = probName; |
| 564 |
std::ostringstream os; |
| 565 |
fBoundsName+=".bounds"; |
| 566 |
std::string fModName = probName; |
| 567 |
fModName+= ".mod"; |
| 568 |
std::ofstream fBounds; |
| 569 |
std::ofstream fMod; |
| 570 |
bool hasVarNames = 0; |
| 571 |
|
| 572 |
if(varNames!=NULL ) |
| 573 |
hasVarNames=1; |
| 574 |
if(hasVarNames) |
| 575 |
fMod.open(fModName.c_str()); |
| 576 |
fBounds.open(fBoundsName.c_str()); |
| 577 |
|
| 578 |
for(int i = 0 ; i < numcols ; i++) { |
| 579 |
if(!eq(currentLower[i],originalLower[i])) { |
| 580 |
if(hasVarNames) |
| 581 |
fMod<<"bounds"<<i<<": " |
| 582 |
<<varNames[i]<<" >= " |
| 583 |
<<currentLower[i]<<";\n"; |
| 584 |
|
| 585 |
|
| 586 |
fBounds<<"LO"<<"\t"<<i<<"\t"<<currentLower[i]<<std::endl; |
| 587 |
} |
| 588 |
if(!eq(currentUpper[i],originalUpper[i])) { |
| 589 |
if(hasVarNames) |
| 590 |
fMod<<"bounds"<<i<<": " |
| 591 |
<<varNames[i]<<" <= " |
| 592 |
<<currentUpper[i]<<";\n"; |
| 593 |
|
| 594 |
fBounds<<"UP"<<"\t"<<i<<"\t"<<currentUpper[i]<<std::endl; |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
//write a file with starting point |
| 599 |
std::string fStartPointName=probName; |
| 600 |
fStartPointName+=".start"; |
| 601 |
|
| 602 |
std::ofstream fStartPoint(fStartPointName.c_str()); |
| 603 |
const double * primals = x_init(); |
| 604 |
const double * duals = duals_init(); |
| 605 |
fStartPoint.precision(17); |
| 606 |
fStartPoint<<numcols<<"\t"<<2*numcols+numrows<<std::endl; |
| 607 |
for(int i = 0 ; i < numcols ; i++) |
| 608 |
fStartPoint<<primals[i]<<std::endl; |
| 609 |
int end = 2*numcols + numrows; |
| 610 |
if(duals) { |
| 611 |
for(int i = 0 ; i < end; i++) |
| 612 |
fStartPoint<<duals[i]<<std::endl; |
| 613 |
} |
| 614 |
|
| 615 |
} |
| 616 |
|
| 617 |
/** force solution to be fractionnal.*/ |
| 618 |
void |
| 619 |
TMINLP2TNLP::force_fractionnal_sol() |
| 620 |
{ |
| 621 |
for(int i=0 ; i < num_variables() ; i++) { |
| 622 |
if( ( var_types_[i] == TMINLP::INTEGER || |
| 623 |
var_types_[i] == TMINLP::BINARY )&& |
| 624 |
x_l_[i] < x_u_[i] + 0.5)//not fixed |
| 625 |
{ |
| 626 |
x_sol_[i] = ceil(x_l_[i]) + 0.5;//make it integer infeasible |
| 627 |
} |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
bool |
| 632 |
TMINLP2TNLP::get_scaling_parameters(Number& obj_scaling, |
| 633 |
bool& use_x_scaling, Index n, |
| 634 |
Number* x_scaling, |
| 635 |
bool& use_g_scaling, Index m, |
| 636 |
Number* g_scaling) |
| 637 |
{ |
| 638 |
return tminlp_->get_scaling_parameters(obj_scaling, use_x_scaling, n, |
| 639 |
x_scaling, |
| 640 |
use_g_scaling, m, g_scaling); |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
/** Method called to check wether a problem has still some variable not fixed. If there are no more |
| 645 |
unfixed vars, checks wether the solution given by the bounds is feasible.*/ |
| 646 |
|
| 647 |
/** @name Methods for setting and getting the warm starter */ |
| 648 |
void |
| 649 |
TMINLP2TNLP::SetWarmStarter(SmartPtr<IpoptInteriorWarmStarter> warm_starter) |
| 650 |
{ |
| 651 |
curr_warm_starter_ = warm_starter; |
| 652 |
} |
| 653 |
SmartPtr<IpoptInteriorWarmStarter> |
| 654 |
TMINLP2TNLP::GetWarmStarter() |
| 655 |
{ |
| 656 |
return curr_warm_starter_; |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
/** Evaluate the upper bounding function at given point and store the result.*/ |
| 661 |
double |
| 662 |
TMINLP2TNLP::evaluateUpperBoundingFunction(const double * x){ |
| 663 |
Number help; |
| 664 |
tminlp_->eval_upper_bound_f(num_variables(), x, help); |
| 665 |
return help; |
| 666 |
} |
| 667 |
|
| 668 |
double |
| 669 |
TMINLP2TNLP::check_solution(OsiObject ** objects, int nObjects){ |
| 670 |
assert(x_sol_.size() == num_variables()); |
| 671 |
assert(g_sol_.size() == num_constraints()); |
| 672 |
if (objects) { |
| 673 |
for (int i = 0 ; i < nObjects ; i++) { |
| 674 |
OsiSimpleInteger * obj = dynamic_cast<OsiSimpleInteger *>(objects[i]); |
| 675 |
if(obj){ |
| 676 |
int colNumber = obj->columnNumber(); |
| 677 |
x_sol_[colNumber] = floor(x_sol_[colNumber]+0.5); |
| 678 |
} |
| 679 |
} |
| 680 |
} |
| 681 |
else { |
| 682 |
for (unsigned int i = 0; i < x_sol_.size() ; i++) { |
| 683 |
if (var_types_[i] == TMINLP::INTEGER || var_types_[i] == TMINLP::BINARY) { |
| 684 |
x_sol_[i] = floor(x_sol_[i]+0.5); |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
eval_g((int)x_sol_.size(), x_sol_(), true, (int)g_sol_.size(), g_sol_()); |
| 689 |
eval_f((int)x_sol_.size(), x_sol_(), false, obj_value_); |
| 690 |
double error = 0; |
| 691 |
for(unsigned int i = 0 ; i < g_sol_.size() ; i++){ |
| 692 |
error = std::max(error, std::max(0., g_l_[i] - g_sol_[i])); |
| 693 |
error = std::max(error, std::max(0., - g_u_[i] + g_sol_[i])); |
| 694 |
} |
| 695 |
return error; |
| 696 |
} |
| 697 |
|
| 698 |
}// namespace Bonmin |
| 699 |
|