Ejemplo n.º 1
0
 def test_bad_init_log_types(self):
     # lists are not of length: (2^n) + 1
     with self.assertRaises(ValueError):
         transforms.piecewise([1, 2, 3, 4], [1, 2, 3, 4],
                              repn='dlog',
                              validate=False)
     with self.assertRaises(ValueError):
         transforms.piecewise([1, 2, 3, 4], [1, 2, 3, 4],
                              repn='log',
                              validate=False)
Ejemplo n.º 2
0
    def test_bad_repn(self):
        repn = list(transforms.registered_transforms.keys())[0]
        self.assertTrue(repn in transforms.registered_transforms)
        transforms.piecewise([1,2,3],
                             [1,2,1],
                             validate=False,
                             repn=repn)

        repn = '_bad_repn_'
        self.assertFalse(repn in transforms.registered_transforms)
        with self.assertRaises(ValueError):
            transforms.piecewise([1,2,3],
                                 [1,2,1],
                                 validate=False,
                                 repn=repn)
        with self.assertRaises(ValueError):
            transforms.piecewise([1,2,3],
                                 [1,2,1],
                                 input=variable(lb=1,ub=3),
                                 validate=True,
                                 simplify=False,
                                 repn=repn)
        with self.assertRaises(ValueError):
            transforms.piecewise([1,2,3],
                                 [1,2,1],
                                 input=variable(lb=1,ub=3),
                                 validate=True,
                                 simplify=True,
                                 repn=repn)
Ejemplo n.º 3
0
 def test_pickle(self):
     for key in transforms.registered_transforms:
         v = variable(lb=1,ub=3)
         p = transforms.piecewise([1,2,3],
                                  [1,2,1],
                                  input=v,
                                  validate=False,
                                  repn=key)
         self.assertEqual(p.parent, None)
         self.assertEqual(p.input.expr.parent, None)
         self.assertIs(p.input.expr, v)
         pup = pickle.loads(
             pickle.dumps(p))
         self.assertEqual(pup.parent, None)
         self.assertEqual(pup.input.expr.parent, None)
         self.assertIsNot(pup.input.expr, v)
         b = block()
         b.v = v
         b.p = p
         self.assertIs(p.parent, b)
         self.assertEqual(p.input.expr.parent, b)
         bup = pickle.loads(
             pickle.dumps(b))
         pup = bup.p
         self.assertIs(pup.parent, bup)
         self.assertEqual(pup.input.expr.parent, bup)
         self.assertIs(pup.input.expr, bup.v)
         self.assertIsNot(pup.input.expr, b.v)
Ejemplo n.º 4
0
 def test_step(self):
     breakpoints = [1,2,2]
     values = [1,0,1]
     v = variable()
     v.bounds = min(breakpoints), max(breakpoints)
     for key in transforms.registered_transforms:
         if key in ('mc','convex'):
             with self.assertRaises(util.PiecewiseValidationError):
                 transforms.piecewise(breakpoints,
                                      values,
                                      input=v,
                                      repn=key)
         else:
             p = transforms.piecewise(breakpoints,
                                      values,
                                      input=v,
                                      repn=key)
             self.assertEqual(p.validate(), 4)
Ejemplo n.º 5
0
 def test_init(self):
     for key in transforms.registered_transforms:
         for bound in ['lb', 'ub', 'eq', 'bad']:
             for args in [([1, 2, 3], [1, 2, 1]),
                          ([1, 2, 3, 4, 5], [1, 2, 1, 2, 1]),
                          ([1, 2, 3, 4, 5, 6, 7, 8,
                            9], [1, 2, 1, 2, 1, 2, 1, 2, 1])]:
                 kwds = {'repn': key, 'bound': bound, 'validate': False}
                 if bound == 'bad':
                     with self.assertRaises(ValueError):
                         transforms.piecewise(*args, **kwds)
                     kwds['simplify'] = True
                     with self.assertRaises(ValueError):
                         transforms.piecewise(*args, **kwds)
                     kwds['simplify'] = False
                     with self.assertRaises(ValueError):
                         transforms.piecewise(*args, **kwds)
                 else:
                     p = transforms.piecewise(*args, **kwds)
                     self.assertTrue(
                         isinstance(p,
                                    transforms.registered_transforms[key]))
                     self.assertTrue(
                         isinstance(p, TransformedPiecewiseLinearFunction))
                     self.assertEqual(p.active, True)
                     self.assertIs(p.parent, None)
                     kwds['simplify'] = True
                     p = transforms.piecewise(*args, **kwds)
                     self.assertTrue(
                         isinstance(p,
                                    transforms.registered_transforms[key]))
                     self.assertTrue(
                         isinstance(p, TransformedPiecewiseLinearFunction))
                     self.assertEqual(p.active, True)
                     self.assertIs(p.parent, None)
                     kwds['simplify'] = False
                     p = transforms.piecewise(*args, **kwds)
                     self.assertTrue(
                         isinstance(p,
                                    transforms.registered_transforms[key]))
                     self.assertTrue(
                         isinstance(p, TransformedPiecewiseLinearFunction))
                     self.assertEqual(p.active, True)
                     self.assertIs(p.parent, None)
