CONOPT
Loading...
Searching...
No Matches
leastsq.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 Leastsq_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 }
133
138 int FDEval(const double x[], double *g, double jac[], int rowno, const int jacnum[], int mode,
139 int ignerr, int *errcnt, int numvar, int numjac, int thread) override
140 {
141 if (rowno == consobj)
142 {
143 /* */
144 /* Mode = 1 or 3. Function value: g = P * Out */
145 /* */
146 if (mode == 1 || mode == 3)
147 {
148 double sum = 0.0;
149 for (int i = 0; i < nobs; i++)
150 {
151 sum += pow(x[varres[i]], 2);
152 }
153 *g = sum;
154 }
155 /* */
156 /* Mode = 2 or 3: Derivative values: */
157 /* */
158 if (mode == 2 || mode == 3)
159 {
160 for (int i = 0; i < nobs; i++)
161 {
162 jac[varres[i]] = 2 * x[varres[i]];
163 }
164 }
165 }
166 /*
167 Row 0 to nobs-1: The observation definitions: */
168 else
169 {
170 /*
171 Mode = 1 or 3: Function value - x-part only */
172
173 if (mode == 1 || mode == 3)
174 {
175 int k = rowno * dimx;
176 double sum = 0.0;
177 for (int i = 0; i < dimx; i++)
178 {
179 sum += A[k] * x[varx[i]] + B[k] * pow(x[varx[i]], 2);
180 k++;
181 }
182 *g = sum;
183 }
184 /*
185 Mode = 2 or 3: Derivatives */
186
187 if (mode == 2 || mode == 3)
188 {
189 int k = rowno * dimx;
190 for (int i = 0; i < dimx; i++)
191 {
192 jac[varx[i]] = A[k] + 2 * B[k] * x[varx[i]];
193 k++;
194 }
195 }
196 }
197
198 return 0;
199 }
200};
201
202#include "std.cpp"
203
207int main(int argc, char **argv)
208{
209 int COI_Error = 0;
210
211 // getting the program name from the executable path
212 std::string pname = getProgramName(argv[0]);
213
214 // initialising the Conopt Object
216 Leastsq_ModelData modeldata(700, 500);
217 Tut_MessageHandler msghandler(pname);
218
219 // adding the message handler to the conopt interface
220 conopt.setMessageHandler(msghandler);
221
222 // building the model
223 modeldata.buildModel();
224
225 // loading the model in the conopt object
226 conopt.loadModel(modeldata);
227
228#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
229 std::string license = CONOPT_LICENSE_TEXT;
230 COI_Error += conopt.setLicense(CONOPT_LICENSE_INT_1, CONOPT_LICENSE_INT_2, CONOPT_LICENSE_INT_3, license);
231#endif
232
233 if (COI_Error)
234 cpp_log(
235 conopt, "Skipping COI_Solve due to license error. COI_Error = " + std::to_string(COI_Error), COI_Error);
236
237 COI_Error = conopt.solve(); /* Optimize */
238
239 // checking the status and objective value
240 if (!(conopt.solutionStatus() == 1 && conopt.modelStatus() == 2))
241 {
242 cpp_log(conopt, "Incorrect Model or Solver Status", -1);
243 }
244 else if (fabs(conopt.objectiveValue() - 19.4443) > 0.001)
245 {
246 cpp_log(conopt, "Incorrect objective returned", -1);
247 }
248
249 // printing the final status of the optimisation
250 conopt.printStatus();
251
252 cpp_log(conopt, "Successful Solve", COI_Error);
253}
The Conopt class.
Definition conopt.hpp:27
static constexpr double Infinity
Definition conopt.hpp:30
void defineData()
Defines the data for the problem.
Definition leastsq.cpp:54
std::vector< int > varres
Definition leastsq.cpp:27
std::vector< double > Obs
Definition leastsq.cpp:22
Leastsq_ModelData(int numobs, int dimensionx)
Definition leastsq.cpp:32
std::vector< int > varx
Definition leastsq.cpp:26
std::vector< int > consresidual
Definition leastsq.cpp:30
double rndx()
Defines a pseudo random number between 0 and 1.
Definition leastsq.cpp:41
std::vector< double > B
Definition leastsq.cpp:21
std::vector< double > A
Definition leastsq.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.
void buildModel()
adds the variables and constraints for the problem
Definition leastsq.cpp:77
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition leastsq.cpp:207
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 leastsq.cpp:138
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.
int seed
Definition leastsq.c:14
int seed
Definition leastsq.cpp:15
void cpp_log(Conopt &conopt, std::string msg, int code)
Definition std.cpp:111
std::string getProgramName(char *execname)
Definition std.cpp:95