Skip to content

Commit

Permalink
pydrake symbolic: Use user-defined dtype
Browse files Browse the repository at this point in the history
  • Loading branch information
EricCousineau-TRI committed May 11, 2018
1 parent c7c537f commit 7ab9719
Show file tree
Hide file tree
Showing 6 changed files with 408 additions and 291 deletions.
1 change: 1 addition & 0 deletions bindings/pydrake/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ drake_pybind_library(
name = "symbolic_py",
cc_deps = [
":symbolic_types_pybind",
"//bindings/pydrake/util:numpy_dtypes_pybind",
"//bindings/pydrake/util:wrap_pybind",
"@fmt",
],
Expand Down
15 changes: 9 additions & 6 deletions bindings/pydrake/solvers/test/mathematicalprogram_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ def test_program_construction(self):
def test_mixed_integer_optimization(self):
prog = mp.MathematicalProgram()
x = prog.NewBinaryVariables(3, "x")
xe = x.astype(sym.Expression)
c = np.array([-1.0, -1.0, -2.0])
prog.AddLinearCost(c.dot(x))
prog.AddLinearCost(c.dot(xe))
a = np.array([1.0, 2.0, 3.0])
prog.AddLinearConstraint(a.dot(x) <= 4)
prog.AddLinearConstraint(a.dot(xe) <= 4)
prog.AddLinearConstraint(x[0] + x[1], 1, np.inf)
self.assertIsNone(prog.GetSolverId())
result = prog.Solve()
Expand Down Expand Up @@ -223,14 +224,16 @@ def test_sos(self):
# d(1)*x^2 is SOS.
prog = mp.MathematicalProgram()
x = prog.NewIndeterminates(1, "x")
xe = x.astype(sym.Expression)
poly = prog.NewFreePolynomial(sym.Variables(x), 1)
(poly, binding) = prog.NewSosPolynomial(sym.Variables(x), 2)
y = prog.NewIndeterminates(1, "y")
ye = y.astype(sym.Expression)
(poly, binding) = prog.NewSosPolynomial((sym.Monomial(x[0]),
sym.Monomial(y[0])))
d = prog.NewContinuousVariables(2, "d")
prog.AddSosConstraint(d[0]*x.dot(x))
prog.AddSosConstraint(d[1]*x.dot(x), [sym.Monomial(x[0])])
prog.AddSosConstraint(d[0]*xe.dot(xe))
prog.AddSosConstraint(d[1]*xe.dot(xe), [sym.Monomial(x[0])])
result = prog.Solve()
self.assertEqual(result, mp.SolutionResult.kSolutionFound)

Expand All @@ -241,11 +244,11 @@ def test_sos(self):
prog.SubstituteSolution(d[0] + d[1]).Evaluate(),
prog.GetSolution(d[0]) + prog.GetSolution(d[1]))
# Test SubstituteSolution(sym.Polynomial)
poly = d[0]*x.dot(x)
poly = d[0]*xe.dot(xe)
poly_sub_actual = prog.SubstituteSolution(
sym.Polynomial(poly, sym.Variables(x)))
poly_sub_expected = sym.Polynomial(
prog.SubstituteSolution(d[0])*x.dot(x), sym.Variables(x))
prog.SubstituteSolution(d[0])*xe.dot(xe), sym.Variables(x))
# TODO(soonho): At present, these must be converted to `Expression` to
# compare, because as `Polynomial`s the comparison fails with
# `0*x(0)^2` != `0`, which indicates that simplification is not
Expand Down
Loading

0 comments on commit 7ab9719

Please sign in to comment.