CONOPT
Loading...
Searching...
No Matches
tutorial2.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 "conopticpp.h"
14
16{
17public:
20 {
21 }
22
27 {
28 /* */
29 /* Information about Variables: */
30 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
31 /* Default: the status information in Vsta is not used. */
32 /* */
33 /* Lower bound on L = X[0] = 0.1 and initial value = 0.5: */
34 /* */
35 addVariable(0.1, CONOPT_INF, 0.5);
36 /* */
37 /* Lower bound on INP = X[1] = 0.1 and initial value = 0.5: */
38 /* */
39 addVariable(0.1, CONOPT_INF, 0.5);
40 /* */
41 /* Lower bound on OUT = X[2] and P = X[3] are both 0 and the */
42 /* default initial value of 0 is used: */
43 /* */
46 /* */
47 /* Information about Constraints: */
48 /* Default: Rhs = 0 */
49 /* Default: the status information in Esta and the function */
50 /* value in FV are not used. */
51 /* Default: Type: There is no default. */
52 /* 0 = Equality, */
53 /* 1 = Greater than or equal, */
54 /* 2 = Less than or equal, */
55 /* 3 = Non binding. */
56 /* */
57 /* Constraint 0 (Objective) */
58 /* Rhs = -0.1 and type Non binding */
59 /* */
60 addConstraint(ConoptConstraintType::Free, -0.1, {0, 1, 2, 3}, {-1, -1, 0, 0}, {0, 0, 1, 1});
61 /* */
62 /* Constraint 1 (Production Function) */
63 /* Rhs = 0 and type Equality */
64 /* */
65 addConstraint(ConoptConstraintType::Eq, 0.0, {0, 1, 2}, {0, 0, -1}, {1, 1, 0});
66 /* */
67 /* Constraint 2 (Price equation) */
68 /* Rhs = 4.0 and type Equality */
69 /* */
70 addConstraint(ConoptConstraintType::Eq, 4.0, {2, 3}, {1, 2}, {0, 0});
71
72 /* setting the objective constraint */
74
75 /* setting the optimisation direction */
77
78 /* setting the structure of the hessian */
79 setSDLagrangianStructure({0, 1, 1, 3}, {0, 0, 1, 2});
80 }
81
86 int FDEval(const double x[], double* g, double jac[], int rowno, const int jacnum[], int mode, int ignerr,
87 int* errcnt, int numvar, int numjac, int thread)
88 {
89 /* */
90 /* Declare local copies of the optimization variables. This is */
91 /* just for convenience to make the expressions easier to read. */
92 /* */
93 double L, Inp, Out, P;
94 /* */
95 /* Declare parameters and their data values. */
96 /* */
97 double Al = 0.16;
98 double Ak = 2.0;
99 double Ainp = 0.16;
100 double Rho = 1.0;
101 double K = 4.0;
102 double hold1, hold2, hold3;
103
104 /* */
105 /* Move the optimization variables from the X vector to a set */
106 /* of local variables with the same names as the variables in */
107 /* the model description. This is not necessary, but it should make*/
108 /* the equations easier to recognize. */
109 /* This time we work with the C numbering convention */
110 /* */
111 L = x[0];
112 Inp = x[1];
113 Out = x[2];
114 P = x[3];
115 /* */
116 /* Row 0: the objective function is nonlinear */
117 /* */
118
119 if ( rowno == 0 ) {
120 /* */
121 /* Mode = 1 or 3. Function value: G = P * Out */
122 /* */
123 if ( mode == 1 || mode == 3 )
124 *g = P * Out;
125 /* */
126 /* Mode = 2 or 3: Derivative values: */
127 /* */
128 if ( mode == 2 || mode == 3 ) {
129 jac[2] = P; /* derivative w.r.t. Out = x[2] */
130 jac[3] = Out; /* derivative w.r.t. P = x[3] */
131 }
132 }
133 /* */
134 /* Row 1: The production function is nonlinear */
135 /* */
136 else if ( rowno == 1 ) {
137 /* */
138 /* Compute some common terms */
139 /* */
140 hold1 = (Al*pow(L,(-Rho)) + Ak*pow(K,(-Rho)) + Ainp*pow(Inp,(-Rho)));
141 hold2 = pow(hold1,( -1./Rho ));
142 /* */
143 /* Mode = 1 or 3: Function value */
144 /* */
145 if ( mode == 1 || mode == 3 )
146 *g = hold2;
147 /* */
148 /* Mode = 2 or 3: Derivatives */
149 /* */
150 if ( mode == 2 || mode == 3 ) {
151 hold3 = hold2 / hold1;
152 jac[0] = hold3 * Al * pow(L ,(-Rho-1.)); /* derivative w.r.t. L = x[0] */
153 jac[1] = hold3 * Ainp * pow(Inp,(-Rho-1.)); /* derivative w.r.t. Inp = x[1] */
154 }
155 }
156 /* */
157 /* Row = 2: The row is linear and will not be called. */
158 /* */
159
160 return 0;
161 }
162
167 int SDLagrVal(const double x[], const double u[], const int hsrw[], const int hscl[], double hsvl[],
168 int* nodrv, int numvar, int numcon, int nhess)
169 {
170 /*
171 Declare local copies of the optimization variables. This is
172 just for convenience to make the expressions easier to read. */
173
174 double L, Inp, Out, P;
175 /* */
176 /* Declare parameters and their data values. */
177 /* */
178 double Al = 0.16;
179 double Ak = 2.0;
180 double Ainp = 0.16;
181 double Rho = 1.0;
182 double K = 4.0;
183 double hold1, hold2, hold3, hold4;
184 int i;
185
186 /* Normal Evaluation mode */
187
188 hsvl[3] = u[0]; /* Second derivative of constraint 0 is 1 */
189 if ( u[1] != 0.0 ) {
190 L = x[0];
191 Inp = x[1];
192 Out = x[2];
193 P = x[3];
194 hold1 = (Al*pow(L,-Rho) + Ak*pow(K,-Rho) + Ainp*pow(Inp,-Rho));
195 hold2 = pow( hold1,-1.0/Rho);
196 hold3 = hold2 / hold1;
197 /*
198 The code for the first derivative was:
199 jac(1) = hold3 * Al * L ** (-Rho-1.0)
200 jac(2) = hold3 * Ainp * Inp ** (-Rho-1.0)
201 Define Hold4 as the derivative of Hold3 with respect to Hold1 */
202
203 hold4 = hold3 / hold1 * (-1.0/Rho-1.0);
204 /*
205 The second derivatives are computed as:
206 (1) The derivative of Hold3 multiplied by the other terms PLUS
207 (2) Hold3 multiplied by the derivative of the other terms
208 where the derivative of Hold3 is Hold4 * the derivative of Hold1
209 with respect to the variable in question. */
210
211 hsvl[0] = hold4 * (-Rho) * pow(Al*pow(L,-Rho-1.0),2) +
212 hold3 * Al * (-Rho-1.0)*pow(L,-Rho-2.0);
213 hsvl[1] = hold4 * (-Rho) * (Al*pow(L,-Rho-1.0)) *
214 (Ainp*pow(Inp,-Rho-1.0));
215 hsvl[2] = hold4 * (-Rho) * pow(Ainp*pow(Inp,-Rho-1.0),2) +
216 hold3 * Ainp * (-Rho-1.0)*pow(Inp,-Rho-2.0);
217
218 /* Scale the derivatives with the Lagrange multiplier, u[1]: */
219
220 for ( i = 0; i < 3; i++ )
221 hsvl[i] = hsvl[i] * u[1];
222 }
223
224 (void)P;
225 (void)Out;
226
227 return 0;
228 }
229
230};
231
232#include "std.cpp"
233
237int main(int argc, char** argv)
238{
239 int COI_Error = 0;
240
241 // getting the program name from the executable path
242 std::string pname = getProgramName(argv[0]);
243
244 // initialising the Conopt Object
245 ConoptCpp conopt(pname);
246 Tut2_ModelData modeldata;
247 Tut_MessageHandler msghandler(pname);
248
249 // adding the message handler to the conopt interface
250 conopt.setMessageHandler(msghandler);
251
252 // building the model
253 modeldata.buildModel();
254
255 // loading the model in the conopt object
256 conopt.loadModel(modeldata);
257
258#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
259 std::string license = LICENSE_TEXT;
260 COI_Error += conopt.setLicense(LICENSE_INT_1, LICENSE_INT_2, LICENSE_INT_3, license);
261#endif
262
263 if ( COI_Error ) {
264 conopt.sendMessage("Skipping COI_Solve due to setup errors. COI_Error = " + std::to_string(COI_Error));
265 cpp_log(conopt, "Skipping Solve due to setup errors", COI_Error );
266 }
267
268 COI_Error = conopt.solve(); /* Optimize */
269
270 conopt.sendMessage("After solving. COI_Error = " + std::to_string(COI_Error));
271 if ( conopt.modelStatus() != 2 || conopt.solutionStatus() != 1 )
272 cpp_log(conopt, "Incorrect Model or Solver Status", -1);
273 else if ( fabs( conopt.objectiveValue() - 0.572943 ) > 0.000001 )
274 cpp_log(conopt, "Incorrect objective returned", -1);
275
276 conopt.printStatus();
277
278 cpp_log(conopt, "Successful Solve", COI_Error );
279}
The Conopt class.
Definition conopticpp.h:27
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.
#define CONOPT_INF
Definition defines.h:18
COI_API int sendMessage(std::string msg)
sends a message to the message handler
COI_API void setMessageHandler(ConoptMessageHandler &msghandler)
sets the message handler to the user supplied handler.
COI_API void loadModel(ConoptModelData &modeldata)
loads the model and stores the pointer in the interface
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
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 tutorial2.cpp:86
void buildModel()
adds the variables and constraints for the problem
Definition tutorial2.cpp:26
int SDLagrVal(const double x[], const double u[], const int hsrw[], const int hscl[], double hsvl[], int *nodrv, int numvar, int numcon, int nhess)
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
COI_API int setLicense(int licint1, int licint2, int licint3, std::string licstring)
define the License Information.
COI_API int modelStatus()
returns the model status
COI_API double objectiveValue()
returns the objective value
COI_API int solutionStatus()
return the solution status
COI_API void printStatus()
prints the status of the optimisation
COI_API int solve()
method for starting the solving process of CONOPT.
void cpp_log(ConoptCpp &conopt, std::string msg, int code)
Definition std.cpp:111
std::string getProgramName(char *execname)
Definition std.cpp:95
double Ak
Definition tutoriali.c:24
double L
Definition tutoriali.c:16
double Ainp
Definition tutoriali.c:25
double K
Definition tutoriali.c:27
double Al
Definition tutoriali.c:23
double P
Definition tutoriali.c:16
double hold2
Definition tutoriali.c:28
double hold3
Definition tutoriali.c:28
double Rho
Definition tutoriali.c:26
double hold1
Definition tutoriali.c:28
double Inp
Definition tutoriali.c:16
double Out
Definition tutoriali.c:16