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