CONOPT
Loading...
Searching...
No Matches
tutorial2r.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
15
20int COI_CALLCONV Tut_ReadMatrix( double LOWER[], double CURR[], double UPPER[], int VSTA[], int TYPE[], double RHS[],
21 int ESTA[], int COLSTA[], int ROWNO[], double VALUE[],
22 int NLFLAG[], int NUMVAR, int NUMCON, int NUMNZ, void* USRMEM )
23{
24 double Al = 0.16;
25 double Ak = 2.0;
26 double Ainp = 0.16;
27 double Rho = 1.0;
28 double K = 4.0;
29 /* */
30 /* Information about Variables: */
31 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
32 /* Default: the status information in Vsta is not used. */
33 /* */
34 /* Lower bound on L = X[0] = 0.1 and initial value = 0.5: */
35 /* */
36 LOWER[0] = 0.1 ;
37 CURR[0] = 0.5 ;
38 /* */
39 /* Lower bound on INP = X[1] = 0.1 and initial value = 0.5: */
40 /* */
41 LOWER[1] = 0.1 ;
42 CURR[1] = 0.5 ;
43 /* */
44 /* Lower bound on OUT = X[2] and P = X[3] are both 0 and the */
45 /* default initial value of 0 is used: */
46 /* */
47 LOWER[2] = 0. ;
48 LOWER[3] = 0. ;
49 /* */
50 /* Lower bound on the new Int is set to 0.01 and the initial */
51 /* value is defined from row 1 */
52 /* */
53 LOWER[4] = 0. ;
54 CURR[4] = Al*pow(CURR[0],-Rho) + Ak*pow(K,-Rho) + Ainp*pow(CURR[1],-Rho);
55 /* */
56 /* Information about Constraints: */
57 /* Default: Rhs = 0 */
58 /* Default: the status information in Esta and the function */
59 /* value in FV are not used. */
60 /* Default: Type: There is no default. */
61 /* 0 = Equality, */
62 /* 1 = Greater than or equal, */
63 /* 2 = Less than or equal, */
64 /* 3 = Non binding. */
65 /* */
66 /* Constraint 0 (Objective) */
67 /* Rhs = -0.1 and type Non binding */
68 /* */
69 RHS[0] = -0.1 ;
70 TYPE[0] = 3 ;
71 /* */
72 /* Constraint 1 (Production Function) */
73 /* Rhs = 0 and type Equality */
74 /* */
75 TYPE[1] = 0 ;
76 /* */
77 /* Constraint 2 (Price equation) */
78 /* Rhs = 4.0 and type Equality */
79 /* */
80 RHS[2] = 4. ;
81 TYPE[2] = 0 ;
82 /* */
83 /* Constraint 3 (New equation) */
84 /* Rhs = 0.0 and type Equality */
85 /* */
86 TYPE[3] = 0 ;
87 /* */
88 /* Information about the Jacobian. We use the standard method with */
89 /* Rowno, Value, Nlflag and Colsta and we do not use Colno. */
90 /* */
91 /* Colsta = Start of column indices (No Defaults): */
92 /* Rowno = Row indices */
93 /* Value = Value of derivative (by default only linear */
94 /* derivatives are used) */
95 /* Nlflag = 0 for linear and 1 for nonlinear derivative */
96 /* (not needed for completely linear models) */
97 /* */
98 /* Indices */
99 /* X[0] X[1] X[2] X[3] X[4] */
100 /* 0: 0 2 4 7 */
101 /* 1: 1 3 9 */
102 /* 2: 5 8 */
103 /* 3: 6 10 */
104 /* */
105 COLSTA[0] = 0 ;
106 COLSTA[1] = 2 ;
107 COLSTA[2] = 4 ;
108 COLSTA[3] = 7 ;
109 COLSTA[4] = 9 ;
110 COLSTA[5] = 11;
111 ROWNO[0] = 0 ;
112 ROWNO[1] = 1 ;
113 ROWNO[2] = 0 ;
114 ROWNO[3] = 1 ;
115 ROWNO[4] = 0 ;
116 ROWNO[5] = 2 ;
117 ROWNO[6] = 3 ;
118 ROWNO[7] = 0 ;
119 ROWNO[8] = 2 ;
120 ROWNO[9] = 1 ;
121 ROWNO[10] = 3 ;
122 /* */
123 /* Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear */
124 /* X[0] X[1] X[2] X[3] X[4] */
125 /* 0: L L NL NL */
126 /* 1: NL NL L */
127 /* 2: L L */
128 /* 3: L NL */
129 /* */
130 NLFLAG[0] = 0 ;
131 NLFLAG[1] = 1 ;
132 NLFLAG[2] = 0 ;
133 NLFLAG[3] = 1 ;
134 NLFLAG[4] = 1 ;
135 NLFLAG[5] = 0 ;
136 NLFLAG[6] = 0 ;
137 NLFLAG[7] = 1 ;
138 NLFLAG[8] = 0 ;
139 NLFLAG[9] = 0 ;
140 NLFLAG[10]= 1 ;
141 /* */
142 /* Value (Linear only) */
143 /* X[0] X[1] X[2] X[3] X[4] */
144 /* 0: -1 -1 NL NL */
145 /* 1: NL NL -1 */
146 /* 2: 1 2 */
147 /* 3: -1 NL */
148 /* */
149 VALUE[0] = -1. ;
150 VALUE[2] = -1. ;
151 VALUE[5] = 1. ;
152 VALUE[6] = -1. ;
153 VALUE[8] = 2. ;
154 VALUE[9] = -1. ;
155
156 return 0;
157}
158
159
164int COI_CALLCONV Tut_FDEval( const double X[], double* G, double JAC[], int ROWNO, const int JACNUM[], int MODE,
165 int IGNERR, int* ERRCNT, int NUMVAR, int NUMJAC, int THREAD, void* USRMEM )
166{
167 /* */
168 /* Declare local copies of the optimization variables. This is */
169 /* just for convenience to make the expressions easier to read. */
170 /* */
171 double L, Inp, Out, P, Int;
172 /* */
173 /* Declare parameters and their data values. */
174 /* */
175 double Al = 0.16;
176 double Ak = 2.0;
177 double Ainp = 0.16;
178 double Rho = 1.0;
179 double K = 4.0;
180
181 /* */
182 /* Move the optimization variables from the X vector to a set */
183 /* of local variables with the same names as the variables in */
184 /* the model description. This is not necessary, but it should make*/
185 /* the equations easier to recognize. */
186 /* This time we work with the C numbering convention */
187 /* */
188 L = X[0];
189 Inp = X[1];
190 Out = X[2];
191 P = X[3];
192 Int = X[4];
193 /* */
194 /* Row 0: the objective function is nonlinear */
195 /* */
196
197 if ( ROWNO == 0 ) {
198 /* */
199 /* Mode = 1 or 3. Function value: G = P * Out */
200 /* */
201 if ( MODE == 1 || MODE == 3 )
202 *G = P * Out;
203 /* */
204 /* Mode = 2 or 3: Derivative values: */
205 /* */
206 if ( MODE == 2 || MODE == 3 ) {
207 JAC[2] = P; /* derivative w.r.t. Out = X[2] */
208 JAC[3] = Out; /* derivative w.r.t. P = X[3] */
209 }
210 }
211 /* */
212 /* Row 1: The first part of the production function is nonlinear */
213 /* Al*L**(-Rho) + Ak*K**(-Rho) + Ainp*Inp**(-Rho)) */
214 /* */
215 else if ( ROWNO == 1 ) {
216 /* */
217 /* Mode = 1 or 3: Function value */
218 /* */
219 if ( MODE == 1 || MODE == 3 )
220 *G = (Al*pow(L,(-Rho)) + Ak*pow(K,(-Rho)) + Ainp*pow(Inp,(-Rho)));
221 /* */
222 /* Mode = 2 or 3: Derivatives */
223 /* */
224 if ( MODE == 2 || MODE == 3 ) {
225 JAC[0] = Al * (-Rho) * pow(L ,(-Rho-1.)); /* derivative w.r.t. L = X[0] */
226 JAC[1] = Ainp * (-Rho) * pow(Inp,(-Rho-1.)); /* derivative w.r.t. Inp = X[1] */
227 }
228 }
229 /* */
230 /* Row = 2: The row is linear and will not be called. */
231 /* */
232 /* Row = 3: The second part of the production function */
233 /* Int**(-1/Rho) */
234 /* */
235 else if ( ROWNO == 3 ) {
236 /* */
237 /* Mode = 1 or 3: Function value */
238 /* */
239 if ( MODE == 1 || MODE == 3 )
240 *G = pow(Int,(-1.0/Rho));
241 /* */
242 /* Mode = 2 or 3: Derivatives */
243 /* */
244 if ( MODE == 2 || MODE == 3 ) {
245 JAC[4] = (-1.0/Rho)*pow(Int,(-1.0/Rho-1.0));
246 }
247 }
248
249 return 0;
250}
251
252
257int COI_CALLCONV Tut_2DLagrStr( int HSRW[], int HSCL[], int* NODRV,
258 int NUMVAR, int NUMCON, int NHESS, void* USRMEM )
259{
260
261/* There are three nonlinear terms, P * Out in row 0,
262 Al*L**(-Rho) + Ak*K**(-Rho) + Ainp*Inp**(-Rho) in
263 row 1 and Int**(-1/Rho) in row 3 and they have the following Hessians:
264
265 Row 0: P * Out
266 Hessian (diagonal and lower triangle): A total of 1 nonzero element
267 Out P
268 2 3
269 2
270 3 1
271
272 Row 1: Al*L**(-Rho) + Ak*K**(-Rho) + Ainp*Inp**(-Rho)
273 = hold1**(-1.d0/Rh0) = hold2
274 with L and Inp being the variables.
275 Hessian (diagonal and lower triangle): A total of 3 nonzero element
276 L Inp
277 0 1
278 0 d1
279 1 d2
280
281 where d1 = (-Rho)*(-Rho-1)*Al*L**(-Rho-2)
282 and d2 = (-Rho)*(-Rho-1)*Ainp*Inp**(-Rho-2)
283
284 Row 3: Int**(-1/Rho)
285 Hessian (diagonal and lower triangle): A total of 1 nonzero element
286 Int
287 4
288 4 Int d3
289
290 where d3 = (-1/Rho)*(-1/Rho-1)*Int**(-1/Rho-2)
291
292 The three Hessian matrices do not overlap so the total number of
293 nonzeros is 4. The structure using numerical indices for rows and
294 columns and running indices for the Hessian element is:
295
296 0 1 2 3 4
297 0 0
298 1 1
299 2
300 3 3
301 4 4
302
303 and the same with names of variables and numbers of contributing
304 equations:
305
306 L Inp Out P Int
307 L 1
308 Inp 1
309 Out
310 P 0
311 Int 3
312*/
313/* Define structure of Hessian */
314
315 HSRW[0] = 0; HSCL[0] = 0;
316 HSRW[1] = 1; HSCL[1] = 1;
317 HSRW[2] = 3; HSCL[2] = 2;
318 HSRW[3] = 4; HSCL[3] = 4;
319
320 return 0;
321}
322
323
328int COI_CALLCONV Tut_2DLagrVal( const double X[], const double U[], const int HSRW[], const int HSCL[], double HSVL[],
329 int* NODRV, int NUMVAR, int NUMCON, int NHESS, void* USRMEM )
330{
331/*
332 Declare local copies of the optimization variables. This is
333 just for convenience to make the expressions easier to read. */
334
335 double L, Inp;
336 /* */
337 /* Declare parameters and their data values. */
338 /* */
339 double Al = 0.16;
340 double Ainp = 0.16;
341 double Rho = 1.0;
342
343
344/* Normal Evaluation mode */
345
346 HSVL[2] = U[0]; /* Second derivative of constraint 0 is 1 */
347 if ( U[1] != 0.0 ) {
348 L = X[0];
349 Inp = X[1];
350 HSVL[0] = (-Rho) * (-Rho-1.0) * Al * pow(L ,-Rho-2.0) * U[1];
351 HSVL[1] = (-Rho) * (-Rho-1.0) * Ainp * pow(Inp,-Rho-2.0) * U[1];
352 };
353 if ( U[3] != 0.0 )
354 HSVL[3] = (-1.0/Rho)*(-1.0/Rho-1.0)*pow(X[4],-1.0/Rho-2.0);
355
356 return 0;
357}
358
359#include "std.c"
360
363int main(int argc, char** argv)
364{
365 c_log( "Starting to execute", START );
366 /*
367 Tell CONOPT about the sizes in the model
368 */
369 COI_Error += COIDEF_NumVar ( CntVect, 5 ); /* 5 variables */
370 COI_Error += COIDEF_NumCon ( CntVect, 4 ); /* 4 constraints */
371 COI_Error += COIDEF_NumNz ( CntVect, 11 ); /* 11 nonzeros in the Jacobian */
372 COI_Error += COIDEF_NumNlNz ( CntVect, 5 ); /* 5 of which are nonlinear */
373 COI_Error += COIDEF_NumHess ( CntVect, 4 ); /* 4 Hessian nonzeros */
374 COI_Error += COIDEF_OptDir ( CntVect, 1 ); /* Maximize */
375 COI_Error += COIDEF_ObjCon ( CntVect, 0 ); /* Objective is constraint 0 */
376 COI_Error += COIDEF_DebugFV ( CntVect, 0 ); /* 0 means no debugging, 1 each iter */
377 COI_Error += COIDEF_Debug2D ( CntVect, 0 ); /* 0 means no debugging, 1 each iter */
378 COI_Error += COIDEF_StdOut ( CntVect, 0 ); /* 1 means Allow output to StdOut */
379 COI_Error += COIDEF_Optfile ( CntVect, "tutorial.opt"); /* Register the Options file */
380 /*
381 Register the necessary callback routines with CONOPT
382 */
383 COI_Error += COIDEF_Message ( CntVect, &Std_Message ); /* Register the callback Message */
384 COI_Error += COIDEF_ErrMsg ( CntVect, &Std_ErrMsg ); /* Register the callback ErrMsg */
385 COI_Error += COIDEF_Status ( CntVect, &Std_Status ); /* Register the callback Status */
386 COI_Error += COIDEF_Solution ( CntVect, &Std_Solution ); /* Register the callback Solution */
387 COI_Error += COIDEF_ReadMatrix( CntVect, &Tut_ReadMatrix); /* Register the callback ReadMatrix */
388 COI_Error += COIDEF_FDEval ( CntVect, &Tut_FDEval); /* Register the callback FDEval */
389 COI_Error += COIDEF_2DLagrStr ( CntVect, &Tut_2DLagrStr); /* Register the callback Tut_2DLagrStr */
390 COI_Error += COIDEF_2DLagrVal ( CntVect, &Tut_2DLagrVal); /* Register the callback Tut_2DLagrVal */
391
392#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
393 COI_Error += COIDEF_License ( CntVect, LICENSE_INT_1, LICENSE_INT_2, LICENSE_INT_3, LICENSE_TEXT);
394#endif
395
396 if ( COI_Error ) {
397 printf("Skipping COI_Solve due to setup errors. COI_Error = %d\n",COI_Error);
398 c_log( "Skipping Solve due to setup errors", COI_Error );
399 }
400 COI_Error = COI_Solve ( CntVect ); /* Optimize */
401 printf("After solving. COI_Error =%d\n",COI_Error);
402 if ( COI_Error ) {
403 c_log( "Errors encountered during solution", COI_Error); }
404 else if ( stacalls == 0 || solcalls == 0 ) {
405 c_log( "Status or Solution routine was not called", -1); }
406 else if ( mstat != 2 || sstat != 1 ) {
407 c_log( "Incorrect Model or Solver Status", -1); }
408 else if ( fabs( OBJ-0.572943 ) > 0.000001 ) {
409 c_log( "Incorrect objective returned", -1); }
410
411 c_log( "Successful Solve", OK );
412}
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 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_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 main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition tutorial2r.c:363
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 tutorial2r.c:328
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 tutorial2r.c:20
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 tutorial2r.c:164
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 tutorial2r.c:257
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 Rho
Definition tutoriali.c:26
double Inp
Definition tutoriali.c:16
double Out
Definition tutoriali.c:16