CONOPT
Loading...
Searching...
No Matches
qp3.py
Go to the documentation of this file.
7
8import os
9import sys
10
11import pyconopt
12
13sys.path.append('../common/')
14import std
15
16
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, pyconopt.CONOPT_INF)
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(pyconopt.ConstraintType_Free, 0.0, varidx, zeros,
46 ones)
47
48 # the second constraint is the summation constraint: sum(x) = 1
49 self.addConstraint(pyconopt.ConstraintType_Eq, 1.0, varidx, ones,
50 zeros)
51
52 # setting the objective constraint
53 self.setObjectiveElement(pyconopt.ObjectiveElement_Constraint, 0)
54
55 # setting the optimisation direction
56 self.setOptimizationSense(pyconopt.Sense_Minimize)
57
58 # setting the lagrangian structure for computing the second derivative
60
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 hessiancol = [f for x in range(self.NN - 1) for f in [x, x]] \
68 + [self.NN - 1]
69
70 self.setSDLagrangianStructure(hessianrow, hessiancol)
71
72 def evaluateNonlinearTerm(self, x, rowno, ignerr, thread):
73 """
74 @ingroup PYTHON1THREAD_QP3
75 """
76 # only the objective function is nonlinear, so this function will only be
77 # called when rowno == 0
78 g = 0
79 if rowno == 0:
80 g += sum([(x[i] - self.target[i])*q*(x[i] - self.target[i])
81 for i, q in enumerate(self.Qdiag)])
82 g += 2*sum([(x[i + 1] - self.target[i + 1])*q*(x[i] - self.target[i])
83 for i, q in enumerate(self.Qlowerdiag)])
84
85 return g/2
86
87 def evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread):
88 """
89 @ingroup PYTHON1THREAD_QP3
90 """
91 jac = [0]*self.NN
92 if rowno == 0:
93 for i in range(self.NN):
94 jac[i] += self.Qdiag[i]*(x[i] - self.target[i])
95 if i < self.NN - 1:
96 jac[i + 1] += self.Qlowerdiag[i]*(x[i] - self.target[i])
97 jac[i] += self.Qlowerdiag[i]*(x[i + 1] - self.target[i + 1])
98
99
100 return jac
101
102 def evaluateSDLagrangian(self, x, u, hessianrow, hessiancol):
103 """
104 @ingroup PYTHON1THREAD_QP3
105 """
106 hessian = [x*u[0] for i in range(self.NN - 1) \
107 for x in [self.Qdiag[i], self.Qlowerdiag[i]]] \
108 + [self.Qdiag[-1]*u[0]]
109
110 return hessian
111
112
113if __name__ == "__main__":
114 name = os.path.basename(__file__)[:-3]
115
116 conopt = pyconopt.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('LICENSE_INT_1', None)
127 license_int_2 = os.environ.get('LICENSE_INT_2', None)
128 license_int_3 = os.environ.get('LICENSE_INT_3', None)
129 license_text = os.environ.get('LICENSE_TEXT', None)
130 if license_int_1 is not None and license_int_2 is not None \
131 and license_int_3 is not None and license_text is not None:
132 conopt.setLicense(int(license_int_1), int(license_int_2),
133 int(license_int_3), license_text)
134
135 coi_error = conopt.solve()
136
137 retcode = std.checkSolve(conopt, 59978.0, coi_error, 0.001)
138
139 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
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 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:16
evaluateSDLagrangian(self, x, u, hessianrow, hessiancol)
Computes and returns the numerical values of the Lagrangian of the Hessian.
Definition qp3.py:102
setLagrangianStructure(self)
Definition qp3.py:61
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:87
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
Definition qp3.py:72