def test_direct_transcription_continuous_time(self):
     # Test that the continuous-time constructor is also spelled correctly.
     plant = LinearSystem(A=[0.], B=[1.], C=[1.], D=[0.])
     context = plant.CreateDefaultContext()
     dirtran = DirectTranscription(plant, context, num_time_samples=3,
                                   fixed_timestep=TimeStep(0.1))
     self.assertEqual(len(dirtran.linear_equality_constraints()), 3)
 def test_direct_transcription_continuous_time(self):
     # Test that the continuous-time constructor is also spelled correctly.
     plant = LinearSystem(A=[0.], B=[1.], C=[1.], D=[0.])
     context = plant.CreateDefaultContext()
     dirtran = DirectTranscription(plant, context, num_time_samples=3,
                                   fixed_timestep=TimeStep(0.1))
     self.assertEqual(len(dirtran.linear_equality_constraints()), 3)
 def test_direct_transcription_continuous_time(self):
     # Test that the continuous-time constructor is also spelled correctly.
     plant = LinearSystem(A=[0.], B=[1.], C=[1.], D=[0.])
     context = plant.CreateDefaultContext()
     dirtran = DirectTranscription(plant,
                                   context,
                                   num_time_samples=3,
                                   fixed_timestep=TimeStep(0.1))
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter("once", DrakeDeprecationWarning)
         self.assertEqual(len(dirtran.linear_equality_constraints()), 3)
     self.assertEqual(len(dirtran.prog().linear_equality_constraints()), 3)
    def test_deprecated_math_prog_methods(self):
        plant = LinearSystem(A=[0.], B=[1.], C=[1.], D=[0.])
        context = plant.CreateDefaultContext()
        dirtran = DirectTranscription(plant,
                                      context,
                                      num_time_samples=3,
                                      fixed_timestep=TimeStep(0.1))

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("once", DrakeDeprecationWarning)
            dirtran.num_vars()
            self.assertEqual(len(w), 1)
            expected_message = ("Use trajopt.prog().num_vars(...) instead "
                                "of trajopt.num_vars(...).")
            self.assertIn(expected_message, str(w[0].message))

        # Test a small subset of the exhaustive list.
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("once", DrakeDeprecationWarning)
            x = dirtran.NewContinuousVariables(3, 'x')
            dirtran.AddCost(x[0] + 2)
            dirtran.AddLinearCost(x[0])
            dirtran.AddConstraint(x[1] == x[2])
            self.assertEqual(len(w), 4)

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("once", DrakeDeprecationWarning)
            result = mp.Solve(dirtran)
            self.assertEqual(len(w), 1)
            expected_message = (
                "The trajectory optimization classes no longer derive from "
                "MathematicalProgram.  Use Solve(trajopt.prog()).")
            self.assertIn(expected_message, str(w[0].message))
    def _do_test_direct_transcription(self, use_deprecated_solve):
        # Integrator.
        plant = LinearSystem(A=[0.], B=[1.], C=[1.], D=[0.], time_period=0.1)
        context = plant.CreateDefaultContext()

        dirtran = DirectTranscription(plant, context, num_time_samples=21)

        # Spell out most of the methods, regardless of whether they make sense
        # as a consistent optimization.  The goal is to check the bindings,
        # not the implementation.
        t = dirtran.time()
        dt = dirtran.fixed_timestep()
        x = dirtran.state()
        x2 = dirtran.state(2)
        x0 = dirtran.initial_state()
        xf = dirtran.final_state()
        u = dirtran.input()
        u2 = dirtran.input(2)

        dirtran.AddRunningCost(x.dot(x))
        dirtran.AddConstraintToAllKnotPoints(u[0] == 0)
        dirtran.AddFinalCost(2*x.dot(x))

        initial_u = PiecewisePolynomial.ZeroOrderHold([0, .3*21],
                                                      np.zeros((1, 2)))
        initial_x = PiecewisePolynomial()
        dirtran.SetInitialTrajectory(initial_u, initial_x)

        if use_deprecated_solve:
            with catch_drake_warnings(expected_count=6):
                dirtran.Solve()
                times = dirtran.GetSampleTimes()
                inputs = dirtran.GetInputSamples()
                states = dirtran.GetStateSamples()
                input_traj = dirtran.ReconstructInputTrajectory()
                state_traj = dirtran.ReconstructStateTrajectory()
        else:
            result = mp.Solve(dirtran)
            times = dirtran.GetSampleTimes(result)
            inputs = dirtran.GetInputSamples(result)
            states = dirtran.GetStateSamples(result)
            input_traj = dirtran.ReconstructInputTrajectory(result)
            state_traj = dirtran.ReconstructStateTrajectory(result)
    def test_direct_transcription(self):
        # Integrator.
        plant = LinearSystem(A=[0.], B=[1.], C=[1.], D=[0.], time_period=0.1)
        context = plant.CreateDefaultContext()

        dirtran = DirectTranscription(plant, context, num_time_samples=21)

        # Spell out most of the methods, regardless of whether they make sense
        # as a consistent optimization.  The goal is to check the bindings,
        # not the implementation.
        t = dirtran.time()
        dt = dirtran.fixed_timestep()
        x = dirtran.state()
        x2 = dirtran.state(2)
        x0 = dirtran.initial_state()
        xf = dirtran.final_state()
        u = dirtran.input()
        u2 = dirtran.input(2)

        dirtran.AddRunningCost(x.dot(x))
        dirtran.AddConstraintToAllKnotPoints(u[0] == 0)
        dirtran.AddFinalCost(2 * x.dot(x))

        initial_u = PiecewisePolynomial.ZeroOrderHold([0, .3 * 21],
                                                      np.zeros((1, 2)))
        initial_x = PiecewisePolynomial()
        dirtran.SetInitialTrajectory(initial_u, initial_x)

        result = mp.Solve(dirtran.prog())
        times = dirtran.GetSampleTimes(result)
        inputs = dirtran.GetInputSamples(result)
        states = dirtran.GetStateSamples(result)
        input_traj = dirtran.ReconstructInputTrajectory(result)
        state_traj = dirtran.ReconstructStateTrajectory(result)
    def _do_test_direct_transcription(self, use_deprecated_solve):
        # Integrator.
        plant = LinearSystem(A=[0.], B=[1.], C=[1.], D=[0.], time_period=0.1)
        context = plant.CreateDefaultContext()

        dirtran = DirectTranscription(plant, context, num_time_samples=21)

        # Spell out most of the methods, regardless of whether they make sense
        # as a consistent optimization.  The goal is to check the bindings,
        # not the implementation.
        t = dirtran.time()
        dt = dirtran.fixed_timestep()
        x = dirtran.state()
        x2 = dirtran.state(2)
        x0 = dirtran.initial_state()
        xf = dirtran.final_state()
        u = dirtran.input()
        u2 = dirtran.input(2)

        dirtran.AddRunningCost(x.dot(x))
        dirtran.AddConstraintToAllKnotPoints(u[0] == 0)
        dirtran.AddFinalCost(2*x.dot(x))

        initial_u = PiecewisePolynomial.ZeroOrderHold([0, .3*21],
                                                      np.zeros((1, 2)))
        initial_x = PiecewisePolynomial()
        dirtran.SetInitialTrajectory(initial_u, initial_x)

        if use_deprecated_solve:
            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always', DrakeDeprecationWarning)
                dirtran.Solve()
                times = dirtran.GetSampleTimes()
                inputs = dirtran.GetInputSamples()
                states = dirtran.GetStateSamples()
                input_traj = dirtran.ReconstructInputTrajectory()
                state_traj = dirtran.ReconstructStateTrajectory()
                self.assertEqual(len(w), 6)
        else:
            result = mp.Solve(dirtran)
            times = dirtran.GetSampleTimes(result)
            inputs = dirtran.GetInputSamples(result)
            states = dirtran.GetStateSamples(result)
            input_traj = dirtran.ReconstructInputTrajectory(result)
            state_traj = dirtran.ReconstructStateTrajectory(result)