CONOPT
Loading...
Searching...
No Matches
tutorial2r.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;
30 int varint;
34
36 : Al(0.16), Ak(2.0), Ainp(0.16), Rho(1.0), K(4.0), varl(-1), varinp(-1), varout(-1),
37 varp(-1), consobj(-1), consprod(-1)
38 {
39 }
40
45 {
46
47 double initL = 0.5;
48 double initInp = 0.5;
49 /* */
50 /* Information about Variables: */
51 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
52 /* Default: the status information in Vsta is not used. */
53 /* */
54 /* Lower bound on L = X[0] = 0.1 and initial value = 0.5: */
55 /* */
56 varl = addVariable(0.1, Conopt::Infinity, initL);
57 /* */
58 /* Lower bound on INP = X[1] = 0.1 and initial value = 0.5: */
59 /* */
60 varinp = addVariable(0.1, Conopt::Infinity, initInp);
61 /* */
62 /* Lower bound on OUT = X[2] and P = X[3] are both 0 and the */
63 /* default initial value of 0 is used: */
64 /* */
67 /* */
68 /* Lower bound on the new Int is set to 0.01 and the initial */
69 /* value is defined from row 1 */
70 /* */
72 0., Conopt::Infinity, Al * pow(initL, -Rho) + Ak * pow(K, -Rho) + Ainp * pow(initInp, -Rho));
73 /* */
74 /* Information about Constraints: */
75 /* Default: Rhs = 0 */
76 /* Default: the status information in Esta and the function */
77 /* value in FV are not used. */
78 /* Default: Type: There is no default. */
79 /* 0 = Equality, */
80 /* 1 = Greater than or equal, */
81 /* 2 = Less than or equal, */
82 /* 3 = Non binding. */
83 /* */
84 /* Constraint 0 (Objective) */
85 /* Rhs = -0.1 and type Non binding */
86 /* */
88 {-1, -1, 0, 0}, {0, 0, 1, 1});
89 /* */
90 /* Constraint 1 (Production Function) */
91 /* Rhs = 0 and type Equality */
92 /* */
94 ConoptConstraintType::Eq, 0.0, {varl, varinp, varint}, {0, 0, -1}, {1, 1, 0});
95 /* */
96 /* Constraint 2 (Price equation) */
97 /* Rhs = 4.0 and type Equality */
98 /* */
99 addConstraint(ConoptConstraintType::Eq, 4.0, {varout, varp}, {1, 2}, {0, 0});
100 /* */
101 /* Constraint 3 (New equation) */
102 /* Rhs = 0.0 and type Equality */
103 /* */
104 consnew = addConstraint(ConoptConstraintType::Eq, 0.0, {varout, varint}, {-1, 0}, {0, 1});
105
106 /* setting the objective constraint */
108
109 /* setting the optimisation direction */
111
112 /* setting the structure of the hessian */
113 setSDLagrangianStructure({0, 1, 3, 4}, {0, 1, 2, 4});
114 }
115
120 int FDEval(const double x[], double *g, double jac[], int rowno, const int jacnum[], int mode,
121 int ignerr, int *errcnt, int numvar, int numjac, int thread) override
122 {
123 /* */
124 /* Declare local copies of the optimization variables. This is */
125 /* just for convenience to make the expressions easier to read. */
126 /* */
127 double L, Inp, Out, P, Int;
128
129 /* */
130 /* Move the optimization variables from the X vector to a set */
131 /* of local variables with the same names as the variables in */
132 /* the model description. This is not necessary, but it should make*/
133 /* the equations easier to recognize. */
134 /* This time we work with the C numbering convention */
135 /* */
136 L = x[varl];
137 Inp = x[varinp];
138 Out = x[varout];
139 P = x[varp];
140 Int = x[varint];
141 /* */
142 /* Row 0: the objective function is nonlinear */
143 /* */
144
145 if (rowno == consobj)
146 {
147 /* */
148 /* Mode = 1 or 3. Function value: g = P * Out */
149 /* */
150 if (mode == 1 || mode == 3)
151 *g = P * Out;
152 /* */
153 /* Mode = 2 or 3: Derivative values: */
154 /* */
155 if (mode == 2 || mode == 3)
156 {
157 jac[varout] = P; /* derivative w.r.t. Out = X[2] */
158 jac[varp] = Out; /* derivative w.r.t. P = X[3] */
159 }
160 }
161 /* */
162 /* Row 1: The first part of the production function is nonlinear */
163 /* Al*L**(-Rho) + Ak*K**(-Rho) + Ainp*Inp**(-Rho)) */
164 /* */
165 else if (rowno == consprod)
166 {
167 /* */
168 /* Mode = 1 or 3: Function value */
169 /* */
170 if (mode == 1 || mode == 3)
171 *g = (Al * pow(L, (-Rho)) + Ak * pow(K, (-Rho)) + Ainp * pow(Inp, (-Rho)));
172 /* */
173 /* Mode = 2 or 3: Derivatives */
174 /* */
175 if (mode == 2 || mode == 3)
176 {
177 jac[varl] = Al * (-Rho) * pow(L, (-Rho - 1.)); /* derivative w.r.t. L = X[0] */
178 jac[varinp] = Ainp * (-Rho) * pow(Inp, (-Rho - 1.)); /* derivative w.r.t. Inp = X[1] */
179 }
180 }
181 /* */
182 /* Row = 2: The row is linear and will not be called. */
183 /* */
184 /* Row = 3: The second part of the production function */
185 /* Int**(-1/Rho) */
186 /* */
187 else if (rowno == consnew)
188 {
189 /* */
190 /* Mode = 1 or 3: Function value */
191 /* */
192 if (mode == 1 || mode == 3)
193 *g = pow(Int, (-1.0 / Rho));
194 /* */
195 /* Mode = 2 or 3: Derivatives */
196 /* */
197 if (mode == 2 || mode == 3)
198 {
199 jac[varint] = (-1.0 / Rho) * pow(Int, (-1.0 / Rho - 1.0));
200 }
201 }
202
203 return 0;
204 }
205
210 int SDLagrVal(const double x[], const double u[], const int hsrw[], const int hscl[],
211 double hsvl[], int *nodrv, int numvar, int numcon, int nhess) override
212 {
213 /*
214 Declare local copies of the optimization variables. This is
215 just for convenience to make the expressions easier to read. */
216
217 double L, Inp;
218
219 /* Normal Evaluation mode */
220
221 hsvl[2] = u[consobj]; /* Second derivative of constraint 0 is 1 */
222 if (u[consprod] != 0.0)
223 {
224 L = x[varl];
225 Inp = x[varinp];
226 hsvl[0] = (-Rho) * (-Rho - 1.0) * Al * pow(L, -Rho - 2.0) * u[consprod];
227 hsvl[1] = (-Rho) * (-Rho - 1.0) * Ainp * pow(Inp, -Rho - 2.0) * u[consprod];
228 };
229 if (u[consnew] != 0.0)
230 hsvl[3] = (-1.0 / Rho) * (-1.0 / Rho - 1.0) * pow(x[varint], -1.0 / Rho - 2.0);
231
232 return 0;
233 }
234};
235
236#include "std.cpp"
237
241int main(int argc, char **argv)
242{
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 Tut2r_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}
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 main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
void buildModel()
adds the variables and constraints for the problem
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.
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, 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 Inp
Definition tutoriali.c:16
double Out
Definition tutoriali.c:16