Debian Codegraph @

Bonmin/examples/OptionDocGen/MyTMINLP.cpp

raw
1 // (C) Copyright Carnegie Mellon University 2006
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors :
6 // P. Bonami, Carnegie Mellon University
7 //
8 // Date : 03/17/2006
9 #include "MyTMINLP.hpp"
10 #include "BonAmplInterface.hpp"
11
12 bool
13 MyTMINLP::get_variables_types(Index n, VariableType* var_types)
14 {
15 var_types[0] = BINARY;
16 var_types[1] = CONTINUOUS;
17 var_types[2] = CONTINUOUS;
18 var_types[3] = INTEGER;
19 return true;
20 }
21
22
23 bool
24 MyTMINLP::get_variables_linearity(Index n, Ipopt::TNLP::LinearityType* var_types)
25 {
26 var_types[0] = Ipopt::TNLP::LINEAR;
27 var_types[1] = Ipopt::TNLP::NON_LINEAR;
28 var_types[2] = Ipopt::TNLP::NON_LINEAR;
29 var_types[3] = Ipopt::TNLP::LINEAR;
30 return true;
31 }
32
33
34 bool
35 MyTMINLP::get_constraints_linearity(Index m, Ipopt::TNLP::LinearityType* const_types)
36 {
37 assert (m==3);
38 const_types[0] = Ipopt::TNLP::NON_LINEAR;
39 const_types[1] = Ipopt::TNLP::LINEAR;
40 const_types[2] = Ipopt::TNLP::LINEAR;
41 return true;
42 }
43 bool
44 MyTMINLP::get_nlp_info(Index& n, Index&m, Index& nnz_jac_g,
45 Index& nnz_h_lag, TNLP::IndexStyleEnum& index_style)
46 {
47 n = 4;//number of variable
48 m = 3;//number of constraints
49 nnz_jac_g = 7;//number of non zeroes in Jacobian
50 nnz_h_lag = 2;//number of non zeroes in Hessian of Lagrangean
51 index_style = TNLP::FORTRAN_STYLE;
52 return true;
53 }
54
55 bool
56 MyTMINLP::get_bounds_info(Index n, Number* x_l, Number* x_u,
57 Index m, Number* g_l, Number* g_u)
58 {
59 assert(n==4);
60 assert(m==3);
61 x_l[0] = 0.;
62 x_u[0] = 1.;
63
64 x_l[1] = 0.;
65 x_u[1] = DBL_MAX;
66
67 x_l[2] =0.;
68 x_u[2] = DBL_MAX;
69
70 x_l[3] = 0;
71 x_u[3] = 5;
72
73 g_l[0] = -DBL_MAX;
74 g_u[0] = 1./4.;
75
76 g_l[1] = -DBL_MAX;
77 g_u[1] = 0;
78
79 g_l[2] = -DBL_MAX;
80 g_u[2] = 2;
81 return true;
82 }
83
84 bool
85 MyTMINLP::get_starting_point(Index n, bool init_x, Number* x,
86 bool init_z, Number* z_L, Number* z_U,
87 Index m, bool init_lambda,
88 Number* lambda)
89 {
90 assert(n==4);
91 assert(m==3);
92
93 assert(init_x);
94 assert(!init_lambda);
95 x[0] = 0;
96 x[1] = 0;
97 x[2] = 0;
98 x[3] = 0;
99 return true;
100 }
101
102 bool
103 MyTMINLP::eval_f(Index n, const Number* x, bool new_x, Number& obj_value)
104 {
105 assert(n==4);
106 obj_value = - x[0] - x[1] - x[2];
107 return true;
108 }
109
110 bool
111 MyTMINLP::eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f)
112 {
113 assert(n==4);
114 grad_f[0] = -1.;
115 grad_f[1] = -1.;
116 grad_f[2] = -1.;
117 grad_f[3] = 0.;
118 return true;
119 }
120
121 bool
122 MyTMINLP::eval_g(Index n, const Number* x, bool new_x, Index m, Number* g)
123 {
124 assert(n==4);
125 assert(m==3);
126
127 g[0] = (x[1] - 1./2.)*(x[1] - 1./2.) + (x[2] - 1./2.)*(x[2] - 1./2.);
128 g[1] = x[0] - x[1];
129 g[2] = x[0] + x[2] + x[3];
130
131 return true;
132 }
133
134 bool
135 MyTMINLP::eval_jac_g(Index n, const Number* x, bool new_x,
136 Index m, Index nnz_jac, Index* iRow, Index *jCol,
137 Number* values)
138 {
139 assert(n==4);
140 assert(nnz_jac == 7);
141 if(values == NULL) {
142 iRow[0] = 2;
143 jCol[0] = 1;
144
145 iRow[1] = 3;
146 jCol[1] = 1;
147
148 iRow[2] = 1;
149 jCol[2] = 2;
150
151 iRow[3] = 2;
152 jCol[3] = 2;
153
154 iRow[4] = 1;
155 jCol[4] = 3;
156
157 iRow[5] = 3;
158 jCol[5] = 3;
159
160 iRow[6] = 3;
161 jCol[6] = 4;
162 return true;
163 }
164 else {
165 values[0] = 1.;
166 values[1] = 1;
167
168 values[2] = 2*x[1] - 1;
169 values[3] = -1.;
170
171 values[4] = 2*x[2] - 1;
172 values[5] = 1.;
173
174 values[6] = 1.;
175
176 return true;
177 }
178 }
179
180 bool
181 MyTMINLP::eval_h(Index n, const Number* x, bool new_x,
182 Number obj_factor, Index m, const Number* lambda,
183 bool new_lambda, Index nele_hess, Index* iRow,
184 Index* jCol, Number* values)
185 {
186 assert (n==4);
187 assert (m==3);
188 assert(nele_hess==2);
189 if(values==NULL)
190 {
191 iRow[0] = 2;
192 jCol[0] = 2;
193
194 iRow[1] = 3;
195 jCol[1] = 3;
196 }
197 else {
198 values[0] = 2*lambda[0];
199 values[1] = 2*lambda[0];
200 }
201 return true;
202 }
203
204 void
205 MyTMINLP::finalize_solution(TMINLP::SolverReturn status,
206 Index n, const Number* x, Number obj_value)
207 {
208 std::cout<<"Problem status: "<<status<<std::endl;
209 std::cout<<"Objective value: "<<obj_value<<std::endl;
210 if(printSol_ && x != NULL){
211 std::cout<<"Solution:"<<std::endl;
212 for(int i = 0 ; i < n ; i++){
213 std::cout<<"x["<<i<<"] = "<<x[i];
214 if(i < n-1) std::cout<<", ";}
215 std::cout<<std::endl;
216 }
217 }