CONOPT
Loading...
Searching...
No Matches
leastsq.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 "conopt.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
22float rndx()
23{
24 int times;
25 float result;
26 seed = seed*1027+25;
27 times = seed / 1048576;
28 seed = seed - 1048576*times;
29 result = (float)seed;
30 result = result / 1048576;
31 return result;
32}
33
35void defdata()
36{
37 int i,j,k;
38 double Xtarg = -1.0;
39 double Noise = 1.0;
40 double O;
41
42 k = 0;
43 for ( i = 0; i < nobs; i++ ) {
44 O = 0.0;
45 for ( j = 0; j < dimx; j++ ) {
46 A[k] = rndx();
47 B[k] = rndx();
48 O = O + A[k] * Xtarg + B[k] * pow(Xtarg,2);
49 k++;
50 }
51 Obs[i] = O + Noise * rndx();
52 }
53}
54
59int COI_CALLCONV Leastsq_ReadMatrix( double LOWER[], double CURR[], double UPPER[], int VSTA[], int TYPE[], double RHS[],
60 int ESTA[], int COLSTA[], int ROWNO[], double VALUE[],
61 int NLFLAG[], int NUMVAR, int NUMCON, int NUMNZ, void* USRMEM )
62{
63 int i, j, k;
64 /* */
65 /* Information about Variables: */
66 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
67 /* Default: the status information in Vsta is not used. */
68 /* */
69 for ( i=0; i<dimx; i++ )
70 CURR[i] = -0.8 ;
71 /* Information about Constraints: */
72 /* Default: Rhs = 0 */
73 /* Default: the status information in Esta and the function */
74 /* value in FV are not used. */
75 /* Default: Type: There is no default. */
76 /* 0 = Equality, */
77 /* 1 = Greater than or equal, */
78 /* 2 = Less than or equal, */
79 /* 3 = Non binding. */
80 /* */
81 for ( i=0; i<nobs; i++ ) {
82 RHS[i] = Obs[i] ;
83 TYPE[i] = 0;
84 }
85
86 /* Constraint nobs (Objective) */
87 /* Rhs = 0.0 and type Non binding */
88 /* */
89 TYPE[nobs] = 3 ;
90 /* */
91 /* Information about the Jacobian. CONOPT expects a columnwise */
92 /* representation in Rowno, Value, Nlflag and Colsta. */
93 /* */
94 /* Colsta = Start of column indices (No Defaults): */
95 /* Rowno = Row indices */
96 /* Value = Value of derivative (by default only linear */
97 /* derivatives are used) */
98 /* Nlflag = 0 for linear and 1 for nonlinear derivative */
99 /* (not needed for completely linear models) */
100 /* */
101 /* Indices */
102 /* x(i) res(j) */
103 /* j: NL L=1 */
104 /* obj: NL */
105
106 k = 0; /* counter for current nonzero */
107 for ( j=0; j<dimx; j++ ) {
108 COLSTA[j] = k;
109 for (i=0; i<nobs; i++ ) {
110 ROWNO[k] = i;
111 NLFLAG[k] = 1;
112 k++;
113 }
114 }
115 for (i=0; i<nobs; i++ ) {
116 COLSTA[dimx+i] = k;
117 ROWNO[k] = i;
118 NLFLAG[k] = 0;
119 VALUE[k] = 1.0;
120 k++;
121 ROWNO[k] = nobs;
122 NLFLAG[k] = 1;
123 k++;
124 }
125 COLSTA[dimx+nobs] = k;
126 return 0;
127}
128
133int COI_CALLCONV Leastsq_FDEval( const double X[], double* G, double JAC[], int ROWNO, const int JACNUM[], int MODE,
134 int IGNERR, int* ERRCNT, int NUMVAR, int NUMJAC, int THREAD, void* USRMEM )
135{
136 int i, j, k;
137 double sum;
138 /* */
139 /* Row nobs: the objective function is nonlinear */
140 /* */
141
142 if ( ROWNO == nobs ) {
143 /* */
144 /* Mode = 1 or 3. Function value: G = P * Out */
145 /* */
146 if ( MODE == 1 || MODE == 3 ) {
147 sum = 0.0;
148 for (i=0; i<nobs; i++ ) {
149 sum += pow(X[dimx+i],2);
150 }
151 *G = sum;
152 }
153 /* */
154 /* Mode = 2 or 3: Derivative values: */
155 /* */
156 if ( MODE == 2 || MODE == 3 ) {
157 for (i=0; i<nobs; i++ ) {
158 JAC[dimx+i] = 2*X[dimx+i];
159 }
160 }
161 }
162/*
163 Row 0 to nobs-1: The observation definitions: */
164 else {
165/*
166 Mode = 1 or 3: Function value - x-part only */
167
168 if ( MODE == 1 || MODE == 3 ) {
169 k = ROWNO*dimx;
170 sum = 0.0;
171 for ( j=0; j<dimx; j++ ) {
172 sum += A[k]*X[j] + B[k]*pow(X[j],2);
173 k++;
174 }
175 *G = sum;
176 }
177/*
178 Mode = 2 or 3: Derivatives */
179
180 if ( MODE == 2 || MODE == 3 ) {
181 k = ROWNO*dimx;
182 for ( j=0; j<dimx; j++ ) {
183 JAC[j] = A[k] + 2*B[k]*X[j];
184 k++;
185 }
186 }
187 }
188 return 0;
189}
190
191#include "std.c"
192
195int main(int argc, char** argv)
196{
197 c_log( "Starting to execute", START );
198
199 defdata();
200 /*
201 Tell CONOPT about the sizes in the model
202 */
203 COI_Error += COIDEF_NumVar ( CntVect, nobs+dimx ); /* nobs+dimx variables */
204 COI_Error += COIDEF_NumCon ( CntVect, nobs+1 ); /* nobs+1 constraints */
205 COI_Error += COIDEF_NumNz ( CntVect, nobs*(dimx+2) ); /* nobs*(dimx+2) nonzeros in the Jacobian */
206 COI_Error += COIDEF_NumNlNz ( CntVect, nobs*(dimx+1) ); /* nobs*(dimx+1) of which are nonlinear */
207 COI_Error += COIDEF_OptDir ( CntVect, -1 ); /* Minimize */
208 COI_Error += COIDEF_ObjCon ( CntVect, nobs ); /* Objective is constraint nobs */
209 COI_Error += COIDEF_DebugFV ( CntVect, 0 ); /* 0 means no debugging, 1 each iter */
210 COI_Error += COIDEF_StdOut ( CntVect, 0 ); /* 1 means Allow output to StdOut */
211 COI_Error += COIDEF_Optfile ( CntVect, "leastsq.opt" ); /* Register the options file */
212 /*
213 Register the necessary callback routines with CONOPT
214 */
215 COI_Error += COIDEF_Message ( CntVect, &Std_Message ); /* Register the callback Message */
216 COI_Error += COIDEF_ErrMsg ( CntVect, &Std_ErrMsg ); /* Register the callback ErrMsg */
217 COI_Error += COIDEF_Status ( CntVect, &Std_Status ); /* Register the callback Status */
218 COI_Error += COIDEF_Solution ( CntVect, &Std_Solution ); /* Register the callback Solution */
219 COI_Error += COIDEF_ReadMatrix( CntVect, &Leastsq_ReadMatrix); /* Register the callback ReadMatrix */
220 COI_Error += COIDEF_FDEval ( CntVect, &Leastsq_FDEval); /* Register the callback FDEval */
221
222#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
223 COI_Error += COIDEF_License ( CntVect, CONOPT_LICENSE_INT_1, CONOPT_LICENSE_INT_2, CONOPT_LICENSE_INT_3, CONOPT_LICENSE_TEXT);
224#endif
225
226 if ( COI_Error ) {
227 printf("Skipping COI_Solve due to setup errors. COI_Error = %d\n",COI_Error);
228 c_log( "Skipping Solve due to setup errors", COI_Error); }
229 COI_Error = COI_Solve ( CntVect ); /* Optimize */
230 printf("End of Least Squares Example 1. Return code =%d\n",COI_Error);
231 if ( COI_Error ) {
232 c_log( "Errors encountered during first solve", COI_Error); }
233 else if ( stacalls == 0 || solcalls == 0 ) {
234 c_log( "Status or Solution routine was not during first solve called", -1); }
235 else if ( !( sstat == 1 && mstat == 2 ) ) {
236 c_log( "Solver or Model status was not as expected (1,2)", -1); }
237 else if ( fabs( OBJ-19.4443 ) > 0.001 ) {
238 c_log( "Incorrect objective returned from first solve", -1); };
239
240 c_log( "Successful Solve", OK );
241}
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
public C API header file
#define COI_CALLCONV
Definition conopt.h:19
int COIDEF_ReadMatrix(coiHandle_t cntvect, COI_READMATRIX_t coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
int COIDEF_Message(coiHandle_t cntvect, COI_MESSAGE_t coi_message)
define callback routine for handling messages returned during the solution process.
int COIDEF_FDEval(coiHandle_t cntvect, COI_FDEVAL_t coi_fdeval)
define callback routine for performing function and derivative evaluations.
int COIDEF_ErrMsg(coiHandle_t cntvect, COI_ERRMSG_t coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
int COIDEF_Solution(coiHandle_t cntvect, COI_SOLUTION_t coi_solution)
define callback routine for returning the final solution values.
int COIDEF_Status(coiHandle_t cntvect, COI_STATUS_t coi_status)
define callback routine for returning the completion status.
int COIDEF_Optfile(coiHandle_t cntvect, const char *optfile)
define callback routine for defining an options file.
int COIDEF_StdOut(coiHandle_t cntvect, int tostdout)
allow output to StdOut.
int COIDEF_DebugFV(coiHandle_t cntvect, int debugfv)
turn Debugging of FDEval on and off.
int COIDEF_License(coiHandle_t cntvect, int licint1, int licint2, int licint3, const char *licstring)
define the License Information.
int COIDEF_OptDir(coiHandle_t cntvect, int optdir)
defines the Optimization Direction.
int COIDEF_ObjCon(coiHandle_t cntvect, int objcon)
defines the Objective Constraint.
int COIDEF_NumVar(coiHandle_t cntvect, int numvar)
defines the number of variables in the model.
int COIDEF_NumNlNz(coiHandle_t cntvect, int numnlnz)
defines the Number of Nonlinear Nonzeros.
int COIDEF_NumNz(coiHandle_t cntvect, int numnz)
defines the number of nonzero elements in the Jacobian.
int COIDEF_NumCon(coiHandle_t cntvect, int numcon)
defines the number of constraints in the model.
int COI_Solve(coiHandle_t cntvect)
method for starting the solving process of CONOPT.
int seed
Definition leastsq.c:14
double A[nobs *dimx]
Definition leastsq.c:17
#define dimx
Definition leastsq.c:16
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition leastsq.c:195
double Obs[nobs]
Definition leastsq.c:19
#define nobs
Definition leastsq.c:15
int 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:133
void defdata()
Defines the data for the problem.
Definition leastsq.c:35
double B[nobs *dimx]
Definition leastsq.c:18
int 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:59
float rndx()
Defines a pseudo random number between 0 and 1.
Definition leastsq.c:22
int Std_Status(int MODSTA, int SOLSTA, int ITER, double OBJVAL, void *USRMEM)
Definition std.c:45
int Std_Message(int SMSG, int DMSG, int NMSG, char *MSGV[], void *USRMEM)
Definition std.c:8
int 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
void c_log(char *msgt, int code)
Definition std.c:202
int Std_ErrMsg(int ROWNO, int COLNO, int POSNO, const char *MSG, void *USRMEM)
Definition std.c:26