14sys.path.append(
'../common/')
19 def __init__(self, numobs: int, dimensionx: int):
40 Defines a pseudo random number between 0 and 1
42 NOTE: it would be possible to use random module to generate the random
43 numbers. We have written our own random number generator to be consistent
44 with the original Fortran example.
47 times = int(self.
seed / 1048576)
48 self.
seed = self.
seed - 1048576 * times
49 return self.
seed / 1048576.0
53 Defines the data for the problem
59 for i
in range(self.
nobs):
61 for j
in range(self.
dimx):
62 self.
A[k] = self.
rndx()
63 self.
B[k] = self.
rndx()
64 O += self.
A[k] * Xtarg + self.
B[k] * math.pow(Xtarg, 2)
66 self.
Obs[i] = O + Noise * self.
rndx()
70 adding the variables and constraints to the model
71 @ingroup PYTHON1THREAD_LEASTSQ
74 for i
in range(self.
dimx):
75 varidx = self.addVariable(
76 -co.Conopt.Infinity, co.Conopt.Infinity, -0.8
78 self.
varx.append(varidx)
80 for i
in range(self.
nobs):
81 varidx = self.addVariable(-co.Conopt.Infinity, co.Conopt.Infinity, 0.0)
85 for i
in range(self.
nobs):
86 varidx = self.
varx.copy()
87 coeffs = [0.0] * self.
dimx
91 varidx.append(self.
varres[i])
95 considx = self.addConstraint(
96 co.ConstraintType_Eq, self.
Obs[i], varidx, coeffs, nlf
101 objVarIdx = self.
varres.copy()
102 objCoeffs = [1.0] * self.
nobs
103 objNlf = [1] * self.
nobs
105 self.
consobj = self.addConstraint(
106 co.ConstraintType_Free, 0.0, objVarIdx, objCoeffs, objNlf
110 self.setObjectiveElement(co.ObjectiveElement_Constraint, self.
consobj)
113 self.setOptimizationSense(co.Sense_Minimize)
117 @copydoc conopt.ModelData.evaluateNonlinearTerm
118 @ingroup PYTHON1THREAD_LEASTSQ
123 for i
in range(self.
nobs):
124 sum += pow(x[self.
varres[i]], 2)
128 k = rowno * self.
dimx
130 for i
in range(self.
dimx):
131 sum += self.
A[k] * x[self.
varx[i]] + self.
B[k] * pow(
141 @copydoc conopt.ModelData.evaluateNonlinearJacobian
142 @ingroup PYTHON1THREAD_LEASTSQ
146 for i
in range(self.
nobs):
147 jac.append(2 * x[self.
varres[i]])
150 k = rowno * self.
dimx
151 for i
in range(self.
dimx):
152 jac.append(self.
A[k] + 2 * self.
B[k] * x[self.
varx[i]])
158if __name__ ==
'__main__':
159 name = os.path.basename(__file__)[:-3]
161 conopt = co.Conopt(name)
162 model = LeastSqModelData(700, 500)
165 conopt.setMessageHandler(msghdlr)
168 conopt.loadModel(model)
171 license_int_1 = os.environ.get(
'CONOPT_LICENSE_INT_1',
None)
172 license_int_2 = os.environ.get(
'CONOPT_LICENSE_INT_2',
None)
173 license_int_3 = os.environ.get(
'CONOPT_LICENSE_INT_3',
None)
174 license_text = os.environ.get(
'CONOPT_LICENSE_TEXT',
None)
176 license_int_1
is not None
177 and license_int_2
is not None
178 and license_int_3
is not None
179 and license_text
is not None
188 coi_error = conopt.solve()
rndx(self)
Defines a pseudo random number between 0 and 1.
__init__(self, int numobs, int dimensionx)
define_data(self)
Defines the data for the problem.
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
buildModel(self)
adding the variables and constraints to the model
float rndx()
Defines a pseudo random number between 0 and 1.