ub_new = np.array([5.0, -0.5]) lbA_new = np.array([-2.0]) ubA_new = np.array([1.0]) # Setting up QProblem object. example = QProblem(2, 1) options = Options() #options.printLevel = PrintLevel.NONE example.setOptions(options) # Solve first QP. nWSR = np.array([10]) example.init(H, g, A, lb, ub, lbA, ubA, nWSR) xOpt = np.zeros(2) example.getPrimalSolution(xOpt) print("\nxOpt = [ %e, %e ]; objVal = %e\n\n" % (xOpt[0], xOpt[1], example.getObjVal())) # Solve second QP. nWSR = np.array([10]) example.hotstart(g_new, lb_new, ub_new, lbA_new, ubA_new, nWSR) # Get and print solution of second QP. example.getPrimalSolution(xOpt) print("\nxOpt = [ %e, %e ]; objVal = %e\n\n" % (xOpt[0], xOpt[1], example.getObjVal())) example.printOptions()
def test_example1(self): return 0 # Example for qpOASES main function using the QProblem class. #Setup data of first QP. H = np.array([1.0, 0.0, 0.0, 0.5 ]).reshape((2,2)) A = np.array([1.0, 1.0 ]).reshape((2,1)) g = np.array([1.5, 1.0 ]) lb = np.array([0.5, -2.0]) ub = np.array([5.0, 2.0 ]) lbA = np.array([-1.0 ]) ubA = np.array([2.0]) # Setup data of second QP. g_new = np.array([1.0, 1.5]) lb_new = np.array([0.0, -1.0]) ub_new = np.array([5.0, -0.5]) lbA_new = np.array([-2.0]) ubA_new = np.array([1.0]) # Setting up QProblemB object. qp = QProblem(2, 1) options = Options() options.printLevel = PrintLevel.NONE qp.setOptions(options) # Solve first QP. nWSR = 10 qp.init(H, g, A, lb, ub, lbA, ubA, nWSR) # Solve second QP. nWSR = 10 qp.hotstart(g_new, lb_new, ub_new, lbA_new, ubA_new, nWSR) # Get and print solution of second QP. xOpt_actual = np.zeros(2) qp.getPrimalSolution(xOpt_actual) xOpt_actual = np.asarray(xOpt_actual, dtype=float) objVal_actual = qp.getObjVal() objVal_actual = np.asarray(objVal_actual, dtype=float) cmd = os.path.join(bin_path, "example1") p = Popen(cmd, shell=True, stdout=PIPE) stdout, stderr = p.communicate() stdout = str(stdout).replace('\\n', '\n') stdout = stdout.replace("'", '') print(stdout) # get c++ solution from std pattern = re.compile(r'xOpt\s*=\s*\[\s+(?P<xOpt>([0-9., e+-])*)\];') match = pattern.search(stdout) xOpt_expected = match.group('xOpt') xOpt_expected = xOpt_expected.split(",") xOpt_expected = np.asarray(xOpt_expected, dtype=float) pattern = re.compile(r'objVal = (?P<objVal>[0-9-+e.]*)') match = pattern.search(stdout) objVal_expected = match.group('objVal') objVal_expected = np.asarray(objVal_expected, dtype=float) print("xOpt_actual =", xOpt_actual) print("xOpt_expected =", xOpt_expected) print("objVal_actual = ", objVal_actual) print("objVal_expected = ", objVal_expected) assert_almost_equal(xOpt_actual, xOpt_expected, decimal=7) assert_almost_equal(objVal_actual, objVal_expected, decimal=7)
lbA_new = np.array([-2.0]) ubA_new = np.array([1.0]) # Setting up QProblem object. example = QProblem(2, 1) options = Options() options.printLevel = PrintLevel.NONE example.setOptions(options) # Solve first QP. nWSR = np.array([10]) example.init(H, g, A, lb, ub, lbA, ubA, nWSR) # Solve second QP. nWSR = np.array([10]) for i in range(100000): for j in range(1, 100): g_new[0] = i%j example.hotstart( g_new, lb_new, ub_new, lbA_new, ubA_new, nWSR) # Get and print solution of second QP. xOpt = np.zeros(2) example.getPrimalSolution(xOpt) print("\nxOpt = [ %e, %e ]; objVal = %e\n\n"%(xOpt[0],xOpt[1],example.getObjVal())) example.printOptions()