コード例 #1
0
 def test_pickle(self):
     s = suffix(direction=suffix.EXPORT,
                datatype=suffix.FLOAT)
     self.assertEqual(s.direction, suffix.EXPORT)
     self.assertEqual(s.datatype, suffix.FLOAT)
     self.assertEqual(s.parent, None)
     sup = pickle.loads(
         pickle.dumps(s))
     self.assertEqual(sup.direction, suffix.EXPORT)
     self.assertEqual(sup.datatype, suffix.FLOAT)
     self.assertEqual(sup.parent, None)
     b = block()
     b.s = s
     self.assertIs(s.parent, b)
     bup = pickle.loads(
         pickle.dumps(b))
     sup = bup.s
     self.assertEqual(sup.direction, suffix.EXPORT)
     self.assertEqual(sup.datatype, suffix.FLOAT)
     self.assertIs(sup.parent, bup)
     b.v = variable(lb=1)
     b.s[b.v] = 1.0
     bup = pickle.loads(
         pickle.dumps(b))
     sup = bup.s
     vup = bup.v
     self.assertEqual(sup[vup], 1.0)
コード例 #2
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
 def test_type(self):
     s = suffix()
     self.assertTrue(isinstance(s, ICategorizedObject))
     self.assertTrue(isinstance(s, collections.Mapping))
     self.assertTrue(isinstance(s, collections.MutableMapping))
     self.assertTrue(issubclass(type(s), collections.Mapping))
     self.assertTrue(issubclass(type(s), collections.MutableMapping))
コード例 #3
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
 def test_pickle(self):
     s = suffix(direction=suffix.EXPORT,
                datatype=suffix.FLOAT)
     self.assertEqual(s.direction, suffix.EXPORT)
     self.assertEqual(s.datatype, suffix.FLOAT)
     self.assertEqual(s.parent, None)
     sup = pickle.loads(
         pickle.dumps(s))
     self.assertEqual(sup.direction, suffix.EXPORT)
     self.assertEqual(sup.datatype, suffix.FLOAT)
     self.assertEqual(sup.parent, None)
     b = block()
     b.s = s
     self.assertIs(s.parent, b)
     bup = pickle.loads(
         pickle.dumps(b))
     sup = bup.s
     self.assertEqual(sup.direction, suffix.EXPORT)
     self.assertEqual(sup.datatype, suffix.FLOAT)
     self.assertIs(sup.parent, bup)
     b.v = variable(lb=1)
     b.s[b.v] = 1.0
     bup = pickle.loads(
         pickle.dumps(b))
     sup = bup.s
     vup = bup.v
     self.assertEqual(sup[vup], 1.0)
コード例 #4
0
 def test_type(self):
     s = suffix()
     self.assertTrue(isinstance(s, ICategorizedObject))
     self.assertTrue(isinstance(s, collections_Mapping))
     self.assertTrue(isinstance(s, collections_MutableMapping))
     self.assertTrue(issubclass(type(s), collections_Mapping))
     self.assertTrue(issubclass(type(s), collections_MutableMapping))
コード例 #5
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
 def test_getset_datatype(self):
     s = suffix()
     s.set_datatype(suffix.FLOAT)
     self.assertEqual(s.get_datatype(), suffix.FLOAT)
     s.set_datatype(suffix.INT)
     self.assertEqual(s.get_datatype(), suffix.INT)
     with self.assertRaises(ValueError):
         s.set_datatype('something')
コード例 #6
0
 def test_getset_datatype(self):
     s = suffix()
     s.set_datatype(suffix.FLOAT)
     self.assertEqual(s.get_datatype(), suffix.FLOAT)
     s.set_datatype(suffix.INT)
     self.assertEqual(s.get_datatype(), suffix.INT)
     with self.assertRaises(ValueError):
         s.set_datatype('something')
コード例 #7
0
 def test_datatype(self):
     s = suffix()
     s.datatype = suffix.FLOAT
     self.assertEqual(s.datatype, suffix.FLOAT)
     s.datatype = suffix.INT
     self.assertEqual(s.datatype, suffix.INT)
     with self.assertRaises(ValueError):
         s.datatype = 'something'
コード例 #8
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
 def test_datatype(self):
     s = suffix()
     s.datatype = suffix.FLOAT
     self.assertEqual(s.datatype, suffix.FLOAT)
     s.datatype = suffix.INT
     self.assertEqual(s.datatype, suffix.INT)
     with self.assertRaises(ValueError):
         s.datatype = 'something'
コード例 #9
0
    def test_clear_value(self):
        x = variable()

        s = suffix()
        s[x] = 1.0

        self.assertEqual(len(s), 1)
        s.clear_value(x)
        self.assertEqual(len(s), 0)
        s.clear_value(x)
コード例 #10
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
    def test_clear_value(self):
        x = variable()

        s = suffix()
        s[x] = 1.0

        self.assertEqual(len(s), 1)
        s.clear_value(x)
        self.assertEqual(len(s), 0)
        s.clear_value(x)
コード例 #11
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
    def test_clear(self):
        x = variable()
        y = variable()

        s = suffix()
        s[x] = 1.0
        s[y] = None

        self.assertEqual(len(s), 2)
        s.clear()
        self.assertEqual(len(s), 0)
