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