CONOPT
Loading...
Searching...
No Matches
leastsq2.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> Obs;
23 int nobs;
24 int dimx;
25
26 std::vector<int> varx;
27 std::vector<int> varres;
28
30 std::vector<int> consresidual;
31
32 Leastsq2_ModelData(int numobs, int dimensionx) : nobs(numobs), dimx(dimensionx)
33 {
34 A.resize(nobs * dimx);
35 B.resize(nobs * dimx);
36 Obs.resize(nobs);
37 defineData();
38 }
39
41 double rndx()
42 {
43 int times;
44 double result;
45 seed = seed * 1027 + 25;
46 times = seed / 1048576;
47 seed = seed - 1048576 * times;
48 result = static_cast<double>(seed);
49 result = result / 1048576.0;
50 return result;
51 }
52
55 {
56 double Xtarg = -1.0;
57 double Noise = 1.0;
58
59 int k = 0;
60 for (int i = 0; i < nobs; ++i)
61 {
62 double O = 0.0;
63 for (int j = 0; j < dimx; ++j)
64 {
65 A[k] = rndx();
66 B[k] = rndx();
67 O += A[k] * Xtarg + B[k] * std::pow(Xtarg, 2);
68 ++k;
69 }
70 Obs[i] = O + Noise * rndx();
71 }
72 }
73
78 {
79
80 /* */
81 /* Information about Variables: */
82 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
83 /* Default: the status information in Vsta is not used. */
84 /* */
85 for (int i = 0; i < dimx; ++i)
86 {
87 int varidx = addVariable(-Conopt::Infinity, Conopt::Infinity, -0.8);
88 varx.push_back(varidx);
89 }
90 for (int i = 0; i < nobs; ++i)
91 {
93 varres.push_back(varidx);
94 }
95
96 /* Information about Constraints: */
97 /* Default: Rhs = 0 */
98 /* Default: the status information in Esta and the function */
99 /* value in FV are not used. */
100 /* Default: Type: There is no default. */
101 /* 0 = Equality, */
102 /* 1 = Greater than or equal, */
103 /* 2 = Less than or equal, */
104 /* 3 = Non binding. */
105 /* */
106 for (int i = 0; i < nobs; ++i)
107 {
108 std::vector<int> varidx(varx.begin(), varx.end());
109 std::vector<double> coeffs(dimx, 0.0);
110 std::vector<int> nlf(dimx, 1);
111
112 // ADD THE OUTPUT RESIDUAL VARIABLE
113 varidx.push_back(varres[i]); // residual variable index
114 coeffs.push_back(-1.0); // linear coefficient
115 nlf.push_back(0); // linear
116
117 int considx = addConstraint(ConoptConstraintType::Eq, Obs[i], varidx, coeffs, nlf);
118 consresidual.push_back(considx);
119 }
120
121 /* Constraint nobs (Objective) */
122 /* Rhs = 0.0 and type Non binding */
123 /* */
124 std::vector<int> objVarIdx(varres.begin(), varres.end());
125 std::vector<double> objCoeffs(nobs, 1.0);
126 std::vector<int> objNlf(nobs, 1);
127
128 consobj = addConstraint(ConoptConstraintType::Free, 0.0, objVarIdx, objCoeffs, objNlf);
129
132
144
145 /* setting the structure of the hessian */
146 std::vector<int> Qrow(dimx + nobs);
147 std::vector<int> Qcol(dimx + nobs);
148
149 for (int i = 0; i < dimx + nobs; ++i)
150 {
151 Qrow[i] = i;
152 Qcol[i] = i;
153 }
154
156 }
157
162 int FDEval(const double x[], double *g, double jac[], int rowno, const int jacnum[], int mode,
163 int ignerr, int *errcnt, int numvar, int numjac, int thread) override
164 {
165 if (rowno == consobj)
166 {
167 /* */
168 /* Mode = 1 or 3. Function value: g = P * Out */
169 /* */
170 if (mode == 1 || mode == 3)
171 {
172 double sum = 0.0;
173 for (int i = 0; i < nobs; i++)
174 {
175 sum += pow(x[varres[i]], 2);
176 }
177 *g = sum;
178 }
179 /* */
180 /* Mode = 2 or 3: Derivative values: */
181 /* */
182 if (mode == 2 || mode == 3)
183 {
184 for (int i = 0; i < nobs; i++)
185 {
186 jac[varres[i]] = 2 * x[varres[i]];
187 }
188 }
189 }
190 /*
191 Row 0 to nobs-1: The observation definitions: */
192 else
193 {
194 /*
195 Mode = 1 or 3: Function value - x-part only */
196
197 if (mode == 1 || mode == 3)
198 {
199 int k = rowno * dimx;
200 double sum = 0.0;
201 for (int i = 0; i < dimx; i++)
202 {
203 sum += A[k] * x[varx[i]] + B[k] * pow(x[varx[i]], 2);
204 k++;
205 }
206 *g = sum;
207 }
208 /*
209 Mode = 2 or 3: Derivatives */
210
211 if (mode == 2 || mode == 3)
212 {
213 int k = rowno * dimx;
214 for (int i = 0; i < dimx; i++)
215 {
216 jac[varx[i]] = A[k] + 2 * B[k] * x[varx[i]];
217 k++;
218 }
219 }
220 }
221
222 return 0;
223 }
224
229 int SDLagrVal(const double x[], const double u[], const int hsrw[], const int hscl[],
230 double hsvl[], int *nodrv, int numvar, int numcon, int nhess) override
231 {
232 /* Evaluation mode */
233
234 for (int i = 0; i < dimx; i++)
235 hsvl[varx[i]] = 0.0;
236 for (int i = 0; i < nobs; i++)
237 hsvl[varres[i]] = 2.0 * u[nobs];
238
239 return 0;
240 }
241};
242
243#include "std.cpp"
244
248int main(int argc, char **argv)
249{
250 int COI_Error = 0;
251
252 // getting the program name from the executable path
253 std::string pname = getProgramName(argv[0]);
254
255 // initialising the Conopt Object
257 Leastsq2_ModelData modeldata(700, 500);
258 Tut_MessageHandler msghandler(pname);
259
260 // adding the message handler to the conopt interface
261 conopt.setMessageHandler(msghandler);
262
263 // building the model
264 modeldata.buildModel();
265
266 // loading the model in the conopt object
267 conopt.loadModel(modeldata);
268
269#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
270 std::string license = CONOPT_LICENSE_TEXT;
271 COI_Error += conopt.setLicense(CONOPT_LICENSE_INT_1, CONOPT_LICENSE_INT_2, CONOPT_LICENSE_INT_3, license);
272#endif
273
274 if (COI_Error)
275 cpp_log(
276 conopt, "Skipping COI_Solve due to license error. COI_Error = " + std::to_string(COI_Error), COI_Error);
277
278 COI_Error = conopt.solve(); /* Optimize */
279
280 // checking the status and objective value
281 if (!(conopt.solutionStatus() == 1 && conopt.modelStatus() == 2))
282 {
283 cpp_log(conopt, "Incorrect Model or Solver Status", -1);
284 }
285 else if (fabs(conopt.objectiveValue() - 19.4443) > 0.001)
286 {
287 cpp_log(conopt, "Incorrect objective returned", -1);
288 }
289
290 // printing the final status of the optimisation
291 conopt.printStatus();
292
293 cpp_log(conopt, "Successful Solve", COI_Error);
294}
The Conopt class.
Definition conopt.hpp:27
static constexpr double Infinity
Definition conopt.hpp:30
std::vector< double > A
Definition leastsq2.cpp:20
void defineData()
Defines the data for the problem.
Definition leastsq2.cpp:54
std::vector< int > consresidual
Definition leastsq2.cpp:30
std::vector< double > Obs
Definition leastsq2.cpp:22
std::vector< double > B
Definition leastsq2.cpp:21
Leastsq2_ModelData(int numobs, int dimensionx)
Definition leastsq2.cpp:32
std::vector< int > varx
Definition leastsq2.cpp:26
double rndx()
Defines a pseudo random number between 0 and 1.
Definition leastsq2.cpp:41
std::vector< int > varres
Definition leastsq2.cpp:27
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 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.
Definition leastsq2.cpp:162
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition leastsq2.cpp:248
int SDLagrVal(const double x[], const double u[], const int hsrw[], const int hscl[], double hsvl[], int *nodrv, int numvar, int numcon, int nhess) override
Computes and returns the numerical values of the Hessian.
Definition leastsq2.cpp:229
void buildModel()
adds the variables and constraints for the problem
Definition leastsq2.cpp:77
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 setSDLagrangianStructure(const std::vector< int > &rownum, const std::vector< int > &colnum)
sets the structure of the second derivatives of the Lagrangian
int seed
Definition leastsq2.cpp:15
int seed
Definition leastsq.c:14
int Qrow[NQ]
Definition qp1.c:25
int Qcol[NQ]
Definition qp1.c:26
void cpp_log(Conopt &conopt, std::string msg, int code)
Definition std.cpp:111
std::string getProgramName(char *execname)
Definition std.cpp:95