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