CONOPT
Loading...
Searching...
No Matches
qp3.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_QP3
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 lagrangian structure for computing the second derivative
58
60 """
61 define the lagrangian structure and supplies it to CONOPT
62
63 @ingroup PYTHON1THREAD_QP3
64 """
65 hessianrow = [f for x in range(self.NN - 1) for f in [x, x + 1]] + [
66 self.NN - 1
67 ]
68 hessiancol = [f for x in range(self.NN - 1) for f in [x, x]] + [
69 self.NN - 1
70 ]
71
72 self.setSDLagrangianStructure(hessianrow, hessiancol)
73
74 def evaluateNonlinearTerm(self, x, rowno, ignerr, thread):
75 """
76 @copydoc conopt.ModelData.evaluateNonlinearTerm
77 @ingroup PYTHON1THREAD_QP3
78 """
79 # only the objective function is nonlinear, so this function will only be
80 # called when rowno == 0
81 g = 0
82 if rowno == 0:
83 g += sum(
84 [
85 (x[i] - self.target[i]) * q * (x[i] - self.target[i])
86 for i, q in enumerate(self.Qdiag)
87 ]
88 )
89 g += 2 * sum(
90 [
91 (x[i + 1] - self.target[i + 1]) * q * (x[i] - self.target[i])
92 for i, q in enumerate(self.Qlowerdiag)
93 ]
94 )
95
96 return g / 2
97
98 def evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread):
99 """
100 @copydoc conopt.ModelData.evaluateNonlinearJacobian
101 @ingroup PYTHON1THREAD_QP3
102 """
103 jac = [0] * self.NN
104 if rowno == 0:
105 for i in range(self.NN):
106 jac[i] += self.Qdiag[i] * (x[i] - self.target[i])
107 if i < self.NN - 1:
108 jac[i + 1] += self.Qlowerdiag[i] * (x[i] - self.target[i])
109 jac[i] += self.Qlowerdiag[i] * (x[i + 1] - self.target[i + 1])
110
111 return jac
112
113 def evaluateSDLagrangian(self, x, u, hessianrow, hessiancol):
114 """
115 @copydoc conopt.ModelData.evaluateSDLagrangian
116 @ingroup PYTHON1THREAD_QP3
117 """
118 hessian = [
119 x * u[0]
120 for i in range(self.NN - 1)
121 for x in [self.Qdiag[i], self.Qlowerdiag[i]]
122 ] + [self.Qdiag[-1] * u[0]]
123
124 return hessian
125
126
127if __name__ == '__main__':
128 name = os.path.basename(__file__)[:-3]
129
130 conopt = co.Conopt(name)
131 model = QPModelData()
132 msghdlr = std.TutMessageHandler(name)
133
134 model.buildModel()
135
136 conopt.loadModel(model)
137 conopt.setMessageHandler(msghdlr)
138
139 # getting the license variables
140 license_int_1 = os.environ.get('CONOPT_LICENSE_INT_1', None)
141 license_int_2 = os.environ.get('CONOPT_LICENSE_INT_2', None)
142 license_int_3 = os.environ.get('CONOPT_LICENSE_INT_3', None)
143 license_text = os.environ.get('CONOPT_LICENSE_TEXT', None)
144 if (
145 license_int_1 is not None
146 and license_int_2 is not None
147 and license_int_3 is not None
148 and license_text is not None
149 ):
150 conopt.setLicense(
151 int(license_int_1),
152 int(license_int_2),
153 int(license_int_3),
154 license_text,
155 )
156
157 coi_error = conopt.solve()
158
159 retcode = std.checkSolve(conopt, 59978.0, coi_error, 0.001)
160
161 sys.exit(retcode)
list target
Definition qp3.py:25
list Qlowerdiag
Definition qp3.py:27
__init__(self)
Definition qp3.py:18
list Qdiag
Definition qp3.py:26
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
Definition std.java:20
evaluateSDLagrangian(self, x, u, hessianrow, hessiancol)
Computes and returns the numerical values of the Lagrangian of the Hessian.
Definition qp3.py:113
setLagrangianStructure(self)
define the lagrangian structure and supplies it to CONOPT
Definition qp3.py:59
buildModel(self)
adding the variables and constraints to the model
Definition qp3.py:31
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
Definition qp3.py:98
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
Definition qp3.py:74