コード例 #12
0
    def test_clear(self):
        x = variable()
        y = variable()

        s = suffix()
        s[x] = 1.0
        s[y] = None

        self.assertEqual(len(s), 2)
        s.clear()
        self.assertEqual(len(s), 0)
コード例 #13
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
    def test_del(self):
        x = variable()

        s = suffix()
        s[x] = 1.0

        self.assertEqual(len(s), 1)
        del s[x]
        self.assertEqual(len(s), 0)
        with self.assertRaises(KeyError):
            del s[x]
コード例 #14
0
    def test_del(self):
        x = variable()

        s = suffix()
        s[x] = 1.0

        self.assertEqual(len(s), 1)
        del s[x]
        self.assertEqual(len(s), 0)
        with self.assertRaises(KeyError):
            del s[x]
コード例 #15
0
 def test_getset_direction(self):
     s = suffix()
     s.set_direction(suffix.LOCAL)
     self.assertEqual(s.get_direction(), suffix.LOCAL)
     s.set_direction(suffix.IMPORT)
     self.assertEqual(s.get_direction(), suffix.IMPORT)
     s.set_direction(suffix.EXPORT)
     self.assertEqual(s.get_direction(), suffix.EXPORT)
     s.set_direction(suffix.IMPORT_EXPORT)
     self.assertEqual(s.get_direction(), suffix.IMPORT_EXPORT)
     with self.assertRaises(ValueError):
         s.set_direction('export')
コード例 #16
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
 def test_getset_direction(self):
     s = suffix()
     s.set_direction(suffix.LOCAL)
     self.assertEqual(s.get_direction(), suffix.LOCAL)
     s.set_direction(suffix.IMPORT)
     self.assertEqual(s.get_direction(), suffix.IMPORT)
     s.set_direction(suffix.EXPORT)
     self.assertEqual(s.get_direction(), suffix.EXPORT)
     s.set_direction(suffix.IMPORT_EXPORT)
     self.assertEqual(s.get_direction(), suffix.IMPORT_EXPORT)
     with self.assertRaises(ValueError):
         s.set_direction('export')
コード例 #17
0
    def test_set_all_values(self):
        x = variable()
        y = variable()

        s = suffix()
        s[x] = 1.0
        s[y] = None

        self.assertEqual(s[x], 1.0)
        self.assertEqual(s[y], None)
        s.set_all_values(0)
        self.assertEqual(s[x], 0)
        self.assertEqual(s[y], 0)
コード例 #18
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
    def test_set_all_values(self):
        x = variable()
        y = variable()

        s = suffix()
        s[x] = 1.0
        s[y] = None

        self.assertEqual(s[x], 1.0)
        self.assertEqual(s[y], None)
        s.set_all_values(0)
        self.assertEqual(s[x], 0)
        self.assertEqual(s[y], 0)
コード例 #19
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
 def test_import_export_enabled(self):
     s = suffix()
     s.direction = suffix.LOCAL
     self.assertEqual(s.direction, suffix.LOCAL)
     self.assertEqual(s.export_enabled, False)
     self.assertEqual(s.import_enabled, False)
     s.direction = suffix.IMPORT
     self.assertEqual(s.direction, suffix.IMPORT)
     self.assertEqual(s.export_enabled, False)
     self.assertEqual(s.import_enabled, True)
     s.direction = suffix.EXPORT
     self.assertEqual(s.direction, suffix.EXPORT)
     self.assertEqual(s.export_enabled, True)
     self.assertEqual(s.import_enabled, False)
     s.direction = suffix.IMPORT_EXPORT
     self.assertEqual(s.direction, suffix.IMPORT_EXPORT)
     self.assertEqual(s.export_enabled, True)
     self.assertEqual(s.import_enabled, True)
     with self.assertRaises(ValueError):
         s.direction = 'export'
コード例 #20
0
 def test_import_export_enabled(self):
     s = suffix()
     s.direction = suffix.LOCAL
     self.assertEqual(s.direction, suffix.LOCAL)
     self.assertEqual(s.export_enabled, False)
     self.assertEqual(s.import_enabled, False)
     s.direction = suffix.IMPORT
     self.assertEqual(s.direction, suffix.IMPORT)
     self.assertEqual(s.export_enabled, False)
     self.assertEqual(s.import_enabled, True)
     s.direction = suffix.EXPORT
     self.assertEqual(s.direction, suffix.EXPORT)
     self.assertEqual(s.export_enabled, True)
     self.assertEqual(s.import_enabled, False)
     s.direction = suffix.IMPORT_EXPORT
     self.assertEqual(s.direction, suffix.IMPORT_EXPORT)
     self.assertEqual(s.export_enabled, True)
     self.assertEqual(s.import_enabled, True)
     with self.assertRaises(ValueError):
         s.direction = 'export'
コード例 #21
0
    def test_pprint(self):
        # Not really testing what the output is, just that
        # an error does not occur. The pprint functionality
        # is still in the early stages.
        v = variable()
        clist = constraint_list([constraint()])
        s = suffix()
        s[v] = 1
        s[clist] = None
        pprint(s)
        b = block()
        b.s = s
        pprint(s)
        pprint(b)
        m = block()
        m.b = b
        pprint(s)
        pprint(b)
        pprint(m)

        pprint({'a': 1, 'b': 2})
