CONOPT
Loading...
Searching...
No Matches
leastsq5.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
14int seed = 12359; /* seed for random number generator */
15#define nobs 700
16#define dimx 500
17double A[nobs*dimx];
18double B[nobs*dimx];
19double Obs[nobs];
20
21float rndx()
22{
23/*
24 Defines a pseudo random number between 0 and 1 */
25 int times;
26 float result;
27 seed = seed*1027+25;
28 times = seed / 1048576;
29 seed = seed - 1048576*times;
30 result = (float)seed;
31 result = result / 1048576;
32/* fprintf(stdout,"Seed=%d, result=%f8 \n",seed, result); */
33 return result;
34}
35
36void defdata()
37{
38 int i,j,k;
39 double Xtarg = -1.0;
40 double Noise = 1.0;
41 double O;
42
43 k = 0;
44 for ( i = 0; i < nobs; i++ ) {
45 O = 0.0;
46 for ( j = 0; j < dimx; j++ ) {
47 A[k] = rndx();
48 B[k] = rndx();
49 O = O + A[k] * Xtarg + B[k] * pow(Xtarg,2);
50 k = k + 1;
51 }
52 Obs[i] = O + Noise * rndx();
53 }
54}
55
56
61int COI_CALLCONV Leastsq_ReadMatrix( double LOWER[], double CURR[], double UPPER[], int VSTA[], int TYPE[], double RHS[],
62 int ESTA[], int COLSTA[], int ROWNO[], double VALUE[],
63 int NLFLAG[], int NUMVAR, int NUMCON, int NUMNZ, void* USRMEM )
64{
65 int i, j, k;
66 /* */
67 /* Information about Variables: */
68 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
69 /* Default: the status information in Vsta is not used. */
70 /* */
71 for ( i=0; i<dimx; i++ ) {
72 CURR[i] = -0.8 ;
73 }
74 /* Information about Constraints: */
75 /* Default: Rhs = 0 */
76 /* Default: the status information in Esta and the function */
77 /* value in FV are not used. */
78 /* Default: Type: There is no default. */
79 /* 0 = Equality, */
80 /* 1 = Greater than or equal, */
81 /* 2 = Less than or equal, */
82 /* 3 = Non binding. */
83 /* */
84 for ( i=0; i<nobs; i++ ) {
85 RHS[i] = Obs[i] ;
86 TYPE[i] = 0;
87 }
88
89 /* Constraint nobs (Objective) */
90 /* Rhs = 0.0 and type Non binding */
91 /* */
92 TYPE[nobs] = 3 ;
93 /* */
94 /* Information about the Jacobian. We use the standard method with */
95 /* Rowno, Value, Nlflag and Colsta and we do not use Colno. */
96 /* */
97 /* Colsta = Start of column indices (No Defaults): */
98 /* Rowno = Row indices */
99 /* Value = Value of derivative (by default only linear */
100 /* derivatives are used) */
101 /* Nlflag = 0 for linear and 1 for nonlinear derivative */
102 /* (not needed for completely linear models) */
103 /* */
104 /* Indices */
105 /* x(i) res(j) */
106 /* j: NL L=1 */
107 /* obj: NL */
108
109 k = 0; /* counter for current nonzero */
110 for ( j=0; j<dimx; j++ ) {
111 COLSTA[j] = k;
112 for (i=0; i<nobs; i++ ) {
113 ROWNO[k] = i;
114 NLFLAG[k] = 1;
115 k = k + 1;
116 }
117 }
118 for (i=0; i<nobs; i++ ) {
119 COLSTA[dimx+i] = k;
120 ROWNO[k] = i;
121 NLFLAG[k] = 0;
122 VALUE[k] = 1.0;
123 k = k + 1;
124 ROWNO[k] = nobs;
125 NLFLAG[k] = 1;
126 k = k + 1;
127 }
128 COLSTA[dimx+nobs] = k;
129 return 0;
130}
131
132
137int COI_CALLCONV Leastsq_FDEval( const double X[], double* G, double JAC[], int ROWNO, const int JACNUM[], int MODE,
138 int IGNERR, int* ERRCNT, int NUMVAR, int NUMJAC, int THREAD, void* USRMEM )
139{
140 int i, j, k;
141 double sum;
142 /* */
143 /* Row nobs: the objective function is nonlinear */
144 /* */
145
146 if ( ROWNO == nobs ) {
147 /* */
148 /* Mode = 1 or 3. Function value: G = P * Out */
149 /* */
150 if ( MODE == 1 || MODE == 3 ) {
151 sum = 0.0;
152 for (i=0; i<nobs; i++ ) {
153 sum = sum + pow(X[dimx+i],2);
154 }
155 *G = sum;
156 }
157 /* */
158 /* Mode = 2 or 3: Derivative values: */
159 /* */
160 if ( MODE == 2 || MODE == 3 ) {
161 for (i=0; i<nobs; i++ ) {
162 JAC[dimx+i] = 2*X[dimx+i];
163 }
164 }
165 }
166/*
167 Row 0 to nobs-1: The observation definitions: */
168 else {
169/*
170 Mode = 1 or 3: Function value - x-part only */
171
172 if ( MODE == 1 || MODE == 3 ) {
173 k = ROWNO*dimx;
174 sum = 0.0;
175 for ( j=0; j<dimx; j++ ) {
176 sum = sum + A[k]*X[j] + B[k]*pow(X[j],2);
177 k = k + 1;
178 }
179 *G = sum;
180 }
181/*
182 Mode = 2 or 3: Derivatives */
183
184 if ( MODE == 2 || MODE == 3 ) {
185 k = ROWNO*dimx;
186 for ( j=0; j<dimx; j++ ) {
187 JAC[j] = A[k] + 2*B[k]*X[j];
188 k = k + 1;
189 }
190 }
191 }
192 return 0;
193}
194
199int COI_CALLCONV Leastsq_2DDir( const double X[], const double DX[], double D2G[],
200 int ROWNO, const int JACNUM[], int* NODRV, int NUMVAR, int NUMJAC,
201 int THREAD, void* USRMEM )
202{
203int i, j, k;
204
205 if ( ROWNO == nobs ) {
206
207/* Objective row: sum(i, res(i)**2 ) */
208
209 for ( j=0; j<dimx; j++ ) D2G[j] = 0;
210 for ( i=0; i<nobs; i++ ) D2G[dimx+i] = 2.0*DX[dimx+i];
211 }
212 else {
213
214/* Constraint row with b(i,j)*x(j)**2 as only nonlinear term */
215
216 k = ROWNO*dimx;
217 for ( j=0; j<dimx; j++ ) {
218 D2G[j] = 2.0*B[k]*DX[j];
219 k++; }
220 for ( i=0; i<nobs; i++ ) D2G[dimx+i] = 0;
221 }
222 return 0;
223}
224
225#include "std.c"
226
229int main(int argc, char** argv)
230{
231 c_log( "Starting to execute", START );
232
233 defdata();
234 /*
235 Tell CONOPT about the sizes in the model
236 */
237 COI_Error += COIDEF_NumVar ( CntVect, nobs+dimx ); /* nobs+dimx variables */
238 COI_Error += COIDEF_NumCon ( CntVect, nobs+1 ); /* nobs+1 constraints */
239 COI_Error += COIDEF_NumNz ( CntVect, nobs*(dimx+2) ); /* nobs*(dimx+2) nonzeros in the Jacobian */
240 COI_Error += COIDEF_NumNlNz ( CntVect, nobs*(dimx+1) ); /* nobs*(dimx+1) of which are nonlinear */
241 COI_Error += COIDEF_OptDir ( CntVect, -1 ); /* Minimize */
242 COI_Error += COIDEF_ObjCon ( CntVect, nobs ); /* Objective is constraint nobs */
243 COI_Error += COIDEF_DebugFV ( CntVect, 0 ); /* 0 means no debugging, 1 each iter, -1 first */
244 COI_Error += COIDEF_Debug2D ( CntVect, 0 ); /* 0 means no debugging, 1 each iter, -1 first */
245 COI_Error += COIDEF_StdOut ( CntVect, 0 ); /* 1 means Allow output to StdOut */
246 COI_Error += COIDEF_Optfile ( CntVect, "leastsq5.opt" ); /* Register the options file */
247 /*
248 Register the necessary callback routines with CONOPT
249 */
250 COI_Error += COIDEF_Message ( CntVect, &Std_Message ); /* Register the callback Message */
251 COI_Error += COIDEF_ErrMsg ( CntVect, &Std_ErrMsg ); /* Register the callback ErrMsg */
252 COI_Error += COIDEF_Status ( CntVect, &Std_Status ); /* Register the callback Status */
253 COI_Error += COIDEF_Solution ( CntVect, &Std_Solution ); /* Register the callback Solution */
254 COI_Error += COIDEF_ReadMatrix( CntVect, &Leastsq_ReadMatrix); /* Register the callback ReadMatrix */
255 COI_Error += COIDEF_FDEval ( CntVect, &Leastsq_FDEval); /* Register the callback FDEval */
256 COI_Error += COIDEF_2DDir ( CntVect, &Leastsq_2DDir ); /* Register the callback 2DDir */
257
258#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
259 COI_Error += COIDEF_License ( CntVect, LICENSE_INT_1, LICENSE_INT_2, LICENSE_INT_3, LICENSE_TEXT);
260#endif
261
262 if ( COI_Error ) {
263 printf("Skipping COI_Solve due to setup errors. COI_Error = %d\n",COI_Error);
264 c_log( "Skipping Solve due to setup errors", COI_Error); }
265 COI_Error = COI_Solve ( CntVect ); /* Optimize */
266 printf("End of Least Squares Example 5. Return code =%d\n",COI_Error);
267 if ( COI_Error ) {
268 c_log( "Errors encountered during solve", COI_Error); }
269 else if ( stacalls == 0 || solcalls == 0 ) {
270 c_log( "Status or Solution routine was not called", -1); }
271 else if ( sstat != 1 || mstat != 2 ) {
272 c_log( "Solver or Model status was not as expected (1,2)", -1); }
273 else if ( fabs( OBJ-19.4443 ) > 0.001 ) {
274 c_log( "Incorrect objective returned from first solve", -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_2DDir(coiHandle_t cntvect, COI_2DDIR_t coi_2ddir)
define callback routine for computing the second derivative for a constraint in a direction.
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_Debug2D(coiHandle_t cntvect, int debug2d)
turn debugging of 2nd derivatives on and off.
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 COI_CALLCONV Leastsq_2DDir(const double X[], const double DX[], double D2G[], int ROWNO, const int JACNUM[], int *NODRV, int NUMVAR, int NUMJAC, int THREAD, void *USRMEM)
Computes the second derivative of a constraint in a direction.
Definition leastsq10.c:229
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition leastsq5.c:229
int COI_CALLCONV Leastsq_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 leastsq5.c:61
int COI_CALLCONV Leastsq_2DDir(const double X[], const double DX[], double D2G[], int ROWNO, const int JACNUM[], int *NODRV, int NUMVAR, int NUMJAC, int THREAD, void *USRMEM)
Computes the second derivative of a constraint in a direction.
Definition leastsq5.c:199
void defdata()
Definition leastsq5.c:36
int COI_CALLCONV Leastsq_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 leastsq5.c:137
float rndx()
Definition leastsq5.c:21
int seed
Definition leastsq.c:15
double A[nobs *dimx]
Definition leastsq.c:18
#define dimx
Definition leastsq.c:17
double Obs[nobs]
Definition leastsq.c:20
#define nobs
Definition leastsq.c:16
int COI_CALLCONV Leastsq_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 leastsq.c:60
int COI_CALLCONV Leastsq_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 leastsq.c:134
double B[nobs *dimx]
Definition leastsq.c:19
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