CONOPT
Loading...
Searching...
No Matches
leastsq10.cpp
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 <iostream>
13#include "conopt.hpp"
14
15int seed = 12359; /* seed for random number generator */
16
18{
19public:
20 std::vector<double> A;
21 std::vector<double> B;
22 std::vector<double> C;
23 std::vector<double> Obs;
24 int nobs;
25 int dimx;
26
27 std::vector<int> varx;
28 std::vector<int> varres;
29
31 std::vector<int> consresidual;
32
33 Leastsq10_ModelData(int numobs, int dimensionx) : nobs(numobs), dimx(dimensionx)
34 {
35 A.resize(nobs * dimx);
36 B.resize(nobs * dimx);
37 C.resize(nobs * dimx);
38 Obs.resize(nobs);
39 defineData();
40 }
41
43 double rndx()
44 {
45 int times;
46 double result;
47 seed = seed * 1027 + 25;
48 times = seed / 1048576;
49 seed = seed - 1048576 * times;
50 result = static_cast<double>(seed);
51 result = result / 1048576.0;
52 return result;
53 }
54
57 {
58 double Xtarg = -1.0;
59 double Noise = 1.0;
60
61 int k = 0;
62 for (int i = 0; i < nobs; ++i)
63 {
64 double O = 0.0;
65 for (int j = 0; j < dimx; ++j)
66 {
67 A[k] = rndx();
68 B[k] = rndx();
69 O += A[k] * Xtarg + B[k] * std::pow(Xtarg, 2);
70 ++k;
71 }
72 Obs[i] = O + Noise * rndx();
73 }
74 }
75
80 {
81 /* */
82 /* Information about Variables: */
83 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
84 /* Default: the status information in Vsta is not used. */
85 /* */
86 for (int i = 0; i < dimx; ++i)
87 {
88 int varidx = addVariable(-Conopt::Infinity, Conopt::Infinity, -0.8);
89 varx.push_back(varidx);
90 }
91 for (int i = 0; i < nobs; ++i)
92 {
94 varres.push_back(varidx);
95 }
96
97 /* Information about Constraints: */
98 /* Default: Rhs = 0 */
99 /* Default: the status information in Esta and the function */
100 /* value in FV are not used. */
101 /* Default: Type: There is no default. */
102 /* 0 = Equality, */
103 /* 1 = Greater than or equal, */
104 /* 2 = Less than or equal, */
105 /* 3 = Non binding. */
106 /* */
107 for (int i = 0; i < nobs; ++i)
108 {
109 std::vector<int> varidx(varx.begin(), varx.end());
110 std::vector<double> coeffs(dimx, 0.0);
111 std::vector<int> nlf(dimx, 1);
112
113 // ADD THE OUTPUT RESIDUAL VARIABLE
114 varidx.push_back(varres[i]); // residual variable index
115 coeffs.push_back(-1.0); // linear coefficient
116 nlf.push_back(0); // linear
117
118 int considx = addConstraint(ConoptConstraintType::Eq, Obs[i], varidx, coeffs, nlf);
119 consresidual.push_back(considx);
120 }
121
122 /* Constraint nobs (Objective) */
123 /* Rhs = 0.0 and type Non binding */
124 /* */
125 std::vector<int> objVarIdx(varres.begin(), varres.end());
126 std::vector<double> objCoeffs(nobs, 1.0);
127 std::vector<int> objNlf(nobs, 1);
128
129 consobj = addConstraint(ConoptConstraintType::Free, 0.0, objVarIdx, objCoeffs, objNlf);
130
133
134 /* setting the second derivative evaluation type */
136 }
137
142 int FDEval(const double x[], double *g, double jac[], int rowno, const int jacnum[], int mode,
143 int ignerr, int *errcnt, int numvar, int numjac, int thread) override
144 {
145 if (rowno == consobj)
146 {
147 /* */
148 /* Mode = 1 or 3. Function value: g = P * Out */
149 /* */
150 if (mode == 1 || mode == 3)
151 {
152 double sum = 0.0;
153 for (int i = 0; i < nobs; i++)
154 {
155 sum += pow(x[varres[i]], 2);
156 }
157 *g = sum;
158 }
159 /* */
160 /* Mode = 2 or 3: Derivative values: */
161 /* */
162 if (mode == 2 || mode == 3)
163 {
164 for (int i = 0; i < nobs; i++)
165 {
166 jac[varres[i]] = 2 * x[varres[i]];
167 }
168 }
169 }
170 /*
171 Row 0 to nobs-1: The observation definitions: */
172 else
173 {
174 /*
175 Mode = 1 or 3: Function value - x-part only */
176
177 if (mode == 1 || mode == 3)
178 {
179 int k = rowno * dimx;
180 double sum = 0.0;
181 for (int i = 0; i < dimx; i++)
182 {
183 sum += A[k] * x[varx[i]] + B[k] * pow(x[varx[i]], 2);
184 k++;
185 }
186 *g = sum;
187 }
188 /*
189 Mode = 2 or 3: Derivatives */
190
191 if (mode == 2 || mode == 3)
192 {
193 int k = rowno * dimx;
194 for (int i = 0; i < dimx; i++)
195 {
196 jac[varx[i]] = A[k] + 2 * B[k] * x[varx[i]];
197 k++;
198 }
199 }
200 }
201
202 return 0;
203 }
204
209 int SDDirIni(const double x[], const double dx[], const int rowlist[], int listsize,
210 int numthread, int newpt, int *nodrv, int numvar) override
211 {
212 /* Constraint row with b(i,j)*x(j)**2 as only nonlinear term.
213 Compute the directional term 2*b(i,j)*dx(j) and keep for later */
214
215 int k = 0;
216 for (int i = 0; i < nobs; i++)
217 {
218 for (int j = 0; j < dimx; j++)
219 {
220 C[k] = 2.0 * B[k] * dx[varx[j]];
221 k++;
222 }
223 }
224 return 0;
225 }
226
231 int SDDir(const double x[], const double dx[], double d2g[], int rowno, const int jacnum[],
232 int *nodrv, int numvar, int numjac, int thread) override
233 {
234 if (rowno == nobs)
235 {
236
237 /* Objective row: sum(i, res(i)**2 ) */
238
239 for (int i = 0; i < dimx; i++)
240 d2g[varx[i]] = 0;
241 for (int i = 0; i < nobs; i++)
242 d2g[varres[i]] = 2.0 * dx[varres[i]];
243 }
244 else
245 {
246
247 /* Constraint row with b(i,j)*x(j)**2 as only nonlinear term */
248
249 int k = rowno * dimx;
250 for (int i = 0; i < dimx; i++)
251 {
252 d2g[varx[i]] = C[k];
253 k++;
254 }
255 for (int i = 0; i < nobs; i++)
256 d2g[varres[i]] = 0;
257 }
258
259 return 0;
260 }
261};
262
263#include "std.cpp"
264
268int main(int argc, char **argv)
269{
270 int COI_Error = 0;
271
272 // getting the program name from the executable path
273 std::string pname = getProgramName(argv[0]);
274
275 // initialising the Conopt Object
277 Leastsq10_ModelData modeldata(700, 500);
278 Tut_MessageHandler msghandler(pname);
279
280 // adding the message handler to the conopt interface
281 conopt.setMessageHandler(msghandler);
282
283 // building the model
284 modeldata.buildModel();
285
286 // loading the model in the conopt object
287 conopt.loadModel(modeldata);
288
289#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
290 std::string license = CONOPT_LICENSE_TEXT;
291 COI_Error += conopt.setLicense(CONOPT_LICENSE_INT_1, CONOPT_LICENSE_INT_2, CONOPT_LICENSE_INT_3, license);
292#endif
293
294 if (COI_Error)
295 cpp_log(
296 conopt, "Skipping COI_Solve due to license error. COI_Error = " + std::to_string(COI_Error), COI_Error);
297
298 COI_Error = conopt.solve(); /* Optimize */
299
300 // checking the status and objective value
301 if (!(conopt.solutionStatus() == 1 && conopt.modelStatus() == 2))
302 {
303 cpp_log(conopt, "Incorrect Model or Solver Status", -1);
304 }
305 else if (fabs(conopt.objectiveValue() - 19.4443) > 0.001)
306 {
307 cpp_log(conopt, "Incorrect objective returned", -1);
308 }
309
310 // printing the final status of the optimisation
311 conopt.printStatus();
312
313 cpp_log(conopt, "Successful Solve", COI_Error);
314}
The Conopt class.
Definition conopt.hpp:27
static constexpr double Infinity
Definition conopt.hpp:30
std::vector< int > varres
Definition leastsq10.cpp:28
std::vector< double > Obs
Definition leastsq10.cpp:23
double rndx()
Defines a pseudo random number between 0 and 1.
Definition leastsq10.cpp:43
std::vector< double > C
Definition leastsq10.cpp:22
std::vector< int > consresidual
Definition leastsq10.cpp:31
Leastsq10_ModelData(int numobs, int dimensionx)
Definition leastsq10.cpp:33
std::vector< int > varx
Definition leastsq10.cpp:27
void defineData()
Defines the data for the problem.
Definition leastsq10.cpp:56
std::vector< double > B
Definition leastsq10.cpp:21
std::vector< double > A
Definition leastsq10.cpp:20
char pname[MAXLINE]
Definition comdecl.h:10
int COI_Error
Definition comdecl.h:15
CONOPT C++ interface header file. This is the main object for the CONOPT C++ interface.
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
void buildModel()
adds the variables and constraints for the problem
Definition leastsq10.cpp:79
int SDDirIni(const double x[], const double dx[], const int rowlist[], int listsize, int numthread, int newpt, int *nodrv, int numvar) override
called by CONOPT before a sequence of 2DDir calls each time either the point or the direction changes...
int 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) override
defines the nonlinearities of the model by returning numerical values.
int SDDir(const double x[], const double dx[], double d2g[], int rowno, const int jacnum[], int *nodrv, int numvar, int numjac, int thread) override
computes the directional second derivative for a single constraint
int addVariable(double lower, double upper, double curr=0, int varstatus=-1)
adds a variable to the model. The non-zero coefficients are added later.
void setObjectiveElement(ConoptObjectiveElement elem, int elemindex)
sets the index for the objective variable or constraint
int addConstraint(ConoptConstraintType constype, double rhs, int slackstatus=-1)
adds a constraint to the problem. The non-zero coefficients are added later
void setOptimizationSense(ConoptSense sense)
sets the optimisation direction.
void setSDEvaluationType(ConoptSDEvaluationType sdevaltype)
informs CONOPT of the method for evaluating the second derivative
int seed
Definition leastsq10.cpp:15
int seed
Definition leastsq.c:14
void cpp_log(Conopt &conopt, std::string msg, int code)
Definition std.cpp:111
std::string getProgramName(char *execname)
Definition std.cpp:95