コード例 #22
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
    def test_name(self):
        s = suffix()
        self.assertTrue(s.parent is None)
        self.assertEqual(s.local_name, None)
        self.assertEqual(s.name, None)

        model = block()
        model.s = s
        self.assertTrue(model.parent is None)
        self.assertTrue(s.parent is model)
        self.assertEqual(s.local_name, "s")
        self.assertEqual(s.name, "s")

        b = block()
        b.model = model
        self.assertTrue(b.parent is None)
        self.assertTrue(model.parent is b)
        self.assertTrue(s.parent is model)
        self.assertEqual(s.local_name, "s")
        self.assertEqual(s.name, "model.s")

        bdict = block_dict()
        bdict[0] = b
        self.assertTrue(bdict.parent is None)
        self.assertTrue(b.parent is bdict)
        self.assertTrue(model.parent is b)
        self.assertTrue(s.parent is model)
        self.assertEqual(s.local_name, "s")
        self.assertEqual(s.name, "[0].model.s")

        m = block()
        m.bdict = bdict
        self.assertTrue(m.parent is None)
        self.assertTrue(bdict.parent is m)
        self.assertTrue(b.parent is bdict)
        self.assertTrue(model.parent is b)
        self.assertTrue(s.parent is model)
        self.assertEqual(s.local_name, "s")
        self.assertEqual(s.name, "bdict[0].model.s")
コード例 #23
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
    def test_pprint(self):
        import pyomo.kernel
        # Not really testing what the output is, just that
        # an error does not occur. The pprint functionality
        # is still in the early stages.
        v = variable()
        clist = constraint_list([constraint()])
        s = suffix()
        s[v] = 1
        s[clist] = None
        pyomo.kernel.pprint(s)
        b = block()
        b.s = s
        pyomo.kernel.pprint(s)
        pyomo.kernel.pprint(b)
        m = block()
        m.b = b
        pyomo.kernel.pprint(s)
        pyomo.kernel.pprint(b)
        pyomo.kernel.pprint(m)

        pyomo.kernel.pprint({'a': 1, 'b': 2})
コード例 #24
0
    def test_name(self):
        s = suffix()
        self.assertTrue(s.parent is None)
        self.assertEqual(s.local_name, None)
        self.assertEqual(s.name, None)

        model = block()
        model.s = s
        self.assertTrue(model.parent is None)
        self.assertTrue(s.parent is model)
        self.assertEqual(s.local_name, "s")
        self.assertEqual(s.name, "s")

        b = block()
        b.model = model
        self.assertTrue(b.parent is None)
        self.assertTrue(model.parent is b)
        self.assertTrue(s.parent is model)
        self.assertEqual(s.local_name, "s")
        self.assertEqual(s.name, "model.s")

        bdict = block_dict()
        bdict[0] = b
        self.assertTrue(bdict.parent is None)
        self.assertTrue(b.parent is bdict)
        self.assertTrue(model.parent is b)
        self.assertTrue(s.parent is model)
        self.assertEqual(s.local_name, "s")
        self.assertEqual(s.name, "[0].model.s")

        m = block()
        m.bdict = bdict
        self.assertTrue(m.parent is None)
        self.assertTrue(bdict.parent is m)
        self.assertTrue(b.parent is bdict)
        self.assertTrue(model.parent is b)
        self.assertTrue(s.parent is model)
        self.assertEqual(s.local_name, "s")
        self.assertEqual(s.name, "bdict[0].model.s")
