Debian Codegraph @

Bonmin/src/Interfaces/BonTMINLP2OsiLP.hpp

raw
1 // (C) Copyright International Business Machines Corporation 2007
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors :
6 // Pierre Bonami, International Business Machines Corporation
7 //
8 // Date : 10/16/2007
9 #ifndef BonminTMINLP2OsiLP_H
10 #define BonminTMINLP2OsiLP_H
11
12 #include <cmath>
13 #include <cstdio>
14 #include "IpSmartPtr.hpp"
15 #include "IpTNLP.hpp"
16 #include "BonTypes.hpp"
17
18 class OsiSolverInterface;
19 class OsiCuts;
20
21 namespace Bonmin {
22 class TMINLP2TNLP;
23 class BabSetupBase;
24
25 /** A transformer class to build outer approximations i.e. transfomrs nonlinear programs into linear programs.*/
26 class TMINLP2OsiLP: public Ipopt::ReferencedObject {
27
28 public:
29
30 /** Default constructor.*/
31 TMINLP2OsiLP():
32 tiny_(-0.),
33 very_tiny_(-0.)
34 {}
35
36 /** Copy constructor.*/
37 TMINLP2OsiLP(const TMINLP2OsiLP & other):
38 tiny_(other.tiny_),
39 very_tiny_(other.very_tiny_),
40 model_(other.model_){
41 }
42
43 /** virtual copy constructor*/
44 virtual TMINLP2OsiLP * clone() const = 0;
45
46 void set_tols(double tiny, double very_tiny, double rhs_relax, double infty){
47 tiny_ = tiny;
48 very_tiny_ = very_tiny;
49 rhs_relax_ = rhs_relax;
50 infty_ = infty;
51 }
52
53 void set_model(Bonmin::TMINLP2TNLP * model){
54 model_ = model;
55 initialize_jac_storage();
56 }
57
58 /** Assignment operator.*/
59 TMINLP2OsiLP & operator=(const TMINLP2OsiLP& rhs){
60 if(this != & rhs){
61 tiny_ = rhs.tiny_;
62 very_tiny_ = rhs.very_tiny_;
63 model_ = rhs.model_;
64 }
65 return (*this);
66 }
67
68 /** Destructor.*/
69 ~TMINLP2OsiLP(){}
70
71 /** Build the Outer approximation of model_ in x and put it in si.*/
72 virtual void extract(OsiSolverInterface *si,
73 const double * x, bool getObj) = 0;
74
75
76 /** Get OAs of nonlinear constraints in x.*/
77 virtual void get_refined_oa(OsiCuts & cs
78 ) const = 0;
79
80 /** Get OAs of nonlinear constraints in x.*/
81 virtual void get_oas(OsiCuts & cs,
82 const double * x, bool getObj, bool global) const = 0;
83
84
85
86 protected:
87 /** Facilitator to clean up coefficient.*/
88 inline bool cleanNnz(double &value, double colLower, double colUpper,
89 double rowLower, double rowUpper, double colsol,
90 double & lb, double &ub, double tiny, double veryTiny) const;
91 /** If constraint coefficient is below this, we try to remove it.*/
92 double tiny_;
93 /** If constraint coefficient is below this, we neglect it.*/
94 double very_tiny_;
95 /** Amount by which to relax OA constraints RHSes*/
96 double rhs_relax_;
97 /** infinity.*/
98 double infty_;
99 /** Count the number of linear outer approximations taken.*/
100 static int nTimesCalled;
101
102 /** Cache Jacobian matrix*/
103 /** Columns of jacobian.*/
104 mutable vector<int> jCol_;
105 /** Rows of jacobian.*/
106 mutable vector<int> iRow_;
107 /** Values of jacobian.*/
108 mutable vector<double> value_;
109
110 vector<Ipopt::TNLP::LinearityType> const_types_;
111
112 void initialize_jac_storage();
113
114 Ipopt::SmartPtr<Bonmin::TMINLP2TNLP> model_;
115 };
116
117 //A procedure to try to remove small coefficients in OA cuts (or make it non small
118 inline
119 bool
120 TMINLP2OsiLP::cleanNnz(double &value, double colLower, double colUpper,
121 double rowLower, double rowUpper, double colsol,
122 double & lb, double &ub, double tiny, double veryTiny) const
123 {
124 if(fabs(value)>= tiny) return 1;
125 //fprintf(stderr, "Warning: small coefficient %g\n", tiny);
126
127 if(fabs(value)<veryTiny) return 0;//Take the risk?
128
129 //try and remove
130 double infty = 1e20;
131 bool colUpBounded = colUpper < 10000;
132 bool colLoBounded = colLower > -10000;
133 bool rowNotLoBounded = rowLower <= - infty;
134 bool rowNotUpBounded = rowUpper >= infty;
135 bool pos = value > 0;
136
137 if(colLoBounded && !pos && rowNotUpBounded) {
138 lb += value * (colsol - colLower);
139 return 0;
140 }
141 else
142 if(colLoBounded && pos && rowNotLoBounded) {
143 ub += value * (colsol - colLower);
144 return 0;
145 }
146 else
147 if(colUpBounded && pos && rowNotUpBounded) {
148 lb += value * (colsol - colUpper);
149 return 0;
150 }
151 else
152 if(colUpBounded && !pos && rowNotLoBounded) {
153 ub += value * (colsol - colUpper);
154 return 0;
155 }
156 //can not remove coefficient
157 return 1;
158 }
159
160
161 }
162
163 #endif
164