Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests for construct_search_clf_cbf_program #22

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/test_clf_cbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pydrake.symbolic as sym

import compatible_clf_cbf.utils as utils
import tests.test_utils as test_utils


class TestCompatibleLagrangianDegrees(object):
Expand Down Expand Up @@ -480,3 +481,10 @@ def test_search_clf_cbf(self):
env = {self.x[i]: 0 for i in range(self.nx)}
assert V_result.Evaluate(env) == 0
assert sym.Monomial() not in V.monomial_to_coefficient_map()
assert test_utils.is_sos(V_result, solvers.ClarabelSolver.id())
assert V_result.TotalDegree() == 2
rho_result = result.GetSolution(rho)
assert rho_result >= 0

b_result = np.array([result.GetSolution(b[i]) for i in range(b.size)])
assert all([b_result[i].TotalDegree() <= 2 for i in range(b.size)])
19 changes: 19 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import compatible_clf_cbf.utils as mut

from typing import Optional

import numpy as np
import pytest # noqa

Expand Down Expand Up @@ -33,3 +35,20 @@ def tester(lower):

tester(2.0)
tester(3.0)


def is_sos(
poly: sym.Polynomial,
solver_id: Optional[solvers.SolverId] = None,
solver_options: Optional[solvers.SolverOptions] = None,
):
prog = solvers.MathematicalProgram()
prog.AddIndeterminates(poly.indeterminates())
assert poly.decision_variables().empty()
prog.AddSosConstraint(poly)
if solver_id is None:
result = solvers.Solve(prog, None, solver_options)
else:
solver = solvers.MakeSolver(solver_id)
result = solver.Solve(prog, None, solver_options)
return result.is_success()