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