CONOPT
Loading...
Searching...
No Matches
tutorial.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};
174
175#include "std.cpp"
176
181int main(int argc, char **argv)
182{
183 int COI_Error = 0;
184
185 // getting the program name from the executable path
186 std::string pname = getProgramName(argv[0]);
187
188 // initialising the Conopt Object
190 Tut_ModelData modeldata;
191 Tut_MessageHandler msghandler(pname);
192
193 // adding the message handler to the conopt interface
194 conopt.setMessageHandler(msghandler);
195
196 // building the model
197 modeldata.buildModel();
198
199 // loading the model in the conopt object
200 conopt.loadModel(modeldata);
201
202#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
203 std::string license = CONOPT_LICENSE_TEXT;
204 COI_Error += conopt.setLicense(CONOPT_LICENSE_INT_1, CONOPT_LICENSE_INT_2, CONOPT_LICENSE_INT_3, license);
205#endif
206
207 if (COI_Error)
208 cpp_log(
209 conopt, "Skipping COI_Solve due to license error. COI_Error = " + std::to_string(COI_Error), COI_Error);
210
211 COI_Error = conopt.solve(); /* Optimize */
212
213 // checking the statuses and objective value
214 if (conopt.modelStatus() != 2 || conopt.solutionStatus() != 1)
215 {
216 cpp_log(conopt, "Incorrect Model or Solver Status", -1);
217 }
218 else if (fabs(conopt.objectiveValue() - 0.572943) > 0.000001)
219 {
220 cpp_log(conopt, "Incorrect objective returned", -1);
221 }
222
223 // printing the final status of the optimisation
224 conopt.printStatus();
225
226 cpp_log(conopt, "Successful Solve", COI_Error);
227}
int main(int argc, char **argv)
Definition tutorial.cpp:200
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.
Definition tutorial.cpp:101
void buildModel()
adds the variables and constraints for the problem
Definition tutorial.cpp:42
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