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