| 1 |
// Copyright (C) 2007 International Business Machines and others. |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// $Id$ |
| 6 |
// |
| 7 |
// Author: Andreas Waechter IBM 2007-03-29 |
| 8 |
|
| 9 |
#ifndef __BONCUTSTRENGTHENER_HPP__ |
| 10 |
#define __BONCUTSTRENGTHENER_HPP__ |
| 11 |
|
| 12 |
#include "BonTMINLP.hpp" |
| 13 |
#include "CoinPackedVector.hpp" |
| 14 |
#include "BonTNLPSolver.hpp" |
| 15 |
|
| 16 |
namespace Bonmin |
| 17 |
{ |
| 18 |
enum CutStrengtheningType{ |
| 19 |
CS_None=0, |
| 20 |
CS_StrengthenedGlobal=1, |
| 21 |
CS_UnstrengthenedGlobal_StrengthenedLocal=2, |
| 22 |
CS_StrengthenedGlobal_StrengthenedLocal=3 |
| 23 |
}; |
| 24 |
|
| 25 |
enum DisjunctiveCutType{ |
| 26 |
DC_None=0, |
| 27 |
DC_MostFractional=1 |
| 28 |
}; |
| 29 |
|
| 30 |
/** Class for strengthening OA cuts, and generating additional ones. |
| 31 |
*/ |
| 32 |
class CutStrengthener: public Ipopt::ReferencedObject |
| 33 |
{ |
| 34 |
/** Class implementing the TNLP for strengthening one cut. We |
| 35 |
* assume that the cut has a lower bound. */ |
| 36 |
class StrengtheningTNLP: public Ipopt::TNLP { |
| 37 |
public: |
| 38 |
/** Contructor */ |
| 39 |
StrengtheningTNLP(Ipopt::SmartPtr<TMINLP> tminlp, |
| 40 |
const CoinPackedVector& cut, |
| 41 |
bool lower_bound, |
| 42 |
Ipopt::Index n, |
| 43 |
const Ipopt::Number* starting_point, |
| 44 |
const double* x_l_orig, |
| 45 |
const double* x_u_orig, |
| 46 |
Ipopt::Index constr_index, |
| 47 |
Ipopt::Index nvar_constr /** Ipopt::Number of variables in constraint */, |
| 48 |
const Ipopt::Index* jCol); |
| 49 |
|
| 50 |
/** Destructor */ |
| 51 |
~StrengtheningTNLP(); |
| 52 |
|
| 53 |
/**@name Overloaded from TNLP */ |
| 54 |
//@{ |
| 55 |
/** Method to return some info about the nlp */ |
| 56 |
virtual bool get_nlp_info(Ipopt::Index& n, Ipopt::Index& m, Ipopt::Index& nnz_jac_g, |
| 57 |
Ipopt::Index& nnz_h_lag, Ipopt::TNLP::IndexStyleEnum& index_style); |
| 58 |
|
| 59 |
/** Method to return the bounds for my problem */ |
| 60 |
virtual bool get_bounds_info(Ipopt::Index n, Ipopt::Number* x_l, Ipopt::Number* x_u, |
| 61 |
Ipopt::Index m, Ipopt::Number* g_l, Ipopt::Number* g_u); |
| 62 |
|
| 63 |
/** Method to return the starting point for the algorithm */ |
| 64 |
virtual bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number* x, |
| 65 |
bool init_z, Ipopt::Number* z_L, Ipopt::Number* z_U, |
| 66 |
Ipopt::Index m, bool init_lambda, |
| 67 |
Ipopt::Number* lambda); |
| 68 |
|
| 69 |
/** Method to return the objective value */ |
| 70 |
virtual bool eval_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x, Ipopt::Number& obj_value); |
| 71 |
|
| 72 |
/** Method to return the gradient of the objective */ |
| 73 |
virtual bool eval_grad_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x, Ipopt::Number* grad_f); |
| 74 |
|
| 75 |
/** Method to return the constraint residuals */ |
| 76 |
virtual bool eval_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x, Ipopt::Index m, Ipopt::Number* g); |
| 77 |
|
| 78 |
/** Method to return: |
| 79 |
* 1) The structure of the jacobian (if "values" is NULL) |
| 80 |
* 2) The values of the jacobian (if "values" is not NULL) |
| 81 |
*/ |
| 82 |
virtual bool eval_jac_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x, |
| 83 |
Ipopt::Index m, Ipopt::Index nele_jac, Ipopt::Index* iRow, Ipopt::Index *jCol, |
| 84 |
Ipopt::Number* values); |
| 85 |
|
| 86 |
/** Method to return: |
| 87 |
* 1) The structure of the hessian of the lagrangian (if "values" is NULL) |
| 88 |
* 2) The values of the hessian of the lagrangian (if "values" is not NULL) |
| 89 |
*/ |
| 90 |
virtual bool eval_h(Ipopt::Index n, const Ipopt::Number* x, bool new_x, |
| 91 |
Ipopt::Number obj_factor, Ipopt::Index m, const Ipopt::Number* lambda, |
| 92 |
bool new_lambda, Ipopt::Index nele_hess, Ipopt::Index* iRow, |
| 93 |
Ipopt::Index* jCol, Ipopt::Number* values); |
| 94 |
|
| 95 |
//@} |
| 96 |
/** @name Solution Methods */ |
| 97 |
//@{ |
| 98 |
/** This method is called when the algorithm is complete so the TNLP can store/write the solution */ |
| 99 |
virtual void finalize_solution(Ipopt::SolverReturn status, |
| 100 |
Ipopt::Index n, const Ipopt::Number* x, const Ipopt::Number* z_L, const Ipopt::Number* z_U, |
| 101 |
Ipopt::Index m, const Ipopt::Number* g, const Ipopt::Number* lambda, |
| 102 |
Ipopt::Number obj_value, |
| 103 |
const Ipopt::IpoptData* ip_data, |
| 104 |
Ipopt::IpoptCalculatedQuantities* ip_cq); |
| 105 |
//@} |
| 106 |
|
| 107 |
/** Method for asking for the strengthened bound. */ |
| 108 |
Ipopt::Number StrengthenedBound() const; |
| 109 |
|
| 110 |
private: |
| 111 |
/**@name Methods to block default compiler methods. */ |
| 112 |
//@{ |
| 113 |
StrengtheningTNLP(); |
| 114 |
StrengtheningTNLP(const StrengtheningTNLP&); |
| 115 |
StrengtheningTNLP& operator=(const StrengtheningTNLP&); |
| 116 |
//@} |
| 117 |
|
| 118 |
/** TMINLP (with current bounds) for which the cut it to be |
| 119 |
* generated */ |
| 120 |
const Ipopt::SmartPtr<TMINLP> tminlp_; |
| 121 |
|
| 122 |
/** Gradient of the (linear) objective function */ |
| 123 |
Ipopt::Number* obj_grad_; |
| 124 |
|
| 125 |
/** Dimension of original problem */ |
| 126 |
const Ipopt::Index n_orig_; |
| 127 |
|
| 128 |
/** Ipopt::Number of constraints in original problem */ |
| 129 |
Ipopt::Index m_orig_; |
| 130 |
|
| 131 |
/** Starting point */ |
| 132 |
Ipopt::Number* starting_point_; |
| 133 |
|
| 134 |
/** Full dimentional x which is used to call the TMINLP |
| 135 |
* evaluation routines */ |
| 136 |
Ipopt::Number* x_full_; |
| 137 |
|
| 138 |
/** Lower bounds for constraint variables */ |
| 139 |
Ipopt::Number* x_l_; |
| 140 |
|
| 141 |
/** Upper bounds for constraint variables */ |
| 142 |
Ipopt::Number* x_u_; |
| 143 |
|
| 144 |
/** Ipopt::Index of the constraint */ |
| 145 |
const Ipopt::Index constr_index_; |
| 146 |
|
| 147 |
/** Ipopt::Number of variables appearing in the constraint */ |
| 148 |
const Ipopt::Index nvar_constr_; |
| 149 |
|
| 150 |
/** List of variables appearing on the constraints */ |
| 151 |
Ipopt::Index* var_indices_; |
| 152 |
|
| 153 |
/** Flag indicating if the cut has a lower or upper bound */ |
| 154 |
bool lower_bound_; |
| 155 |
|
| 156 |
/** Flag indicating if we TNLP has been solved successfully */ |
| 157 |
bool have_final_bound_; |
| 158 |
|
| 159 |
/** Final strengthened bound */ |
| 160 |
Ipopt::Number strengthened_bound_; |
| 161 |
|
| 162 |
/** space for original gradient if objective function is handled */ |
| 163 |
Ipopt::Number* grad_f_; |
| 164 |
|
| 165 |
/** Auxilliary method for updating the full x variable */ |
| 166 |
void update_x_full(const Ipopt::Number *x); |
| 167 |
}; |
| 168 |
|
| 169 |
public: |
| 170 |
/** @name Constructor/Destructor */ |
| 171 |
//@{ |
| 172 |
/** Constructor. It is given a TNLP solver to solve the internal |
| 173 |
* NLPs. */ |
| 174 |
CutStrengthener(Ipopt::SmartPtr<TNLPSolver> tnlp_solver, |
| 175 |
Ipopt::SmartPtr<Ipopt::OptionsList> options); |
| 176 |
|
| 177 |
/** Destructor */ |
| 178 |
virtual ~CutStrengthener(); |
| 179 |
//@} |
| 180 |
|
| 181 |
/** Method for generating and strenghtening all desired cuts */ |
| 182 |
bool ComputeCuts(OsiCuts &cs, |
| 183 |
TMINLP* tminlp, |
| 184 |
TMINLP2TNLP* problem, |
| 185 |
const int gindex, CoinPackedVector& cut, |
| 186 |
double& cut_lb, double& cut_ub, |
| 187 |
const double g_val, const double g_lb, |
| 188 |
const double g_ub, |
| 189 |
int n, const double* x, |
| 190 |
double infty); |
| 191 |
|
| 192 |
private: |
| 193 |
/**@name Default Compiler Generated Methods |
| 194 |
* (Hidden to avoid implicit creation/calling). |
| 195 |
* These methods are not implemented and |
| 196 |
* we do not want the compiler to implement |
| 197 |
* them for us, so we declare them private |
| 198 |
* and do not define them. This ensures that |
| 199 |
* they will not be implicitly created/called. */ |
| 200 |
//@{ |
| 201 |
/** Default Constructor */ |
| 202 |
CutStrengthener(); |
| 203 |
|
| 204 |
/** Copy Constructor */ |
| 205 |
CutStrengthener(const CutStrengthener&); |
| 206 |
|
| 207 |
/** Overloaded Equals Operator */ |
| 208 |
void operator=(const CutStrengthener&); |
| 209 |
//@} |
| 210 |
|
| 211 |
/** Method for strengthening one cut. */ |
| 212 |
bool StrengthenCut(Ipopt::SmartPtr<TMINLP> tminlp /** current TMINLP */, |
| 213 |
int constr_index /** Ipopt::Index number of the constraint to be strengthened, -1 means objective function */, |
| 214 |
const CoinPackedVector& row /** Cut to be strengthened */, |
| 215 |
int n /** Ipopt::Number of variables */, |
| 216 |
const double* x /** solution from node */, |
| 217 |
const double* x_l /** Lower bounds for x in which should be valid. */, |
| 218 |
const double* x_u /** Upper bounds for x in which should be valid. */, |
| 219 |
double& lb, |
| 220 |
double& ub); |
| 221 |
|
| 222 |
/** Method for generating one type of cut (strengthened or disjunctive) */ |
| 223 |
bool HandleOneCut(bool is_tight, TMINLP* tminlp, |
| 224 |
TMINLP2TNLP* problem, |
| 225 |
const double* minlp_lb, |
| 226 |
const double* minlp_ub, |
| 227 |
const int gindex, CoinPackedVector& cut, |
| 228 |
double& cut_lb, double& cut_ub, |
| 229 |
int n, const double* x, |
| 230 |
double infty); |
| 231 |
|
| 232 |
/** Object for solving the TNLPs */ |
| 233 |
Ipopt::SmartPtr<TNLPSolver> tnlp_solver_; |
| 234 |
|
| 235 |
/** Type of OA cut strengthener */ |
| 236 |
int cut_strengthening_type_; |
| 237 |
/** What kind of disjuntion should be done */ |
| 238 |
int disjunctive_cut_type_; |
| 239 |
/** verbosity level for OA-related output */ |
| 240 |
int oa_log_level_; |
| 241 |
}; |
| 242 |
|
| 243 |
} // namespace Ipopt |
| 244 |
#endif |