CONOPT
Loading...
Searching...
No Matches
mp_trans.c
Go to the documentation of this file.
1
37
38#include <stdlib.h>
39#include <stdio.h>
40#include <math.h>
41#include <string.h>
42#include "coiheader.h"
43#include "comdecl.h"
44
45#define ni 30
46#define nj 50
47double Tot = 5000.0;
48double A[ni];
49double B[nj];
50double Cost[ni*nj];
51
52void defdata()
53{
54 int i,j,k;
55 double si, sj;
56
57 si = 0; for ( i = 0; i < ni; i++ ) { si = si + i+1; };
58 sj = 0; for ( j = 0; j < nj; j++ ) { sj = sj + j+1; };
59 for ( i = 0; i < ni; i++ ) { A[i] = Tot*(i+1)/si; };
60 for ( j = 0; j < nj; j++ ) { B[j] = Tot*(j+1)/sj; };
61 k = 0;
62 for ( i = 0; i < ni; i++ ) {
63 for ( j = 0; j < nj; j++ )
64 { Cost[k] = i+1 + 2*(j+1); k++; };
65 }
66}
67
68
73int COI_CALLCONV Trans_ReadMatrix( double LOWER[], double CURR[], double UPPER[], int VSTA[], int TYPE[], double RHS[],
74 int ESTA[], int COLSTA[], int ROWNO[], double VALUE[],
75 int NLFLAG[], int NUMVAR, int NUMCON, int NUMNZ, void* USRMEM )
76{
77 int i, j, k, l;
78 /* */
79 /* Information about Variables: */
80 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
81 /* Default: the status information in Vsta is not used. */
82 /* */
83 k = 0;
84 for ( i = 0; i < ni; i++ ) {
85 for ( j = 0; j < nj; j++ )
86 { LOWER[k] = 0.0; k++; };
87 }
88
89 /* Information about Constraints: */
90 /* Default: Rhs = 0 */
91 /* Default: the status information in Esta and the function */
92 /* value in FV are not used. */
93 /* Default: Type: There is no default. */
94 /* 0 = Equality, */
95 /* 1 = Greater than or equal, */
96 /* 2 = Less than or equal, */
97 /* 3 = Non binding. */
98 /* */
99 for ( i=0; i<ni; i++ ) {
100 RHS[i] = A[i] ;
101 TYPE[i] = 1;
102 }
103 for ( j=0; j<nj; j++ ) {
104 RHS[ni+j] = B[j] ;
105 TYPE[ni+j] = 2;
106 }
107 /* Constraint Objective */
108 /* Rhs = 0.0 and type =E= */
109 /* */
110 TYPE[ni+nj] = 0 ;
111 /* */
112 /* Information about the Jacobian. We use the standard method with */
113 /* Rowno, Value, Nlflag and Colsta and we do not use Colno. */
114 /* */
115 /* Colsta = Start of column indices (No Defaults): */
116 /* Rowno = Row indices */
117 /* Value = Value of derivative (by default only linear */
118 /* derivatives are used) */
119 /* Nlflag = 0 for linear and 1 for nonlinear derivative */
120 /* (not needed for completely linear models) */
121 /* */
122 /* Indices */
123 /* x(i,j) obj */
124 /* i: 1 */
125 /* j: 1 */
126 /* obj cost -1 */
127
128 k = 0; /* counter for current variable */
129 l = 0; /* counter for current nonzero */
130 for ( i = 0; i < ni; i++ ) {
131 for ( j = 0; j < nj; j++ ) {
132 COLSTA[k] = l;
133 ROWNO[l] = i;
134 NLFLAG[l] = 0;
135 VALUE[l] = 1.0;
136 l++;
137 ROWNO[l] = ni+j;
138 NLFLAG[l] = 0;
139 VALUE[l] = 1.0;
140 l++;
141 ROWNO[l] = ni+nj;
142 NLFLAG[l] = 0;
143 VALUE[l] = Cost[k];
144 l++;
145 k++;
146 }
147 }
148 COLSTA[k] = l;
149 ROWNO[l] = ni+nj;
150 NLFLAG[l] = 0;
151 VALUE[l] = -1.0;
152 l++; k++;
153 COLSTA[k] = l;
154 return 0;
155}
156
157
162int COI_CALLCONV Trans_FDEval( const double X[], double* G, double JAC[], int ROWNO, const int JACNUM[], int MODE,
163 int IGNERR, int* ERRCNT, int NUMVAR, int NUMJAC, int THREAD, void* USRMEM )
164{
165 /* */
166 /* The model is linear and the routine should not be called */
167 /* so we return 1 = error */
168 /* */
169 return 1;
170}
171
172#include "std.c"
173
176int main(int argc, char** argv)
177{
178 c_log( "Starting to execute", START );
179
180 defdata();
181 /*
182 Tell CONOPT about the sizes in the model
183 */
184 COI_Error += COIDEF_NumVar ( CntVect, ni*nj+1 ); /* ni*nj+1 variables */
185 COI_Error += COIDEF_NumCon ( CntVect, ni+nj+1 ); /* ni+nj+1 constraints */
186 COI_Error += COIDEF_NumNz ( CntVect, 3*ni*nj+1 ); /* 3*ni*nj+1 nonzeros in the Jacobian */
187 COI_Error += COIDEF_NumNlNz ( CntVect, 0 ); /* 0 of which are nonlinear */
188 COI_Error += COIDEF_OptDir ( CntVect, -1 ); /* Minimize */
189 COI_Error += COIDEF_ObjVar ( CntVect, ni*nj ); /* Objective is variable ni*nj */
190 COI_Error += COIDEF_DebugFV ( CntVect, 0 ); /* 0 means no debugging, 1 each iter */
191 COI_Error += COIDEF_StdOut ( CntVect, 0 ); /* 1 means Allow output to StdOut */
192 /*
193 Register the necessary callback routines with CONOPT
194 */
195 COI_Error += COIDEF_Message ( CntVect, &Std_Message ); /* Register the callback Message */
196 COI_Error += COIDEF_ErrMsg ( CntVect, &Std_ErrMsg ); /* Register the callback ErrMsg */
197 COI_Error += COIDEF_Status ( CntVect, &Std_Status ); /* Register the callback Status */
198 COI_Error += COIDEF_Solution ( CntVect, &Std_Solution ); /* Register the callback Solution */
199 COI_Error += COIDEF_ReadMatrix( CntVect, &Trans_ReadMatrix); /* Register the callback ReadMatrix */
200 COI_Error += COIDEF_FDEval ( CntVect, &Trans_FDEval); /* Register the callback FDEval */
201
202#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
203 COI_Error += COIDEF_License ( CntVect, LICENSE_INT_1, LICENSE_INT_2, LICENSE_INT_3, LICENSE_TEXT);
204#endif
205
206 if ( COI_Error ) {
207 printf("Skipping COI_Solve due to setup errors. COI_Error = %d\n",COI_Error);
208 c_log( "Skipping Solve due to setup errors", COI_Error); }
209 COI_Error = COI_Solve ( CntVect ); /* Optimize */
210 printf("1 Thread: End of Trans Example 1. Return code =%d\n",COI_Error);
211 if ( COI_Error ) {
212 c_log( "1 Thread: Errors encountered during first solve", COI_Error); }
213 else if ( stacalls == 0 || solcalls == 0 ) {
214 c_log( "1 Thread: Status or Solution routine was not called during first solve called", -1); }
215 else if ( !( sstat == 1 && mstat == 1 ) ) {
216 c_log( "1 Thread: Solver or Model status was not as expected (1,1)", -1); }
217 else if ( fabs( OBJ-438333.3333 ) > 0.001 ) {
218 c_log( "1 Thread: Incorrect objective returned from first solve", -1); };
219
220/* Register free use of threads and run again */
221
222 COI_Error += COIDEF_ThreadS ( CntVect, 0 ); /* 0 means take what you can get */
223 COI_Error = COI_Solve ( CntVect ); /* Optimize */
224 printf("Multi Thread: End of Trans Example 1. Return code =%d\n",COI_Error);
225 if ( COI_Error ) {
226 c_log( "Multi Thread: Errors encountered during second solve", COI_Error); }
227 else if ( stacalls == 0 || solcalls == 0 ) {
228 c_log( "Multi Thread: Status or Solution routine was not called during first second called", -1); }
229 else if ( !( sstat == 1 && mstat == 1 ) ) {
230 c_log( "Multi Thread: Solver or Model status was not as expected (1,1)", -1); }
231 else if ( fabs( OBJ-438333.3333 ) > 0.001 ) {
232 c_log( "Multi Thread: Incorrect objective returned from second solve", -1); };
233
234 c_log( "Successful Solve", OK );
235}
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_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_ThreadS(coiHandle_t cntvect, int threads)
number of threads allowed internally in CONOPT.
int COI_CALLCONV COIDEF_ObjVar(coiHandle_t cntvect, int objvar)
defines the Objective Variable.
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.
double A[nobs *dimx]
Definition leastsq.c:18
double B[nobs *dimx]
Definition leastsq.c:19
double Cost[ni *nj]
Definition mp_trans.c:50
int COI_CALLCONV Trans_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 mp_trans.c:73
#define ni
Definition mp_trans.c:45
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition mp_trans.c:176
int COI_CALLCONV Trans_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 mp_trans.c:162
#define nj
Definition mp_trans.c:46
void defdata()
Definition mp_trans.c:52
double Tot
Definition mp_trans.c:47
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