BonTMINLPLinObj.cpp
Go to the documentation of this file.
1 // (C) Copyright International Business Machines Corporation 2007
2 // All Rights Reserved.
3 //
4 // Authors :
5 // Pierre Bonami, International Business Machines Corporation
6 //
7 // Date : 08/16/2007
8 
9 #include "BonTMINLPLinObj.hpp"
10 
11 using namespace Ipopt;
12 
13 namespace Bonmin{
15  TMINLPLinObj::TMINLPLinObj():
16  tminlp_(NULL), m_(0), n_(0),
17  nnz_jac_(0)
18  {}
19 
20  void
22  tminlp_ = NULL;
23  }
24 
25 
28 
29 
30  void
33  tminlp_ = tminlp;
34  int n,m, nnz_jac, nnz_h;
35  Ipopt::TNLP::IndexStyleEnum index_style;
36  tminlp_->get_nlp_info(n, m , nnz_jac, nnz_h, index_style);
37  offset_ = index_style == Ipopt::TNLP::FORTRAN_STYLE;
38  n_ = n+1;
39  m_ = m+1;
40  nnz_jac_ = nnz_jac + n + 1;
41  }
42 
43  bool
44  TMINLPLinObj::get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
45  Index& nnz_h_lag,
46  Ipopt::TNLP::IndexStyleEnum& index_style){
47  assert(IsValid(tminlp_));
48 
49  bool return_value =
50  tminlp_->get_nlp_info(n,m,nnz_jac_g, nnz_h_lag,index_style);
51  m = m_;
52  n = n_;
53  nnz_jac_g = nnz_jac_;
54  return return_value;
55  }
56 
57 
58  bool
60  bool& use_x_scaling, Index n,
61  Number* x_scaling,
62  bool& use_g_scaling, Index m,
63  Number* g_scaling){
64  assert(IsValid(tminlp_));
65  assert(m == m_);
66 
67 
68  if(g_scaling && use_g_scaling)
69  g_scaling[0] = 1.;
70  if(x_scaling && use_x_scaling)
71  x_scaling[n - 1] = 1.;
72  obj_scaling = 1.;
73  double dummy = 1.;
74  double * mod_obj_scaling = &dummy;
75  if(use_g_scaling && g_scaling){
76  mod_obj_scaling = g_scaling;}
77  return tminlp_->get_scaling_parameters(*mod_obj_scaling, use_x_scaling, n - 1, x_scaling,
78  use_g_scaling, m - 1, g_scaling + 1);
79  }
80 
81 
82 
83 
84 
85  bool
87  Ipopt::TNLP::LinearityType* const_types){
88  assert(IsValid(tminlp_));
89  assert(m == m_ );
90  const_types[0] = Ipopt::TNLP::NON_LINEAR;
91  return tminlp_->get_constraints_linearity(m-1, const_types +1);}
92 
93  bool
94  TMINLPLinObj::get_bounds_info(Index n, Number* x_l, Number* x_u,
95  Index m, Number* g_l, Number* g_u){
96  assert(IsValid(tminlp_));
97  assert(m == m_);
98  assert(n == n_);
99 
100  x_l[n-1] = -DBL_MAX;
101  x_u[n-1] = DBL_MAX;
102 
103  g_l[0] = -DBL_MAX;
104  g_u[0] = 0.;
105  return tminlp_->get_bounds_info(n - 1, x_l, x_u, m_ - 1,
106  g_l +1, g_u + 1);
107  }
108 
109  bool
110  TMINLPLinObj::get_starting_point(Index n, bool init_x, Number* x,
111  bool init_z, Number* z_L, Number* z_U,
112  Index m, bool init_lambda,
113  Number* lambda){
114  assert(IsValid(tminlp_));
115  assert(m == m_);
116 
117  bool return_value = tminlp_->get_starting_point(n - 1, init_x, x, init_z, z_L, z_U,
118  m - 1, init_lambda, lambda + 1);
119  tminlp_->eval_f(n-1, x, true, x[n-1]);
120  if(init_lambda && lambda != NULL)
121  lambda[0] = 0;
122  return return_value;
123  }
124 
125 
126  bool
127  TMINLPLinObj::eval_g(Index n, const Number* x, bool new_x,
128  Index m, Number* g){
129  assert(IsValid(tminlp_));
130  assert(m == m_);
131  assert(n == n_);
132 
133  bool ret_val = tminlp_->eval_f(n - 1, x, new_x, g[0]);
134  g[0] -= x[n -1];
135  return ret_val && tminlp_->eval_g(n - 1, x, false, m - 1, g+1);
136  }
137 
138  bool
139  TMINLPLinObj::eval_jac_g(Index n, const Number* x, bool new_x,
140  Index m, Index nele_jac, Index* iRow,
141  Index *jCol, Number* values){
142  assert(IsValid(tminlp_));
143  assert(m == m_);
144  assert(n == n_);
145  assert(nele_jac == nnz_jac_);
146  bool ret_val = true;
147  if(values == NULL)
148  {
149  for(int i = 0 ; i < n_ ; i++){
150  iRow[i] = offset_; jCol[i] = i + offset_;}
151  bool ret_val = tminlp_->eval_jac_g(n -1, x, new_x, m_ -1, nnz_jac_ - n_, iRow + n_, jCol + n_, NULL);
152  for(int i = n_ ; i < nnz_jac_ ; i++){//shift by 1
153  iRow[i]++;}
154  return ret_val;
155  }
156  else {
157  ret_val &= tminlp_->eval_grad_f(n-1, x, new_x, values);
158  values[n-1] = -1;
159  ret_val &= tminlp_->eval_jac_g(n - 1, x, false, m - 1, nele_jac - n_, NULL, NULL, values + n);
160  }
161  return ret_val;
162  }
163 
164  bool
165  TMINLPLinObj::eval_h(Index n, const Number* x, bool new_x,
166  Number obj_factor, Index m, const Number* lambda,
167  bool new_lambda, Index nele_hess,
168  Index* iRow, Index* jCol, Number* values){
169  assert(IsValid(tminlp_));
170  assert(m == m_);
171  assert(n == n_);
172  return tminlp_->eval_h(n_ - 1, x, new_x, (lambda != NULL)? lambda[0]: 1., m_ - 1, (lambda != NULL)? lambda + 1: NULL, new_lambda,
173  nele_hess, iRow, jCol, values);
174  }
175 
176 
177  bool
178  TMINLPLinObj:: eval_gi(Index n, const Number* x, bool new_x,
179  Index i, Number& gi){
180  assert(IsValid(tminlp_));
181  assert(i < m_);
182  assert(n == n_);
183  if(i == 0){
184  bool ret_val = tminlp_->eval_f(n-1, x, new_x, gi);
185  gi -= x[n -1];
186  return ret_val;}
187  else
188  return tminlp_->eval_gi(n-1, x, new_x, i - 1, gi);
189  }
190 
191  bool
192  TMINLPLinObj::eval_grad_gi(Index n, const Number* x, bool new_x,
193  Index i, Index& nele_grad_gi, Index* jCol,
194  Number* values){
195  assert(IsValid(tminlp_));
196  assert(i < m_);
197  assert(n == n_);
198  if(i == 0){
199  if(jCol != NULL){
200  for(int i = 0 ; i < n ; i++) jCol[i] = i + offset_;
201  }
202  bool ret_val= tminlp_->eval_grad_f(n-1, x, new_x, values);
203  values[n-1] = -1;
204  return ret_val;}
205  else
206  return tminlp_->eval_grad_gi(n - 1, x, new_x, i - 1, nele_grad_gi,
207  jCol, values);
208  }
209 
210 
211 }/* Ends namespace Bonmin.*/
int nnz_jac_
number of non-zeroes in the jacobian of the transformed MINLP.
double * values
virtual bool eval_h(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Number obj_factor, Ipopt::Index m, const Ipopt::Number *lambda, bool new_lambda, Ipopt::Index nele_hess, Ipopt::Index *iRow, Ipopt::Index *jCol, Ipopt::Number *values)
Return the hessian of the lagrangian.
Ipopt::SmartPtr< TMINLP > tminlp()
return pointer to tminlp_.
int n_
Ipopt::Number of variables in the transformed MINLP.
virtual bool get_constraints_linearity(Ipopt::Index m, Ipopt::TNLP::LinearityType *const_types)
Return the constraints linearity.
virtual bool get_scaling_parameters(Ipopt::Number &obj_scaling, bool &use_x_scaling, Ipopt::Index n, Ipopt::Number *x_scaling, bool &use_g_scaling, Ipopt::Index m, Ipopt::Number *g_scaling)
Return scaling parameters.
bool IsValid(const OSSmartPtr< U > &smart_ptr)
Definition: OSSmartPtr.hpp:465
int m_
Ipopt::Number of constraints in the transformed MINLP.
virtual bool eval_g(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Index m, Ipopt::Number *g)
Return the vector of constraint values.
void gutsOfDestructor()
Reset all data.
virtual bool eval_grad_gi(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Index i, Ipopt::Index &nele_grad_gi, Ipopt::Index *jCol, Ipopt::Number *values)
Compute the structure or values of the gradient for one constraint.
Ipopt::SmartPtr< TMINLP > tminlp_
Reference TMINLP which is to be relaxed.
virtual bool get_bounds_info(Ipopt::Index n, Ipopt::Number *x_l, Ipopt::Number *x_u, Ipopt::Index m, Ipopt::Number *g_l, Ipopt::Number *g_u)
Return the information about the bound on the variables and constraints.
virtual bool get_nlp_info(Ipopt::Index &n, Ipopt::Index &m, Ipopt::Index &nnz_jac_g, Ipopt::Index &nnz_h_lag, Ipopt::TNLP::IndexStyleEnum &index_style)
Return the number of variables and constraints, and the number of non-zeros in the jacobian and the h...
virtual bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number *x, bool init_z, Ipopt::Number *z_L, Ipopt::Number *z_U, Ipopt::Index m, bool init_lambda, Ipopt::Number *lambda)
Return the starting point.
void setTminlp(Ipopt::SmartPtr< TMINLP > tminlp)
set reference TMINLP
int offset_
offset for jacobian.
static fint nnz_h
void fint fint fint real fint real real real real real real * g
virtual bool eval_gi(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Index i, Ipopt::Number &gi)
Compute the value of a single constraint.
void fint * m
virtual bool eval_jac_g(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Index m, Ipopt::Index nele_jac, Ipopt::Index *iRow, Ipopt::Index *jCol, Ipopt::Number *values)
Return the jacobian of the constraints.
virtual ~TMINLPLinObj()
destructor.
void fint * n
void fint fint fint real fint real * x