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