Example #1
0
def check_products():
    F1 = R_Weight
    F2 = R_Time
    F3 = R_Energy
    M, pack, unpack = get_product_compact(F1, F2, F3)

    print(M)
    s = pack(F1.get_top(), F2.get_bottom(), F3.get_top())
    print(s)
    u = unpack(s)
    assert_equal(u, s)

    F1 = R_Time
    F2 = PosetProduct(())
    F3 = R_Energy
    F = PosetProduct((F1, F2, F3))
    print('F: %s' % F)
    M, pack, unpack = get_product_compact(F1, F2, F3)

    print('M: %s' % M)
    element = (F1.get_top(), F2.get_bottom(), F3.get_top())
    print('elements: %s' % F.format(element))
    s = pack(*element)
    print('packed: %s' % str(s))
    u = unpack(s)
    print('depacked: %s' % str(u))
    assert_equal(u, element)
Example #2
0
def check_products():
    F1 = R_Weight
    F2 = R_Time
    F3 = R_Energy
    M, pack, unpack = get_product_compact(F1, F2, F3)

    print(M)
    s = pack(F1.get_top(), F2.get_bottom(), F3.get_top())
    print(s)
    u = unpack(s)
    assert_equal(u, s)

    F1 = R_Time
    F2 = PosetProduct(())
    F3 = R_Energy
    F = PosetProduct((F1, F2, F3))
    print('F: %s' % F)
    M, pack, unpack = get_product_compact(F1, F2, F3)

    print('M: %s' % M)
    element = (F1.get_top(), F2.get_bottom(), F3.get_top())
    print('elements: %s' % F.format(element))
    s = pack(*element)
    print('packed: %s' % str(s))
    u = unpack(s)
    print('depacked: %s' % str(u))
    assert_equal(u, element)
Example #3
0
 def _unpack_m(self, m):
     if do_extra_checks():
         self.M.belongs(m)
     from mcdp_dp.dp_series import get_product_compact
     _, _, unpack = get_product_compact(self.M0, self.F2, self.R2)
     m0, f2, r2 = unpack(m)
     return m0, f2, r2
Example #4
0
 def _unpack_m(self, m):
     if do_extra_checks():
         self.M.belongs(m)
     from mcdp_dp.dp_series import get_product_compact
     _, _, unpack = get_product_compact(self.M0, self.F2, self.R2)
     m0, f2, r2 = unpack(m)
     return m0, f2, r2
Example #5
0
def check_products2():
    print('------')
    F1 = PosetProduct(())
    M, pack, unpack = get_product_compact(F1)

    print('M: %s' % M)
    element = ()
    F1.belongs(element)
    print('elements: %s' % F1.format(element))

    s = pack(*(element, ))
    print('packed: %s' % str(s))
    u, = unpack(s)
    print('depacked: %s' % str(u))
    assert_equal(u, element)
Example #6
0
def check_products2():
    print('------')
    F1 = PosetProduct(())
    M, pack, unpack = get_product_compact(F1)

    print('M: %s' % M)
    element = ()
    F1.belongs(element)
    print('elements: %s' % F1.format(element))
    
    s = pack(*(element,))
    print('packed: %s' % str(s))
    u,  = unpack(s)
    print('depacked: %s' % str(u))
    assert_equal(u, element)
Example #7
0
    def check_product(S1, S2, expected):
        S, pack, unpack = get_product_compact(S1, S2)
        print('product(%s, %s) = %s  expected %s' % (S1, S2, S, expected))
        assert_equal(S, expected)

        a = S1.witness()
        b = S2.witness()
        S1.belongs(a)
        S2.belongs(b)
        c = pack(a, b)
        a2, b2 = unpack(c)
        S1.check_equal(a, a2)
        S2.check_equal(b, b2)

        print('a = %s  b = %s' % (a, b))
        print('c = %s ' % S.format(c))
        print('a2 = %s  b2 = %s' % (a2, b2))
Example #8
0
    def check_product(S1, S2, expected):
        S, pack, unpack = get_product_compact(S1, S2)
        print('product(%s, %s) = %s  expected %s' % (S1, S2, S, expected))
        assert_equal(S, expected)

        a = S1.witness()
        b = S2.witness()
        S1.belongs(a)
        S2.belongs(b)
        c = pack(a, b)
        a2, b2 = unpack(c)
        S1.check_equal(a, a2)
        S2.check_equal(b, b2)

        print('a = %s  b = %s' % (a, b))
        print('c = %s ' % S.format(c))
        print('a2 = %s  b2 = %s' % (a2, b2))
Example #9
0
    def get_implementations_f_r(self, f1, r1):
        from mcdp_posets.category_product import get_product_compact
        M, M_pack, _ = get_product_compact(self.M0, self.F2, self.R2)
        options = set()

        R = self.solve_all_cached(f1, Tracer())
        res = R['res_all']

        for (r1_, r2_) in res.minimals:
            if self.R1.leq(r1_, r1):
                f2 = r2_
                m0s = self.dp1.get_implementations_f_r((f1, f2), (r1_, r2_))
                for m0 in m0s:
                    m = M_pack(m0, f2, r2_)
                    options.add(m)

        if do_extra_checks():
            for _ in options:
                M.belongs(_)

        return options
