Example #1
0
    def test_eval(self):
        """Tests evaluation (e.g. caching, API sugars, etc.)."""
        # `Eval` and `EvalAbstract`: Test with constant systems.
        model_values = [
            AbstractValue.Make("Hello World"),
            AbstractValue.Make(BasicVector([1., 2., 3.])),
        ]
        for model_value in model_values:
            is_abstract = not isinstance(model_value.get_value(), BasicVector)
            if is_abstract:
                zoh = ConstantValueSource(copy.copy(model_value))
            else:
                zoh = ConstantVectorSource(model_value.get_value().get_value())
            context = zoh.CreateDefaultContext()
            output_port = zoh.get_output_port(0)
            value_abstract = output_port.EvalAbstract(context)
            value = output_port.Eval(context)
            self.assertEqual(type(value_abstract), type(model_value))
            self.assertEqual(type(value), type(model_value.get_value()))
            if is_abstract:
                check = self.assertEqual
            else:

                def check(a, b):
                    self.assertEqual(type(a.get_value()), type(b.get_value()))
                    np.testing.assert_equal(a.get_value(), b.get_value())

            check(value_abstract.get_value(), model_value.get_value())
            check(value, model_value.get_value())
Example #2
0
    def test_eval(self):
        """Tests evaluation (e.g. caching, API sugars, etc.)."""
        # `Eval` and `EvalAbstract`: Test with constant systems.
        model_values = [
            AbstractValue.Make("Hello World"),
            AbstractValue.Make(BasicVector([1., 2., 3.])),
        ]
        for model_value in model_values:
            is_abstract = not isinstance(model_value.get_value(), BasicVector)
            if is_abstract:
                zoh = ConstantValueSource(copy.copy(model_value))
            else:
                zoh = ConstantVectorSource(model_value.get_value().get_value())
            context = zoh.CreateDefaultContext()
            output_port = zoh.get_output_port(0)
            value_abstract = output_port.EvalAbstract(context)
            value = output_port.Eval(context)
            self.assertEqual(type(value_abstract), type(model_value))
            self.assertEqual(type(value), type(model_value.get_value()))
            if is_abstract:
                check = self.assertEqual
            else:

                def check(a, b):
                    self.assertEqual(type(a.get_value()), type(b.get_value()))
                    np.testing.assert_equal(a.get_value(), b.get_value())

            check(value_abstract.get_value(), model_value.get_value())
            check(value, model_value.get_value())
Example #3
0
    def test_abstract_output_port_eval(self):
        model_value = AbstractValue.Make("Hello World")
        source = ConstantValueSource(copy.copy(model_value))
        context = source.CreateDefaultContext()
        output_port = source.get_output_port(0)

        value = output_port.Eval(context)
        self.assertEqual(type(value), type(model_value.get_value()))
        self.assertEqual(value, model_value.get_value())

        value_abs = output_port.EvalAbstract(context)
        self.assertEqual(type(value_abs), type(model_value))
        self.assertEqual(value_abs.get_value(), model_value.get_value())
Example #4
0
    def test_abstract_output_port_eval(self):
        model_value = AbstractValue.Make("Hello World")
        source = ConstantValueSource(copy.copy(model_value))
        context = source.CreateDefaultContext()
        output_port = source.get_output_port(0)

        value = output_port.Eval(context)
        self.assertEqual(type(value), type(model_value.get_value()))
        self.assertEqual(value, model_value.get_value())

        value_abs = output_port.EvalAbstract(context)
        self.assertEqual(type(value_abs), type(model_value))
        self.assertEqual(value_abs.get_value(), model_value.get_value())
def ConstantSource(value):
    """Sugar function to create either a ConstantVectorSource or
    ConstantValueSource based on an input value."""
    if _is_array(value):
        # Use template to permit different dtypes.
        return ConstantVectorSource_(value)
    else:
        return ConstantValueSource(AbstractValue.Make(value))
Example #6
0
 def test_ctor_api(self):
     """Tests construction of systems for systems whose executions semantics
     are not tested above.
     """
     ConstantValueSource(AbstractValue.Make("Hello world"))
     ConstantVectorSource(source_value=[1., 2.])
     ZeroOrderHold(period_sec=0.1, vector_size=2)
     ZeroOrderHold(period_sec=0.1,
                   abstract_model_value=AbstractValue.Make("Hello world"))
Example #7
0
 def test_ctor_api(self):
     """Tests construction of systems for systems whose executions semantics
     are not tested above.
     """
     ConstantValueSource(AbstractValue.Make("Hello world"))
     DiscreteTimeDelay(update_sec=0.1, delay_timesteps=5, vector_size=2)
     DiscreteTimeDelay(
         update_sec=0.1,
         delay_timesteps=5,
         abstract_model_value=AbstractValue.Make("Hello world"))
     ZeroOrderHold(period_sec=0.1, vector_size=2)
     ZeroOrderHold(period_sec=0.1,
                   abstract_model_value=AbstractValue.Make("Hello world"))