def test_mosek_license(self): # Nominal use case. with MosekSolver.AcquireLicense(): pass # Inspect. with MosekSolver.AcquireLicense() as license: self.assertTrue(license.is_valid()) self.assertFalse(license.is_valid())
def test_mosek_solver(self): prog = mp.MathematicalProgram() x = prog.NewContinuousVariables(2, "x") prog.AddLinearConstraint(x[0] >= 1) prog.AddLinearConstraint(x[1] >= 1) prog.AddQuadraticCost(np.eye(2), np.zeros(2), x) solver = MosekSolver() self.assertTrue(solver.available()) self.assertEqual(solver.solver_type(), mp.SolverType.kMosek) result = solver.Solve(prog) self.assertEqual(result, mp.SolutionResult.kSolutionFound) x_expected = np.array([1, 1]) self.assertTrue(np.allclose(prog.GetSolution(x), x_expected))
def linear_program_drake(f, A, b, C=None, d=None, x_bound=None, solver='gurobi', **kwargs): """ Solves the linear program minimize f^T * x s. t. A * x <= b C * x = d INPUTS: f: gradient of the cost function (2D numpy array) A: left hand side of the constraints (2D numpy array) b: right hand side of the constraints (2D numpy array) C: left hand side of the equalities (2D numpy array) d: right hand side of the equalities (2D numpy array) OUTPUTS: x_min: argument which minimizes the cost (its elements are nan if unfeasible or unbounded) cost_min: minimum of the cost function (nan if unfeasible or unbounded) """ # program dimensions n_variables = f.shape[0] n_constraints = A.shape[0] # build program prog = mp.MathematicalProgram() x = prog.NewContinuousVariables(n_variables, "x") for i in range(0, n_constraints): prog.AddLinearConstraint((A[i,:] + 1e-15).dot(x) <= b[i]) if C is not None and d is not None: for i in range(C.shape[0]): prog.AddLinearConstraint(C[i, :].dot(x) == d[i]) prog.AddLinearCost((f.flatten() + 1e-15).dot(x)) # options if solver == 'gurobi': for (key, value) in kwargs.items(): prog.SetSolverOption(mp.SolverType.kGurobi, key, value) # set bounds to the solution if x_bound is not None: for i in range(0, n_variables): prog.AddLinearConstraint(x[i] <= x_bound) prog.AddLinearConstraint(x[i] >= -x_bound) # solve if solver == 'gurobi': solver = GurobiSolver() elif solver == 'mosek': solver = MosekSolver() result = solver.Solve(prog) x_min = np.reshape(prog.GetSolution(x), (n_variables,1)) cost_min = f.T.dot(x_min) return [x_min, cost_min]
def test_mosek_solver(self): prog = mp.MathematicalProgram() x = prog.NewContinuousVariables(2, "x") prog.AddLinearConstraint(x[0] >= 1) prog.AddLinearConstraint(x[1] >= 1) prog.AddQuadraticCost(np.eye(2), np.zeros(2), x) solver = MosekSolver() # Mosek prints output to the terminal. solver.set_stream_logging(True, "") self.assertTrue(solver.available()) self.assertEqual(solver.solver_type(), mp.SolverType.kMosek) result = solver.Solve(prog, None, None) self.assertTrue(result.is_success()) x_expected = np.array([1, 1]) self.assertTrue(np.allclose(result.GetSolution(x), x_expected))
def test_mosek_solver(self): prog = mp.MathematicalProgram() x = prog.NewContinuousVariables(2, "x") prog.AddLinearConstraint(x[0] >= 1) prog.AddLinearConstraint(x[1] >= 1) prog.AddQuadraticCost(np.eye(2), np.zeros(2), x) solver = MosekSolver() self.assertEqual(solver.solver_id(), MosekSolver.id()) # Mosek prints output to the terminal. solver_options = mp.SolverOptions() solver_options.SetOption(mp.CommonSolverOption.kPrintToConsole, 1) self.assertTrue(solver.available()) self.assertEqual(solver.solver_type(), mp.SolverType.kMosek) result = solver.Solve(prog, None, solver_options) self.assertTrue(result.is_success()) x_expected = np.array([1, 1]) self.assertTrue(np.allclose(result.GetSolution(x), x_expected)) self.assertEqual(result.get_solver_details().solution_status, 1) self.assertEqual(result.get_solver_details().rescode, 0) self.assertGreater(result.get_solver_details().optimizer_time, 0.)