CONOPT
Loading...
Searching...
No Matches
leastsq4.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
195
200int COI_CALLCONV Leastsq_2DLagrStr( int HSRW[], int HSCL[], int* NODRV, int NUMVAR,
201 int NUMCON, int NHESS, void* USRMEM )
202{
203 int i;
204/*
205 We assume that this is a Large Residual model, where the most
206 important 2nd derivatives are those that appear directly in the
207 objective.
208 We will therefore only define the 2nd derivatives corresponding to
209 the objective constraint where the 2nd derivatives is 2*I.
210 CONOPT will by default complain that some nonlinear variables do
211 not appear in the Hessian. We will therefore define one dummy element
212 on the diagonal of the Hessian for each of the nonlinear X-variables
213 and give it the numerical value 0.
214 */
215/* Define structure of Hessian */
216
217 for (i=0; i<dimx+nobs; i++ ) {
218 HSRW[i] = i;
219 HSCL[i] = i;
220 }
221
222 return 0;
223}
224
225
230int COI_CALLCONV Leastsq_2DLagrVal( const double X[], const double U[], const int HSRW[], const int HSCL[], double HSVL[],
231 int* NODRV, int NUMVAR, int NUMCON, int NHESS, void* USRMEM )
232{
233 int i;
234/*
235 See assumptions above
236 */
237/* Evaluation mode */
238
239 for (i=0; i<dimx; i++ ) HSVL[i] = 0.0;
240 for (i=0; i<nobs; i++ ) HSVL[dimx+i] = 2.0*U[nobs];
241
242 return 0;
243}
244
245#include "std.c"
246
249int main(int argc, char** argv)
250{
251 c_log( "Starting to execute", START );
252
253 defdata();
254 /*
255 Tell CONOPT about the sizes in the model
256 */
257 COI_Error += COIDEF_NumVar ( CntVect, nobs+dimx ); /* nobs+dimx variables */
258 COI_Error += COIDEF_NumCon ( CntVect, nobs+1 ); /* nobs+1 constraints */
259 COI_Error += COIDEF_NumNz ( CntVect, nobs*(dimx+2) ); /* nobs*(dimx+2) nonzeros in the Jacobian */
260 COI_Error += COIDEF_NumNlNz ( CntVect, nobs*(dimx+1) ); /* nobs*(dimx+1) of which are nonlinear */
261 COI_Error += COIDEF_NumHess ( CntVect, nobs+dimx ); /* nobs+dimx nonzeros in the Hessian */
262 COI_Error += COIDEF_OptDir ( CntVect, -1 ); /* Minimize */
263 COI_Error += COIDEF_ObjCon ( CntVect, nobs ); /* Objective is constraint nobs */
264 COI_Error += COIDEF_DebugFV ( CntVect, 0 ); /* 0 means no debugging, 1 each iter */
265 COI_Error += COIDEF_Debug2D ( CntVect, 0 ); /* 0 means no debugging, 1 each iter */
266 COI_Error += COIDEF_StdOut ( CntVect, 0 ); /* 1 means Allow output to StdOut */
267 COI_Error += COIDEF_Optfile ( CntVect, "leastsq4.opt"); /* Register the options file */
268 /*
269 Register the necessary callback routines with CONOPT
270 */
271 COI_Error += COIDEF_Message ( CntVect, &Std_Message ); /* Register the callback Message */
272 COI_Error += COIDEF_ErrMsg ( CntVect, &Std_ErrMsg ); /* Register the callback ErrMsg */
273 COI_Error += COIDEF_Status ( CntVect, &Std_Status ); /* Register the callback Status */
274 COI_Error += COIDEF_Solution ( CntVect, &Std_Solution ); /* Register the callback Solution */
275 COI_Error += COIDEF_ReadMatrix( CntVect, &Leastsq_ReadMatrix); /* Register the callback ReadMatrix */
276 COI_Error += COIDEF_FDEval ( CntVect, &Leastsq_FDEval); /* Register the callback FDEval */
277 COI_Error += COIDEF_2DLagrStr ( CntVect, &Leastsq_2DLagrStr); /* Register the callback 2DLagrStr */
278 COI_Error += COIDEF_2DLagrVal ( CntVect, &Leastsq_2DLagrVal); /* Register the callback 2DLagrVal */
279
280#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
281 COI_Error += COIDEF_License ( CntVect, LICENSE_INT_1, LICENSE_INT_2, LICENSE_INT_3, LICENSE_TEXT);
282#endif
283
284 if ( COI_Error ) {
285 printf("Skipping COI_Solve due to setup errors. COI_Error = %d\n",COI_Error);
286 c_log( "Skipping Solve due to setup errors", COI_Error); }
287 COI_Error = COI_Solve ( CntVect ); /* Optimize */
288 printf("End of Least Squares Example 4. Return code =%d\n",COI_Error);
289 if ( COI_Error ) {
290 c_log( "Errors encountered during first solve", COI_Error); }
291 else if ( stacalls == 0 || solcalls == 0 ) {
292 c_log( "Status or Solution routine was not during first solve called", -1); }
293 else if ( sstat != 1 || mstat != 2 ) {
294 c_log( "Solver or Model status was not as expected (1,2)", -1); }
295 else if ( fabs( OBJ-19.4443 ) > 0.001 ) {
296 c_log( "Incorrect objective returned from first solve", -1); };
297
298 c_log( "Successful Solve", OK );
299}
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_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_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.
int COI_CALLCONV Leastsq_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 leastsq2.c:200
int COI_CALLCONV Leastsq_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 leastsq2.c:230
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition leastsq4.c:249
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 leastsq4.c:61
int COI_CALLCONV Leastsq_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 leastsq4.c:200
void defdata()
Definition leastsq4.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 leastsq4.c:137
int COI_CALLCONV Leastsq_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 leastsq4.c:230
float rndx()
Definition leastsq4.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