CONOPT
Loading...
Searching...
No Matches
tutorial2.c
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 "coiheader.h"
12
13#include "comdecl.h"
14
19int COI_CALLCONV Tut_ReadMatrix( double LOWER[], double CURR[], double UPPER[], int VSTA[], int TYPE[], double RHS[],
20 int ESTA[], int COLSTA[], int ROWNO[], double VALUE[],
21 int NLFLAG[], int NUMVAR, int NUMCON, int NUMNZ, void* USRMEM )
22{
23 /* */
24 /* Information about Variables: */
25 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
26 /* Default: the status information in Vsta is not used. */
27 /* */
28 /* Lower bound on L = X[0] = 0.1 and initial value = 0.5: */
29 /* */
30 LOWER[0] = 0.1 ;
31 CURR[0] = 0.5 ;
32 /* */
33 /* Lower bound on INP = X[1] = 0.1 and initial value = 0.5: */
34 /* */
35 LOWER[1] = 0.1 ;
36 CURR[1] = 0.5 ;
37 /* */
38 /* Lower bound on OUT = X[2] and P = X[3] are both 0 and the */
39 /* default initial value of 0 is used: */
40 /* */
41 LOWER[2] = 0. ;
42 LOWER[3] = 0. ;
43 /* */
44 /* Information about Constraints: */
45 /* Default: Rhs = 0 */
46 /* Default: the status information in Esta and the function */
47 /* value in FV are not used. */
48 /* Default: Type: There is no default. */
49 /* 0 = Equality, */
50 /* 1 = Greater than or equal, */
51 /* 2 = Less than or equal, */
52 /* 3 = Non binding. */
53 /* */
54 /* Constraint 0 (Objective) */
55 /* Rhs = -0.1 and type Non binding */
56 /* */
57 RHS[0] = -0.1 ;
58 TYPE[0] = 3 ;
59 /* */
60 /* Constraint 1 (Production Function) */
61 /* Rhs = 0 and type Equality */
62 /* */
63 TYPE[1] = 0 ;
64 /* */
65 /* Constraint 2 (Price equation) */
66 /* Rhs = 4.0 and type Equality */
67 /* */
68 RHS[2] = 4. ;
69 TYPE[2] = 0 ;
70 /* */
71 /* Information about the Jacobian. We use the standard method with */
72 /* Rowno, Value, Nlflag and Colsta and we do not use Colno. */
73 /* */
74 /* Colsta = Start of column indices (No Defaults): */
75 /* Rowno = Row indices */
76 /* Value = Value of derivative (by default only linear */
77 /* derivatives are used) */
78 /* Nlflag = 0 for linear and 1 for nonlinear derivative */
79 /* (not needed for completely linear models) */
80 /* */
81 /* Indices */
82 /* X[0] X[1] X[2] X[3] */
83 /* 0: 0 2 4 7 */
84 /* 1: 1 3 5 */
85 /* 2: 6 8 */
86 /* */
87 COLSTA[0] = 0 ;
88 COLSTA[1] = 2 ;
89 COLSTA[2] = 4 ;
90 COLSTA[3] = 7 ;
91 COLSTA[4] = 9 ;
92 ROWNO[0] = 0 ;
93 ROWNO[1] = 1 ;
94 ROWNO[2] = 0 ;
95 ROWNO[3] = 1 ;
96 ROWNO[4] = 0 ;
97 ROWNO[5] = 1 ;
98 ROWNO[6] = 2 ;
99 ROWNO[7] = 0 ;
100 ROWNO[8] = 2 ;
101 /* */
102 /* Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear */
103 /* X[0] X[1] X[2] X[3] */
104 /* 0: L L NL NL */
105 /* 1: NL NL L */
106 /* 2: L L */
107 /* */
108 NLFLAG[0] = 0 ;
109 NLFLAG[1] = 1 ;
110 NLFLAG[2] = 0 ;
111 NLFLAG[3] = 1 ;
112 NLFLAG[4] = 1 ;
113 NLFLAG[5] = 0 ;
114 NLFLAG[6] = 0 ;
115 NLFLAG[7] = 1 ;
116 NLFLAG[8] = 0 ;
117 /* */
118 /* Value (Linear only) */
119 /* X[0] X[1] X[2] X[3] */
120 /* 0: -1 -1 NL NL */
121 /* 1: NL NL -1 */
122 /* 2: 1 2 */
123 /* */
124 VALUE[0] = -1. ;
125 VALUE[2] = -1. ;
126 VALUE[5] = -1. ;
127 VALUE[6] = 1. ;
128 VALUE[8] = 2. ;
129
130 return 0;
131}
132
133
138int COI_CALLCONV Tut_FDEval( const double X[], double* G, double JAC[], int ROWNO, const int JACNUM[], int MODE,
139 int IGNERR, int* ERRCNT, int NUMVAR, int NUMJAC, int THREAD, void* USRMEM )
140{
141 /* */
142 /* Declare local copies of the optimization variables. This is */
143 /* just for convenience to make the expressions easier to read. */
144 /* */
145 double L, Inp, Out, P;
146 /* */
147 /* Declare parameters and their data values. */
148 /* */
149 double Al = 0.16;
150 double Ak = 2.0;
151 double Ainp = 0.16;
152 double Rho = 1.0;
153 double K = 4.0;
154 double hold1, hold2, hold3;
155
156 /* */
157 /* Move the optimization variables from the X vector to a set */
158 /* of local variables with the same names as the variables in */
159 /* the model description. This is not necessary, but it should make*/
160 /* the equations easier to recognize. */
161 /* This time we work with the C numbering convention */
162 /* */
163 L = X[0];
164 Inp = X[1];
165 Out = X[2];
166 P = X[3];
167 /* */
168 /* Row 0: the objective function is nonlinear */
169 /* */
170
171 if ( ROWNO == 0 ) {
172 /* */
173 /* Mode = 1 or 3. Function value: G = P * Out */
174 /* */
175 if ( MODE == 1 || MODE == 3 )
176 *G = P * Out;
177 /* */
178 /* Mode = 2 or 3: Derivative values: */
179 /* */
180 if ( MODE == 2 || MODE == 3 ) {
181 JAC[2] = P; /* derivative w.r.t. Out = X[2] */
182 JAC[3] = Out; /* derivative w.r.t. P = X[3] */
183 }
184 }
185 /* */
186 /* Row 1: The production function is nonlinear */
187 /* */
188 else if ( ROWNO == 1 ) {
189 /* */
190 /* Compute some common terms */
191 /* */
192 hold1 = (Al*pow(L,(-Rho)) + Ak*pow(K,(-Rho)) + Ainp*pow(Inp,(-Rho)));
193 hold2 = pow(hold1,( -1./Rho ));
194 /* */
195 /* Mode = 1 or 3: Function value */
196 /* */
197 if ( MODE == 1 || MODE == 3 )
198 *G = hold2;
199 /* */
200 /* Mode = 2 or 3: Derivatives */
201 /* */
202 if ( MODE == 2 || MODE == 3 ) {
203 hold3 = hold2 / hold1;
204 JAC[0] = hold3 * Al * pow(L ,(-Rho-1.)); /* derivative w.r.t. L = X[0] */
205 JAC[1] = hold3 * Ainp * pow(Inp,(-Rho-1.)); /* derivative w.r.t. Inp = X[1] */
206 }
207 }
208 /* */
209 /* Row = 2: The row is linear and will not be called. */
210 /* */
211
212 return 0;
213}
214
219int COI_CALLCONV Tut_2DLagrStr( int HSRW[], int HSCL[], int* NODRV,
220 int NUMVAR, int NUMCON, int NHESS, void* USRMEM )
221{
222/* There are two nonlinear constraints, P * Out in row 0 and
223 Al*L**(-Rho) + Ak*K**(-Rho) + Ainp*Inp**(-Rho)) ** ( -1.d0/Rho ) in
224 row 1 and they have the following Hessians:
225
226 Row 0: P * Out
227 Hessian (diagonal and lower triangle): A total of 1 nonzero element
228 Out P
229 2 3
230 2
231 3 1
232
233 Row 1: Al*L**(-Rho) + Ak*K**(-Rho) + Ainp*Inp**(-Rho)) ** ( -1.d0/Rho )
234 = hold1**(-1.d0/Rh0) = hold2
235 with L and Inp being the variables.
236 Hessian (diagonal and lower triangle): A total of 3 nonzero element
237 L Inp
238 0 1
239 0 d1
240 1 d2 d3
241
242 where d1, d2, and d3 are derived below.
243
244 The two Hessian matrices do not overlap so the total number of nonzeros
245 is 4. The structure using numerical indices for rows and columns and
246 running indices for the Hessian element is:
247
248 0 1 2 3
249 0 0
250 1 1 2
251 2
252 3 3
253
254 and the same with names of variables and numbers of contributing
255 equations:
256
257 L Inp Out P
258 L 1
259 Inp 1 1
260 Out
261 P 0
262 */
263
264/* Define structure of Hessian */
265
266 HSRW[0] = 0; HSCL[0] = 0;
267 HSRW[1] = 1; HSCL[1] = 0;
268 HSRW[2] = 1; HSCL[2] = 1;
269 HSRW[3] = 3; HSCL[3] = 2;
270
271 return 0;
272}
273
278int COI_CALLCONV Tut_2DLagrVal( const double X[], const double U[], const int HSRW[], const int HSCL[], double HSVL[],
279 int* NODRV, int NUMVAR, int NUMCON, int NHESS, void* USRMEM )
280{
281/*
282 Declare local copies of the optimization variables. This is
283 just for convenience to make the expressions easier to read. */
284
285 double L, Inp, Out, P;
286 /* */
287 /* Declare parameters and their data values. */
288 /* */
289 double Al = 0.16;
290 double Ak = 2.0;
291 double Ainp = 0.16;
292 double Rho = 1.0;
293 double K = 4.0;
294 double hold1, hold2, hold3, hold4;
295 int i;
296
297/* Normal Evaluation mode */
298
299 HSVL[3] = U[0]; /* Second derivative of constraint 0 is 1 */
300 if ( U[1] != 0.0 ) {
301 L = X[0];
302 Inp = X[1];
303 Out = X[2];
304 P = X[3];
305 hold1 = (Al*pow(L,-Rho) + Ak*pow(K,-Rho) + Ainp*pow(Inp,-Rho));
306 hold2 = pow( hold1,-1.0/Rho);
307 hold3 = hold2 / hold1;
308/*
309 The code for the first derivative was:
310 jac(1) = hold3 * Al * L ** (-Rho-1.0)
311 jac(2) = hold3 * Ainp * Inp ** (-Rho-1.0)
312 Define Hold4 as the derivative of Hold3 with respect to Hold1 */
313
314 hold4 = hold3 / hold1 * (-1.0/Rho-1.0);
315/*
316 The second derivatives are computed as:
317 (1) The derivative of Hold3 multiplied by the other terms PLUS
318 (2) Hold3 multiplied by the derivative of the other terms
319 where the derivative of Hold3 is Hold4 * the derivative of Hold1
320 with respect to the variable in question. */
321
322 HSVL[0] = hold4 * (-Rho) * pow(Al*pow(L,-Rho-1.0),2) +
323 hold3 * Al * (-Rho-1.0)*pow(L,-Rho-2.0);
324 HSVL[1] = hold4 * (-Rho) * (Al*pow(L,-Rho-1.0)) *
325 (Ainp*pow(Inp,-Rho-1.0));
326 HSVL[2] = hold4 * (-Rho) * pow(Ainp*pow(Inp,-Rho-1.0),2) +
327 hold3 * Ainp * (-Rho-1.0)*pow(Inp,-Rho-2.0);
328
329/* Scale the derivatives with the Lagrange multiplier, U[1]: */
330
331 for ( i = 0; i < 3; i++ )
332 HSVL[i] = HSVL[i] * U[1];
333 }
334
335 (void)P;
336 (void)Out;
337
338 return 0;
339}
340
341#include "std.c"
342
345int main(int argc, char** argv)
346{
347 c_log( "Starting to execute", START );
348
349 /*
350 Tell CONOPT about the sizes in the model
351 */
352 COI_Error += COIDEF_NumVar ( CntVect, 4 ); /* 4 variables */
353 COI_Error += COIDEF_NumCon ( CntVect, 3 ); /* 3 constraints */
354 COI_Error += COIDEF_NumNz ( CntVect, 9 ); /* 9 nonzeros in the Jacobian */
355 COI_Error += COIDEF_NumNlNz ( CntVect, 4 ); /* 4 of which are nonlinear */
356 COI_Error += COIDEF_NumHess ( CntVect, 4 ); /* 4 Hessian nonzeros */
357 COI_Error += COIDEF_OptDir ( CntVect, 1 ); /* Maximize */
358 COI_Error += COIDEF_ObjCon ( CntVect, 0 ); /* Objective is constraint 0 */
359 COI_Error += COIDEF_DebugFV ( CntVect, 0 ); /* 0 means no debugging, 1 each iter */
360 COI_Error += COIDEF_Debug2D ( CntVect, 0 ); /* 0 means no debugging, 1 each iter */
361 COI_Error += COIDEF_StdOut ( CntVect, 0 ); /* 1 means Allow output to StdOut */
362 COI_Error += COIDEF_Optfile ( CntVect, "tutorial.opt"); /* Register the Options file */
363 /*
364 Register the necessary callback routines with CONOPT
365 */
366 COI_Error += COIDEF_Message ( CntVect, &Std_Message ); /* Register the callback Message */
367 COI_Error += COIDEF_ErrMsg ( CntVect, &Std_ErrMsg ); /* Register the callback ErrMsg */
368 COI_Error += COIDEF_Status ( CntVect, &Std_Status ); /* Register the callback Status */
369 COI_Error += COIDEF_Solution ( CntVect, &Std_Solution ); /* Register the callback Solution */
370 COI_Error += COIDEF_ReadMatrix( CntVect, &Tut_ReadMatrix); /* Register the callback ReadMatrix */
371 COI_Error += COIDEF_FDEval ( CntVect, &Tut_FDEval); /* Register the callback FDEval */
372 COI_Error += COIDEF_2DLagrStr ( CntVect, &Tut_2DLagrStr); /* Register the callback Tut_2DLagrStr */
373 COI_Error += COIDEF_2DLagrVal ( CntVect, &Tut_2DLagrVal); /* Register the callback Tut_2DLagrVal */
374
375#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
376 COI_Error += COIDEF_License ( CntVect, LICENSE_INT_1, LICENSE_INT_2, LICENSE_INT_3, LICENSE_TEXT);
377#endif
378
379 if ( COI_Error ) {
380 printf("Skipping COI_Solve due to setup errors. COI_Error = %d\n",COI_Error);
381 c_log( "Skipping Solve due to setup errors", COI_Error );
382 }
383 COI_Error = COI_Solve ( CntVect ); /* Optimize */
384 printf("After solving. COI_Error =%d\n",COI_Error);
385 if ( COI_Error ) {
386 c_log( "Errors encountered during solution", COI_Error); }
387 else if ( stacalls == 0 || solcalls == 0 ) {
388 c_log( "Status or Solution routine was not called", -1); }
389 else if ( mstat != 2 || sstat != 1 ) {
390 c_log( "Incorrect Model or Solver Status", -1); }
391 else if ( fabs( OBJ-0.572943 ) > 0.000001 ) {
392 c_log( "Incorrect objective returned", -1); }
393
394 c_log( "Successful Solve", OK );
395}
C language header file for direct linking against the COI library generated by apiwrapper for GAMS Ve...
int stacalls
Definition comdecl.h:4
coiHandle_t CntVect
Definition comdecl.h:14
#define START
Definition comdecl.h:13
int solcalls
Definition comdecl.h:5
double OBJ
Definition comdecl.h:6
int mstat
Definition comdecl.h:7
#define OK
Definition comdecl.h:12
int sstat
Definition comdecl.h:8
int COI_Error
Definition comdecl.h:15
int COI_CALLCONV COIDEF_ReadMatrix(coiHandle_t cntvect, COI_READMATRIX_t coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
int COI_CALLCONV COIDEF_Message(coiHandle_t cntvect, COI_MESSAGE_t coi_message)
define callback routine for handling messages returned during the solution process.
int COI_CALLCONV COIDEF_Solution(coiHandle_t cntvect, COI_SOLUTION_t coi_solution)
define callback routine for returning the final solution values.
int COI_CALLCONV COIDEF_Status(coiHandle_t cntvect, COI_STATUS_t coi_status)
define callback routine for returning the completion status.
int COI_CALLCONV COIDEF_ErrMsg(coiHandle_t cntvect, COI_ERRMSG_t coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
int COI_CALLCONV COIDEF_FDEval(coiHandle_t cntvect, COI_FDEVAL_t coi_fdeval)
define callback routine for performing function and derivative evaluations.
int COI_CALLCONV COIDEF_2DLagrVal(coiHandle_t cntvect, COI_2DLAGRVAL_t coi_2dlagrval)
define callback routine for computing the values of the second derivatives of the Lagrangian.
int COI_CALLCONV COIDEF_2DLagrStr(coiHandle_t cntvect, COI_2DLAGRSTR_t coi_2dlagrstr)
define callback routine for providing the structure of the second derivatives of the Lagrangian.
int COI_CALLCONV COIDEF_Optfile(coiHandle_t cntvect, const char *optfile)
define callback routine for defining an options file.
int COI_CALLCONV COIDEF_DebugFV(coiHandle_t cntvect, int debugfv)
turn Debugging of FDEval on and off.
int COI_CALLCONV COIDEF_License(coiHandle_t cntvect, int licint1, int licint2, int licint3, const char *licstring)
define the License Information.
int COI_CALLCONV COIDEF_Debug2D(coiHandle_t cntvect, int debug2d)
turn debugging of 2nd derivatives on and off.
int COI_CALLCONV COIDEF_StdOut(coiHandle_t cntvect, int tostdout)
allow output to StdOut.
int COI_CALLCONV COIDEF_NumHess(coiHandle_t cntvect, int numhess)
defines the Number of Hessian Nonzeros.
int COI_CALLCONV COIDEF_ObjCon(coiHandle_t cntvect, int objcon)
defines the Objective Constraint.
int COI_CALLCONV COIDEF_NumVar(coiHandle_t cntvect, int numvar)
defines the number of variables in the model.
int COI_CALLCONV COIDEF_NumNz(coiHandle_t cntvect, int numnz)
defines the number of nonzero elements in the Jacobian.
int COI_CALLCONV COIDEF_NumCon(coiHandle_t cntvect, int numcon)
defines the number of constraints in the model.
int COI_CALLCONV COIDEF_OptDir(coiHandle_t cntvect, int optdir)
defines the Optimization Direction.
int COI_CALLCONV COIDEF_NumNlNz(coiHandle_t cntvect, int numnlnz)
defines the Number of Nonlinear Nonzeros.
int COI_CALLCONV COI_Solve(coiHandle_t cntvect)
method for starting the solving process of CONOPT.
int COI_CALLCONV Std_Status(int MODSTA, int SOLSTA, int ITER, double OBJVAL, void *USRMEM)
Definition std.c:45
int COI_CALLCONV Std_Message(int SMSG, int DMSG, int NMSG, char *MSGV[], void *USRMEM)
Definition std.c:8
void c_log(char *msgt, int code)
Definition std.c:202
int COI_CALLCONV Std_ErrMsg(int ROWNO, int COLNO, int POSNO, const char *MSG, void *USRMEM)
Definition std.c:26
int COI_CALLCONV Std_Solution(const double XVAL[], const double XMAR[], const int XBAS[], const int XSTA[], const double YVAL[], const double YMAR[], const int YBAS[], const int YSTA[], int NUMVAR, int NUMCON, void *USRMEM)
Definition std.c:77
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition tutorial2.c:345
int COI_CALLCONV Tut_2DLagrVal(const double X[], const double U[], const int HSRW[], const int HSCL[], double HSVL[], int *NODRV, int NUMVAR, int NUMCON, int NHESS, void *USRMEM)
Compute the Lagrangian of the Hessian.
Definition tutorial2.c:278
int COI_CALLCONV Tut_ReadMatrix(double LOWER[], double CURR[], double UPPER[], int VSTA[], int TYPE[], double RHS[], int ESTA[], int COLSTA[], int ROWNO[], double VALUE[], int NLFLAG[], int NUMVAR, int NUMCON, int NUMNZ, void *USRMEM)
Define information about the model.
Definition tutorial2.c:19
int COI_CALLCONV Tut_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, void *USRMEM)
Compute nonlinear terms and non-constant Jacobian elements.
Definition tutorial2.c:138
int COI_CALLCONV Tut_2DLagrStr(int HSRW[], int HSCL[], int *NODRV, int NUMVAR, int NUMCON, int NHESS, void *USRMEM)
Specify the structure of the Lagrangian of the Hessian.
Definition tutorial2.c:219
int COI_CALLCONV Tut_ReadMatrix(double LOWER[], double CURR[], double UPPER[], int VSTA[], int TYPE[], double RHS[], int ESTA[], int COLSTA[], int ROWNO[], double VALUE[], int NLFLAG[], int NUMVAR, int NUMCON, int NUMNZ, void *USRMEM)
Define information about the model.
Definition tutorial.c:18
int COI_CALLCONV Tut_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, void *USRMEM)
Compute nonlinear terms and non-constant Jacobian elements.
Definition tutorial.c:137
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