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_LEASTSQ5
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)
116 self.setSDEvaluationType(co.SDEvaluationType_Constraint)
120 @copydoc conopt.ModelData.evaluateNonlinearTerm
121 @ingroup PYTHON1THREAD_LEASTSQ5
126 for i
in range(self.
nobs):
127 sum += pow(x[self.
varres[i]], 2)
131 k = rowno * self.
dimx
133 for i
in range(self.
dimx):
134 sum += self.
A[k] * x[self.
varx[i]] + self.
B[k] * pow(
144 @copydoc conopt.ModelData.evaluateNonlinearJacobian
145 @ingroup PYTHON1THREAD_LEASTSQ5
150 for i
in range(self.
nobs):
151 jac.append(2 * x[self.
varres[i]])
154 k = rowno * self.
dimx
155 for i
in range(self.
dimx):
156 jac.append(self.
A[k] + 2 * self.
B[k] * x[self.
varx[i]])
163 @copydoc conopt.ModelData.evaluateDirectionalSD
164 @ingroup PYTHON1THREAD_LEASTSQ5
169 dirsd = [0.0] * (self.
dimx + self.
nobs)
170 for i
in range(self.
dimx):
171 dirsd[self.
varx[i]] = 0
173 for i
in range(self.
nobs):
178 k = rowno * self.
dimx
179 for i
in range(self.
dimx):
180 dirsd[self.
varx[i]] = 2.0 * self.
B[k] * dx[self.
varx[i]]
183 for i
in range(self.
nobs):
189if __name__ ==
'__main__':
190 name = os.path.basename(__file__)[:-3]
192 conopt = co.Conopt(name)
193 model = LeastSqModelData(700, 500)
196 conopt.setMessageHandler(msghdlr)
199 conopt.loadModel(model)
202 license_int_1 = os.environ.get(
'CONOPT_LICENSE_INT_1',
None)
203 license_int_2 = os.environ.get(
'CONOPT_LICENSE_INT_2',
None)
204 license_int_3 = os.environ.get(
'CONOPT_LICENSE_INT_3',
None)
205 license_text = os.environ.get(
'CONOPT_LICENSE_TEXT',
None)
207 license_int_1
is not None
208 and license_int_2
is not None
209 and license_int_3
is not None
210 and license_text
is not None
219 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
evaluateDirectionalSD(self, x, dx, rowno, jacnum, thread)
computes the directional second derivative for a single constraint
float rndx()
Defines a pseudo random number between 0 and 1.