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
101 int FDEval(const double x[], double *g, double jac[], int rowno, const int jacnum[], int mode,
102 int ignerr, int *errcnt, int numvar, int numjac, int thread) override
103 {
104 /* */
105 /* Declare local copies of the optimization variables. This is */
106 /* just for convenience to make the expressions easier to read. */
107 /* */
108 double L, Inp, Out, P;
109 double hold1, hold2, hold3;
110
111 /* */
112 /* Move the optimization variables from the X vector to a set */
113 /* of local variables with the same names as the variables in */
114 /* the model description. This is not necessary, but it should make*/
115 /* the equations easier to recognize. */
116 /* This time we work with the C numbering convention */
117 /* */
118 L = x[varl];
119 Inp = x[varinp];
120 Out = x[varout];
121 P = x[varp];
122 /* */
123 /* Row 0: the objective function is nonlinear */
124 /* */
125
126 if (rowno == consobj)
127 {
128 /* */
129 /* Mode = 1 or 3. Function value: G = P * Out */
130 /* */
131 if (mode == 1 || mode == 3)
132 *g = P * Out;
133 /* */
134 /* Mode = 2 or 3: Derivative values: */
135 /* */
136 if (mode == 2 || mode == 3)
137 {
138 jac[varout] = P; /* derivative w.r.t. Out = x[2] */
139 jac[varp] = Out; /* derivative w.r.t. P = x[3] */
140 }
141 }
142 /* */
143 /* Row 1: The production function is nonlinear */
144 /* */
145 else if (rowno == consprod)
146 {
147 /* */
148 /* Compute some common terms */
149 /* */
150 hold1 = (Al * pow(L, (-Rho)) + Ak * pow(K, (-Rho)) + Ainp * pow(Inp, (-Rho)));
151 hold2 = pow(hold1, (-1. / Rho));
152 /* */
153 /* Mode = 1 or 3: Function value */
154 /* */
155 if (mode == 1 || mode == 3)
156 *g = hold2;
157 /* */
158 /* Mode = 2 or 3: Derivatives */
159 /* */
160 if (mode == 2 || mode == 3)
161 {
162 hold3 = hold2 / hold1;
163 jac[varl] = hold3 * Al * pow(L, (-Rho - 1.)); /* derivative w.r.t. L = x[0] */
164 jac[varinp] = hold3 * Ainp * pow(Inp, (-Rho - 1.)); /* derivative w.r.t. Inp = x[1] */
165 }
166 }
167 /* */
168 /* Row = 2: The row is linear and will not be called. */
169 /* */
170
171 return 0;
172 }
173
178 int SDLagrVal(const double x[], const double u[], const int hsrw[], const int hscl[],
179 double hsvl[], int *nodrv, int numvar, int numcon, int nhess) override
180 {
181 /*
182 Declare local copies of the optimization variables. This is
183 just for convenience to make the expressions easier to read. */
184
185 double L, Inp, Out, P;
186 /* */
187 /* Declare parameters and their data values. */
188 /* */
189 double hold1, hold2, hold3, hold4;
190 int i;
191
192 /* Normal Evaluation mode */
193
194 hsvl[3] = u[consobj]; /* Second derivative of constraint 0 is 1 */
195 if (u[consprod] != 0.0)
196 {
197 L = x[varl];
198 Inp = x[varinp];
199 Out = x[varout];
200 P = x[varp];
201 hold1 = (Al * pow(L, -Rho) + Ak * pow(K, -Rho) + Ainp * pow(Inp, -Rho));
202 hold2 = pow(hold1, -1.0 / Rho);
203 hold3 = hold2 / hold1;
204 /*
205 The code for the first derivative was:
206 jac(1) = hold3 * Al * L ** (-Rho-1.0)
207 jac(2) = hold3 * Ainp * Inp ** (-Rho-1.0)
208 Define Hold4 as the derivative of Hold3 with respect to Hold1 */
209
210 hold4 = hold3 / hold1 * (-1.0 / Rho - 1.0);
211 /*
212 The second derivatives are computed as:
213 (1) The derivative of Hold3 multiplied by the other terms PLUS
214 (2) Hold3 multiplied by the derivative of the other terms
215 where the derivative of Hold3 is Hold4 * the derivative of Hold1
216 with respect to the variable in question. */
217
218 hsvl[0] = hold4 * (-Rho) * pow(Al * pow(L, -Rho - 1.0), 2) +
219 hold3 * Al * (-Rho - 1.0) * pow(L, -Rho - 2.0);
220 hsvl[1] = hold4 * (-Rho) * (Al * pow(L, -Rho - 1.0)) * (Ainp * pow(Inp, -Rho - 1.0));
221 hsvl[2] = hold4 * (-Rho) * pow(Ainp * pow(Inp, -Rho - 1.0), 2) +
222 hold3 * Ainp * (-Rho - 1.0) * pow(Inp, -Rho - 2.0);
223
224 /* Scale the derivatives with the Lagrange multiplier, u[1]: */
225
226 for (i = 0; i < 3; i++)
227 hsvl[i] = hsvl[i] * u[consprod];
228 }
229
230 (void)P;
231 (void)Out;
232
233 return 0;
234 }
235};
236
237#include "std.cpp"
238
242int main(int argc, char **argv)
243{
244 int COI_Error = 0;
245
246 // getting the program name from the executable path
247 std::string pname = getProgramName(argv[0]);
248
249 // initialising the Conopt Object
251 Tut2_ModelData modeldata;
252 Tut_MessageHandler msghandler(pname);
253
254 // adding the message handler to the conopt interface
255 conopt.setMessageHandler(msghandler);
256
257 // building the model
258 modeldata.buildModel();
259
260 // loading the model in the conopt object
261 conopt.loadModel(modeldata);
262
263#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
264 std::string license = CONOPT_LICENSE_TEXT;
265 COI_Error += conopt.setLicense(CONOPT_LICENSE_INT_1, CONOPT_LICENSE_INT_2, CONOPT_LICENSE_INT_3, license);
266#endif
267
268 if (COI_Error)
269 {
270 conopt.sendMessage("Skipping COI_Solve due to setup errors. COI_Error = " + std::to_string(COI_Error));
271 cpp_log(conopt, "Skipping Solve due to setup errors", COI_Error);
272 }
273
274 COI_Error = conopt.solve(); /* Optimize */
275
276 conopt.sendMessage("After solving. COI_Error = " + std::to_string(COI_Error));
277 if (conopt.modelStatus() != 2 || conopt.solutionStatus() != 1)
278 cpp_log(conopt, "Incorrect Model or Solver Status", -1);
279 else if (fabs(conopt.objectiveValue() - 0.572943) > 0.000001)
280 cpp_log(conopt, "Incorrect objective returned", -1);
281
282 conopt.printStatus();
283
284 cpp_log(conopt, "Successful Solve", COI_Error);
285}
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 cpp_log(Conopt &conopt, std::string msg, int code)
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