14sys.path.append(
'../common/')
19 def __init__(self, numobs: int, dimensionx: int):
41 Defines a pseudo random number between 0 and 1
43 NOTE: it would be possible to use random module to generate the random
44 numbers. We have written our own random number generator to be consistent
45 with the original Fortran example.
48 times = int(self.
seed / 1048576)
49 self.
seed = self.
seed - 1048576 * times
50 return self.
seed / 1048576.0
54 Defines the data for the problem
60 for i
in range(self.
nobs):
62 for j
in range(self.
dimx):
63 self.
A[k] = self.
rndx()
64 self.
B[k] = self.
rndx()
65 O += self.
A[k] * Xtarg + self.
B[k] * math.pow(Xtarg, 2)
67 self.
Obs[i] = O + Noise * self.
rndx()
71 adding the variables and constraints to the model
72 @ingroup PYTHON1THREAD_LEASTSQ10
75 for i
in range(self.
dimx):
76 varidx = self.addVariable(
77 -co.Conopt.Infinity, co.Conopt.Infinity, -0.8
79 self.
varx.append(varidx)
81 for i
in range(self.
nobs):
82 varidx = self.addVariable(-co.Conopt.Infinity, co.Conopt.Infinity, 0.0)
86 for i
in range(self.
nobs):
87 varidx = self.
varx.copy()
88 coeffs = [0.0] * self.
dimx
92 varidx.append(self.
varres[i])
96 considx = self.addConstraint(
97 co.ConstraintType_Eq, self.
Obs[i], varidx, coeffs, nlf
102 objVarIdx = self.
varres.copy()
103 objCoeffs = [1.0] * self.
nobs
104 objNlf = [1] * self.
nobs
106 self.
consobj = self.addConstraint(
107 co.ConstraintType_Free, 0.0, objVarIdx, objCoeffs, objNlf
111 self.setObjectiveElement(co.ObjectiveElement_Constraint, self.
consobj)
114 self.setOptimizationSense(co.Sense_Minimize)
117 self.setSDEvaluationType(co.SDEvaluationType_Constraint)
121 @copydoc conopt.ModelData.evaluateNonlinearTerm
122 @ingroup PYTHON1THREAD_LEASTSQ10
127 for i
in range(self.
nobs):
128 sum += pow(x[self.
varres[i]], 2)
132 k = rowno * self.
dimx
134 for i
in range(self.
dimx):
135 sum += self.
A[k] * x[self.
varx[i]] + self.
B[k] * pow(
145 @copydoc conopt.ModelData.evaluateNonlinearJacobian
146 @ingroup PYTHON1THREAD_LEASTSQ10
151 for i
in range(self.
nobs):
152 jac.append(2 * x[self.
varres[i]])
155 k = rowno * self.
dimx
156 for i
in range(self.
dimx):
157 jac.append(self.
A[k] + 2 * self.
B[k] * x[self.
varx[i]])
164 @copydoc conopt.ModelData.initDirectionalSDEval
165 @ingroup PYTHON1THREAD_LEASTSQ10
172 for i
in range(self.
nobs):
173 for j
in range(self.
dimx):
174 self.
C[k] = 2.0 * self.
B[k] * dx[self.
varx[j]]
179 @copydoc conopt.ModelData.evaluateDirectionalSD
180 @ingroup PYTHON1THREAD_LEASTSQ10
183 if rowno == self.
nobs:
185 dirsd = [0.0] * (self.
dimx + self.
nobs)
186 for i
in range(self.
dimx):
187 dirsd[self.
varx[i]] = 0
189 for i
in range(self.
nobs):
194 k = rowno * self.
dimx
195 for i
in range(self.
dimx):
196 dirsd[self.
varx[i]] = self.
C[k]
199 for i
in range(self.
nobs):
205if __name__ ==
'__main__':
206 name = os.path.basename(__file__)[:-3]
208 conopt = co.Conopt(name)
209 model = LeastSqModelData(700, 500)
212 conopt.setMessageHandler(msghdlr)
215 conopt.loadModel(model)
218 license_int_1 = os.environ.get(
'CONOPT_LICENSE_INT_1',
None)
219 license_int_2 = os.environ.get(
'CONOPT_LICENSE_INT_2',
None)
220 license_int_3 = os.environ.get(
'CONOPT_LICENSE_INT_3',
None)
221 license_text = os.environ.get(
'CONOPT_LICENSE_TEXT',
None)
223 license_int_1
is not None
224 and license_int_2
is not None
225 and license_int_3
is not None
226 and license_text
is not None
235 coi_error = conopt.solve()
__init__(self, int numobs, int dimensionx)
define_data(self)
Defines the data for the problem.
rndx(self)
Defines a pseudo random number between 0 and 1.
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
initDirectionalSDEval(self, x, dx, rowlist, numthread, newpoint)
a callback for the initialisation of the second derivative evaluation.
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
evaluateDirectionalSD(self, x, dx, rowno, jacnum, thread)
computes the directional second derivative for a single constraint
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for 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.