Ejemplo n.º 6
0
 def test_type(self):
     for key in transforms.registered_transforms:
         p = transforms.piecewise([1, 2, 3], [1, 2, 1],
                                  repn=key,
                                  validate=False)
         self.assertTrue(isinstance(p, TransformedPiecewiseLinearFunction))
         self.assertTrue(
             isinstance(p, transforms.registered_transforms[key]))
         self.assertTrue(isinstance(p, ICategorizedObject))
         self.assertTrue(isinstance(p, IComponent))
         self.assertTrue(isinstance(p, IComponentContainer))
         self.assertTrue(isinstance(p, _ActiveObjectMixin))
         self.assertTrue(isinstance(p, tiny_block))
         self.assertTrue(isinstance(p, IBlockStorage))
Ejemplo n.º 7
0
class Test_piecewise_list(_TestActiveComponentListBase,
                          unittest.TestCase):
    _container_type = block_list
    _ctype_factory = lambda self: transforms.piecewise([1,2,3],
                                                       [1,2,1],
                                                       validate=False)
Ejemplo n.º 8
0
    def test_simplify(self):
        v = variable(lb=1, ub=3)
        convex_breakpoints = [1,2,3]
        convex_values = [1,0,1]
        for key in transforms.registered_transforms:
            for bound in ('lb','ub','eq'):
                if (key == 'convex') and \
                   (bound != 'lb'):
                    with self.assertRaises(util.PiecewiseValidationError):
                        transforms.piecewise(convex_breakpoints,
                                             convex_values,
                                             input=v,
                                             repn=key,
                                             bound=bound,
                                             simplify=False)
                    with self.assertRaises(util.PiecewiseValidationError):
                        transforms.piecewise(convex_breakpoints,
                                             convex_values,
                                             input=v,
                                             repn=key,
                                             bound=bound,
                                             simplify=True)
                else:
                    p = transforms.piecewise(convex_breakpoints,
                                             convex_values,
                                             input=v,
                                             repn=key,
                                             bound=bound,
                                             simplify=False)
                    self.assertTrue(
                        isinstance(p, transforms.registered_transforms[key]))
                    self.assertEqual(p.validate(), util.characterize_function.convex)
                    p = transforms.piecewise(convex_breakpoints,
                                             convex_values,
                                             input=v,
                                             repn=key,
                                             bound=bound,
                                             simplify=True)
                    if bound == 'lb':
                        self.assertTrue(
                            isinstance(p, transforms.registered_transforms['convex']))
                    else:
                        self.assertTrue(
                            isinstance(p, transforms.registered_transforms[key]))

        concave_breakpoints = [1,2,3]
        concave_values = [-1,0,-1]
        for key in transforms.registered_transforms:
            for bound in ('lb','ub','eq'):
                if (key == 'convex') and \
                   (bound != 'ub'):
                    with self.assertRaises(util.PiecewiseValidationError):
                        transforms.piecewise(concave_breakpoints,
                                             concave_values,
                                             input=v,
                                             repn=key,
                                             bound=bound,
                                             simplify=False)
                    with self.assertRaises(util.PiecewiseValidationError):
                        transforms.piecewise(concave_breakpoints,
                                             concave_values,
                                             input=v,
                                             repn=key,
                                             bound=bound,
                                             simplify=True)
                else:
                    p = transforms.piecewise(concave_breakpoints,
                                             concave_values,
                                             input=v,
                                             repn=key,
                                             bound=bound,
                                             simplify=False)
                    self.assertTrue(
                        isinstance(p, transforms.registered_transforms[key]))
                    self.assertEqual(p.validate(), util.characterize_function.concave)
                    p = transforms.piecewise(concave_breakpoints,
                                             concave_values,
                                             input=v,
                                             repn=key,
                                             bound=bound,
                                             simplify=True)
                    if bound == 'ub':
                        self.assertTrue(
                            isinstance(p, transforms.registered_transforms['convex']))
                    else:
                        self.assertTrue(
                            isinstance(p, transforms.registered_transforms[key]))

        affine_breakpoints = [1,3]
        affine_values = [1,3]
        for key in transforms.registered_transforms:
            for bound in ('lb','ub','eq'):
                p = transforms.piecewise(affine_breakpoints,
                                         affine_values,
                                         input=v,
                                         repn=key,
                                         bound=bound,
                                         simplify=False)
                self.assertTrue(
                    isinstance(p, transforms.registered_transforms[key]))
                self.assertEqual(p.validate(), util.characterize_function.affine)
                p = transforms.piecewise(affine_breakpoints,
                                         affine_values,
                                         input=v,
                                         repn=key,
                                         bound=bound,
                                         simplify=True)
                self.assertTrue(
                    isinstance(p, transforms.registered_transforms['convex']))