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