CONOPT
Loading...
Searching...
No Matches
tutorialk.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 = Upper bound = Initial value for K = X[1] = 4 */
34 /* */
35 LOWER[1] = 4.0 ;
36 CURR[1] = 4.0 ;
37 UPPER[1] = 4.0 ;
38 /* */
39 /* Lower bound on INP = X[2] = 0.1 and initial value = 0.5: */
40 /* */
41 LOWER[2] = 0.1 ;
42 CURR[2] = 0.5 ;
43 /* */
44 /* Lower bound on OUT = X[3] and P = X[4] are both 0 and the */
45 /* default initial value of 0 is used: */
46 /* */
47 LOWER[3] = 0. ;
48 LOWER[4] = 0. ;
49 /* */
50 /* Information about Constraints: */
51 /* Default: Rhs = 0 */
52 /* Default: the status information in Esta and the function */
53 /* value in FV are not used. */
54 /* Default: Type: There is no default. */
55 /* 0 = Equality, */
56 /* 1 = Greater than or equal, */
57 /* 2 = Less than or equal, */
58 /* 3 = Non binding. */
59 /* */
60 /* Constraint 0 (Objective) */
61 /* Rhs = -0.1 and type Non binding */
62 /* */
63 RHS[0] = -0.1 ;
64 TYPE[0] = 3 ;
65 /* */
66 /* Constraint 1 (Production Function) */
67 /* Rhs = 0 and type Equality */
68 /* */
69 TYPE[1] = 0 ;
70 /* */
71 /* Constraint 2 (Price equation) */
72 /* Rhs = 4.0 and type Equality */
73 /* */
74 RHS[2] = 4. ;
75 TYPE[2] = 0 ;
76 /* */
77 /* Information about the Jacobian. We use the standard method with */
78 /* Rowno, Value, Nlflag and Colsta and we do not use Colno. */
79 /* */
80 /* Colsta = Start of column indices (No Defaults): */
81 /* Rowno = Row indices */
82 /* Value = Value of derivative (by default only linear */
83 /* derivatives are used) */
84 /* Nlflag = 0 for linear and 1 for nonlinear derivative */
85 /* (not needed for completely linear models) */
86 /* */
87 /* Indices */
88 /* X[0] X[1] X[2] X[3] X[4] */
89 /* 0: 0 3 5 8 */
90 /* 1: 1 2 4 6 */
91 /* 2: 7 9 */
92 /* */
93 COLSTA[0] = 0 ;
94 COLSTA[1] = 2 ;
95 COLSTA[2] = 3 ;
96 COLSTA[3] = 5 ;
97 COLSTA[4] = 8 ;
98 COLSTA[5] = 10 ;
99 ROWNO[0] = 0 ;
100 ROWNO[1] = 1 ;
101 ROWNO[2] = 1 ;
102 ROWNO[3] = 0 ;
103 ROWNO[4] = 1 ;
104 ROWNO[5] = 0 ;
105 ROWNO[6] = 1 ;
106 ROWNO[7] = 2 ;
107 ROWNO[8] = 0 ;
108 ROWNO[9] = 2 ;
109 /* */
110 /* Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear */
111 /* X[0] X[1] X[2] X[3] X[4] */
112 /* 0: L L NL NL */
113 /* 1: NL NL NL L */
114 /* 2: L L */
115 /* */
116 NLFLAG[0] = 0 ;
117 NLFLAG[1] = 1 ;
118 NLFLAG[2] = 1 ;
119 NLFLAG[3] = 0 ;
120 NLFLAG[4] = 1 ;
121 NLFLAG[5] = 1 ;
122 NLFLAG[6] = 0 ;
123 NLFLAG[7] = 0 ;
124 NLFLAG[8] = 1 ;
125 NLFLAG[9] = 0 ;
126 /* */
127 /* Value (Linear only) */
128 /* X[0] X[1] X[2] X[3] X[4] */
129 /* 0: -1 -1 NL NL */
130 /* 1: NL NL NL -1 */
131 /* 2: 1 2 */
132 /* */
133 VALUE[0] = -1. ;
134 VALUE[3] = -1. ;
135 VALUE[6] = -1. ;
136 VALUE[7] = 1. ;
137 VALUE[9] = 2. ;
138
139 return 0;
140}
141
142
147int COI_CALLCONV Tut_FDEval( const double X[], double* G, double JAC[], int ROWNO, const int JACNUM[], int MODE,
148 int IGNERR, int* ERRCNT, int NUMVAR, int NUMJAC, int THREAD, void* USRMEM )
149{
150 /* */
151 /* Declare local copies of the optimization variables. This is */
152 /* just for convenience to make the expressions easier to read. */
153 /* */
154 double L, Inp, Out, P, K;
155 /* */
156 /* Declare parameters and their data values. */
157 /* */
158 double Al = 0.16;
159 double Ak = 2.0;
160 double Ainp = 0.16;
161 double Rho = 1.0;
162 double hold1, hold2, hold3;
163
164 /* */
165 /* Move the optimization variables from the X vector to a set */
166 /* of local variables with the same names as the variables in */
167 /* the model description. This is not necessary, but it should make*/
168 /* the equations easier to recognize. */
169 /* This time we work with the C numbering convention */
170 /* */
171 L = X[0];
172 K = X[1];
173 Inp = X[2];
174 Out = X[3];
175 P = X[4];
176 /* */
177 /* Row 0: the objective function is nonlinear */
178 /* */
179
180 if ( ROWNO == 0 ) {
181 /* */
182 /* Mode = 1 or 3. Function value: G = P * Out */
183 /* */
184 if ( MODE == 1 || MODE == 3 )
185 *G = P * Out;
186 /* */
187 /* Mode = 2 or 3: Derivative values: */
188 /* */
189 if ( MODE == 2 || MODE == 3 ) {
190 JAC[3] = P; /* derivative w.r.t. Out = X[3] */
191 JAC[4] = Out; /* derivative w.r.t. P = X[4] */
192 }
193 }
194 /* */
195 /* Row 1: The production function is nonlinear */
196 /* */
197 else if ( ROWNO == 1 ) {
198 /* */
199 /* Compute some common terms */
200 /* */
201 hold1 = (Al*pow(L,(-Rho)) + Ak*pow(K,(-Rho)) + Ainp*pow(Inp,(-Rho)));
202 hold2 = pow(hold1,( -1./Rho ));
203 /* */
204 /* Mode = 1 or 3: Function value */
205 /* */
206 if ( MODE == 1 || MODE == 3 )
207 *G = hold2;
208 /* */
209 /* Mode = 2 or 3: Derivatives */
210 /* */
211 if ( MODE == 2 || MODE == 3 ) {
212 hold3 = hold2 / hold1;
213 JAC[0] = hold3 * Al * pow(L ,(-Rho-1.)); /* derivative w.r.t. L = X[0] */
214 JAC[1] = hold3 * Ak * pow(K ,(-Rho-1.)); /* derivative w.r.t. K = X[1] */
215 JAC[2] = hold3 * Ainp * pow(Inp,(-Rho-1.)); /* derivative w.r.t. Inp = X[2] */
216 }
217 }
218 /* */
219 /* Row = 2: The row is linear and will not be called. */
220 /* */
221
222 return 0;
223}
224
225#include "std.c"
226
229int main(int argc, char** argv)
230{
231 c_log( "Starting to execute", START );
232 /*
233 Tell CONOPT about the sizes in the model
234 */
235 COI_Error += COIDEF_NumVar ( CntVect, 5 ); /* 5 variables */
236 COI_Error += COIDEF_NumCon ( CntVect, 3 ); /* 3 constraints */
237 COI_Error += COIDEF_NumNz ( CntVect, 10 ); /* 10 nonzeros in the Jacobian */
238 COI_Error += COIDEF_NumNlNz ( CntVect, 5 ); /* 5 of which are nonlinear */
239 COI_Error += COIDEF_OptDir ( CntVect, 1 ); /* Maximize */
240 COI_Error += COIDEF_ObjCon ( CntVect, 0 ); /* Objective is constraint 0 */
241 COI_Error += COIDEF_DebugFV ( CntVect, 0 ); /* 0 means no debugging, 1 each iter */
242 COI_Error += COIDEF_StdOut ( CntVect, 0 ); /* 1 means Allow output to StdOut */
243 COI_Error += COIDEF_Optfile ( CntVect, "tutorial.opt"); /* Register the options file */
244 /*
245 Register the necessary callback routines with CONOPT
246 */
247 COI_Error += COIDEF_Message ( CntVect, &Std_Message ); /* Register the callback Message */
248 COI_Error += COIDEF_ErrMsg ( CntVect, &Std_ErrMsg ); /* Register the callback ErrMsg */
249 COI_Error += COIDEF_Status ( CntVect, &Std_Status ); /* Register the callback Status */
250 COI_Error += COIDEF_Solution ( CntVect, &Std_Solution ); /* Register the callback Solution */
251 COI_Error += COIDEF_ReadMatrix( CntVect, &Tut_ReadMatrix); /* Register the callback ReadMatrix */
252 COI_Error += COIDEF_FDEval ( CntVect, &Tut_FDEval); /* Register the callback FDEval */
253
254#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
255 COI_Error += COIDEF_License ( CntVect, LICENSE_INT_1, LICENSE_INT_2, LICENSE_INT_3, LICENSE_TEXT);
256#endif
257
258 if ( COI_Error ) {
259 printf("Skipping COI_Solve due to setup errors. COI_Error = %d\n",COI_Error);
260 c_log( "Skipping Solve due to setup errors", COI_Error);
261 }
262 COI_Error = COI_Solve ( CntVect ); /* Optimize */
263 printf("After solving. COI_Error =%d\n",COI_Error);
264 if ( COI_Error ) {
265 c_log( "Errors encountered during solution", COI_Error); }
266 else if ( stacalls == 0 || solcalls == 0 ) {
267 c_log( "Status or Solution routine was not called", -1); }
268 else if ( mstat != 2 || sstat != 1 ) {
269 c_log( "Incorrect Model or Solver Status", -1); }
270 else if ( fabs( OBJ-0.572943 ) > 0.000001 ) {
271 c_log( "Incorrect objective returned", -1); }
272
273 c_log( "Successful Solve", OK );
274}
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_StdOut(coiHandle_t cntvect, int tostdout)
allow output to StdOut.
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_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
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition tutorialk.c:229
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 tutorialk.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 tutorialk.c:147