コード例 #25
0
ファイル: test_component_set.py プロジェクト: jialuw96/pyomo
class TestComponentSet(unittest.TestCase):

    _components = [
        variable(),
        variable_dict(),
        variable_list(),
        constraint(),
        constraint_dict(),
        constraint_list(),
        objective(),
        objective_dict(),
        objective_list(),
        expression(),
        expression_dict(),
        expression_list(),
        block(),
        block_dict(),
        block_list(),
        suffix()
    ]

    def test_pickle(self):
        c = ComponentSet()
        self.assertEqual(len(c), 0)
        cup = pickle.loads(pickle.dumps(c))
        self.assertIsNot(cup, c)
        self.assertEqual(len(cup), 0)

        v = variable()
        c.add(v)
        self.assertEqual(len(c), 1)
        self.assertTrue(v in c)
        cup = pickle.loads(pickle.dumps(c))
        vup = cup.pop()
        cup.add(vup)
        self.assertIsNot(cup, c)
        self.assertIsNot(vup, v)
        self.assertEqual(len(cup), 1)
        self.assertTrue(vup in cup)
        self.assertEqual(vup.parent, None)

        b = block()
        V = b.V = variable_list()
        b.V.append(v)
        b.c = c
        self.assertEqual(len(c), 1)
        self.assertTrue(v in c)
        self.assertIs(v.parent, b.V)
        self.assertIs(V.parent, b)
        self.assertIs(b.parent, None)
        bup = pickle.loads(pickle.dumps(b))
        Vup = bup.V
        vup = Vup[0]
        cup = bup.c
        self.assertIsNot(cup, c)
        self.assertIsNot(vup, v)
        self.assertIsNot(Vup, V)
        self.assertIsNot(bup, b)
        self.assertEqual(len(cup), 1)
        self.assertTrue(vup in cup)
        self.assertIs(vup.parent, Vup)
        self.assertIs(Vup.parent, bup)
        self.assertIs(bup.parent, None)

        self.assertEqual(len(c), 1)
        self.assertTrue(v in c)

    def test_init(self):
        cset = ComponentSet()
        cset = ComponentSet(self._components)
        with self.assertRaises(TypeError):
            cset = ComponentSet(*self._components)

    def test_type(self):
        cset = ComponentSet()
        self.assertTrue(isinstance(cset, collections.abc.Set))
        self.assertTrue(isinstance(cset, collections.abc.MutableSet))
        self.assertTrue(issubclass(type(cset), collections.abc.Set))
        self.assertTrue(issubclass(type(cset), collections.abc.MutableSet))

    def test_str(self):
        cset = ComponentSet()
        self.assertEqual(str(cset), "ComponentSet([])")
        cset.update(self._components)
        str(cset)

    def test_len(self):
        cset = ComponentSet()
        self.assertEqual(len(cset), 0)
        cset.update(self._components)
        self.assertEqual(len(cset), len(self._components))
        cset = ComponentSet(self._components)
        self.assertEqual(len(cset), len(self._components))
        self.assertTrue(len(self._components) > 0)

    def test_iter(self):
        cset = ComponentSet()
        self.assertEqual(list(iter(cset)), [])
        cset.update(self._components)
        ids_seen = set()
        for c in cset:
            ids_seen.add(id(c))
        self.assertEqual(ids_seen, set(id(c) for c in self._components))

    def set_add(self):
        cset = ComponentSet()
        self.assertEqual(len(cset), 0)
        for i, c in enumerate(self._components):
            self.assertTrue(c not in cset)
            cset.add(c)
            self.assertTrue(c in cset)
            self.assertEqual(len(cset), i + 1)
        self.assertEqual(len(cset), len(self._components))
        for c in self._components:
            self.assertTrue(c in cset)
            cset.add(c)
            self.assertTrue(c in cset)
            self.assertEqual(len(cset), len(self._components))

    def test_pop(self):
        cset = ComponentSet()
        self.assertEqual(len(cset), 0)
        with self.assertRaises(KeyError):
            cset.pop()
        v = variable()
        cset.add(v)
        self.assertTrue(v in cset)
        self.assertEqual(len(cset), 1)
        v_ = cset.pop()
        self.assertIs(v, v_)
        self.assertTrue(v not in cset)
        self.assertEqual(len(cset), 0)

    def test_update(self):
        cset = ComponentSet()
        self.assertEqual(len(cset), 0)
        cset.update(self._components)
        self.assertEqual(len(cset), len(self._components))
        for c in self._components:
            self.assertTrue(c in cset)

    def test_clear(self):
        cset = ComponentSet()
        self.assertEqual(len(cset), 0)
        cset.update(self._components)
        self.assertEqual(len(cset), len(self._components))
        cset.clear()
        self.assertEqual(len(cset), 0)

    def test_remove(self):
        cset = ComponentSet()
        self.assertEqual(len(cset), 0)
        cset.update(self._components)
        self.assertEqual(len(cset), len(self._components))
        for i, c in enumerate(self._components):
            cset.remove(c)
            self.assertEqual(len(cset), len(self._components) - (i + 1))
        for c in self._components:
            self.assertTrue(c not in cset)
            with self.assertRaises(KeyError):
                cset.remove(c)

    def test_discard(self):
        cset = ComponentSet()
        self.assertEqual(len(cset), 0)
        cset.update(self._components)
        self.assertEqual(len(cset), len(self._components))
        for i, c in enumerate(self._components):
            cset.discard(c)
            self.assertEqual(len(cset), len(self._components) - (i + 1))
        for c in self._components:
            self.assertTrue(c not in cset)
            cset.discard(c)

    def test_isdisjoint(self):
        cset1 = ComponentSet()
        cset2 = ComponentSet()
        self.assertTrue(cset1.isdisjoint(cset2))
        self.assertTrue(cset2.isdisjoint(cset1))
        v = variable()
        cset1.add(v)
        self.assertTrue(cset1.isdisjoint(cset2))
        self.assertTrue(cset2.isdisjoint(cset1))
        cset2.add(v)
        self.assertFalse(cset1.isdisjoint(cset2))
        self.assertFalse(cset2.isdisjoint(cset1))

    def test_misc_set_ops(self):
        v1 = variable()
        cset1 = ComponentSet([v1])
        v2 = variable()
        cset2 = ComponentSet([v2])
        cset3 = ComponentSet([v1, v2])
        empty = ComponentSet([])
        self.assertEqual(cset1 | cset2, cset3)
        self.assertEqual((cset1 | cset2) - cset3, empty)
        self.assertEqual(cset1 ^ cset2, cset3)
        self.assertEqual(cset1 ^ cset3, cset2)
        self.assertEqual(cset2 ^ cset3, cset1)
        self.assertEqual(cset1 & cset2, empty)
        self.assertEqual(cset1 & cset3, cset1)
        self.assertEqual(cset2 & cset3, cset2)

    def test_eq(self):
        cset1 = ComponentSet()
        self.assertEqual(cset1, set())
        self.assertTrue(cset1 == set())
        self.assertNotEqual(cset1, list())
        self.assertFalse(cset1 == list())
        self.assertNotEqual(cset1, tuple())
        self.assertFalse(cset1 == tuple())
        self.assertNotEqual(cset1, dict())
        self.assertFalse(cset1 == dict())

        cset1.update(self._components)
        self.assertNotEqual(cset1, set())
        self.assertFalse(cset1 == set())
        self.assertNotEqual(cset1, list())
        self.assertFalse(cset1 == list())
        self.assertNotEqual(cset1, tuple())
        self.assertFalse(cset1 == tuple())
        self.assertNotEqual(cset1, dict())
        self.assertFalse(cset1 == dict())

        self.assertTrue(cset1 == cset1)
        self.assertEqual(cset1, cset1)

        cset2 = ComponentSet(self._components)
        self.assertTrue(cset2 == cset1)
        self.assertFalse(cset2 != cset1)
        self.assertEqual(cset2, cset1)
        self.assertTrue(cset1 == cset2)
        self.assertFalse(cset1 != cset2)
        self.assertEqual(cset1, cset2)

        cset2.remove(self._components[0])
        self.assertFalse(cset2 == cset1)
        self.assertTrue(cset2 != cset1)
        self.assertNotEqual(cset2, cset1)
        self.assertFalse(cset1 == cset2)
        self.assertTrue(cset1 != cset2)
        self.assertNotEqual(cset1, cset2)
