CONOPT
Loading...
Searching...
No Matches
qp1.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
14/* QP Model: Min (x-target)*Q*(x-target)/2 s.t.
15 sum( x ) = 1;
16 where x is a vector of non-negative variables of length NN
17 target is a vector of +10, and
18 Q is a matrix with +1 on the diagonal and 0.1 on the
19 first upper and lower bi-diagonal */
20
21#define NN 1000 /* Size of the QP model */
22#define NQ (NN*2-1)
23double Q[NQ];
24double Target[NN];
25int Qrow[NQ];
26int Qcol[NQ];
27
28
33int COI_CALLCONV QP_ReadMatrix( double LOWER[], double CURR[], double UPPER[], int VSTA[], int TYPE[], double RHS[],
34 int ESTA[], int COLSTA[], int ROWNO[], double VALUE[],
35 int NLFLAG[], int NUMVAR, int NUMCON, int NUMNZ, void* USRMEM )
36{
37 int i, j;
38 /* */
39 /* Information about Variables: */
40 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
41 /* Default: the status information in Vsta is not used. */
42 /* */
43 /* Lower bound = 0 on all variables: */
44 /* */
45 for ( i=0; i<NN; i++ )
46 LOWER[i] = 0.0 ;
47 /* */
48 /* Information about Constraints: */
49 /* Default: Rhs = 0 */
50 /* Default: the status information in Esta and the function */
51 /* value in FV are not used. */
52 /* Default: Type: There is no default. */
53 /* 0 = Equality, */
54 /* 1 = Greater than or equal, */
55 /* 2 = Less than or equal, */
56 /* 3 = Non binding. */
57 /* */
58 /* Constraint 0 (Objective) */
59 /* Rhs = 0.0 and type Non binding */
60 /* */
61 TYPE[0] = 3 ;
62 /* */
63 /* Constraint 1 (Sum to 1) */
64 /* Rhs = 1 and type Equality */
65 /* */
66 RHS[1] = 1.0 ;
67 TYPE[1] = 0 ;
68 /* */
69 /* Information about the Jacobian. We use the standard method with */
70 /* Rowno, Value, Nlflag and Colsta and we do not use Colno. */
71 /* */
72 /* Colsta = Start of column indices (No Defaults): */
73 /* Rowno = Row indices */
74 /* Value = Value of derivative (by default only linear */
75 /* derivatives are used) */
76 /* Nlflag = 0 for linear and 1 for nonlinear derivative */
77 /* (not needed for completely linear models) */
78 /* */
79 j = 0; /* counter for current nonzero */
80 for ( i=0; i<NN; i++ ) {
81 COLSTA[i] = j;
82 ROWNO[j] = 0;
83 NLFLAG[j] = 1;
84 j++;
85 ROWNO[j] = 1;
86 VALUE[j] = 1.0;
87 NLFLAG[j] = 0;
88 j++;
89 }
90 COLSTA[NN] = j;
91 return 0;
92}
93
94
99int COI_CALLCONV QP_FDEval( const double X[], double* G, double JAC[], int ROWNO, const int JACNUM[], int MODE,
100 int IGNERR, int* ERRCNT, int NUMVAR, int NUMJAC, int THREAD, void* USRMEM )
101{
102 int i, j, k;
103 double sum;
104 /* */
105 /* Row 0: the objective function is nonlinear */
106 /* */
107
108 if ( ROWNO == 0 ) {
109 /* */
110 /* Mode = 1 or 3. Function value: G = P * Out */
111 /* */
112 if ( MODE == 1 || MODE == 3 ) {
113 sum = 0.0;
114 for ( k=0; k<NQ; k++ ) {
115 i = Qrow[k]; j = Qcol[k];
116 if ( i == j )
117 sum += (X[i]-Target[i])*Q[k]*(X[i]-Target[i]);
118 else
119 sum += 2*(X[i]-Target[i])*Q[k]*(X[j]-Target[j]);
120 }
121 *G = sum / 2;
122 }
123 /* */
124 /* Mode = 2 or 3: Derivative values: */
125 /* */
126 if ( MODE == 2 || MODE == 3 ) {
127 for ( i=0; i<NN; i++ ) {
128 JAC[i] = 0.0;
129 }
130 for ( k=0; k<NQ; k++ ) {
131 i = Qrow[k]; j = Qcol[k];
132 if ( i == j )
133 JAC[i] += Q[k]*(X[i]-Target[i]);
134 else {
135 JAC[i] += Q[k]*(X[j]-Target[j]);
136 JAC[j] += Q[k]*(X[i]-Target[i]);
137 }
138 }
139 }
140 }
141 /* */
142 /* Row = 2: The row is linear and will not be called. */
143 /* */
144
145 return 0;
146}
147
148#include "std.c"
149
152int main(int argc, char** argv)
153{
154 int i,j;
155
156 c_log( "Starting to execute", START );
157
158/*
159 Initialize the Q matrix
160 */
161 j = 0;
162 for ( i=0; i<NN; i++ ) {
163 Target[i] = 10.0;
164 Q[j] = 1.0;
165 Qrow[j] = i;
166 Qcol[j] = i;
167 j++;
168 if ( i < NN-1 ) {
169 Q[j] = 0.1;
170 Qrow[j] = i+1;
171 Qcol[j] = i;
172 j++;
173 }
174 }
175 /*
176 Tell CONOPT about the sizes in the model
177 */
178 COI_Error += COIDEF_NumVar ( CntVect, NN ); /* NN variables */
179 COI_Error += COIDEF_NumCon ( CntVect, 2 ); /* 2 constraints */
180 COI_Error += COIDEF_NumNz ( CntVect, 2*NN ); /* 2*NN nonzeros in the Jacobian */
181 COI_Error += COIDEF_NumNlNz ( CntVect, NN ); /* NN of which are nonlinear */
182 COI_Error += COIDEF_OptDir ( CntVect, -1 ); /* Minimize */
183 COI_Error += COIDEF_ObjCon ( CntVect, 0 ); /* Objective is constraint 0 */
184 COI_Error += COIDEF_DebugFV ( CntVect, 0 ); /* 0 means no debugging, 1 each iter */
185 COI_Error += COIDEF_StdOut ( CntVect, 0 ); /* 1 means Allow output to StdOut */
186 /*
187 Register the necessary callback routines with CONOPT
188 */
189 COI_Error += COIDEF_Message ( CntVect, &Std_Message ); /* Register the callback Message */
190 COI_Error += COIDEF_ErrMsg ( CntVect, &Std_ErrMsg ); /* Register the callback ErrMsg */
191 COI_Error += COIDEF_Status ( CntVect, &Std_Status ); /* Register the callback Status */
192 COI_Error += COIDEF_Solution ( CntVect, &Std_Solution ); /* Register the callback Solution */
193 COI_Error += COIDEF_ReadMatrix( CntVect, &QP_ReadMatrix); /* Register the callback ReadMatrix */
194 COI_Error += COIDEF_FDEval ( CntVect, &QP_FDEval); /* Register the callback FDEval */
195
196#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
197 COI_Error += COIDEF_License ( CntVect, LICENSE_INT_1, LICENSE_INT_2, LICENSE_INT_3, LICENSE_TEXT);
198#endif
199
200 if ( COI_Error ) {
201 printf("Skipping COI_Solve due to setup errors. COI_Error = %d\n",COI_Error);
202 c_log( "Skipping Solve due to setup errors", COI_Error); }
203 COI_Error = COI_Solve ( CntVect ); /* Optimize */
204 printf("After first solve. COI_Error =%d\n",COI_Error);
205 if ( COI_Error ) {
206 c_log( "Errors encountered during first solve", COI_Error); }
207 else if ( stacalls == 0 || solcalls == 0 ) {
208 c_log( "Status or Solution routine was not called during first solve.", -1); }
209 else if ( mstat != 2 || sstat != 1 ) {
210 c_log( "Incorrect Model or Solver Status from first solve.", -1); }
211 else if ( fabs( OBJ-59978.000 ) > 0.001 ) {
212 c_log( "Incorrect objective returned from first solve.", -1); };
213
214 stacalls = 0; solcalls = 0;
215/* Define a large limit on the number of superbasics using COIDEF_MaxSup */
216
218 if ( COI_Error ) {
219 printf("Skipping second COI_Solve due to setup errors. COI_Error = %d\n",COI_Error);
220 c_log( "Skipping second Solve due to setup errors", COI_Error); }
221 COI_Error = COI_Solve ( CntVect ); /* Optimize */
222 printf("After second solve. COI_Error =%d\n",COI_Error);
223 if ( COI_Error ) {
224 c_log( "Errors encountered during second solve", COI_Error); }
225 else if ( stacalls == 0 || solcalls == 0 ) {
226 c_log( "Status or Solution routine was not called during second called", -1); }
227 else if ( mstat != 2 || sstat != 1 ) {
228 c_log( "Incorrect Model or Solver Status from second solve", -1); }
229 else if ( fabs( OBJ-59978.000 ) > 0.001 ) {
230 c_log( "Incorrect objective returned from second solve", -1); };
231
232 stacalls = 0; solcalls = 0;
233/* Register an options file and solve again after resetting MaxSup to a small number */
234
235 COI_Error += COIDEF_Optfile( CntVect, "qp.opt"); /* Register the Options file */
237 if ( COI_Error ) {
238 printf("Skipping third COI_Solve due to setup errors. COI_Error = %d\n",COI_Error);
239 c_log( "Skipping third Solve due to setup errors", COI_Error); }
240 COI_Error = COI_Solve ( CntVect ); /* Optimize */
241 printf("After third solve. COI_Error =%d\n",COI_Error);
242 if ( COI_Error ) {
243 c_log( "Errors encountered during third solve", COI_Error); }
244 else if ( stacalls == 0 || solcalls == 0 ) {
245 c_log( "Status or Solution routine was not called during third called", -1); }
246 else if ( mstat != 2 || sstat != 1 ) {
247 c_log( "Incorrect Model or Solver Status from third solve", -1); }
248 else if ( fabs( OBJ-59978.000 ) > 0.001 ) {
249 c_log( "Incorrect objective returned from third solve", -1); }
250 c_log( "Successful Solve", OK );
251}
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_MaxSup(coiHandle_t cntvect, int maxsup)
limit on superbasics.
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.
#define NN
Definition qp1.c:21
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition qp1.c:152
int COI_CALLCONV QP_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 qp1.c:99
#define NQ
Definition qp1.c:22
double Q[NQ]
Definition qp1.c:23
int COI_CALLCONV QP_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 qp1.c:33
int Qrow[NQ]
Definition qp1.c:25
double Target[NN]
Definition qp1.c:24
int Qcol[NQ]
Definition qp1.c:26
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