CONOPT
Loading...
Searching...
No Matches
tutorial2.cpp
Go to the documentation of this file.
1
7
8#include <stdlib.h>
9#include <stdio.h>
10#include <math.h>
11#include <string.h>
12#include <iostream>
13#include "conopt.hpp"
14
16{
17public:
18 /* declaring the parameters */
19 double Al;
20 double Ak;
21 double Ainp;
22 double Rho;
23 double K;
24
25 /* declaring the variable and constraint indices. */
26 int varl;
27 int varinp;
28 int varout;
29 int varp;
32
34 : Al(0.16), Ak(2.0), Ainp(0.16), Rho(1.0), K(4.0), varl(-1), varinp(-1), varout(-1),
35 varp(-1), consobj(-1), consprod(-1)
36 {
37 }
38
43 {
44 /* */
45 /* Information about Variables: */
46 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
47 /* Default: the status information in Vsta is not used. */
48 /* */
49 /* Lower bound on L = X[0] = 0.1 and initial value = 0.5: */
50 /* */
52 /* */
53 /* Lower bound on INP = X[1] = 0.1 and initial value = 0.5: */
54 /* */
56 /* */
57 /* Lower bound on OUT = X[2] and P = X[3] are both 0 and the */
58 /* default initial value of 0 is used: */
59 /* */
62 /* */
63 /* Information about Constraints: */
64 /* Default: Rhs = 0 */
65 /* Default: the status information in Esta and the function */
66 /* value in FV are not used. */
67 /* Default: Type: There is no default. */
68 /* 0 = Equality, */
69 /* 1 = Greater than or equal, */
70 /* 2 = Less than or equal, */
71 /* 3 = Non binding. */
72 /* */
73 /* Constraint 0 (Objective) */
74 /* Rhs = -0.1 and type Non binding */
75 /* */
77 {-1, -1, 0, 0}, {0, 0, 1, 1});
78 /* */
79 /* Constraint 1 (Production Function) */
80 /* Rhs = 0 and type Equality */
81 /* */
83 ConoptConstraintType::Eq, 0.0, {varl, varinp, varout}, {0, 0, -1}, {1, 1, 0});
84 /* */
85 /* Constraint 2 (Price equation) */
86 /* Rhs = 4.0 and type Equality */
87 /* */
88 addConstraint(ConoptConstraintType::Eq, 4.0, {varout, varp}, {1, 2}, {0, 0});
89
90 /* setting the objective constraint */
92
93 /* setting the optimisation direction */
95
96 /* setting the structure of the second derivative of the Lagrangian */
97 setSDLagrangianStructure({0, 1, 1, 3}, {0, 0, 1, 2});
98 }
99
104 int FDEval(const double x[], double *g, double jac[], int rowno, const int jacnum[], int mode,
105 int ignerr, int *errcnt, int numvar, int numjac, int thread) override
106 {
107 /* */
108 /* Declare local copies of the optimization variables. This is */
109 /* just for convenience to make the expressions easier to read. */
110 /* */
111 double L, Inp, Out, P;
112 double hold1, hold2, hold3;
113
114 /* */
115 /* Move the optimization variables from the X vector to a set */
116 /* of local variables with the same names as the variables in */
117 /* the model description. This is not necessary, but it should make*/
118 /* the equations easier to recognize. */
119 /* This time we work with the C numbering convention */
120 /* */
121 L = x[varl];
122 Inp = x[varinp];
123 Out = x[varout];
124 P = x[varp];
125 /* */
126 /* Row 0: the objective function is nonlinear */
127 /* */
128
129 if (rowno == consobj)
130 {
131 /* */
132 /* Mode = 1 or 3. Function value: G = P * Out */
133 /* */
134 if (mode == 1 || mode == 3)
135 *g = P * Out;
136 /* */
137 /* Mode = 2 or 3: Derivative values: */
138 /* */
139 if (mode == 2 || mode == 3)
140 {
141 jac[varout] = P; /* derivative w.r.t. Out = x[2] */
142 jac[varp] = Out; /* derivative w.r.t. P = x[3] */
143 }
144 }
145 /* */
146 /* Row 1: The production function is nonlinear */
147 /* */
148 else if (rowno == consprod)
149 {
150 /* */
151 /* Compute some common terms */
152 /* */
153 hold1 = (Al * pow(L, (-Rho)) + Ak * pow(K, (-Rho)) + Ainp * pow(Inp, (-Rho)));
154 hold2 = pow(hold1, (-1. / Rho));
155 /* */
156 /* Mode = 1 or 3: Function value */
157 /* */
158 if (mode == 1 || mode == 3)
159 *g = hold2;
160 /* */
161 /* Mode = 2 or 3: Derivatives */
162 /* */
163 if (mode == 2 || mode == 3)
164 {
165 hold3 = hold2 / hold1;
166 jac[varl] = hold3 * Al * pow(L, (-Rho - 1.)); /* derivative w.r.t. L = x[0] */
167 jac[varinp] = hold3 * Ainp * pow(Inp, (-Rho - 1.)); /* derivative w.r.t. Inp = x[1] */
168 }
169 }
170 /* */
171 /* Row = 2: The row is linear and will not be called. */
172 /* */
173
174 return 0;
175 }
176
181 int SDLagrVal(const double x[], const double u[], const int hsrw[], const int hscl[],
182 double hsvl[], int *nodrv, int numvar, int numcon, int nhess) override
183 {
184 /*
185 Declare local copies of the optimization variables. This is
186 just for convenience to make the expressions easier to read. */
187
188 double L, Inp, Out, P;
189 /* */
190 /* Declare parameters and their data values. */
191 /* */
192 double hold1, hold2, hold3, hold4;
193 int i;
194
195 /* Normal Evaluation mode */
196
197 hsvl[3] = u[consobj]; /* Second derivative of constraint 0 is 1 */
198 if (u[consprod] != 0.0)
199 {
200 L = x[varl];
201 Inp = x[varinp];
202 Out = x[varout];
203 P = x[varp];
204 hold1 = (Al * pow(L, -Rho) + Ak * pow(K, -Rho) + Ainp * pow(Inp, -Rho));
205 hold2 = pow(hold1, -1.0 / Rho);
206 hold3 = hold2 / hold1;
207 /*
208 The code for the first derivative was:
209 jac(1) = hold3 * Al * L ** (-Rho-1.0)
210 jac(2) = hold3 * Ainp * Inp ** (-Rho-1.0)
211 Define Hold4 as the derivative of Hold3 with respect to Hold1 */
212
213 hold4 = hold3 / hold1 * (-1.0 / Rho - 1.0);
214 /*
215 The second derivatives are computed as:
216 (1) The derivative of Hold3 multiplied by the other terms PLUS
217 (2) Hold3 multiplied by the derivative of the other terms
218 where the derivative of Hold3 is Hold4 * the derivative of Hold1
219 with respect to the variable in question. */
220
221 hsvl[0] = hold4 * (-Rho) * pow(Al * pow(L, -Rho - 1.0), 2) +
222 hold3 * Al * (-Rho - 1.0) * pow(L, -Rho - 2.0);
223 hsvl[1] = hold4 * (-Rho) * (Al * pow(L, -Rho - 1.0)) * (Ainp * pow(Inp, -Rho - 1.0));
224 hsvl[2] = hold4 * (-Rho) * pow(Ainp * pow(Inp, -Rho - 1.0), 2) +
225 hold3 * Ainp * (-Rho - 1.0) * pow(Inp, -Rho - 2.0);
226
227 /* Scale the derivatives with the Lagrange multiplier, u[1]: */
228
229 for (i = 0; i < 3; i++)
230 hsvl[i] = hsvl[i] * u[consprod];
231 }
232
233 (void)P;
234 (void)Out;
235
236 return 0;
237 }
238};
239
240#include "std.cpp"
241
245int main(int argc, char **argv)
246{
247 int COI_Error = 0;
248
249 // getting the program name from the executable path
250 std::string pname = getProgramName(argv[0]);
251
252 // initialising the Conopt Object
254 Tut2_ModelData modeldata;
255 Tut_MessageHandler msghandler(pname);
256
257 // adding the message handler to the conopt interface
258 conopt.setMessageHandler(msghandler);
259
260 // building the model
261 modeldata.buildModel();
262
263 // loading the model in the conopt object
264 conopt.loadModel(modeldata);
265
266#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && \
267 defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
268 std::string license = CONOPT_LICENSE_TEXT;
269 COI_Error += conopt.setLicense(
270 CONOPT_LICENSE_INT_1, CONOPT_LICENSE_INT_2, CONOPT_LICENSE_INT_3, license);
271#endif
272
273 if (COI_Error)
274 {
275 conopt.sendMessage(
276 "Skipping COI_Solve due to setup errors. COI_Error = " + std::to_string(COI_Error));
277 cpp_log(conopt, "Skipping Solve due to setup errors");
278 return -1;
279 }
280
281 COI_Error = conopt.solve(); /* Optimize */
282
283 conopt.sendMessage("After solving. COI_Error = " + std::to_string(COI_Error));
284 if (conopt.modelStatus() != 2 || conopt.solutionStatus() != 1)
285 {
286 cpp_log(conopt, "Incorrect Model or Solver Status");
287 return -1;
288 }
289 else if (fabs(conopt.objectiveValue() - 0.572943) > 0.000001)
290 {
291 cpp_log(conopt, "Incorrect objective returned");
292 return -1;
293 }
294
295 conopt.printStatus();
296
297 cpp_log(conopt, "Successful Solve");
298
299 return 0;
300}
int main(int argc, char **argv)
The Conopt class.
Definition conopt.hpp:27
static constexpr double Infinity
Definition conopt.hpp:30
char pname[MAXLINE]
Definition comdecl.h:10
int COI_Error
Definition comdecl.h:15
CONOPT C++ interface header file. This is the main object for the CONOPT C++ interface.
int FDEval(const double x[], double *g, double jac[], int rowno, const int jacnum[], int mode, int ignerr, int *errcnt, int numvar, int numjac, int thread) override
defines the nonlinearities of the model by returning numerical values.
void buildModel()
adds the variables and constraints for the problem
Definition tutorial2.cpp:42
int SDLagrVal(const double x[], const double u[], const int hsrw[], const int hscl[], double hsvl[], int *nodrv, int numvar, int numcon, int nhess) override
Computes and returns the numerical values of the Hessian.
int addVariable(double lower, double upper, double curr=0, int varstatus=-1)
adds a variable to the model. The non-zero coefficients are added later.
void setObjectiveElement(ConoptObjectiveElement elem, int elemindex)
sets the index for the objective variable or constraint
int addConstraint(ConoptConstraintType constype, double rhs, int slackstatus=-1)
adds a constraint to the problem. The non-zero coefficients are added later
void setOptimizationSense(ConoptSense sense)
sets the optimisation direction.
void setSDLagrangianStructure(const std::vector< int > &rownum, const std::vector< int > &colnum)
sets the structure of the second derivatives of the Lagrangian
void cpp_log(Conopt &conopt, std::string msg)
Definition std.cpp:111
std::string getProgramName(char *execname)
Definition std.cpp:95
double L
Definition tutoriali.c:16
double P
Definition tutoriali.c:16
double hold2
Definition tutoriali.c:28
double hold3
Definition tutoriali.c:28
double hold1
Definition tutoriali.c:28
double Inp
Definition tutoriali.c:16
double Out
Definition tutoriali.c:16