1 //                                               -*- C++ -*-
2 /**
3  *  @brief BonminProblem implements the interface between OT and Bonmin for
4  * optimization problems. It is derived from Bonmin::TMINLP to ensure
5  * compatibility with Bonmin algorithms.
6  *
7  *  Copyright 2005-2021 Airbus-EDF-IMACS-ONERA-Phimeca
8  *
9  *  This library is free software: you can redistribute it and/or modify
10  *  it under the terms of the GNU Lesser General Public License as published by
11  *  the Free Software Foundation, either version 3 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This library is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU Lesser General Public License for more details.
18  *
19  *  You should have received a copy of the GNU Lesser General Public License
20  *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23 #ifndef OPENTURNS_BONMINPROBLEM_HXX
24 #define OPENTURNS_BONMINPROBLEM_HXX
25 
26 #include "openturns/OTprivate.hxx"
27 #include "openturns/OptimizationAlgorithmImplementation.hxx"
28 
29 #include <BonTMINLP.hpp>
30 #include <IpTNLP.hpp>
31 
32 BEGIN_NAMESPACE_OPENTURNS
33 
34 typedef ::Bonmin::TMINLP::VariableType * VariableTypeTable;
35 typedef ::Ipopt::TNLP::LinearityType * LinearityTypeTable;
36 
37 
38 class BonminProblem
39   : public ::Bonmin::TMINLP // Namespace to be confirmed
40 {
41 
42 public:
43 
44   /** Constructor with parameters */
45   BonminProblem( const OptimizationProblem & optimProblem,
46                  const Point & startingPoint,
47                  const UnsignedInteger maximumEvaluationNumber
48                );
49 
50   /** Retrieving objective function input.output history */
51   Sample getInputHistory() const;
52   Sample getOutputHistory() const;
53 
54   /** Overloading functions from Bonmin::TMINLP
55    See BonTMINLP.hpp for description */
56 
57   // n: number of variables
58   // m: number of constraints
59 
60   bool get_nlp_info(int & n,
61                     int & m,
62                     int & nnz_jac_g, // Number of non-zero components in the Jacobian of g
63                     int & nnz_h_lag, // Number of non-zero components in Hessian of Lagrangean
64                     ::Ipopt::TNLP::IndexStyleEnum & index_style);
65 
66   bool get_variables_types( int n,
67                             VariableTypeTable var_types);
68 
69   bool get_variables_linearity( int n,
70                                 LinearityTypeTable var_types);
71 
72   bool get_constraints_linearity( int m,
73                                   LinearityTypeTable const_types);
74 
75   bool get_bounds_info( int n,
76                         double* x_l,  // Lower bounds
77                         double* x_u,  // Upper bounds
78                         int m,
79                         double* g_l,  // Lower bounds
80                         double* g_u); // Upper bounds
81 
82   bool get_starting_point(int n,
83                           bool init_x,
84                           double* x,
85                           bool init_z,
86                           double* z_L,
87                           double* z_U,
88                           int m,
89                           bool init_lambda,
90                           double* lambda);
91 
92   bool eval_f(int n,
93               const double* x,
94               bool new_x,
95               double& obj_value);
96 
97   bool eval_grad_f( int n,
98                     const double* x,
99                     bool new_x,
100                     double* grad_f);
101 
102   bool eval_g(int n,
103               const double* x,
104               bool new_x,
105               int m,
106               double* g);
107 
108   bool eval_jac_g(int n,
109                   const double* x,
110                   bool new_x,
111                   int m,
112                   int nele_jac,
113                   int* iRow,
114                   int *jCol,
115                   double* values);
116 
117   bool eval_h(int n,
118               const double* x,
119               bool new_x,
120               double obj_factor,
121               int m,
122               const double* lambda,
123               bool new_lambda,
124               int nele_hess,
125               int* iRow,
126               int* jCol,
127               double* values);
128 
129   bool eval_gi(int n,
130                const double* x,
131                bool new_x,
132                int i,
133                double& gi);
134 
135   bool eval_grad_gi(int n,
136                     const double* x,
137                     bool new_x,
138                     int i,
139                     int& nele_grad_gi,
140                     int* jCol,
141                     double* values);
142 
143   void finalize_solution( ::Bonmin::TMINLP::SolverReturn status,
144                           ::Ipopt::Index n,
145                           const ::Ipopt::Number* x,
146                           ::Ipopt::Number obj_value);
147 
branchingInfo() const148   const ::Bonmin::TMINLP::BranchingInfo * branchingInfo() const
149   {
150     return NULL;
151   };
152 
sosConstraints() const153   const ::Bonmin::TMINLP::SosInfo * sosConstraints() const
154   {
155     return NULL;
156   };
157 
getOptimalPoint() const158   Point getOptimalPoint() const
159   {
160     return optimalPoint_;
161   }
162 
getOptimalValue() const163   Point getOptimalValue() const
164   {
165     return optimalValue_;
166   }
167 
setProgressCallback(OptimizationAlgorithmImplementation::ProgressCallback callBack,void * state=0)168   virtual void setProgressCallback(OptimizationAlgorithmImplementation::ProgressCallback callBack, void * state = 0)
169   {
170     progressCallback_ = std::pair<OptimizationAlgorithmImplementation::ProgressCallback, void *>(callBack, state);
171   }
172 
setStopCallback(OptimizationAlgorithmImplementation::StopCallback callBack,void * state=0)173   virtual void setStopCallback(OptimizationAlgorithmImplementation::StopCallback callBack, void * state = 0)
174   {
175     stopCallback_ = std::pair<OptimizationAlgorithmImplementation::StopCallback, void *>(callBack, state);
176   }
177 
178 private:
179   const OptimizationProblem optimProblem_;
180   const Point startingPoint_;
181   Sample evaluationInputHistory_;
182   Sample evaluationOutputHistory_;
183   Point optimalPoint_;
184   Point optimalValue_;
185   // Callbacks
186   UnsignedInteger maximumEvaluationNumber_;
187   std::pair< OptimizationAlgorithmImplementation::ProgressCallback, void *> progressCallback_;
188   std::pair< OptimizationAlgorithmImplementation::StopCallback, void *> stopCallback_;
189 
190 
191 };
192 
193 END_NAMESPACE_OPENTURNS
194 
195 #endif
196