CONOPT
Loading...
Searching...
No Matches
tutoriali.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 W;
20 double L0;
21 double Pinp;
22
23 double Al;
24 double Ak;
25 double Ainp;
26 double Rho;
27 double K;
28
29 double L, Inp, Out, P;
30 double hold1, hold2, hold3;
31
32 /* declaring the variable and constraint indices. */
33 int varl;
34 int varinp;
35 int varout;
36 int varp;
39
41 : W(1.0), L0(0.1), Pinp(1.0), Al(0.16), Ak(2.0), Ainp(0.16), Rho(1.0), K(4.0), L(0.0),
42 Inp(0.0), Out(0.0), P(0.0), hold1(0), hold2(0), hold3(0), varl(-1), varinp(-1), varout(-1),
43 varp(-1), consobj(-1), consprod(-1)
44 {
45 }
46
51 {
52 /* */
53 /* Information about Variables: */
54 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
55 /* Default: the status information in Vsta is not used. */
56 /* */
57 /* Lower bound on L = X[0] = 0.1 and initial value = 0.5: */
58 /* */
60 /* */
61 /* Lower bound on INP = X[1] = 0.1 and initial value = 0.5: */
62 /* */
64 /* */
65 /* Lower bound on OUT = X[2] and P = X[3] are both 0 and the */
66 /* default initial value of 0 is used: */
67 /* */
70 /* */
71 /* Information about Constraints: */
72 /* Default: Rhs = 0 */
73 /* Default: the status information in Esta and the function */
74 /* value in FV are not used. */
75 /* Default: Type: There is no default. */
76 /* 0 = Equality, */
77 /* 1 = Greater than or equal, */
78 /* 2 = Less than or equal, */
79 /* 3 = Non binding. */
80 /* */
81 /* Constraint 0 (Objective) */
82 /* Rhs = -0.1 and type Non binding */
83 /* */
85 {-1, -1, 0, 0}, {0, 0, 1, 1});
86 /* */
87 /* Constraint 1 (Production Function) */
88 /* Rhs = 0 and type Equality */
89 /* */
91 ConoptConstraintType::Eq, 0.0, {varl, varinp, varout}, {0, 0, -1}, {1, 1, 0});
92 /* */
93 /* Constraint 2 (Price equation) */
94 /* Rhs = 4.0 and type Equality */
95 /* */
96 addConstraint(ConoptConstraintType::Eq, 4.0, {varout, varp}, {1, 2}, {0, 0});
97
98 /* setting the objective constraint */
100
101 /* setting the optimisation direction */
103 }
104
109 int FDEvalIni(const double x[], const int rowlist[], int mode, int listsize, int numthread,
110 int ignerr, int *errcnt, int numvar) override
111 {
112 /* */
113 /* Move the optimization variables from the x vector to a set */
114 /* of local variables with the same names as the variables in */
115 /* the model description. This is not necessary, but it should make*/
116 /* the equations easier to recognize. */
117 /* This time we work with the C numbering convention */
118 /* */
119 L = x[varl];
120 Inp = x[varinp];
121 Out = x[varout];
122 P = x[varp];
123 /* */
124 /* Compute some common terms */
125 /* */
126 hold1 = (Al * pow(L, (-Rho)) + Ak * pow(K, (-Rho)) + Ainp * pow(Inp, (-Rho)));
127 hold2 = pow(hold1, (-1. / Rho));
128 hold3 = hold2 / hold1;
129
130 return 0;
131 }
132
137 int FDEval(const double x[], double *g, double jac[], int rowno, const int jacnum[], int mode,
138 int ignerr, int *errcnt, int numvar, int numjac, int thread) override
139 {
140
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 production function is nonlinear */
163 /* */
164 else if (rowno == consprod)
165 {
166 /* */
167 /* Mode = 1 or 3: Function value */
168 /* */
169 if (mode == 1 || mode == 3)
170 *g = hold2;
171 /* */
172 /* Mode = 2 or 3: Derivatives */
173 /* */
174 if (mode == 2 || mode == 3)
175 {
176 jac[varl] = hold3 * Al * pow(L, (-Rho - 1.)); /* derivative w.r.t. L = x[0] */
177 jac[varinp] = hold3 * Ainp * pow(Inp, (-Rho - 1.)); /* derivative w.r.t. Inp = x[1] */
178 }
179 }
180 /* */
181 /* Row = 2: The row is linear and will not be called. */
182 /* */
183
184 return 0;
185 }
186};
187
188#include "std.cpp"
189
193int main(int argc, char **argv)
194{
195 int COI_Error = 0;
196
197 // getting the program name from the executable path
198 std::string pname = getProgramName(argv[0]);
199
200 // initialising the Conopt Object
202 Tuti_ModelData modeldata;
203 Tut_MessageHandler msghandler(pname);
204
205 // adding the message handler to the conopt interface
206 conopt.setMessageHandler(msghandler);
207
208 // building the model
209 modeldata.buildModel();
210
211 // loading the model in the conopt object
212 conopt.loadModel(modeldata);
213
214#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
215 std::string license = CONOPT_LICENSE_TEXT;
216 COI_Error += conopt.setLicense(CONOPT_LICENSE_INT_1, CONOPT_LICENSE_INT_2, CONOPT_LICENSE_INT_3, license);
217#endif
218
219 if (COI_Error)
220 {
221 conopt.sendMessage("Skipping COI_Solve due to setup errors. COI_Error = " + std::to_string(COI_Error));
222 cpp_log(conopt, "Skipping Solve due to setup errors", COI_Error);
223 }
224
225 COI_Error = conopt.solve(); /* Optimize */
226
227 conopt.sendMessage("After solving. COI_Error = " + std::to_string(COI_Error));
228 if (conopt.modelStatus() != 2 || conopt.solutionStatus() != 1)
229 cpp_log(conopt, "Incorrect Model or Solver Status", -1);
230 else if (fabs(conopt.objectiveValue() - 0.572943) > 0.000001)
231 cpp_log(conopt, "Incorrect objective returned", -1);
232
233 conopt.printStatus();
234
235 cpp_log(conopt, "Successful Solve", COI_Error);
236}
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.
void buildModel()
adds the variables and constraints for the problem
Definition tutoriali.cpp:50
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
int FDEvalIni(const double x[], const int rowlist[], int mode, int listsize, int numthread, int ignerr, int *errcnt, int numvar) override
an optional function that can be used to improve the efficiency of the function evaulations.
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 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