CONOPT
Loading...
Searching...
No Matches
tutorial.cpp
Go to the documentation of this file.
1
6
7#include <stdlib.h>
8#include <stdio.h>
9#include <math.h>
10#include <string.h>
11#include <iostream>
12#include "conopt.hpp"
13
14#include <adolc/adolc.h>
15
16class Tut_ModelData : public ConoptModelData
17{
18public:
21 {
22 }
23
29 void evaluateNonlinearExpression(adouble* x, adouble* g, int rowno, int numvar)
30 {
31 /* */
32 /* Declare local copies of the optimization variables. This is */
33 /* just for convenience to make the expressions easier to read. */
34 /* */
35 adouble L, Inp, Out, P;
36 /* */
37 /* Declare parameters and their data values. */
38 /* */
39 double Al = 0.16;
40 double Ak = 2.0;
41 double Ainp = 0.16;
42 double Rho = 1.0;
43 double K = 4.0;
44
45 /* helper variables */
46 adouble hold1 = 0, hold2 = 0;
47
48 /* */
49 /* Move the optimization variables from the X vector to a set */
50 /* of local variables with the same names as the variables in */
51 /* the model description. This is not necessary, but it should make*/
52 /* the equations easier to recognize. */
53 /* This time we work with the C numbering convention */
54 /* */
55 L = x[0];
56 Inp = x[1];
57 Out = x[2];
58 P = x[3];
59 /* */
60 /* Row 0: the objective function is nonlinear */
61 /* */
62
63 if ( rowno == 0 ) {
64 *g = P * Out;
65 }
66 /* */
67 /* Row 1: The production function is nonlinear */
68 /* */
69 else if ( rowno == 1 ) {
70 /* */
71 /* Compute some common terms */
72 /* */
73 hold1 = (Al*pow(L,(-Rho)) + Ak*pow(K,(-Rho)) + Ainp*pow(Inp,(-Rho)));
74 hold2 = pow(hold1,( -1./Rho ));
75 *g = hold2;
76 }
77 /* */
78 /* Row = 2: The row is linear and will not be called. */
79 /* */
80
81 }
82
87 {
88 int numcons = numCons();
89 int numvar = numVar();
90
91 for (int c = 0; c < numcons; c++)
92 {
93 double res;
94 /* starting the trace for constraint c */
95 trace_on(c);
96
97 /* these are variable types for ADOL-C. Both the dependent and independent variables must be declared as
98 * adouble
99 */
100 adouble* ax;
101 adouble ag = 0;
102
103 /* setting the values of the independent variables */
104 ax = new adouble[numvar];
105 for (int i = 0; i < numvar; i++)
106 ax[i] <<= getVariable(i).curr;
107
108 evaluateNonlinearExpression(ax, &ag, c, numvar);
109
110 /* setting the results as the value of the independent variable. */
111 ag >>= res;
112
113 delete[] ax;
114
115 /* stopping the trace for constraint c */
116 trace_off();
117 }
118 }
119
124 {
125 /* */
126 /* Information about Variables: */
127 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
128 /* Default: the status information in Vsta is not used. */
129 /* */
130 /* Lower bound on L = X[0] = 0.1 and initial value = 0.5: */
131 /* */
132 addVariable(0.1, CONOPT_INF, 0.5);
133 /* */
134 /* Lower bound on INP = X[1] = 0.1 and initial value = 0.5: */
135 /* */
136 addVariable(0.1, CONOPT_INF, 0.5);
137 /* */
138 /* Lower bound on OUT = X[2] and P = X[3] are both 0 and the */
139 /* default initial value of 0 is used: */
140 /* */
141 addVariable(0., CONOPT_INF);
142 addVariable(0., CONOPT_INF);
143 /* */
144 /* Information about Constraints: */
145 /* Default: Rhs = 0 */
146 /* Default: the status information in Esta and the function */
147 /* value in FV are not used. */
148 /* Default: Type: There is no default. */
149 /* 0 = Equality, */
150 /* 1 = Greater than or equal, */
151 /* 2 = Less than or equal, */
152 /* 3 = Non binding. */
153 /* */
154 /* Constraint 0 (Objective) */
155 /* Rhs = -0.1 and type Non binding */
156 /* */
157 addConstraint(ConoptConstraintType::Free, -0.1, {0, 1, 2, 3}, {-1, -1, 0, 0}, {0, 0, 1, 1});
158 /* */
159 /* Constraint 1 (Production Function) */
160 /* Rhs = 0 and type Equality */
161 /* */
162 addConstraint(ConoptConstraintType::Eq, 0.0, {0, 1, 2}, {0, 0, -1}, {1, 1, 0});
163 /* */
164 /* Constraint 2 (Price equation) */
165 /* Rhs = 4.0 and type Equality */
166 /* */
167 addConstraint(ConoptConstraintType::Eq, 4.0, {2, 3}, {1, 2}, {0, 0});
168
169 /* setting the objective constraint */
171
172 /* setting the optimisation direction */
174
175 /* initialising the automatic differentiation */
177 }
178
183 int FDEval(const double x[], double* g, double jac[], int rowno, const int jacnum[], int mode, int ignerr,
184 int* errcnt, int numvar, int numjac, int thread)
185 {
186 /* using the function() method from ADOL-C to compute the function value from the trace */
187 if (mode == 1 || mode == 3)
188 function(rowno, 1, numvar, const_cast<double*>(x), g);
189
190 /* using the gradient() method from ADOL-C to compute the gradient from the trace */
191 if (mode == 2 || mode == 3)
192 gradient(rowno, numvar, x, jac);
193
194 return 0;
195 }
196};
197
198#include "std.cpp"
199
200int main(int argc, char** argv)
201{
202 int COI_Error = 0;
203
204 // getting the program name from the executable path
205 std::string pname = getProgramName(argv[0]);
206
207 // initialising the Conopt Object
208 ConoptCpp conopt(pname);
209 Tut_ModelData modeldata;
210 Tut_MessageHandler msghandler(pname);
211
212 // adding the message handler to the conopt interface
213 conopt.setMessageHandler(msghandler);
214
215 // building the model
216 modeldata.buildModel();
217
218 // loading the model in the conopt object
219 conopt.loadModel(modeldata);
220
221#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
222 std::string license = CONOPT_LICENSE_TEXT;
223 COI_Error += conopt.setLicense(CONOPT_LICENSE_INT_1, CONOPT_LICENSE_INT_2, CONOPT_LICENSE_INT_3, license);
224#endif
225
226 if ( COI_Error )
227 cpp_log(conopt, "Skipping COI_Solve due to license error. COI_Error = " + COI_Error, COI_Error);
228
229 COI_Error = conopt.solve(); /* Optimize */
230
231 // checking the statuses and objective value
232 if ( conopt.modelStatus() != 2 || conopt.solutionStatus() != 1 )
233 {
234 cpp_log(conopt, "Incorrect Model or Solver Status", -1);
235 }
236 else if ( fabs( conopt.objectiveValue() - 0.572943 ) > 0.000001 )
237 {
238 cpp_log(conopt, "Incorrect objective returned", -1);
239 }
240
241 // printing the final status of the optimisation
242 conopt.printStatus();
243
244 cpp_log(conopt, "Successful Solve", COI_Error);
245}
int main(int argc, char **argv)
Definition tutorial.cpp:200
The Model Data class.
Definition conopt.hpp:604
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)
defines the nonlinearities of the model by returning numerical values.
Definition tutorial.cpp:183
void buildModel()
adds the variables and constraints for the problem
Definition tutorial.cpp:123
void evaluateNonlinearExpression(adouble *x, adouble *g, int rowno, int numvar)
Definition tutorial.cpp:29
void initialiseAutoDiff()
Definition tutorial.cpp:86
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.
int numCons() const
returns the number of constraints in the model
int numVar() const
returns the number of variables in the model
const ConoptVariable & getVariable(int index) const
returns a reference to the variable object
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 hold1
Definition tutoriali.c:28
double Inp
Definition tutoriali.c:16
double Out
Definition tutoriali.c:16