コード例 #26
0
 def test_ctype(self):
     s = suffix()
     self.assertIs(s.ctype, ISuffix)
     self.assertIs(type(s), suffix)
     self.assertIs(type(s)._ctype, ISuffix)
コード例 #27
0
 def test_str(self):
     s = suffix()
     self.assertEqual(str(s), "<suffix>")
     b = block()
     b.s = s
     self.assertEqual(str(s), "s")
コード例 #28
0
class Test_suffix_dict(_TestActiveDictContainerBase,
                       unittest.TestCase):
    _container_type = suffix_dict
    _ctype_factory = lambda self: suffix()
コード例 #29
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
    def test_active(self):

        s = suffix()
        with self.assertRaises(AttributeError):
            s.active = False

        model = block()
        model.s = s
        b = block()
        b.model = model
        bdict = block_dict()
        bdict[0] = b
        bdict[None] = block()
        m = block()
        m.bdict = bdict

        self.assertEqual(m.active, True)
        self.assertEqual(bdict.active, True)
        self.assertEqual(bdict[None].active, True)
        self.assertEqual(b.active, True)
        self.assertEqual(model.active, True)
        self.assertEqual(s.active, True)

        m.deactivate(shallow=False)

        self.assertEqual(m.active, False)
        self.assertEqual(bdict.active, False)
        self.assertEqual(bdict[None].active, False)
        self.assertEqual(b.active, False)
        self.assertEqual(model.active, False)
        self.assertEqual(s.active, False)

        m.activate(shallow=False)

        self.assertEqual(m.active, True)
        self.assertEqual(bdict.active, True)
        self.assertEqual(bdict[None].active, True)
        self.assertEqual(b.active, True)
        self.assertEqual(model.active, True)
        self.assertEqual(s.active, True)

        m.deactivate()

        self.assertEqual(m.active, False)
        self.assertEqual(bdict.active, True)
        self.assertEqual(bdict[None].active, True)
        self.assertEqual(b.active, True)
        self.assertEqual(model.active, True)
        self.assertEqual(s.active, True)

        m.deactivate(shallow=False)

        self.assertEqual(m.active, False)
        self.assertEqual(bdict.active, False)
        self.assertEqual(bdict[None].active, False)
        self.assertEqual(b.active, False)
        self.assertEqual(model.active, False)
        self.assertEqual(s.active, False)

        m.activate()

        self.assertEqual(m.active, True)
        self.assertEqual(bdict.active, False)
        self.assertEqual(bdict[None].active, False)
        self.assertEqual(b.active, False)
        self.assertEqual(model.active, False)
        self.assertEqual(s.active, False)

        m.activate(shallow=False)

        self.assertEqual(m.active, True)
        self.assertEqual(bdict.active, True)
        self.assertEqual(bdict[None].active, True)
        self.assertEqual(b.active, True)
        self.assertEqual(model.active, True)
        self.assertEqual(s.active, True)
