CONOPT
Loading...
Searching...
No Matches
qp3.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 for ( k=0; k<NQ; k++ ) {
130 i = Qrow[k]; j = Qcol[k];
131 if ( i == j )
132 JAC[i] += Q[k]*(X[i]-Target[i]);
133 else {
134 JAC[i] += Q[k]*(X[j]-Target[j]);
135 JAC[j] += Q[k]*(X[i]-Target[i]);
136 }
137 }
138 }
139 }
140 /* */
141 /* Row = 2: The row is linear and will not be called. */
142 /* */
143
144 return 0;
145}
146
147
152int COI_CALLCONV QP_2DLagrStr( int HSRW[], int HSCL[], int* NODRV,
153 int NUMVAR, int NUMCON, int NHESS, void* USRMEM )
154{
155 int k;
156 for ( k=0; k<NQ; k++ ) {
157 HSRW[k] = Qrow[k];
158 HSCL[k] = Qcol[k];
159 }
160 return 0;
161}
162
163
168int COI_CALLCONV QP_2DLagrVal( const double X[], const double U[], const int HSRW[], const int HSCL[],
169 double HSVL[], int* NODRV, int NUMVAR, int NUMCON, int NHESS, void* USRMEM )
170{
171 int k;
172
173 for ( k=0; k<NQ; k++ )
174 HSVL[k] = Q[k]*U[0];
175 return 0;
176}
177
178#include "std.c"
179
182int main(int argc, char** argv)
183{
184 int i,j;
185
186 c_log( "Starting to execute", START );
187/*
188 Initialize the Q matrix
189 */
190 j = 0;
191 for ( i=0; i<NN; i++ ) {
192 Target[i] = 10.0;
193 Q[j] = 1.0;
194 Qrow[j] = i;
195 Qcol[j] = i;
196 j++;
197 if ( i < NN-1 ) {
198 Q[j] = 0.1;
199 Qrow[j] = i+1;
200 Qcol[j] = i;
201 j++;
202 }
203 }
204 /*
205 Tell CONOPT about the sizes in the model
206 */
207 COI_Error += COIDEF_NumVar ( CntVect, NN ); /* NN variables */
208 COI_Error += COIDEF_NumCon ( CntVect, 2 ); /* 2 constraints */
209 COI_Error += COIDEF_NumNz ( CntVect, 2*NN ); /* 2*NN nonzeros in the Jacobian */
210 COI_Error += COIDEF_NumNlNz ( CntVect, NN ); /* NN of which are nonlinear */
211 COI_Error += COIDEF_NumHess ( CntVect, NQ ); /* NQ nonzero Hessian elements */
212 COI_Error += COIDEF_OptDir ( CntVect, -1 ); /* Minimize */
213 COI_Error += COIDEF_ObjCon ( CntVect, 0 ); /* Objective is constraint 0 */
214 COI_Error += COIDEF_DebugFV ( CntVect, 0 ); /* 0 means no debugging, 1 each iter */
215 COI_Error += COIDEF_StdOut ( CntVect, 0 ); /* 1 means Allow output to StdOut */
216 /*
217 Register the necessary callback routines with CONOPT
218 */
219 COI_Error += COIDEF_Message ( CntVect, &Std_Message ); /* Register the callback Message */
220 COI_Error += COIDEF_ErrMsg ( CntVect, &Std_ErrMsg ); /* Register the callback ErrMsg */
221 COI_Error += COIDEF_Status ( CntVect, &Std_Status ); /* Register the callback Status */
222 COI_Error += COIDEF_Solution ( CntVect, &Std_Solution ); /* Register the callback Solution */
223 COI_Error += COIDEF_ReadMatrix( CntVect, &QP_ReadMatrix); /* Register the callback ReadMatrix */
224 COI_Error += COIDEF_FDEval ( CntVect, &QP_FDEval); /* Register the callback FDEval */
225 COI_Error += COIDEF_2DLagrStr ( CntVect, &QP_2DLagrStr); /* Register the callback 2DLagrStr */
226 COI_Error += COIDEF_2DLagrVal ( CntVect, &QP_2DLagrVal); /* Register the callback 2DLagrVal */
227
228#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
229 COI_Error += COIDEF_License ( CntVect, LICENSE_INT_1, LICENSE_INT_2, LICENSE_INT_3, LICENSE_TEXT);
230#endif
231
232 if ( COI_Error ) {
233 printf("Skipping COI_Solve due to setup errors. COI_Error = %d\n",COI_Error);
234 c_log( "Skipping Solve due to setup errors", COI_Error); }
235 COI_Error = COI_Solve ( CntVect ); /* Optimize */
236 printf("After solving. COI_Error =%d\n",COI_Error);
237 if ( COI_Error ) {
238 c_log( "Errors encountered during first solve", COI_Error); }
239 else if ( stacalls == 0 || solcalls == 0 ) {
240 c_log( "Status or Solution routine was not called", -1); }
241 else if ( mstat != 2 || sstat != 1 ) {
242 c_log( "Incorrect Model or Solver Status", -1); }
243 else if ( fabs( OBJ-59978.000 ) > 0.001 ) {
244 c_log( "Incorrect objective returned", -1); };
245
246 c_log( "Successful Solve", OK );
247}
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_2DLagrVal(coiHandle_t cntvect, COI_2DLAGRVAL_t coi_2dlagrval)
define callback routine for computing the values of the second derivatives of the Lagrangian.
int COI_CALLCONV COIDEF_2DLagrStr(coiHandle_t cntvect, COI_2DLAGRSTR_t coi_2dlagrstr)
define callback routine for providing the structure of the second derivatives of the Lagrangian.
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_NumHess(coiHandle_t cntvect, int numhess)
defines the Number of Hessian Nonzeros.
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 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 main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition qp3.c:182
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 qp3.c:99
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 qp3.c:33
int COI_CALLCONV QP_2DLagrStr(int HSRW[], int HSCL[], int *NODRV, int NUMVAR, int NUMCON, int NHESS, void *USRMEM)
Specify the structure of the Lagrangian of the Hessian.
Definition qp3.c:152
int COI_CALLCONV QP_2DLagrVal(const double X[], const double U[], const int HSRW[], const int HSCL[], double HSVL[], int *NODRV, int NUMVAR, int NUMCON, int NHESS, void *USRMEM)
Compute the Lagrangian of the Hessian.
Definition qp3.c:168
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