CONOPT
Loading...
Searching...
No Matches
leastsq.c
Go to the documentation of this file.
1
7
8#include <stdlib.h>
9#include <stdio.h>
10#include <math.h>
11#include <string.h>
12#include "coiheader.h"
13#include "comdecl.h"
14
15int seed = 12359; /* seed for random number generator */
16#define nobs 700
17#define dimx 500
18double A[nobs*dimx];
19double B[nobs*dimx];
20double Obs[nobs];
21
23float rndx()
24{
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 return result;
33}
34
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++;
51 }
52 Obs[i] = O + Noise * rndx();
53 }
54}
55
60int COI_CALLCONV Leastsq_ReadMatrix( double LOWER[], double CURR[], double UPPER[], int VSTA[], int TYPE[], double RHS[],
61 int ESTA[], int COLSTA[], int ROWNO[], double VALUE[],
62 int NLFLAG[], int NUMVAR, int NUMCON, int NUMNZ, void* USRMEM )
63{
64 int i, j, k;
65 /* */
66 /* Information about Variables: */
67 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
68 /* Default: the status information in Vsta is not used. */
69 /* */
70 for ( i=0; i<dimx; i++ )
71 CURR[i] = -0.8 ;
72 /* Information about Constraints: */
73 /* Default: Rhs = 0 */
74 /* Default: the status information in Esta and the function */
75 /* value in FV are not used. */
76 /* Default: Type: There is no default. */
77 /* 0 = Equality, */
78 /* 1 = Greater than or equal, */
79 /* 2 = Less than or equal, */
80 /* 3 = Non binding. */
81 /* */
82 for ( i=0; i<nobs; i++ ) {
83 RHS[i] = Obs[i] ;
84 TYPE[i] = 0;
85 }
86
87 /* Constraint nobs (Objective) */
88 /* Rhs = 0.0 and type Non binding */
89 /* */
90 TYPE[nobs] = 3 ;
91 /* */
92 /* Information about the Jacobian. We use the standard method with */
93 /* Rowno, Value, Nlflag and Colsta and we do not use Colno. */
94 /* */
95 /* Colsta = Start of column indices (No Defaults): */
96 /* Rowno = Row indices */
97 /* Value = Value of derivative (by default only linear */
98 /* derivatives are used) */
99 /* Nlflag = 0 for linear and 1 for nonlinear derivative */
100 /* (not needed for completely linear models) */
101 /* */
102 /* Indices */
103 /* x(i) res(j) */
104 /* j: NL L=1 */
105 /* obj: NL */
106
107 k = 0; /* counter for current nonzero */
108 for ( j=0; j<dimx; j++ ) {
109 COLSTA[j] = k;
110 for (i=0; i<nobs; i++ ) {
111 ROWNO[k] = i;
112 NLFLAG[k] = 1;
113 k++;
114 }
115 }
116 for (i=0; i<nobs; i++ ) {
117 COLSTA[dimx+i] = k;
118 ROWNO[k] = i;
119 NLFLAG[k] = 0;
120 VALUE[k] = 1.0;
121 k++;
122 ROWNO[k] = nobs;
123 NLFLAG[k] = 1;
124 k++;
125 }
126 COLSTA[dimx+nobs] = k;
127 return 0;
128}
129
134int COI_CALLCONV Leastsq_FDEval( const double X[], double* G, double JAC[], int ROWNO, const int JACNUM[], int MODE,
135 int IGNERR, int* ERRCNT, int NUMVAR, int NUMJAC, int THREAD, void* USRMEM )
136{
137 int i, j, k;
138 double sum;
139 /* */
140 /* Row nobs: the objective function is nonlinear */
141 /* */
142
143 if ( ROWNO == nobs ) {
144 /* */
145 /* Mode = 1 or 3. Function value: G = P * Out */
146 /* */
147 if ( MODE == 1 || MODE == 3 ) {
148 sum = 0.0;
149 for (i=0; i<nobs; i++ ) {
150 sum += pow(X[dimx+i],2);
151 }
152 *G = sum;
153 }
154 /* */
155 /* Mode = 2 or 3: Derivative values: */
156 /* */
157 if ( MODE == 2 || MODE == 3 ) {
158 for (i=0; i<nobs; i++ ) {
159 JAC[dimx+i] = 2*X[dimx+i];
160 }
161 }
162 }
163/*
164 Row 0 to nobs-1: The observation definitions: */
165 else {
166/*
167 Mode = 1 or 3: Function value - x-part only */
168
169 if ( MODE == 1 || MODE == 3 ) {
170 k = ROWNO*dimx;
171 sum = 0.0;
172 for ( j=0; j<dimx; j++ ) {
173 sum += A[k]*X[j] + B[k]*pow(X[j],2);
174 k++;
175 }
176 *G = sum;
177 }
178/*
179 Mode = 2 or 3: Derivatives */
180
181 if ( MODE == 2 || MODE == 3 ) {
182 k = ROWNO*dimx;
183 for ( j=0; j<dimx; j++ ) {
184 JAC[j] = A[k] + 2*B[k]*X[j];
185 k++;
186 }
187 }
188 }
189 return 0;
190}
191
192#include "std.c"
193
196int main(int argc, char** argv)
197{
198 c_log( "Starting to execute", START );
199
200 defdata();
201 /*
202 Tell CONOPT about the sizes in the model
203 */
204 COI_Error += COIDEF_NumVar ( CntVect, nobs+dimx ); /* nobs+dimx variables */
205 COI_Error += COIDEF_NumCon ( CntVect, nobs+1 ); /* nobs+1 constraints */
206 COI_Error += COIDEF_NumNz ( CntVect, nobs*(dimx+2) ); /* nobs*(dimx+2) nonzeros in the Jacobian */
207 COI_Error += COIDEF_NumNlNz ( CntVect, nobs*(dimx+1) ); /* nobs*(dimx+1) of which are nonlinear */
208 COI_Error += COIDEF_OptDir ( CntVect, -1 ); /* Minimize */
209 COI_Error += COIDEF_ObjCon ( CntVect, nobs ); /* Objective is constraint nobs */
210 COI_Error += COIDEF_DebugFV ( CntVect, 0 ); /* 0 means no debugging, 1 each iter */
211 COI_Error += COIDEF_StdOut ( CntVect, 0 ); /* 1 means Allow output to StdOut */
212 COI_Error += COIDEF_Optfile ( CntVect, "leastsq.opt" ); /* Register the options file */
213 /*
214 Register the necessary callback routines with CONOPT
215 */
216 COI_Error += COIDEF_Message ( CntVect, &Std_Message ); /* Register the callback Message */
217 COI_Error += COIDEF_ErrMsg ( CntVect, &Std_ErrMsg ); /* Register the callback ErrMsg */
218 COI_Error += COIDEF_Status ( CntVect, &Std_Status ); /* Register the callback Status */
219 COI_Error += COIDEF_Solution ( CntVect, &Std_Solution ); /* Register the callback Solution */
220 COI_Error += COIDEF_ReadMatrix( CntVect, &Leastsq_ReadMatrix); /* Register the callback ReadMatrix */
221 COI_Error += COIDEF_FDEval ( CntVect, &Leastsq_FDEval); /* Register the callback FDEval */
222
223#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
224 COI_Error += COIDEF_License ( CntVect, LICENSE_INT_1, LICENSE_INT_2, LICENSE_INT_3, LICENSE_TEXT);
225#endif
226
227 if ( COI_Error ) {
228 printf("Skipping COI_Solve due to setup errors. COI_Error = %d\n",COI_Error);
229 c_log( "Skipping Solve due to setup errors", COI_Error); }
230 COI_Error = COI_Solve ( CntVect ); /* Optimize */
231 printf("End of Least Squares Example 1. Return code =%d\n",COI_Error);
232 if ( COI_Error ) {
233 c_log( "Errors encountered during first solve", COI_Error); }
234 else if ( stacalls == 0 || solcalls == 0 ) {
235 c_log( "Status or Solution routine was not during first solve called", -1); }
236 else if ( !( sstat == 1 && mstat == 2 ) ) {
237 c_log( "Solver or Model status was not as expected (1,2)", -1); }
238 else if ( fabs( OBJ-19.4443 ) > 0.001 ) {
239 c_log( "Incorrect objective returned from first solve", -1); };
240
241 c_log( "Successful Solve", OK );
242}
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_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_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 seed
Definition leastsq.c:15
double A[nobs *dimx]
Definition leastsq.c:18
#define dimx
Definition leastsq.c:17
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition leastsq.c:196
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
void defdata()
Defines the data for the problem.
Definition leastsq.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 leastsq.c:134
double B[nobs *dimx]
Definition leastsq.c:19
float rndx()
Defines a pseudo random number between 0 and 1.
Definition leastsq.c:23
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