14sys.path.append(
'../common/')
19 def __init__(self, numobs: int, dimensionx: int):
39 Defines a pseudo random number between 0 and 1
41 NOTE: it would be possible to use random module to generate the random
42 numbers. We have written our own random number generator to be consistent
43 with the original Fortran example.
46 times = int(self.
seed / 1048576)
47 self.
seed = self.
seed - 1048576 * times
48 return self.
seed / 1048576.0
52 Defines the data for the problem
58 for i
in range(self.
nobs):
60 for j
in range(self.
dimx):
61 self.
A[k] = self.
rndx()
62 self.
B[k] = self.
rndx()
63 O += self.
A[k] * Xtarg + self.
B[k] * math.pow(Xtarg, 2)
65 self.
Obs[i] = O + Noise * self.
rndx()
69 adding the variables and constraints to the model
70 @ingroup PYTHON1THREAD_LEASTSQ2
73 for i
in range(self.
dimx):
74 varidx = self.addVariable(
75 -co.Conopt.Infinity, co.Conopt.Infinity, -0.8
77 self.
varx.append(varidx)
79 for i
in range(self.
nobs):
80 varidx = self.addVariable(-co.Conopt.Infinity, co.Conopt.Infinity, 0.0)
84 for i
in range(self.
nobs):
85 varidx = self.
varx.copy()
86 coeffs = [0.0] * self.
dimx
90 varidx.append(self.
varres[i])
94 considx = self.addConstraint(
95 co.ConstraintType_Eq, self.
Obs[i], varidx, coeffs, nlf
100 objVarIdx = self.
varres.copy()
101 objCoeffs = [1.0] * self.
nobs
102 objNlf = [1] * self.
nobs
105 co.ConstraintType_Free, 0.0, objVarIdx, objCoeffs, objNlf
109 self.setObjectiveElement(co.ObjectiveElement_Constraint, self.
consobj)
112 self.setOptimizationSense(co.Sense_Minimize)
125 Qrow = [0] * (self.
dimx + self.
nobs)
126 Qcol = [0] * (self.
dimx + self.
nobs)
128 for i
in range(self.
dimx + self.
nobs):
132 self.setSDLagrangianStructure(Qrow, Qcol)
136 @copydoc conopt.ModelData.evaluateNonlinearTerm
137 @ingroup PYTHON1THREAD_LEASTSQ2
142 for i
in range(self.
nobs):
143 sum += pow(x[self.
varres[i]], 2)
147 k = rowno * self.
dimx
149 for i
in range(self.
dimx):
150 sum += self.
A[k] * x[self.
varx[i]] + self.
B[k] * pow(
160 @copydoc conopt.ModelData.evaluateNonlinearJacobian
161 @ingroup PYTHON1THREAD_LEASTSQ2
166 for i
in range(self.
nobs):
167 jac.append(2 * x[self.
varres[i]])
170 k = rowno * self.
dimx
171 for i
in range(self.
dimx):
172 jac.append(self.
A[k] + 2 * self.
B[k] * x[self.
varx[i]])
179 @copydoc conopt.ModelData.evaluateSDLagrangian
180 @ingroup PYTHON1THREAD_LEASTSQ2
182 numhessian = len(hessianrow)
183 hessian = [0
for i
in range(numhessian)]
184 for i
in range(self.
dimx):
185 hessian[self.
varx[i]] = 0.0
186 for i
in range(self.
nobs):
192if __name__ ==
'__main__':
193 name = os.path.basename(__file__)[:-3]
195 conopt = co.Conopt(name)
196 model = LeastSqModelData(700, 500)
201 conopt.loadModel(model)
202 conopt.setMessageHandler(msghdlr)
205 license_int_1 = os.environ.get(
'CONOPT_LICENSE_INT_1',
None)
206 license_int_2 = os.environ.get(
'CONOPT_LICENSE_INT_2',
None)
207 license_int_3 = os.environ.get(
'CONOPT_LICENSE_INT_3',
None)
208 license_text = os.environ.get(
'CONOPT_LICENSE_TEXT',
None)
210 license_int_1
is not None
211 and license_int_2
is not None
212 and license_int_3
is not None
213 and license_text
is not None
222 coi_error = conopt.solve()
rndx(self)
Defines a pseudo random number between 0 and 1.
define_data(self)
Defines the data for the problem.
__init__(self, int numobs, int dimensionx)
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
evaluateSDLagrangian(self, x, u, hessianrow, hessiancol)
Computes and returns the numerical values of the Lagrangian of the Hessian.
buildModel(self)
adding the variables and constraints to the model
float rndx()
Defines a pseudo random number between 0 and 1.