2 This file is part of qpOASES. 4 qpOASES -- An Implementation of the Online Active Set Strategy. 5 Copyright (C) 2007-2015 by Hans Joachim Ferreau et al. All rights reserved. 7 qpOASES is free software; you can redistribute it and/or 8 modify it under the terms of the GNU Lesser General Public 9 License as published by the Free Software Foundation; either 10 version 2.1 of the License, or (at your option) any later version. 12 qpOASES is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 15 See the GNU Lesser General Public License for more details. 17 You should have received a copy of the GNU Lesser General Public 18 License along with qpOASES; if not, write to the Free Software 19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 30 from subprocess
import Popen, PIPE, STDOUT
32 from qpoases
import PyQProblem
as QProblem
33 from qpoases
import PyQProblemB
as QProblemB
34 from qpoases
import PySQProblem
as SQProblem
35 from qpoases
import PySolutionAnalysis
as SolutionAnalysis
36 from qpoases
import PyBooleanType
as BooleanType
37 from qpoases
import PySubjectToStatus
as SubjectToStatus
38 from qpoases
import PyOptions
as Options
39 from qpoases
import PyPrintLevel
as PrintLevel
42 qpoases_path = os.path.dirname(os.path.abspath(__file__))
43 qpoases_path = os.path.dirname(qpoases_path)
44 qpoases_path = os.path.dirname(qpoases_path)
45 qpoases_path = os.path.dirname(qpoases_path)
48 bin_path = os.path.join(qpoases_path,
"bin")
57 H = np.array([1.0, 0.0, 0.0, 0.5 ]).reshape((2,2))
58 A = np.array([1.0, 1.0 ]).reshape((2,1))
59 g = np.array([1.5, 1.0 ])
60 lb = np.array([0.5, -2.0])
61 ub = np.array([5.0, 2.0 ])
62 lbA = np.array([-1.0 ])
67 g_new = np.array([1.0, 1.5])
68 lb_new = np.array([0.0, -1.0])
69 ub_new = np.array([5.0, -0.5])
70 lbA_new = np.array([-2.0])
71 ubA_new = np.array([1.0])
76 options.printLevel = PrintLevel.NONE
77 qp.setOptions(options)
81 qp.init(H, g, A, lb, ub, lbA, ubA, nWSR)
85 qp.hotstart(g_new, lb_new, ub_new, lbA_new, ubA_new, nWSR)
88 xOpt_actual = np.zeros(2)
89 qp.getPrimalSolution(xOpt_actual)
90 xOpt_actual = np.asarray(xOpt_actual, dtype=float)
91 objVal_actual = qp.getObjVal()
92 objVal_actual = np.asarray(objVal_actual, dtype=float)
94 cmd = os.path.join(bin_path,
"example1")
95 p = Popen(cmd, shell=
True, stdout=PIPE)
96 stdout, stderr = p.communicate()
97 stdout = str(stdout).replace(
'\\n',
'\n')
98 stdout = stdout.replace(
"'",
'')
102 pattern = re.compile(
r'xOpt\s*=\s*\[\s+(?P<xOpt>([0-9., e+-])*)\];')
103 match = pattern.search(stdout)
104 xOpt_expected = match.group(
'xOpt')
105 xOpt_expected = xOpt_expected.split(
",")
106 xOpt_expected = np.asarray(xOpt_expected, dtype=float)
108 pattern = re.compile(
r'objVal = (?P<objVal>[0-9-+e.]*)')
109 match = pattern.search(stdout)
110 objVal_expected = match.group(
'objVal')
111 objVal_expected = np.asarray(objVal_expected, dtype=float)
113 print(
"xOpt_actual =", xOpt_actual)
114 print(
"xOpt_expected =", xOpt_expected)
115 print(
"objVal_actual = ", objVal_actual)
116 print(
"objVal_expected = ", objVal_expected)
118 assert_almost_equal(xOpt_actual, xOpt_expected, decimal=7)
119 assert_almost_equal(objVal_actual, objVal_expected, decimal=7)
122 """Example for qpOASES main function using the QProblemB class.""" 124 H = np.array([1.0, 0.0, 0.0, 0.5]).reshape((2, 2))
125 g = np.array([1.5, 1.0])
126 lb = np.array([0.5, -2.0])
127 ub = np.array([5.0, 2.0])
131 g_new = np.array([1.0, 1.5])
132 lb_new = np.array([0.0, -1.0])
133 ub_new = np.array([5.0, -0.5])
140 options.initialStatusBounds = SubjectToStatus.INACTIVE
141 options.numRefinementSteps = 1
142 options.enableCholeskyRefactorisation = 1
143 options.printLevel = PrintLevel.NONE
144 qp.setOptions(options)
148 qp.init(H, g, lb, ub, nWSR)
150 xOpt_actual = np.zeros(2)
151 qp.getPrimalSolution(xOpt_actual)
152 xOpt_actual = np.asarray(xOpt_actual, dtype=float)
153 objVal_actual = qp.getObjVal()
154 objVal_actual = np.asarray(objVal_actual, dtype=float)
155 print 'xOpt_actual:', xOpt_actual
156 print 'objVal_actual:', objVal_actual
160 qp.hotstart(g_new, lb_new, ub_new, nWSR)
162 xOpt_actual = np.zeros(2)
163 qp.getPrimalSolution(xOpt_actual)
164 xOpt_actual = np.asarray(xOpt_actual, dtype=float)
165 objVal_actual = qp.getObjVal()
166 objVal_actual = np.asarray(objVal_actual, dtype=float)
167 print 'xOpt_actual:', xOpt_actual
168 print 'objVal_actual:', objVal_actual
171 xOpt_actual = np.zeros(2)
172 qp.getPrimalSolution(xOpt_actual)
173 xOpt_actual = np.asarray(xOpt_actual, dtype=float)
174 objVal_actual = qp.getObjVal()
175 objVal_actual = np.asarray(objVal_actual, dtype=float)
177 cmd = os.path.join(bin_path,
"example1b")
178 p = Popen(cmd, shell=
True, stdout=PIPE)
179 stdout, stderr = p.communicate()
180 stdout = str(stdout).replace(
'\\n',
'\n')
181 stdout = stdout.replace(
"'",
'')
184 pattern = re.compile(
r'xOpt\s*=\s*\[\s+(?P<xOpt>([0-9., e+-])*)\];')
185 match = pattern.findall(stdout)
186 xOpt_expected = match[-1][0]
187 xOpt_expected = xOpt_expected.split(
",")
188 xOpt_expected = np.asarray(xOpt_expected, dtype=float)
190 pattern = re.compile(
r'objVal = (?P<objVal>[0-9-+e.]*)')
191 match = pattern.findall(stdout)
193 objVal_expected = match[-1]
194 objVal_expected = np.asarray(objVal_expected, dtype=float)
196 print(
"xOpt_actual =", xOpt_actual)
197 print(
"xOpt_expected =", xOpt_expected)
198 print(
"objVal_actual = ", objVal_actual)
199 print(
"objVal_expected = ", objVal_expected)
201 assert_almost_equal(xOpt_actual, xOpt_expected, decimal=7)
202 assert_almost_equal(objVal_actual, objVal_expected, decimal=7)
208 H = np.array([ 1.0, 0.0, 0.0, 0.5 ]).reshape((2,2))
209 A = np.array([ 1.0, 1.0 ]).reshape((2,1))
210 g = np.array([ 1.5, 1.0 ])
211 lb = np.array([ 0.5, -2.0 ])
212 ub = np.array([ 5.0, 2.0 ])
213 lbA = np.array([ -1.0 ])
214 ubA = np.array([ 2.0 ])
217 H_new = np.array([ 1.0, 0.5, 0.5, 0.5 ]).reshape((2,2))
218 A_new = np.array([ 1.0, 5.0 ]).reshape((2,1))
219 g_new = np.array([ 1.0, 1.5 ])
220 lb_new = np.array([ 0.0, -1.0 ])
221 ub_new = np.array([ 5.0, -0.5 ])
222 lbA_new = np.array([ -2.0 ])
223 ubA_new = np.array([ 1.0 ])
228 options.printLevel = PrintLevel.NONE
229 qp.setOptions(options)
234 cmd = os.path.join(bin_path,
"example2")
235 p = Popen(cmd, shell=
True, stdout=PIPE)
236 stdout, stderr = p.communicate()
237 stdout = str(stdout).replace(
'\\n',
'\n')
238 stdout = stdout.replace(
"'",
'')
243 qp.init(H, g, A, lb, ub, lbA, ubA, nWSR)
246 maxViol = np.zeros(1)
247 maxStat = np.zeros(1)
248 maxFeas = np.zeros(1)
249 maxCmpl = np.zeros(1)
250 maxViol[0] = analyser.getKktViolation(
251 qp, maxStat, maxFeas, maxCmpl
253 print(
"maxViol: %e\n" % maxViol)
254 actual = np.asarray(maxViol)
256 pattern = re.compile(
257 r'maxKktViolation: (?P<KktViolation>[0-9+-e.]*)' 260 match = pattern.findall(stdout)
261 expected = np.asarray(match[0], dtype=float)
263 assert_almost_equal(actual, expected, decimal=7)
275 maxViol = np.zeros(1)
276 maxStat = np.zeros(1)
277 maxFeas = np.zeros(1)
278 maxCmpl = np.zeros(1)
279 maxViol[0] = analyser.getKktViolation(
280 qp, maxStat, maxFeas, maxCmpl
282 print(
"maxViol: %e\n" % maxViol)
283 actual = np.asarray(maxViol)
285 expected = np.asarray(match[1], dtype=float)
287 assert_almost_equal(actual, expected, decimal=7)
292 Primal_Dual_Var = np.zeros(5*5)
294 Var.reshape((5, 5))[0, 0] = 1.
295 Var.reshape((5, 5))[1, 1] = 1.
303 analyser.getVarianceCovariance(qp, Var, Primal_Dual_Var)
304 print(
'Primal_Dual_Var=\n', Primal_Dual_Var.reshape((5, 5)))
305 actual = Primal_Dual_Var.reshape((5, 5))
307 pattern = re.compile(
308 r'Primal_Dual_VAR = (?P<VAR>.*)',
313 match = pattern.search(stdout)
314 expected = match.group(
'VAR').strip().split(
"\n")
315 expected = [x.strip().split()
for x
in expected]
317 expected = np.asarray(expected, dtype=float)
319 assert_almost_equal(actual, expected, decimal=7)
323 0.8514828085899353, -0.15739890933036804, -0.081726007163524628, -0.530426025390625, 0.16773293912410736,
324 -0.15739890933036804, 1.1552412509918213, 0.57780224084854126, -0.0072606131434440613, 0.010559185408055782,
325 -0.081726007163524628, 0.57780224084854126, 0.28925251960754395, 5.324830453901086e-006, -3.0256599075073609e-006,
326 -0.530426025390625, -0.0072606131434440613, 5.324830453901086e-006, 0.35609596967697144, -0.15124998986721039,
327 0.16773293912410736, 0.010559185408055782, -3.0256599075073609e-006, -0.15124998986721039, 0.15129712224006653
330 g = np.array([0.30908384919166565, 0.99325823783874512, 0.49822014570236206, -0.26309865713119507, 0.024296050891280174], dtype=float).reshape((5,))
331 A = np.array([1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], dtype=float).reshape((5, 5))
332 lb = np.array([-0.052359879016876221, -0.052359879016876221, -0.052359879016876221, -0.052359879016876221, -0.052359938621520996], dtype=float).reshape((5,))
333 ub = np.array([ 0.052359879016876221, 0.052359879016876221, 0.052359879016876221, 0, 0], dtype=float).reshape((5,))
334 lbA = np.array([-0.052359879016876221, -0.052359879016876221, -0.052359879016876221, -0.052359879016876221, -0.052359938621520996], dtype=float).reshape((5,))
335 ubA = np.array([0.052359879016876221, 0.052359879016876221, 0.052359879016876221, 0, 0], dtype=float).reshape((5,))
340 options.printLevel = PrintLevel.NONE
341 qp.setOptions(options)
345 qp.init(H, g, A, lb, ub, lbA, ubA, nWSR)
347 result = np.zeros((5,))
348 qp.getPrimalSolution(result)
353 if __name__==
"__main__":
359 sys.stderr.write(
"Please install nosestests for python unittesting.\n")
Implements the online active set strategy for box-constrained QPs.
Implements the online active set strategy for QPs with varying matrices.
Provides a generic way to set and pass user-specified options.
Provides additional tools for analysing QP solutions.
BEGIN_NAMESPACE_QPOASES returnValue print(const real_t *const v, int n)
Implements the online active set strategy for QPs with general constraints.