コード例 #30
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
 def test_suffix_generator(self):
     m = block()
     m.s0 = suffix(direction=suffix.LOCAL)
     m.s0i = suffix(direction=suffix.LOCAL,
                   datatype=suffix.INT)
     m.s1 = suffix(direction=suffix.IMPORT_EXPORT)
     m.s1i = suffix(direction=suffix.IMPORT_EXPORT,
                    datatype=suffix.INT)
     m.s2 = suffix(direction=suffix.IMPORT)
     m.s2i = suffix(direction=suffix.IMPORT,
                   datatype=suffix.INT)
     m.s3 = suffix(direction=suffix.EXPORT)
     m.s3i = suffix(direction=suffix.EXPORT,
                    datatype=suffix.INT)
     m.b = block()
     m.b.s0 = suffix(direction=suffix.LOCAL)
     m.b.s0i = suffix(direction=suffix.LOCAL,
                      datatype=suffix.INT)
     m.b.s1 = suffix(direction=suffix.IMPORT_EXPORT)
     m.b.s1i = suffix(direction=suffix.IMPORT_EXPORT,
                      datatype=suffix.INT)
     m.b.s2 = suffix(direction=suffix.IMPORT)
     m.b.s2i = suffix(direction=suffix.IMPORT,
                      datatype=suffix.INT)
     m.b.s3 = suffix(direction=suffix.EXPORT)
     m.b.s3i = suffix(direction=suffix.EXPORT,
                      datatype=suffix.INT)
     # default
     self.assertEqual([id(c_) for c_
                       in suffix_generator(m)],
                      [id(m.s0), id(m.s0i),
                       id(m.s1), id(m.s1i),
                       id(m.s2), id(m.s2i),
                       id(m.s3), id(m.s3i),
                       id(m.b.s0), id(m.b.s0i),
                       id(m.b.s1), id(m.b.s1i),
                       id(m.b.s2), id(m.b.s2i),
                       id(m.b.s3), id(m.b.s3i)])
     # descend_into=False
     self.assertEqual([id(c_) for c_
                       in suffix_generator(m,
                                           descend_into=False)],
                      [id(m.s0), id(m.s0i),
                       id(m.s1), id(m.s1i),
                       id(m.s2), id(m.s2i),
                       id(m.s3), id(m.s3i)])
     # datatype=INT
     self.assertEqual([id(c_) for c_
                       in suffix_generator(m,
                                           datatype=suffix.INT)],
                      [id(m.s0i),
                       id(m.s1i),
                       id(m.s2i),
                       id(m.s3i),
                       id(m.b.s0i),
                       id(m.b.s1i),
                       id(m.b.s2i),
                       id(m.b.s3i)])
     # active=True
     m.s1.deactivate()
     m.b.deactivate()
     self.assertEqual([id(c_) for c_
                       in suffix_generator(m,
                                           active=True)],
                      [id(m.s0),
                       id(m.s0i),
                       id(m.s1i),
                       id(m.s2),
                       id(m.s2i),
                       id(m.s3),
                       id(m.s3i)])
コード例 #31
0
 def test_suffix_generator(self):
     m = block()
     m.s0 = suffix(direction=suffix.LOCAL)
     m.s0i = suffix(direction=suffix.LOCAL,
                   datatype=suffix.INT)
     m.s1 = suffix(direction=suffix.IMPORT_EXPORT)
     m.s1i = suffix(direction=suffix.IMPORT_EXPORT,
                    datatype=suffix.INT)
     m.s2 = suffix(direction=suffix.IMPORT)
     m.s2i = suffix(direction=suffix.IMPORT,
                   datatype=suffix.INT)
     m.s3 = suffix(direction=suffix.EXPORT)
     m.s3i = suffix(direction=suffix.EXPORT,
                    datatype=suffix.INT)
     m.b = block()
     m.b.s0 = suffix(direction=suffix.LOCAL)
     m.b.s0i = suffix(direction=suffix.LOCAL,
                      datatype=suffix.INT)
     m.b.s1 = suffix(direction=suffix.IMPORT_EXPORT)
     m.b.s1i = suffix(direction=suffix.IMPORT_EXPORT,
                      datatype=suffix.INT)
     m.b.s2 = suffix(direction=suffix.IMPORT)
     m.b.s2i = suffix(direction=suffix.IMPORT,
                      datatype=suffix.INT)
     m.b.s3 = suffix(direction=suffix.EXPORT)
     m.b.s3i = suffix(direction=suffix.EXPORT,
                      datatype=suffix.INT)
     # default
     self.assertEqual([id(c_) for c_
                       in suffix_generator(m)],
                      [id(m.s0), id(m.s0i),
                       id(m.s1), id(m.s1i),
                       id(m.s2), id(m.s2i),
                       id(m.s3), id(m.s3i),
                       id(m.b.s0), id(m.b.s0i),
                       id(m.b.s1), id(m.b.s1i),
                       id(m.b.s2), id(m.b.s2i),
                       id(m.b.s3), id(m.b.s3i)])
     # descend_into=False
     self.assertEqual([id(c_) for c_
                       in suffix_generator(m,
                                           descend_into=False)],
                      [id(m.s0), id(m.s0i),
                       id(m.s1), id(m.s1i),
                       id(m.s2), id(m.s2i),
                       id(m.s3), id(m.s3i)])
     # datatype=INT
     self.assertEqual([id(c_) for c_
                       in suffix_generator(m,
                                           datatype=suffix.INT)],
                      [id(m.s0i),
                       id(m.s1i),
                       id(m.s2i),
                       id(m.s3i),
                       id(m.b.s0i),
                       id(m.b.s1i),
                       id(m.b.s2i),
                       id(m.b.s3i)])
     # active=True
     m.s1.deactivate()
     m.b.deactivate()
     self.assertEqual([id(c_) for c_
                       in suffix_generator(m,
                                           active=True)],
                      [id(m.s0),
                       id(m.s0i),
                       id(m.s1i),
                       id(m.s2),
                       id(m.s2i),
                       id(m.s3),
                       id(m.s3i)])
