CONOPT
Loading...
Searching...
No Matches
qp2.py
Go to the documentation of this file.
7
8import os
9import sys
10
11import conopt as co
12
13sys.path.append('../common/')
14import std
15
16
17class QPModelData(co.ModelData):
18 def __init__(self):
19 self.NN = 1000
20 self.NQ = self.NN * 2 - 1
21 # only the lower triangle of the Q matrix is stored, since it is
22 # symmetric. The diagonal has the value 10. While the first off diagonal
23 # has the value 0.1. Note, the indices for the lower diagonal is (i + 1,
24 # i)
25 self.target = [10] * self.NN
26 self.Qdiag = [1] * self.NN
27 self.Qlowerdiag = [0.1] * (self.NN - 1)
28
29 super().__init__()
30
31 def buildModel(self):
32 """
33 adding the variables and constraints to the model
34 @ingroup PYTHON1THREAD_QP2
35 """
36 # adding the variables to the model
37 for i in range(self.NN):
38 self.addVariable(0.0, co.Conopt.Infinity)
39
40 # adding the constraints to the model
41 # the first constraint is the quadratic objective
42 varidx = list(range(self.NN))
43 zeros = [0] * self.NN
44 ones = [1] * self.NN
45 self.addConstraint(co.ConstraintType_Free, 0.0, varidx, zeros, ones)
46
47 # the second constraint is the summation constraint: sum(x) = 1
48 self.addConstraint(co.ConstraintType_Eq, 1.0, varidx, ones, zeros)
49
50 # setting the objective constraint
51 self.setObjectiveElement(co.ObjectiveElement_Constraint, 0)
52
53 # setting the optimisation direction
54 self.setOptimizationSense(co.Sense_Minimize)
55
56 # setting the second derivative evaluation type
57 self.setSDEvaluationType(co.SDEvaluationType_Constraint)
58
59 def evaluateNonlinearTerm(self, x, rowno, ignerr, thread):
60 """
61 @copydoc conopt.ModelData.evaluateNonlinearTerm
62 @ingroup PYTHON1THREAD_QP2
63 """
64 # only the objective function is nonlinear, so this function will only be
65 # called when rowno == 0
66 g = 0
67 if rowno == 0:
68 g += sum(
69 [
70 (x[i] - self.target[i]) * q * (x[i] - self.target[i])
71 for i, q in enumerate(self.Qdiag)
72 ]
73 )
74 g += 2 * sum(
75 [
76 (x[i + 1] - self.target[i + 1]) * q * (x[i] - self.target[i])
77 for i, q in enumerate(self.Qlowerdiag)
78 ]
79 )
80
81 return g / 2
82
83 def evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread):
84 """
85 @copydoc conopt.ModelData.evaluateNonlinearJacobian
86 @ingroup PYTHON1THREAD_QP2
87 """
88 jac = [0] * self.NN
89 if rowno == 0:
90 for i in range(self.NN):
91 jac[i] += self.Qdiag[i] * (x[i] - self.target[i])
92 if i < self.NN - 1:
93 jac[i + 1] += self.Qlowerdiag[i] * (x[i] - self.target[i])
94 jac[i] += self.Qlowerdiag[i] * (x[i + 1] - self.target[i + 1])
95
96 return jac
97
98 def evaluateDirectionalSD(self, x, dx, rowno, jacnum, thread):
99 """
100 @copydoc conopt.ModelData.evaluateDirectionalSD
101 @ingroup PYTHON1THREAD_QP2
102 """
103 dirsd = [0] * self.NN
104 for i in range(self.NN):
105 dirsd[i] += self.Qdiag[i] * dx[i]
106 if i < self.NN - 1:
107 dirsd[i + 1] += self.Qlowerdiag[i] * dx[i]
108 dirsd[i] += self.Qlowerdiag[i] * dx[i + 1]
109
110 return dirsd
111
112
113if __name__ == '__main__':
114 name = os.path.basename(__file__)[:-3]
115
116 conopt = co.Conopt(name)
117 model = QPModelData()
118 msghdlr = std.TutMessageHandler(name)
119
120 model.buildModel()
121
122 conopt.loadModel(model)
123 conopt.setMessageHandler(msghdlr)
124
125 # getting the license variables
126 license_int_1 = os.environ.get('CONOPT_LICENSE_INT_1', None)
127 license_int_2 = os.environ.get('CONOPT_LICENSE_INT_2', None)
128 license_int_3 = os.environ.get('CONOPT_LICENSE_INT_3', None)
129 license_text = os.environ.get('CONOPT_LICENSE_TEXT', None)
130 if (
131 license_int_1 is not None
132 and license_int_2 is not None
133 and license_int_3 is not None
134 and license_text is not None
135 ):
136 conopt.setLicense(
137 int(license_int_1),
138 int(license_int_2),
139 int(license_int_3),
140 license_text,
141 )
142
143 coi_error = conopt.solve()
144
145 retcode = std.checkSolve(conopt, 59978.0, coi_error, 0.001)
146
147 sys.exit(retcode)
__init__(self)
Definition qp2.py:18
list Qdiag
Definition qp2.py:26
list target
Definition qp2.py:25
list Qlowerdiag
Definition qp2.py:27
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
Definition std.java:20
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
Definition qp2.py:59
buildModel(self)
adding the variables and constraints to the model
Definition qp2.py:31
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
Definition qp2.py:83
evaluateDirectionalSD(self, x, dx, rowno, jacnum, thread)
computes the directional second derivative for a single constraint
Definition qp2.py:98