CONOPT
Loading...
Searching...
No Matches
tutorialk.cpp
Go to the documentation of this file.
1
13
14#include <stdlib.h>
15#include <stdio.h>
16#include <math.h>
17#include <string.h>
18#include <iostream>
19#include "conopt.hpp"
20
22{
23public:
24 /* declaring the parameters */
25 double Al;
26 double Ak;
27 double Ainp;
28 double Rho;
29
30 /* declaring the variable and constraint indices. */
31 int varl;
32 int vark;
33 int varinp;
34 int varout;
35 int varp;
38
40 : Al(0.16), Ak(2.0), Ainp(0.16), Rho(1.0), varl(-1), vark(-1), varinp(-1), varout(-1),
41 varp(-1), consobj(-1), consprod(-1)
42 {
43 }
44
49 {
50 /* */
51 /* Information about Variables: */
52 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
53 /* Default: the status information in Vsta is not used. */
54 /* */
55 /* Lower bound on L = X[0] = 0.1 and initial value = 0.5: */
56 /* */
58 /* */
59 /* Lower bound = Upper bound = Initial value for K = X[1] = 4 */
60 /* */
61 vark = addVariable(4.0, 4.0, 4.0);
62 /* */
63 /* Lower bound on INP = X[2] = 0.1 and initial value = 0.5: */
64 /* */
66 /* */
67 /* Lower bound on OUT = X[3] and P = X[4] are both 0 and the */
68 /* default initial value of 0 is used: */
69 /* */
72 /* */
73 /* Information about Constraints: */
74 /* Default: Rhs = 0 */
75 /* Default: the status information in Esta and the function */
76 /* value in FV are not used. */
77 /* Default: Type: There is no default. */
78 /* 0 = Equality, */
79 /* 1 = Greater than or equal, */
80 /* 2 = Less than or equal, */
81 /* 3 = Non binding. */
82 /* */
83 /* Constraint 0 (Objective) */
84 /* Rhs = -0.1 and type Non binding */
85 /* */
87 {-1, -1, 0, 0}, {0, 0, 1, 1});
88 /* */
89 /* Constraint 1 (Production Function) */
90 /* Rhs = 0 and type Equality */
91 /* */
93 {0, 0, 0, -1}, {1, 1, 1, 0});
94 /* */
95 /* Constraint 2 (Price equation) */
96 /* Rhs = 4.0 and type Equality */
97 /* */
98 addConstraint(ConoptConstraintType::Eq, 4, {varout, varp}, {1, 2}, {0, 0});
99
100 /* setting the objective constraint */
102
103 /* setting the optimisation direction */
105 }
106
111 int FDEval(const double x[], double *g, double jac[], int rowno, const int jacnum[], int mode,
112 int ignerr, int *errcnt, int numvar, int numjac, int thread) override
113 {
114
115 /* */
116 /* Declare local copies of the optimization variables. This is */
117 /* just for convenience to make the expressions easier to read. */
118 /* */
119 double L, Inp, Out, P, K;
120 double hold1, hold2, hold3;
121
122 /* */
123 /* Move the optimization variables from the X vector to a set */
124 /* of local variables with the same names as the variables in */
125 /* the model description. This is not necessary, but it should make*/
126 /* the equations easier to recognize. */
127 /* This time we work with the C numbering convention */
128 /* */
129 L = x[varl];
130 K = x[vark];
131 Inp = x[varinp];
132 Out = x[varout];
133 P = x[varp];
134 /* */
135 /* Row 0: the objective function is nonlinear */
136 /* */
137
138 if (rowno == 0)
139 {
140 /* */
141 /* Mode = 1 or 3. Function value: g = P * Out */
142 /* */
143 if (mode == 1 || mode == 3)
144 *g = P * Out;
145 /* */
146 /* Mode = 2 or 3: Derivative values: */
147 /* */
148 if (mode == 2 || mode == 3)
149 {
150 jac[varout] = P; /* derivative w.r.t. Out = x[varout] */
151 jac[varp] = Out; /* derivative w.r.t. P = x[varp] */
152 }
153 }
154 /* */
155 /* Row 1: The production function is nonlinear */
156 /* */
157 else if (rowno == 1)
158 {
159 /* */
160 /* Compute some common terms */
161 /* */
162 hold1 = (Al * pow(L, (-Rho)) + Ak * pow(K, (-Rho)) + Ainp * pow(Inp, (-Rho)));
163 hold2 = pow(hold1, (-1. / Rho));
164 /* */
165 /* Mode = 1 or 3: Function value */
166 /* */
167 if (mode == 1 || mode == 3)
168 *g = hold2;
169 /* */
170 /* Mode = 2 or 3: Derivatives */
171 /* */
172 if (mode == 2 || mode == 3)
173 {
174 hold3 = hold2 / hold1;
175 jac[varl] = hold3 * Al * pow(L, (-Rho - 1.)); /* derivative w.r.t. L = x[varl] */
176 jac[vark] = hold3 * Ak * pow(K, (-Rho - 1.)); /* derivative w.r.t. K = x[vark] */
177 jac[varinp] =
178 hold3 * Ainp * pow(Inp, (-Rho - 1.)); /* derivative w.r.t. Inp = x[varinp] */
179 }
180 }
181 /* */
182 /* Row = 2: The row is linear and will not be called. */
183 /* */
184
185 return 0;
186 }
187};
188
189#include "std.cpp"
190
194int main(int argc, char **argv)
195{
196 int COI_Error = 0;
197
198 // getting the program name from the executable path
199 std::string pname = getProgramName(argv[0]);
200
201 // initialising the Conopt Object
203 Tutk_ModelData modeldata;
204 Tut_MessageHandler msghandler(pname);
205
206 // adding the message handler to the conopt interface
207 conopt.setMessageHandler(msghandler);
208
209 // building the model
210 modeldata.buildModel();
211
212 // loading the model in the conopt object
213 conopt.loadModel(modeldata);
214
215#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
216 std::string license = CONOPT_LICENSE_TEXT;
217 COI_Error += conopt.setLicense(CONOPT_LICENSE_INT_1, CONOPT_LICENSE_INT_2, CONOPT_LICENSE_INT_3, license);
218#endif
219
220 if (COI_Error)
221 {
222 conopt.sendMessage("Skipping COI_Solve due to setup errors. COI_Error = " + std::to_string(COI_Error));
223 cpp_log(conopt, "Skipping Solve due to setup errors", COI_Error);
224 }
225
226 COI_Error = conopt.solve(); /* Optimize */
227
228 conopt.sendMessage("After solving. COI_Error = " + std::to_string(COI_Error));
229 if (conopt.modelStatus() != 2 || conopt.solutionStatus() != 1)
230 cpp_log(conopt, "Incorrect Model or Solver Status", -1);
231 else if (fabs(conopt.objectiveValue() - 0.572943) > 0.000001)
232 cpp_log(conopt, "Incorrect objective returned", -1);
233
234 conopt.printStatus();
235
236 cpp_log(conopt, "Successful Solve", COI_Error);
237}
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
Definition tutorialk.cpp:48
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
double L
Definition tutoriali.c:16
double K
Definition tutoriali.c:27
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