コード例 #32
0
    def test_active(self):

        s = suffix()
        with self.assertRaises(AttributeError):
            s.active = False

        model = block()
        model.s = s
        b = block()
        b.model = model
        bdict = block_dict()
        bdict[0] = b
        bdict[None] = block()
        m = block()
        m.bdict = bdict

        self.assertEqual(m.active, True)
        self.assertEqual(bdict.active, True)
        self.assertEqual(bdict[None].active, True)
        self.assertEqual(b.active, True)
        self.assertEqual(model.active, True)
        self.assertEqual(s.active, True)

        m.deactivate(shallow=False)

        self.assertEqual(m.active, False)
        self.assertEqual(bdict.active, False)
        self.assertEqual(bdict[None].active, False)
        self.assertEqual(b.active, False)
        self.assertEqual(model.active, False)
        self.assertEqual(s.active, False)

        m.activate(shallow=False)

        self.assertEqual(m.active, True)
        self.assertEqual(bdict.active, True)
        self.assertEqual(bdict[None].active, True)
        self.assertEqual(b.active, True)
        self.assertEqual(model.active, True)
        self.assertEqual(s.active, True)

        m.deactivate()

        self.assertEqual(m.active, False)
        self.assertEqual(bdict.active, True)
        self.assertEqual(bdict[None].active, True)
        self.assertEqual(b.active, True)
        self.assertEqual(model.active, True)
        self.assertEqual(s.active, True)

        m.deactivate(shallow=False)

        self.assertEqual(m.active, False)
        self.assertEqual(bdict.active, False)
        self.assertEqual(bdict[None].active, False)
        self.assertEqual(b.active, False)
        self.assertEqual(model.active, False)
        self.assertEqual(s.active, False)

        m.activate()

        self.assertEqual(m.active, True)
        self.assertEqual(bdict.active, False)
        self.assertEqual(bdict[None].active, False)
        self.assertEqual(b.active, False)
        self.assertEqual(model.active, False)
        self.assertEqual(s.active, False)

        m.activate(shallow=False)

        self.assertEqual(m.active, True)
        self.assertEqual(bdict.active, True)
        self.assertEqual(bdict[None].active, True)
        self.assertEqual(b.active, True)
        self.assertEqual(model.active, True)
        self.assertEqual(s.active, True)
コード例 #33
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
 def test_str(self):
     s = suffix()
     self.assertEqual(str(s), "<suffix>")
     b = block()
     b.s = s
     self.assertEqual(str(s), "s")
コード例 #34
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
 def test_ctype(self):
     s = suffix()
     self.assertIs(s.ctype, ISuffix)
     self.assertIs(type(s), suffix)
     self.assertIs(type(s)._ctype, ISuffix)
コード例 #35
0
ファイル: test_suffix.py プロジェクト: Pyomo/pyomo
 def test_init(self):
     s = suffix()
     self.assertTrue(s.parent is None)
     self.assertEqual(s.ctype, ISuffix)
     self.assertEqual(s.direction, suffix.LOCAL)
     self.assertEqual(s.datatype, suffix.FLOAT)
