# source:branches/dev/Examples/Cpp_example/MyNLP.cpp@419

Last change on this file since 419 was 419, checked in by andreasw, 9 years ago
• ran astyle
• corrected definition of finalize_solution
• Property svn:eol-style set to `native`
• Property svn:keywords set to `Author Date Id Revision`
File size: 4.8 KB
Line
3// This code is published under the Common Public License.
4//
5// \$Id\$
6//
7// Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13
8
9#include "MyNLP.hpp"
10
11using namespace Ipopt;
12
13/* Constructor. */
14MyNLP::MyNLP()
15{}
16
17MyNLP::~MyNLP()
18{}
19
20bool MyNLP::get_nlp_info(Index& n, Index& m, Index& nnz_jac_g, Index& nnz_h_lag)
21{
22  // The problem described in MyNLP.hpp has 2 variables, x1, & x2,
23  n = 2;
24
25  // one equality constraint,
26  m = 1;
27
28  // 2 nonzeros in the jacobian (one for x1, and one for x2),
29  nnz_jac_g = 2;
30
31  // and 2 nonzeros in the hessian of the lagrangian
32  // (one in the hessian of the objective for x2,
33  //  and one in the hessian of the constraints for x1)
34  nnz_h_lag = 2;
35
36  return true;
37}
38
39bool MyNLP::get_bounds_info(Index n, Number* x_l, Number* x_u,
40                            Index m, Number* g_l, Number* g_u)
41{
42  // here, the n and m we gave IPOPT in get_nlp_info are passed back to us.
43  // If desired, we could assert to make sure they are what we think they are.
44  assert(n == 2);
45  assert(m == 1);
46
47  // x1 has a lower bound of -1 and an upper bound of 1
48  x_l[0] = -1.0;
49  x_u[0] = 1.0;
50
51  // x2 has no upper or lower bound, so we set them to
52  // a large negative and a large positive number.
53  // The value that is interpretted as -/+infinity can be
54  // set in the options, but it defaults to -/+1e19
55  x_l[1] = -1.0e19;
56  x_u[1] = +1.0e19;
57
58  // we have one equality constraint, so we set the bounds on this constraint
59  // to be equal (and zero).
60  g_l[0] = g_u[0] = 0.0;
61
62  return true;
63}
64
65bool MyNLP::get_starting_point(Index n, bool init_x, Number* x,
66                               bool init_z, Number* z_L, Number* z_U,
67                               Index m, bool init_lambda,
68                               Number* lambda)
69{
70  // Here, we assume we only have starting values for x, if you code
71  // your own NLP, you can provide starting values for the others if
72  // you wish.
73  assert(init_x == true);
74  assert(init_z == false);
75  assert(init_lambda == false);
76
77  // we initialize x in bounds, in the upper right quadrant
78  x[0] = 0.5;
79  x[1] = 1.5;
80
81  return true;
82}
83
84bool MyNLP::eval_f(Index n, const Number* x, bool new_x, Number& obj_value)
85{
86  // return the value of the objective function
87  Number x2 = x[1];
88  obj_value = -(x2 - 2.0) * (x2 - 2.0);
89  return true;
90}
91
93{
95
96  // grad_{x1} f(x): x1 is not in the objective
98
100  Number x2 = x[1];
101  grad_f[1] = -2.0*(x2 - 2.0);
102
103  return true;
104}
105
106bool MyNLP::eval_g(Index n, const Number* x, bool new_x, Index m, Number* g)
107{
108  // return the value of the constraints: g(x)
109  Number x1 = x[0];
110  Number x2 = x[1];
111
112  g[0] = -(x1*x1 + x2 - 1.0);
113
114  return true;
115}
116
117bool MyNLP::eval_jac_g(Index n, const Number* x, bool new_x,
118                       Index m, Index nele_jac, Index* iRow, Index *jCol,
119                       Number* values)
120{
121  if (values == NULL) {
122    // return the structure of the jacobian of the constraints
123
124    // element at 1,1: grad_{x1} g_{1}(x)
125    iRow[0] = 1;
126    jCol[0] = 1;
127
128    // element at 1,2: grad_{x2} g_{1}(x)
129    iRow[1] = 1;
130    jCol[1] = 2;
131  }
132  else {
133    // return the values of the jacobian of the constraints
134    Number x1 = x[0];
135
136    // element at 1,1: grad_{x1} g_{1}(x)
137    values[0] = -2.0 * x1;
138
139    // element at 1,2: grad_{x1} g_{1}(x)
140    values[1] = -1.0;
141  }
142
143  return true;
144}
145
146bool MyNLP::eval_h(Index n, const Number* x, bool new_x,
147                   Number obj_factor, Index m, const Number* lambda,
148                   bool new_lambda, Index nele_hess, Index* iRow,
149                   Index* jCol, Number* values)
150{
151  if (values == NULL) {
152    // return the structure. This is a symmetric matrix, fill the lower left
153    // triangle only.
154
155    // element at 1,1: grad^2_{x1,x1} L(x,lambda)
156    iRow[0] = 1;
157    jCol[0] = 1;
158
159    // element at 2,2: grad^2_{x2,x2} L(x,lambda)
160    iRow[1] = 2;
161    jCol[1] = 2;
162
163    // Note: off-diagonal elements are zero for this problem
164  }
165  else {
166    // return the values
167
168    // element at 1,1: grad^2_{x1,x1} L(x,lambda)
169    values[0] = -2.0 * lambda[0];
170
171    // element at 2,2: grad^2_{x2,x2} L(x,lambda)
172    values[1] = -2.0;
173
174    // Note: off-diagonal elements are zero for this problem
175  }
176
177  return true;
178}
179
180void MyNLP::finalize_solution(SolverReturn status,
181                              Index n, const Number* x, const Number* z_L, const Number* z_U,
182                              Index m, const Number* g, const Number* lambda,
183                              Number obj_value)
184{
185  // here is where we would store the solution to variables, or write to a file, etc
186  // so we could use the solution. Since the solution is displayed to the console,
187  // we currently do nothing here.
188}
Note: See TracBrowser for help on using the repository browser.