Example #10
0
    def get_implementations_f_r(self, f1, r1):
        from mcdp_posets.category_product import get_product_compact
        M, M_pack, _ = get_product_compact(self.M0, self.F2, self.R2)
        options = set()

        R = self.solve_all_cached(f1, Tracer())
        res = R['res_all']

        for (r1_, r2_) in res.minimals:
            if self.R1.leq(r1_, r1):
                f2 = r2_
                m0s = self.dp1.get_implementations_f_r((f1, f2), (r1_, r2_))
                for m0 in m0s:
                    m = M_pack(m0, f2, r2_)
                    options.add(m)
            
        if do_extra_checks():
            for _ in options:
                M.belongs(_)

        return options
Example #11
0
    def __init__(self, dp1):
        self.dp1 = dp1

        F0 = self.dp1.get_fun_space()
        R0 = self.dp1.get_res_space()
        I0 = self.dp1.get_imp_space()

        if not isinstance(F0, PosetProduct) or len(F0.subs) != 2:
            msg = 'The function space must be a product of length 2.'
            raise_desc(ValueError, msg, F0=F0)

        if not isinstance(R0, PosetProduct) or len(R0.subs) != 2:
            msg = 'The resource space must be a product of length 2.'
            raise_desc(ValueError, msg, R0=R0)

        F1, F2 = F0[0], F0[1]
        R1, R2 = R0[0], R0[1]

        tu = get_types_universe()

        try:
            tu.check_equal(F2, R2)
        except NotEqual as e:
            msg = ('The second component of function and resource space '
                   'must be equal.')
            raise_wrapped(ValueError, e, msg, F2=F2, R2=R2)

        F = F1
        R = R1

        from mcdp_dp.dp_series import get_product_compact
        M, _, _ = get_product_compact(I0, F2, R2)
        self.M0 = I0
        self.F1 = F1
        self.F2 = F2
        self.R1 = R1
        self.R2 = R2

        self._solve_cache = {}
        PrimitiveDP.__init__(self, F=F, R=R, I=M)
Example #12
0
    def __init__(self, dp1):
        self.dp1 = dp1

        F0 = self.dp1.get_fun_space()
        R0 = self.dp1.get_res_space()
        I0 = self.dp1.get_imp_space()

        if not isinstance(F0, PosetProduct) or len(F0.subs) != 2:
            msg = 'The function space must be a product of length 2.'
            raise_desc(ValueError, msg, F0=F0)

        if not isinstance(R0, PosetProduct) or len(R0.subs) != 2:
            msg = 'The resource space must be a product of length 2.'
            raise_desc(ValueError, msg, R0=R0)
            
        F1, F2 = F0[0], F0[1]
        R1, R2 = R0[0], R0[1]
        
        tu = get_types_universe()
        
        try:
            tu.check_equal(F2, R2)
        except NotEqual as e:
            msg = ('The second component of function and resource space '
                   'must be equal.')
            raise_wrapped(ValueError, e, msg, F2=F2, R2=R2)

        F = F1
        R = R1

        from mcdp_dp.dp_series import get_product_compact
        M, _, _ = get_product_compact(I0, F2, R2)
        self.M0 = I0
        self.F1 = F1
        self.F2 = F2
        self.R1 = R1
        self.R2 = R2

        self._solve_cache = {}
        PrimitiveDP.__init__(self, F=F, R=R, I=M)
Example #13
0
    def evaluate(self, m):
        from mcdp_posets.category_product import get_product_compact
        _, _, unpack = get_product_compact(self.M0, self.F2, self.R2)
        m0, _f2, _r2 = unpack(m)

        LF0, UR0 = self.dp1.evaluate(m0)
        assert isinstance(LF0, LowerSet), (LF0, self.dp1)
        assert isinstance(UR0, UpperSet), (UR0, self.dp1)

        # now extract first components f1 and r1
        f1s = set()
        for fi in LF0.maximals:
            fi1, _ = fi
            f1s.add(fi1)
        f1s = poset_maxima(f1s, self.F.leq)
        LF = self.F.Ls(f1s)
        r1s = set()
        for ri in UR0.minimals:
            ri1, _ = ri
            r1s.add(ri1)
        r1s = poset_minima(r1s, self.F.leq)
        UR = self.R.Us(r1s)
        return LF, UR
Example #14
0
    def evaluate(self, m):
        from mcdp_posets.category_product import get_product_compact
        _, _, unpack = get_product_compact(self.M0, self.F2, self.R2)
        m0, _f2, _r2 = unpack(m)

        LF0, UR0 = self.dp1.evaluate(m0)
        assert isinstance(LF0, LowerSet), (LF0, self.dp1)
        assert isinstance(UR0, UpperSet), (UR0, self.dp1)

        # now extract first components f1 and r1
        f1s = set()
        for fi in LF0.maximals:
            fi1, _ = fi
            f1s.add(fi1)
        f1s = poset_maxima(f1s, self.F.leq)
        LF = self.F.Ls(f1s)
        r1s = set()
        for ri in UR0.minimals:
            ri1, _ = ri
            r1s.add(ri1)
        r1s = poset_minima(r1s, self.F.leq)
        UR = self.R.Us(r1s)
        return LF, UR