コード例 #36
0
ファイル: test_component_map.py プロジェクト: CanLi1/pyomo-1
class TestComponentMap(unittest.TestCase):

    _components = [(variable(), "v"),
                   (variable_dict(), "vdict"),
                   (variable_list(), "vlist"),
                   (constraint(), "c"),
                   (constraint_dict(), "cdict"),
                   (constraint_list(), "clist"),
                   (objective(), "o"),
                   (objective_dict(), "odict"),
                   (objective_list(), "olist"),
                   (expression(), "e"),
                   (expression_dict(), "edict"),
                   (expression_list(), "elist"),
                   (block(), "b"),
                   (block_dict(), "bdict"),
                   (block_list(), "blist"),
                   (suffix(), "s")]

    def test_pickle(self):
        c = ComponentMap()
        self.assertEqual(len(c), 0)
        cup = pickle.loads(
            pickle.dumps(c))
        self.assertIsNot(cup, c)
        self.assertEqual(len(cup), 0)

        v = variable()
        c[v] = 1.0
        self.assertEqual(len(c), 1)
        self.assertEqual(c[v], 1.0)
        cup = pickle.loads(
            pickle.dumps(c))
        vup = list(cup.keys())[0]
        self.assertIsNot(cup, c)
        self.assertIsNot(vup, v)
        self.assertEqual(len(cup), 1)
        self.assertEqual(cup[vup], 1)
        self.assertEqual(vup.parent, None)

        b = block()
        V = b.V = variable_list()
        b.V.append(v)
        b.c = c
        self.assertEqual(len(c), 1)
        self.assertEqual(c[v], 1.0)
        self.assertIs(v.parent, b.V)
        self.assertIs(V.parent, b)
        self.assertIs(b.parent, None)
        bup = pickle.loads(
            pickle.dumps(b))
        Vup = bup.V
        vup = Vup[0]
        cup = bup.c
        self.assertIsNot(cup, c)
        self.assertIsNot(vup, v)
        self.assertIsNot(Vup, V)
        self.assertIsNot(bup, b)
        self.assertEqual(len(cup), 1)
        self.assertEqual(cup[vup], 1)
        self.assertIs(vup.parent, Vup)
        self.assertIs(Vup.parent, bup)
        self.assertIs(bup.parent, None)

        self.assertEqual(len(c), 1)
        self.assertEqual(c[v], 1)

    def test_init(self):
        cmap = ComponentMap()
        cmap = ComponentMap(self._components)
        with self.assertRaises(TypeError):
            cmap = ComponentMap(*self._components)

    def test_type(self):
        cmap = ComponentMap()
        self.assertTrue(isinstance(cmap, collections_Mapping))
        self.assertTrue(isinstance(cmap, collections_MutableMapping))
        self.assertTrue(issubclass(type(cmap), collections_Mapping))
        self.assertTrue(issubclass(type(cmap), collections_MutableMapping))

    def test_str(self):
        cmap = ComponentMap()
        self.assertEqual(str(cmap), "ComponentMap({})")
        cmap.update(self._components)
        str(cmap)

    def test_len(self):
        cmap = ComponentMap()
        self.assertEqual(len(cmap), 0)
        cmap.update(self._components)
        self.assertEqual(len(cmap), len(self._components))
        cmap = ComponentMap(self._components)
        self.assertEqual(len(cmap), len(self._components))
        self.assertTrue(len(self._components) > 0)

    def test_getsetdelitem(self):
        cmap = ComponentMap()
        for c, val in self._components:
            self.assertTrue(c not in cmap)
        for c, val in self._components:
            cmap[c] = val
            self.assertEqual(cmap[c], val)
            self.assertEqual(cmap.get(c), val)
            del cmap[c]
            with self.assertRaises(KeyError):
                cmap[c]
            with self.assertRaises(KeyError):
                del cmap[c]
            self.assertEqual(cmap.get(c), None)

    def test_iter(self):
        cmap = ComponentMap()
        self.assertEqual(list(iter(cmap)), [])
        cmap.update(self._components)
        ids_seen = set()
        for c in cmap:
            ids_seen.add(id(c))
        self.assertEqual(ids_seen,
                         set(id(c) for c,val in self._components))

    def test_keys(self):
        cmap = ComponentMap(self._components)
        self.assertEqual(sorted(cmap.keys(), key=id),
                         sorted(list(c for c,val in self._components),
                                key=id))

    def test_values(self):
        cmap = ComponentMap(self._components)
        self.assertEqual(sorted(cmap.values()),
                         sorted(list(val for c,val in self._components)))

    def test_items(self):
        cmap = ComponentMap(self._components)
        for x in cmap.items():
            self.assertEqual(type(x), tuple)
            self.assertEqual(len(x), 2)
        self.assertEqual(sorted(cmap.items(),
                                key=lambda _x: (id(_x[0]), _x[1])),
                         sorted(self._components,
                                key=lambda _x: (id(_x[0]), _x[1])))

    def test_update(self):
        cmap = ComponentMap()
        self.assertEqual(len(cmap), 0)
        cmap.update(self._components)
        self.assertEqual(len(cmap), len(self._components))
        for c, val in self._components:
            self.assertEqual(cmap[c], val)

    def test_clear(self):
        cmap = ComponentMap()
        self.assertEqual(len(cmap), 0)
        cmap.update(self._components)
        self.assertEqual(len(cmap), len(self._components))
        cmap.clear()
        self.assertEqual(len(cmap), 0)

    def test_setdefault(self):
        cmap = ComponentMap()
        for c,_ in self._components:
            with self.assertRaises(KeyError):
                cmap[c]
            self.assertTrue(c not in cmap)
            cmap.setdefault(c, []).append(1)
            self.assertEqual(cmap[c], [1])
            del cmap[c]
            with self.assertRaises(KeyError):
                cmap[c]
            self.assertTrue(c not in cmap)
            cmap[c] = []
            cmap.setdefault(c, []).append(1)
            self.assertEqual(cmap[c], [1])

    def test_eq(self):
        cmap1 = ComponentMap()
        self.assertNotEqual(cmap1, set())
        self.assertFalse(cmap1 == set())
        self.assertNotEqual(cmap1, list())
        self.assertFalse(cmap1 == list())
        self.assertNotEqual(cmap1, tuple())
        self.assertFalse(cmap1 == tuple())
        self.assertEqual(cmap1, dict())
        self.assertTrue(cmap1 == dict())

        cmap1.update(self._components)
        self.assertNotEqual(cmap1, set())
        self.assertFalse(cmap1 == set())
        self.assertNotEqual(cmap1, list())
        self.assertFalse(cmap1 == list())
        self.assertNotEqual(cmap1, tuple())
        self.assertFalse(cmap1 == tuple())
        self.assertNotEqual(cmap1, dict())
        self.assertFalse(cmap1 == dict())

        self.assertTrue(cmap1 == cmap1)
        self.assertEqual(cmap1, cmap1)

        cmap2 = ComponentMap(self._components)
        self.assertTrue(cmap2 == cmap1)
        self.assertFalse(cmap2 != cmap1)
        self.assertEqual(cmap2, cmap1)
        self.assertTrue(cmap1 == cmap2)
        self.assertFalse(cmap1 != cmap2)
        self.assertEqual(cmap1, cmap2)

        del cmap2[self._components[0][0]]
        self.assertFalse(cmap2 == cmap1)
        self.assertTrue(cmap2 != cmap1)
        self.assertNotEqual(cmap2, cmap1)
        self.assertFalse(cmap1 == cmap2)
        self.assertTrue(cmap1 != cmap2)
        self.assertNotEqual(cmap1, cmap2)
コード例 #37
0
 def test_init(self):
     s = suffix()
     self.assertTrue(s.parent is None)
     self.assertEqual(s.ctype, ISuffix)
     self.assertEqual(s.direction, suffix.LOCAL)
     self.assertEqual(s.datatype, suffix.FLOAT)