CONOPT
Loading...
Searching...
No Matches
pinsquare.c
Go to the documentation of this file.
1
15
16#include <stdlib.h>
17#include <stdio.h>
18#include <math.h>
19#include <string.h>
20#include "coiheader.h"
21
22#include "comdecl.h"
23
24
25
26int T = 16; /* Number of time periods */
27int Vpp = 7; /* Variables per period */
28int Epp = 6; /* Equations per period */
29
30
35int COI_CALLCONV Pin_ReadMatrix( double LOWER[], double CURR[], double UPPER[], int VSTA[], int TYPE[],
36 double RHS[], int ESTA[], int COLSTA[], int ROWNO[], double VALUE[],
37 int NLFLAG[], int NUMVAR, int NUMCON, int NZ, void* USRMEM )
38{
39 int it, is, i, icol, iz;
40/*
41 Define the information for the columns.
42
43 We should not supply status information, VSTA.
44
45 We order the variables as follows:
46 0: td, 1: cs, 2: s, 3: d, 4: r, 5: p, and 6: rev. All variables for
47 period 1 appears first followed by all variables for period 2 etc.
48
49 td, cs, s, and d have lower bounds of 0, r and p have lower
50 bounds of 1, and rev has no lower bound.
51 All have infinite upper bounds (default).
52 The initial value of td is 18, s is 7, cs is 7*t, d is td-s,
53 p is 14, and r is r(t-1)-d. No initial value for rev. */
54
55 for ( it=1; it<=T; it++ ) {
56 is = Vpp*(it-1);
57 LOWER[is ] = 0;
58 LOWER[is+1] = 0;
59 LOWER[is+2] = 0;
60 LOWER[is+3] = 0;
61 LOWER[is+4] = 1;
62 LOWER[is+5] = 1;
63 CURR[is ] = 18;
64 CURR[is+1] = 7*it;
65 CURR[is+2] = 7;
66 CURR[is+3] = CURR[is] - CURR[is+2];
67 if ( it > 1 )
68 CURR[is+4] = CURR[is+4-Vpp] - CURR[is+3];
69 else
70 CURR[is+4] = 500 - CURR[is+3];
71 CURR[is+5] = 14;
72/* Now fix the lower and upper bounds on P at the initial value */
73 LOWER[is+5] = CURR[is+5];
74 UPPER[is+5] = CURR[is+5];
75 }
76/* To make the model infeasible in the pre-triangular part, add the
77 following lower bound */
78/* LOWER[77] = 15; */ /* feasible value = 14.245 */
79
80/*
81 Define the information for the rows
82
83 We order the constraints as follows: The objective is first,
84 followed by tddef, sdef, csdef, ddef, rdef, and revdef for
85 the first period, the same for the second period, etc.
86
87 The objective is a nonbinding constraint: */
88
89 TYPE[0] = 3;
90
91/* All others are equalities: */
92
93 for ( i = 1; i < NUMCON; i++ )
94 TYPE[i] = 0;
95/*
96 Right hand sides: In all periods except the first, only tddef
97 has a nonzero right hand side of 1+2.3*1.015**(t-1).
98 In the initial period there are contributions from lagged
99 variables in the constraints that have lagged variables. */
100
101 for ( it = 1; it<=T; it++ ) {
102 is = 1 + 6*(it-1);
103 RHS[is] = 1.0+2.3*pow(1.015,(it-1.));
104 }
105
106/* tddef: + 0.87*td(0) */
107
108 RHS[1] = RHS[1]+ 0.87*18.;
109
110/* sdef: +0.75*s(0) */
111
112 RHS[2] = 0.75*6.5;
113
114/* csdef: +1*cs(0) */
115
116 RHS[3] = 0;
117
118/* rdef: +1*r(0) */
119
120 RHS[5] = 500;
121
122/*
123 Define the structure and content of the Jacobian:
124 To help define the Jacobian pattern and values it can be useful to
125 make a picture of the Jacobian. We describe the variables for one
126 period and the constraints they are part of:
127
128 td cs s d r p rev
129 0 1 2 3 4 5 6
130 0: Obj (1+r)**(1-t)
131 Period t:
132 0: tddef 1.0 0.13
133 1: sdef NL 1.0 NL
134 2: csdef 1.0 -1.0
135 3: ddef -1.0 1.0 1.0
136 4: rdef 1.0 1.0
137 5: revdef NL NL NL 1.0
138 Period t+1:
139 0: tddef -0.87
140 1: sdef -0.75
141 2: csdef -1.0
142 3: ddef
143 4: rdef -1.0
144 5: revdef
145
146 The Jacobian has to be sorted column-wise so we will just define
147 the elements column by column according to the table above: */
148
149 iz = 0; /* Running index for Jacobian in C convention 0 to nz-1 */
150 icol = 0; /* Running column index in C convention 0 to N-1 */
151 for ( it = 1; it <= T; it++ )
152 {
153
154/* is points to the position before the first equation for the period */
155
156 is = 1 + 6*(it-1); /* 1 correspond to the objective in row 0 */
157
158/* Column td: */
159 COLSTA[icol] = iz;
160 icol = icol + 1;
161 ROWNO[iz] = is;
162 VALUE[iz] = +1.0;
163 NLFLAG[iz] = 0;
164 iz = iz + 1;
165 ROWNO[iz] = is+3;
166 VALUE[iz] = -1.0;
167 NLFLAG[iz] = 0;
168 iz = iz + 1;
169 if ( it < T ) {
170 ROWNO[iz] = is+Epp;
171 VALUE[iz] = -0.87;
172 NLFLAG[iz] = 0;
173 iz = iz + 1;
174 }
175
176/* Column cs */
177 COLSTA[icol] = iz;
178 icol = icol + 1;
179 ROWNO[iz] = is+1;
180 NLFLAG[iz] = 1;
181 iz = iz + 1;
182 ROWNO[iz] = is+2;
183 VALUE[iz] = +1.0;
184 NLFLAG[iz] = 0;
185 iz = iz + 1;
186 if ( it < T ) {
187 ROWNO[iz] = is+2+Epp;
188 VALUE[iz] = -1.0;
189 NLFLAG[iz] = 0;
190 iz = iz + 1;
191 }
192
193/* Column s */
194 COLSTA[icol] = iz;
195 icol = icol + 1;
196 ROWNO[iz] = is+1;
197 VALUE[iz] = +1.0;
198 NLFLAG[iz] = 0;
199 iz = iz + 1;
200 ROWNO[iz] = is+2;
201 VALUE[iz] = -1.0;
202 NLFLAG[iz] = 0;
203 iz = iz + 1;
204 ROWNO[iz] = is+3;
205 VALUE[iz] = +1.0;
206 NLFLAG[iz] = 0;
207 iz = iz + 1;
208 if ( it < T ) {
209 ROWNO[iz] = is+1+Epp;
210 VALUE[iz] = -0.75;
211 NLFLAG[iz] = 0;
212 iz = iz + 1;
213 }
214
215/* Column d: */
216 COLSTA[icol] = iz;
217 icol = icol + 1;
218 ROWNO[iz] = is+3;
219 VALUE[iz] = +1.0;
220 NLFLAG[iz] = 0;
221 iz = iz + 1;
222 ROWNO[iz] = is+4;
223 VALUE[iz] = +1.0;
224 NLFLAG[iz] = 0;
225 iz = iz + 1;
226 ROWNO[iz] = is+5;
227 NLFLAG[iz] = 1;
228 iz = iz + 1;
229
230/* Column r: */
231 COLSTA[icol] = iz;
232 icol = icol + 1;
233 ROWNO[iz] = is+4;
234 VALUE[iz] = +1.0;
235 NLFLAG[iz] = 0;
236 iz = iz + 1;
237 ROWNO[iz] = is+5;
238 NLFLAG[iz] = 1;
239 iz = iz + 1;
240 if ( it < T ) {
241 ROWNO[iz] = is+4+Epp;
242 VALUE[iz] = -1.0;
243 NLFLAG[iz] = 0;
244 iz = iz + 1;
245 }
246
247/* Column p: */
248 COLSTA[icol] = iz;
249 icol = icol + 1;
250 ROWNO[iz] = is;
251 VALUE[iz] = +0.13;
252 NLFLAG[iz] = 0;
253 iz = iz + 1;
254 ROWNO[iz] = is+1;
255 NLFLAG[iz] = 1;
256 iz = iz + 1;
257 ROWNO[iz] = is+5;
258 NLFLAG[iz] = 1;
259 iz = iz + 1;
260
261/* Column rev: */
262 COLSTA[icol] = iz;
263 icol = icol + 1;
264 ROWNO[iz] = +0; /* Objective -- no is term */
265 VALUE[iz] = pow(1.05,(1.0-it));
266 NLFLAG[iz] = 0;
267 iz = iz + 1;
268 ROWNO[iz] = is+5;
269 VALUE[iz] = 1.0;
270 NLFLAG[iz] = 0;
271 iz = iz + 1;
272 }
273 COLSTA[icol] = iz; /* First elements past last column */
274
275 return 0;
276}
277/*
278 =====================================================================
279 Compute nonlinear terms and non-constant Jacobian elements */
280
281
286int COI_CALLCONV Pin_FDEval( const double X[], double* G, double JAC[], int ROWNO, const int JACNUM[], int MODE,
287 int IGNERR, int* ERRCNT, int NUMROW, int NUMNZ, int THREAD, void* USRMEM )
288{
289
290 int it, is;
291 double h1, h2;
292
293/* Compute the number of the period, it */
294
295 it = (ROWNO+Epp-1) / Epp;
296 is = Vpp*(it-1);
297 if ( 1+(it-1)*Epp+1 == ROWNO ) {
298
299/* sdef equation. Nonlinear term = -(1.1+0.1*p)*1.02**(-cs/7) */
300
301 h1 = (1.1+0.1*X[is+5]);
302 h2 = pow(1.02,-X[is+1]/7.0);
303 if ( 1 == MODE || 3 == MODE )
304 *G = -h1*h2;
305 if ( 2 == MODE || 3 == MODE ) {
306 JAC[is+1] = h1*h2*log(1.02)/7.0;
307 JAC[is+5] = -h2*0.1;
308 }
309 }
310 else if ( 1+(it-1)*Epp+5 == ROWNO ) {
311
312/* revdef equation. Nonlinear term = -d*(p-250/r) */
313
314 if ( 1 == MODE || 3 == MODE )
315 *G = -X[is+3]*(X[is+5]-250./X[is+4]);
316 if ( 2 == MODE || 3 == MODE ) {
317 JAC[is+3] = -(X[is+5]-250./X[is+4]);
318 JAC[is+4] = -X[is+3]*250./pow(X[is+4],2);
319 JAC[is+5] = -X[is+3];
320 }
321 }
322 else {
323
324/* Error - this equation is not nonlinear */
325
326 printf("\nError. Pin_FDEval called with ROWNO = %d. MODE = %d\n\n", ROWNO, MODE);
327 return 1;
328 };
329 return 0;
330}
331
332int COI_CALLCONV Pin_TriOrd( int MODE, int TYPE, int STATUS, int ROWNO, int COLNO,
333 int INF, double VALUE, double RESID, void* USRMEM )
334{
335 if ( -1 == MODE )
336 fprintf(fd,"\nThe recursive order of the equations is described below:\n\n");
337 else if ( -2 == MODE )
338 fprintf(fd,"\nThe recursive order of the critical equations is:\n\n");
339
340 if ( COLNO >= 0 && COLNO < 7 * T ) {
341 if ( 0 == INF )
342 fprintf(fd,"Equation %7d has been solved with respect to variable %7d\nValue = %15f and Residual= %15f\n",
343 ROWNO, COLNO, VALUE, RESID );
344 else if ( 1 == INF )
345 fprintf(fd,"Equation %7d has been solved with respect to variable %7d\nValue = +Infinity and Residual= %15f\n",
346 ROWNO, COLNO, RESID );
347 else
348 fprintf(fd,"Equation %7d has been solved with respect to variable %7d\nValue = -Infinity and Residual= %15f\n",
349 ROWNO, COLNO, RESID );
350 }
351 else
352 fprintf(fd,"Equation %7d has been solved with respect to the slack\nResidual= %15f\n",
353 ROWNO, RESID );
354
355 return 0;
356
357}
358#include "std.c"
359
362int main(int argc, char** argv)
363{
364 int major, minor, patch;
365
366 c_log( "Starting to execute", START );
367
368/* Write which version of CONOPT we are using. */
369
370 COIGET_Version( &major, &minor, &patch );
371 printf("\nSolving Pindyck Model using CONOPT version %d.%d.%d:\n", major, minor, patch );
372
373/* Tell CONOPT about the sizes in the model */
374/* Number of variables (excl. slacks): 7 per period */
375
377
378/* Number of equations: 1 objective + 6 per period */
379
380 COI_Error += COIDEF_NumCon ( CntVect, 1+6*T );
381
382/* Number of nonzeros in the Jacobian. See the counting in */
383/* ReadMatrix above. For each period there is 1 in the objective, 16 */
384/* for unlagged variables and 4 for lagged variables. */
385
386 COI_Error += COIDEF_NumNz ( CntVect, 17*T+4*(T-1) );
387
388/* Number of nonlinear nonzeros. 5 unlagged for each period. */
389
391
392/* Objective: Maximize Constraint no 0 */
393
396
397/* Define a square system */
398
400
401/* Turn debugging of FDEval on (1) or off (0) */
402
404
405/* Register the options file */
406
407 COI_Error += COIDEF_Optfile ( CntVect, "pindyck.opt");
408 /*
409 Register the necessary callback routines with CONOPT
410 */
411 COI_Error += COIDEF_Message ( CntVect, &Std_Message ); /* Register the callback Message */
412 COI_Error += COIDEF_ErrMsg ( CntVect, &Std_ErrMsg ); /* Register the callback ErrMsg */
413 COI_Error += COIDEF_Status ( CntVect, &Std_Status ); /* Register the callback Status */
414 COI_Error += COIDEF_Solution ( CntVect, &Std_Solution ); /* Register the callback Solution */
415 COI_Error += COIDEF_ReadMatrix( CntVect, &Pin_ReadMatrix); /* Register the callback ReadMatrix */
416 COI_Error += COIDEF_FDEval ( CntVect, &Pin_FDEval); /* Register the callback FDEval */
417 COI_Error += COIDEF_TriOrd ( CntVect, &Std_TriOrd); /* Register the callback TriOrd */
418
419#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
420 COI_Error += COIDEF_License ( CntVect, LICENSE_INT_1, LICENSE_INT_2, LICENSE_INT_3, LICENSE_TEXT);
421#endif
422
423 if ( COI_Error ) {
424 printf("Skipping COI_Solve due to setup errors. COI_Error = %d\n",COI_Error);
425 c_log( "Skipping Solve due to setup errors", COI_Error );
426 }
427 COI_Error = COI_Solve ( CntVect ); /* Optimize */
428 printf("After solving. COI_Error =%d\n",COI_Error);
429 if ( COI_Error ) {
430 c_log( "Errors encountered during solution", COI_Error); }
431 else if ( stacalls == 0 || solcalls == 0 ) {
432 c_log( "Status or Solution routine was not called", -1); }
433 else if ( mstat != 16 || sstat != 1 ) {
434 c_log( "The model or solver status was not (16,1) as expected", -1); }
435
436 c_log( "Successful Solve", OK );
437}
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
FILE * fd
Definition comdecl.h:3
int solcalls
Definition comdecl.h:5
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_TriOrd(coiHandle_t cntvect, COI_TRIORD_t coi_triord)
define callback routine for providing the triangular order information.
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_Square(coiHandle_t cntvect, int square)
square models.
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.
void COI_CALLCONV COIGET_Version(int *major, int *minor, int *patch)
returns the version number. It can be used to ensure that the modeler is linked to the correct versio...
int COI_CALLCONV Pin_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 pinadd.c:28
int COI_CALLCONV Pin_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 pinadd.c:298
int T
Definition pinadd.c:14
#define Epp
Definition pinadd.c:18
#define Vpp
Definition pinadd.c:17
int COI_CALLCONV Pin_FDEval(const double X[], double *G, double JAC[], int ROWNO, const int JACNUM[], int MODE, int IGNERR, int *ERRCNT, int NUMROW, int NUMNZ, int THREAD, void *USRMEM)
Compute nonlinear terms and non-constant Jacobian elements.
Definition pinsquare.c:286
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition pinsquare.c:362
int COI_CALLCONV Pin_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 NZ, void *USRMEM)
Define information about the model.
Definition pinsquare.c:35
int COI_CALLCONV Pin_TriOrd(int MODE, int TYPE, int STATUS, int ROWNO, int COLNO, int INF, double VALUE, double RESID, void *USRMEM)
Definition pinsquare.c:332
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
int COI_CALLCONV Std_TriOrd(int mode, int type, int status, int irow, int icol, int inf, double value, double resid, void *USRMEM)
Definition std.c:98
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