| 1 |
// Copyright (C) 2004, International Business Machines and others. |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// |
| 6 |
// Authors: Pierre Bonami 06/10/2005 |
| 7 |
|
| 8 |
#ifndef _TNLP2FPNLP_HPP_ |
| 9 |
#define _TNLP2FPNLP_HPP_ |
| 10 |
|
| 11 |
#include "IpTNLP.hpp" |
| 12 |
#include "BonTMINLP.hpp" |
| 13 |
#include "IpSmartPtr.hpp" |
| 14 |
#include "BonTypes.hpp" |
| 15 |
|
| 16 |
namespace Bonmin |
| 17 |
{ |
| 18 |
/** This is an adapter class to convert an NLP to a Feasibility Pump NLP |
| 19 |
* by changing the objective function to the (2-norm) distance to a point. |
| 20 |
* The extra function is set_dist_to_point_obj(size_t n, const double *, const int *) |
| 21 |
*/ |
| 22 |
class TNLP2FPNLP : public Ipopt::TNLP |
| 23 |
{ |
| 24 |
public: |
| 25 |
/**@name Constructors/Destructors */ |
| 26 |
//@{ |
| 27 |
/** Build using tnlp as source problem.*/ |
| 28 |
TNLP2FPNLP(const Ipopt::SmartPtr<Ipopt::TNLP> tnlp, double objectiveScalingFactor = 100); |
| 29 |
|
| 30 |
/** Build using tnlp as source problem and using other for all other parameters..*/ |
| 31 |
TNLP2FPNLP(const Ipopt::SmartPtr<TNLP> tnlp, const Ipopt::SmartPtr<TNLP2FPNLP> other); |
| 32 |
|
| 33 |
/** Default destructor */ |
| 34 |
virtual ~TNLP2FPNLP(); |
| 35 |
//@} |
| 36 |
void use(Ipopt::SmartPtr<TNLP> tnlp){ |
| 37 |
tnlp_ = GetRawPtr(tnlp);} |
| 38 |
/**@name Methods to select the objective function and extra constraints*/ |
| 39 |
//@{ |
| 40 |
/// Flag to indicate that we want to use the feasibility pump objective |
| 41 |
void set_use_feasibility_pump_objective(bool use_feasibility_pump_objective) |
| 42 |
{ use_feasibility_pump_objective_ = use_feasibility_pump_objective; } |
| 43 |
|
| 44 |
/** Flag to indicate that we want to use a cutoff constraint |
| 45 |
* This constraint has the form f(x) <= (1-epsilon) f(x') */ |
| 46 |
void set_use_cutoff_constraint(bool use_cutoff_constraint) |
| 47 |
{ use_cutoff_constraint_ = use_cutoff_constraint; } |
| 48 |
|
| 49 |
/// Flag to indicate that we want to use a local branching constraint |
| 50 |
void set_use_local_branching_constraint(bool use_local_branching_constraint) |
| 51 |
{ use_local_branching_constraint_ = use_local_branching_constraint; } |
| 52 |
//@} |
| 53 |
|
| 54 |
/**@name Methods to provide the rhs of the extra constraints*/ |
| 55 |
//@{ |
| 56 |
/// Set the cutoff value to use in the cutoff constraint |
| 57 |
void set_cutoff(Ipopt::Number cutoff); |
| 58 |
|
| 59 |
/// Set the rhs of the local branching constraint |
| 60 |
void set_rhs_local_branching_constraint(double rhs_local_branching_constraint) |
| 61 |
{ assert(rhs_local_branching_constraint >= 0); |
| 62 |
rhs_local_branching_constraint_ = rhs_local_branching_constraint; } |
| 63 |
//@} |
| 64 |
|
| 65 |
/**@name Methods to change the objective function*/ |
| 66 |
//@{ |
| 67 |
/** \brief Set the point to which distance is minimized. |
| 68 |
* The distance is minimize in a subspace define by a subset of coordinates |
| 69 |
* \param n number of coordinates on which distance is minimized |
| 70 |
* \param inds indices of the coordinates on which distance is minimized |
| 71 |
* \param vals values of the point for coordinates in ind |
| 72 |
*/ |
| 73 |
void set_dist_to_point_obj(size_t n, const Ipopt::Number * vals, const Ipopt::Index * inds); |
| 74 |
|
| 75 |
/** Set the value for sigma */ |
| 76 |
void setSigma(double sigma){ |
| 77 |
assert(sigma >= 0.); |
| 78 |
sigma_ = sigma;} |
| 79 |
/** Set the value for lambda*/ |
| 80 |
void setLambda(double lambda){ |
| 81 |
assert(lambda >= 0. && lambda <= 1.); |
| 82 |
lambda_ = lambda;} |
| 83 |
/** Set the value for simgma */ |
| 84 |
void setNorm(int norm){ |
| 85 |
assert(norm >0 && norm < 3); |
| 86 |
norm_ = norm;} |
| 87 |
//@} |
| 88 |
|
| 89 |
/**@name methods to gather information about the NLP */ |
| 90 |
//@{ |
| 91 |
/** get info from nlp_ and add hessian information */ |
| 92 |
virtual bool get_nlp_info(Ipopt::Index& n, Ipopt::Index& m, Ipopt::Index& nnz_jac_g, |
| 93 |
Ipopt::Index& nnz_h_lag, Ipopt::TNLP::IndexStyleEnum& index_style); |
| 94 |
|
| 95 |
/** This call is just passed onto tnlp_ |
| 96 |
*/ |
| 97 |
virtual bool get_bounds_info(Ipopt::Index n, Ipopt::Number* x_l, Ipopt::Number* x_u, |
| 98 |
Ipopt::Index m, Ipopt::Number* g_l, Ipopt::Number* g_u); |
| 99 |
|
| 100 |
/** Passed onto tnlp_ |
| 101 |
*/ |
| 102 |
virtual bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number* x, |
| 103 |
bool init_z, Ipopt::Number* z_L, Ipopt::Number* z_U, |
| 104 |
Ipopt::Index m, bool init_lambda, |
| 105 |
Ipopt::Number* lambda) |
| 106 |
{ |
| 107 |
int m2 = m; |
| 108 |
if(use_cutoff_constraint_) { |
| 109 |
m2--; |
| 110 |
if(lambda!=NULL)lambda[m2] = 0; |
| 111 |
} |
| 112 |
if(use_local_branching_constraint_) { |
| 113 |
m2--; |
| 114 |
if(lambda!= NULL)lambda[m2] = 0; |
| 115 |
} |
| 116 |
int ret_code = tnlp_->get_starting_point(n, init_x, x, |
| 117 |
init_z, z_L, z_U, m2, init_lambda, lambda); |
| 118 |
return ret_code; |
| 119 |
} |
| 120 |
|
| 121 |
/** overloaded to return the value of the objective function */ |
| 122 |
virtual bool eval_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x, |
| 123 |
Ipopt::Number& obj_value); |
| 124 |
|
| 125 |
/** overload this method to return the vector of the gradient of |
| 126 |
* the objective w.r.t. x */ |
| 127 |
virtual bool eval_grad_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x, |
| 128 |
Ipopt::Number* grad_f); |
| 129 |
|
| 130 |
/** overload to return the values of the left-hand side of the |
| 131 |
constraints */ |
| 132 |
virtual bool eval_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x, |
| 133 |
Ipopt::Index m, Ipopt::Number* g); |
| 134 |
|
| 135 |
/** overload to return the jacobian of g */ |
| 136 |
virtual bool eval_jac_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x, |
| 137 |
Ipopt::Index m, Ipopt::Index nele_jac, Ipopt::Index* iRow, |
| 138 |
Ipopt::Index *jCol, Ipopt::Number* values); |
| 139 |
|
| 140 |
/** Evaluate the modified Hessian of the Lagrangian*/ |
| 141 |
virtual bool eval_h(Ipopt::Index n, const Ipopt::Number* x, bool new_x, |
| 142 |
Ipopt::Number obj_factor, Ipopt::Index m, const Ipopt::Number* lambda, |
| 143 |
bool new_lambda, Ipopt::Index nele_hess, |
| 144 |
Ipopt::Index* iRow, Ipopt::Index* jCol, Ipopt::Number* values); |
| 145 |
//@} |
| 146 |
|
| 147 |
/** @name Solution Methods */ |
| 148 |
//@{ |
| 149 |
/** This method is called when the algorithm is complete so the TNLP can store/write the solution */ |
| 150 |
virtual void finalize_solution(Ipopt::SolverReturn status, |
| 151 |
Ipopt::Index n, const Ipopt::Number* x, const Ipopt::Number* z_L, const Ipopt::Number* z_U, |
| 152 |
Ipopt::Index m, const Ipopt::Number* g, const Ipopt::Number* lambda, |
| 153 |
Ipopt::Number obj_value, |
| 154 |
const Ipopt::IpoptData* ip_data, |
| 155 |
Ipopt::IpoptCalculatedQuantities* ip_cq); |
| 156 |
//@} |
| 157 |
|
| 158 |
virtual bool get_variables_linearity(Ipopt::Index n, LinearityType* var_types) |
| 159 |
{ |
| 160 |
return tnlp_->get_variables_linearity(n, var_types);; |
| 161 |
} |
| 162 |
|
| 163 |
/** overload this method to return the constraint linearity. |
| 164 |
* array should be alocated with length at least n. (default implementation |
| 165 |
* just return false and does not fill the array).*/ |
| 166 |
virtual bool get_constraints_linearity(Ipopt::Index m, LinearityType* const_types) |
| 167 |
{ |
| 168 |
int m2 = m; |
| 169 |
if(use_cutoff_constraint_) { |
| 170 |
m2--; |
| 171 |
const_types[m2] = Ipopt::TNLP::NON_LINEAR; |
| 172 |
} |
| 173 |
if(use_local_branching_constraint_) { |
| 174 |
m2--; |
| 175 |
const_types[m2] = Ipopt::TNLP::LINEAR; |
| 176 |
} |
| 177 |
return tnlp_->get_constraints_linearity(m2, const_types); |
| 178 |
} |
| 179 |
/** @name Scaling of the objective function */ |
| 180 |
//@{ |
| 181 |
void setObjectiveScaling(double value) |
| 182 |
{ |
| 183 |
objectiveScalingFactor_ = value; |
| 184 |
} |
| 185 |
double getObjectiveScaling() const |
| 186 |
{ |
| 187 |
return objectiveScalingFactor_; |
| 188 |
} |
| 189 |
|
| 190 |
private: |
| 191 |
/** @name Internal methods to help compute the distance, its gradient and hessian */ |
| 192 |
//@{ |
| 193 |
/** Compute the norm-2 distance to the current point to which distance is minimized. */ |
| 194 |
double dist_to_point(const Ipopt::Number *x); |
| 195 |
//@} |
| 196 |
/**@name Default Compiler Generated Methods |
| 197 |
* (Hidden to avoid implicit creation/calling). |
| 198 |
* These methods are not implemented and |
| 199 |
* we do not want the compiler to implement |
| 200 |
* them for us, so we declare them private |
| 201 |
* and do not define them. This ensures that |
| 202 |
* they will not be implicitly created/called. */ |
| 203 |
//@{ |
| 204 |
/** Default Constructor */ |
| 205 |
TNLP2FPNLP(); |
| 206 |
|
| 207 |
/** Copy Constructor */ |
| 208 |
TNLP2FPNLP(const TNLP2FPNLP&); |
| 209 |
|
| 210 |
/** Overloaded Equals Operator */ |
| 211 |
void operator=(const TNLP2FPNLP&); |
| 212 |
//@} |
| 213 |
|
| 214 |
/** pointer to the tminlp that is being adapted */ |
| 215 |
Ipopt::SmartPtr<TNLP> tnlp_; |
| 216 |
|
| 217 |
/** @name Data for storing the point the distance to which is minimized */ |
| 218 |
//@{ |
| 219 |
/// Indices of the variables for which distance is minimized (i.e. indices of integer variables in a feasibility pump setting) |
| 220 |
vector<Ipopt::Index> inds_; |
| 221 |
/// Values of the point to which we separate (if x is the point vals_[i] should be x[inds_[i]] ) |
| 222 |
vector<Ipopt::Number> vals_; |
| 223 |
/** value for the convex combination to take between original objective and distance function. |
| 224 |
* ( take lambda_ * distance + (1-lambda) sigma f(x).*/ |
| 225 |
double lambda_; |
| 226 |
/** Scaling for the original objective.*/ |
| 227 |
double sigma_; |
| 228 |
/** Norm to use (L_1 or L_2).*/ |
| 229 |
int norm_; |
| 230 |
//@} |
| 231 |
|
| 232 |
/// Scaling factor for the objective |
| 233 |
double objectiveScalingFactor_; |
| 234 |
|
| 235 |
/**@name Flags to select the objective function and extra constraints*/ |
| 236 |
//@{ |
| 237 |
/// Flag to indicate that we want to use the feasibility pump objective |
| 238 |
bool use_feasibility_pump_objective_; |
| 239 |
|
| 240 |
/** Flag to indicate that we want to use a cutoff constraint |
| 241 |
* This constraint has the form f(x) <= (1-epsilon) f(x') */ |
| 242 |
bool use_cutoff_constraint_; |
| 243 |
|
| 244 |
/// Flag to indicate that we want to use a local branching constraint |
| 245 |
bool use_local_branching_constraint_; |
| 246 |
//@} |
| 247 |
|
| 248 |
/**@name Data for storing the rhs of the extra constraints*/ |
| 249 |
//@{ |
| 250 |
/// Value of best solution known |
| 251 |
double cutoff_; |
| 252 |
|
| 253 |
/// RHS of local branching constraint |
| 254 |
double rhs_local_branching_constraint_; |
| 255 |
//@} |
| 256 |
|
| 257 |
/// Ipopt::Index style (C++ or Fortran) |
| 258 |
Ipopt::TNLP::IndexStyleEnum index_style_; |
| 259 |
|
| 260 |
}; |
| 261 |
|
| 262 |
} // namespace Ipopt |
| 263 |
|
| 264 |
#endif /*_TNLP2FPNLP_HPP_*/ |