Example #1
0
def get_common(ua, ub):
    Pa = ua.P
    Pb = ub.P

    if not isinstance(Pa, PosetProduct) or not isinstance(Pb, PosetProduct):
        raise NotImplementedError((Pa, Pb))

    # first, it might be that they have different spaces

    # let's find out how to match them

    tu = get_types_universe()
    # for each i in Pa, we will match it to the first
    matches1 = []
    for i, P in enumerate(Pa.subs):
        for j, Q in enumerate(Pb.subs):
            if ('B', j) in matches1: continue
            if tu.leq(P, Q):
                matches1.append(('B', j))
                break
        else:
            matches1.append(('A', i))
    matches2 = []
    for j, Q in enumerate(Pb.subs):
        if ('B', j) in matches1:
            # used by somebody
            matches2.append(('B', j))
        else:
            for i, P in enumerate(Pa.subs):
                if matches1[i] is not None: continue
                if tu.leq(Q, P):
                    matches2.append(('A', i))
                    break
            else:
                matches2.append(('B', j))

    print('matches1:  %s' % matches1)
    print('matches2: %s' % matches2)

    used = sorted(set(matches1 + matches2))

    def get_P(_):
        (which, index) = _
        if which == 'A': return Pa.subs[index]
        if which == 'B': return Pb.subs[index]
        assert False

    Ps = PosetProduct(tuple(map(get_P, used)))
    print('used: %s' % used)
    print('Ps: %s' % Ps)

    # now we need to complete the first
    Ps_a = get(matches1, used, Ps, get_P, Pa, ua)
    Ps_b = get(matches2, used, Ps, get_P, Pb, ub)

    print('Ps_a: %s' % Ps_a)
    print('Ps_b: %s' % Ps_b)

    S = UpperSets(Ps)
    return S, Ps_a, Ps_b
def less_resources2(ua, ub):
    """
    
        ua must be <= ub
    """
    Pa = ua.P
    Pb = ub.P

    if not isinstance(Pa, PosetProduct) or not isinstance(Pb, PosetProduct):
        raise NotImplementedError((Pa, Pb))

    tu = get_types_universe()

    matches = []
    for i, P in enumerate(Pa.subs):
        for j, Q in enumerate(Pb.subs):
            if j in matches: continue
            if tu.leq(P, Q):
                matches.append(j)
                break
        else:
            # msg = 'Could not find match.'
            return False

    # now we have found an embedding

    # first we create a projection for Pb
    m1 = MuxMap(F=Pb, coords=matches)

    ub2 = upperset_project_map(ub, m1)
    Pb2 = ub2.P
    UPb2 = UpperSets(Pb2)

    # now we create the embedding
    A_to_B, _ = tu.get_embedding(Pa, Pb2)
    ua2 = upperset_project_map(ua, A_to_B)

    print('Pa: %s' % Pa)
    print('Pb2:  %s' % Pb2)
    print('ua2: %s' % ua2)
    print('ub2: %s' % ub2)

    return UPb2.leq(ua2, ub2)
Example #3
0
def less_resources2(ua, ub):
    """
    
        ua must be <= ub
    """
    Pa = ua.P
    Pb = ub.P

    if not isinstance(Pa, PosetProduct) or not isinstance(Pb, PosetProduct):
        raise NotImplementedError((Pa, Pb))

    tu = get_types_universe()

    matches = []
    for i, P in enumerate(Pa.subs):
        for j, Q in enumerate(Pb.subs):
            if j in matches: continue
            if tu.leq(P, Q):
                matches.append(j)
                break
        else:
            # msg = 'Could not find match.'
            return False

    # now we have found an embedding

    # first we create a projection for Pb
    m1 = MuxMap(F=Pb, coords=matches)

    ub2 = upperset_project_map(ub, m1)
    Pb2 = ub2.P
    UPb2 = UpperSets(Pb2)

    # now we create the embedding
    A_to_B, _ = tu.get_embedding(Pa, Pb2)
    ua2 = upperset_project_map(ua, A_to_B)

    print('Pa: %s' % Pa)
    print('Pb2:  %s' % Pb2)
    print('ua2: %s' % ua2)
    print('ub2: %s' % ub2)

    return UPb2.leq(ua2, ub2)
Example #4
0
def invmult2_check1():
    
    F = parse_poset('m')
    R1 = parse_poset('m/s')
    R2 = parse_poset('s')
    
    im = InvMult2(F, (R1, R2))
    assert isinstance(im, ApproximableDP)
    n = 4
    iml = im.get_lower_bound(n)
    imu = im.get_upper_bound(n)

    UR = UpperSets(im.get_res_space())

    for i in [1.0, 5.0, 10.0]:
        rl = iml.solve(i)
        ru = imu.solve(i)
        print UR.format(rl)
        print UR.format(ru)
        UR.check_leq(rl, ru)
Example #5
0
def go1(r, ns, dp, plot_nominal, axis):

    f = r.figure(cols=len(ns))

    for n in ns:
        dpL, dpU = get_dp_bounds(dp, n, n)

        f0 = 1.0
        R = dp.get_res_space()
        UR = UpperSets(R)
        space = PosetProduct((UR, UR))

        urL = dpL.solve(f0)
        urU = dpU.solve(f0)
        value = urL, urU

        plotter = get_best_plotter(space)
        figsize = (4, 4)
        with f.plot('plot_n%d' % n, figsize=figsize) as pylab:
            ieee_spines_zoom3(pylab)
            plotter.plot(pylab, axis, space, value)
            plot_nominal(pylab)
            pylab.axis(axis)
Example #6
0
def invmult2_check1():

    F = parse_poset('m')
    R1 = parse_poset('m/s')
    R2 = parse_poset('s')

    im = InvMult2(F, (R1, R2))
    assert isinstance(im, ApproximableDP)
    n = 4
    iml = im.get_lower_bound(n)
    imu = im.get_upper_bound(n)

    UR = UpperSets(im.get_res_space())

    for i in [1.0, 5.0, 10.0]:
        rl = iml.solve(i)
        ru = imu.solve(i)
        print UR.format(rl)
        print UR.format(ru)
        UR.check_leq(rl, ru)
Example #7
0
def invmult2_check2():
    
    F = parse_poset('m')
    R1 = parse_poset('m')
    R2 = parse_poset('m')
    
    im = InvPlus2(F, (R1, R2))

    R = im.get_res_space()
    UR = UpperSets(R)

    ns = [1, 2, 3, 4, 10, 15]
    resL = []
    resU = []
    f0 = 1.0
    for n in ns:
        dpU = im.get_upper_bound(n)
        dpL = im.get_lower_bound(n)
        urL = dpL.solve(f0)
        urU = dpU.solve(f0)
        UR.belongs(urL)
        UR.belongs(urU)
        resL.append(urL)
        resU.append(urU)

    r = Report()
    f = r.figure()
    for n, ur in zip(ns, resL):
        with f.plot('resL-%d' % n) as pylab:
            for n0, ur0 in zip(ns, resL):
                if n0 == n: continue
                points = np.array(list(ur0.minimals))
                pylab.plot(points[:, 0], points[:, 1], 'kx')

            points = np.array(list(ur.minimals))
            pylab.plot(points[:, 0], points[:, 1], 'o')
            pylab.axis((-0.1, 1.1, -0.1, 1.1))

    f = r.figure()
    for n, ur in zip(ns, resU):
        with f.plot('resU-%d' % n) as pylab:
            for n0, ur0 in zip(ns, resU):
                if n0 == n: continue
                points = np.array(list(ur0.minimals))
                pylab.plot(points[:, 0], points[:, 1], 'kx')

            points = np.array(list(ur.minimals))
            pylab.plot(points[:, 0], points[:, 1], 'o')
            pylab.axis((-0.1, 1.1, -0.1, 1.1))

    fn = 'out/invmult2_check2.html'
    print('writing to %s' % fn)
    r.to_html(fn)

    for urU in resU:
        for x, y in urU.minimals:
            assert x + y >= f0, (x, y, f0)
    for urL in resL:
        for x, y in urL.minimals:
            assert x + y <= f0, (x, y, f0)


    # check resU is DECREASING
    for i in range(len(resU) - 1):
        ur0 = resU[i]
        ur1 = resU[i + 1]
        try:
            UR.check_leq(ur1, ur0)
        except NotLeq:
            print('ur[%s]: %s ' % (i, UR.format(ur0)))
            print('ur[%s]: %s ' % (i + 1, UR.format(ur1)))
            raise Exception()

    # check resL is INCREASING
    for i in range(len(resU) - 1):
        ur0 = resL[i]
        ur1 = resL[i + 1]
        try:
            UR.check_leq(ur0, ur1)
        except NotLeq:
            print 'resL is not INCREASING'
            print('ur[%s]: %s x' % (i, UR.format(ur0)))
            print('ur[%s]: %s x ' % (i + 1, UR.format(ur1)))
            raise
            raise Exception('resL is not INCREASING')

    for ur0, ur1 in zip(resL, resU):
        UR.check_leq(ur0, ur1)
Example #8
0
def invmult2_check3():

    F = parse_poset('dimensionless')
    R1 = parse_poset('dimensionless')
    R2 = parse_poset('dimensionless')

    im = InvMult2(F, (R1, R2))

    InvMult2.ALGO = InvMult2.ALGO_VAN_DER_CORPUT

    R = im.get_res_space()
    UR = UpperSets(R)

#     ns = [1, 2, 3, 4, 10, 15]
#     ns = [1, 5, 10, 15, 25, 50, 61, 100]
    ns = [1, 2, 3, 4, 5, 10]
    resL = []
    resU = []
    f0 = 1.0
    for n in ns:
        dpU = im.get_upper_bound(n)
        dpL = im.get_lower_bound(n)
        urL = dpL.solve(f0)
        print urL
        print '%r' % urL.minimals
        check_minimal(urL.minimals, R)
        urU = dpU.solve(f0)
        check_minimal(urU.minimals, R)
        UR.belongs(urL)
        UR.belongs(urU)
        resL.append(urL)
        resU.append(urU)

    def plot_upper(pylab, ur, markers):
        points = np.array(list(ur.minimals))
        eps = np.finfo(float).eps
        points = np.maximum(points, eps)
        points = np.minimum(points, 20)

        pylab.plot(points[:, 0], points[:, 1], markers)

    r = Report()
    f = r.figure()
    for n, ur in zip(ns, resL):
        caption = str(ur)
        with f.plot('resL-%d' % n, caption=caption) as pylab:
            for n0, ur0 in zip(ns, resL):
                if n0 == n: continue
                plot_upper(pylab, ur0, 'kx')

            plot_upper(pylab, ur, 'o')

            pylab.axis((-0.1, 10.1, -0.1, 10.1))

    f = r.figure()
    for n, ur in zip(ns, resU):
        with f.plot('resU-%d' % n) as pylab:
            for n0, ur0 in zip(ns, resU):
                if n0 == n: continue
                plot_upper(pylab, ur0, 'kx')

            plot_upper(pylab, ur, 'o')
            pylab.axis((-0.1, 10.1, -0.1, 10.1))

    fn = 'out/invmult2_check3.html'
    print('writing to %s' % fn)
    r.to_html(fn)

    for urU in resU:
        for x, y in urU.minimals:
            prod = x * y
            if prod > f0:
                continue
            else:
                assert_allclose(x * y, f0)  # , (x, y, f0, x * y)
    for urL in resL:
        for x, y in urL.minimals:
            x = float(x)
            y = float(y)
            assert x * y <= f0, (x, y, f0, x * y)

    # check resU is DECREASING
    for i in range(len(resU) - 1):
        ur0 = resU[i]
        ur1 = resU[i + 1]
        try:
            UR.check_leq(ur1, ur0)
        except NotLeq:
            print('ur[%s]: %s ' % (i, UR.format(ur0)))
            print('ur[%s]: %s ' % (i + 1, UR.format(ur1)))
            raise Exception('resU is not DECREASING')

    # check resL is INCREASING
    for i in range(len(resU) - 1):
        ur0 = resL[i]
        ur1 = resL[i + 1]
        try:
            UR.check_leq(ur0, ur1)
        except NotLeq:
            print 'resL is not INCREASING'
            print('ur[%s]: %s x' % (i, UR.format(ur0)))
            print('ur[%s]: %s x ' % (i + 1, UR.format(ur1)))
            raise
            raise Exception('resL is not INCREASING')

    for ur0, ur1 in zip(resL, resU):
        UR.check_leq(ur0, ur1)
Example #9
0
 def __init__(self, f):
     from mcdp_posets.uppersets import UpperSets
     dom = UpperSets(f.get_domain())
     cod = UpperSets(f.get_codomain())
     self.f = f
     Map.__init__(self, dom=dom, cod=cod)
Example #10
0
    def repr_hd_map(self):
        UR = UpperSets(self.R)

        return 'r ⟼ {⟨⟩} if r ∈ %s, else ø' % UR.format(self.ur)
Example #11
0
def invmult2_check2():

    F = parse_poset('m')
    R1 = parse_poset('m')
    R2 = parse_poset('m')

    im = InvPlus2(F, (R1, R2))

    R = im.get_res_space()
    UR = UpperSets(R)

    ns = [1, 2, 3, 4, 10, 15]
    resL = []
    resU = []
    f0 = 1.0
    for n in ns:
        dpU = im.get_upper_bound(n)
        dpL = im.get_lower_bound(n)
        urL = dpL.solve(f0)
        urU = dpU.solve(f0)
        UR.belongs(urL)
        UR.belongs(urU)
        resL.append(urL)
        resU.append(urU)

    r = Report()
    f = r.figure()
    for n, ur in zip(ns, resL):
        with f.plot('resL-%d' % n) as pylab:
            for n0, ur0 in zip(ns, resL):
                if n0 == n: continue
                points = np.array(list(ur0.minimals))
                pylab.plot(points[:, 0], points[:, 1], 'kx')

            points = np.array(list(ur.minimals))
            pylab.plot(points[:, 0], points[:, 1], 'o')
            pylab.axis((-0.1, 1.1, -0.1, 1.1))

    f = r.figure()
    for n, ur in zip(ns, resU):
        with f.plot('resU-%d' % n) as pylab:
            for n0, ur0 in zip(ns, resU):
                if n0 == n: continue
                points = np.array(list(ur0.minimals))
                pylab.plot(points[:, 0], points[:, 1], 'kx')

            points = np.array(list(ur.minimals))
            pylab.plot(points[:, 0], points[:, 1], 'o')
            pylab.axis((-0.1, 1.1, -0.1, 1.1))

    fn = 'out/invmult2_check2.html'
    print('writing to %s' % fn)
    r.to_html(fn)

    for urU in resU:
        for x, y in urU.minimals:
            assert x + y >= f0, (x, y, f0)
    for urL in resL:
        for x, y in urL.minimals:
            assert x + y <= f0, (x, y, f0)

    # check resU is DECREASING
    for i in range(len(resU) - 1):
        ur0 = resU[i]
        ur1 = resU[i + 1]
        try:
            UR.check_leq(ur1, ur0)
        except NotLeq:
            print('ur[%s]: %s ' % (i, UR.format(ur0)))
            print('ur[%s]: %s ' % (i + 1, UR.format(ur1)))
            raise Exception()

    # check resL is INCREASING
    for i in range(len(resU) - 1):
        ur0 = resL[i]
        ur1 = resL[i + 1]
        try:
            UR.check_leq(ur0, ur1)
        except NotLeq:
            print 'resL is not INCREASING'
            print('ur[%s]: %s x' % (i, UR.format(ur0)))
            print('ur[%s]: %s x ' % (i + 1, UR.format(ur1)))
            raise
            raise Exception('resL is not INCREASING')

    for ur0, ur1 in zip(resL, resU):
        UR.check_leq(ur0, ur1)
Example #12
0
def invmult2_check3():

    F = parse_poset('dimensionless')
    R1 = parse_poset('dimensionless')
    R2 = parse_poset('dimensionless')

    im = InvMult2(F, (R1, R2))

    InvMult2.ALGO = InvMult2.ALGO_VAN_DER_CORPUT

    R = im.get_res_space()
    UR = UpperSets(R)

    #     ns = [1, 2, 3, 4, 10, 15]
    #     ns = [1, 5, 10, 15, 25, 50, 61, 100]
    ns = [1, 2, 3, 4, 5, 10]
    resL = []
    resU = []
    f0 = 1.0
    for n in ns:
        dpU = im.get_upper_bound(n)
        dpL = im.get_lower_bound(n)
        urL = dpL.solve(f0)
        print urL
        print '%r' % urL.minimals
        check_minimal(urL.minimals, R)
        urU = dpU.solve(f0)
        check_minimal(urU.minimals, R)
        UR.belongs(urL)
        UR.belongs(urU)
        resL.append(urL)
        resU.append(urU)

    def plot_upper(pylab, ur, markers):
        points = np.array(list(ur.minimals))
        eps = np.finfo(float).eps
        points = np.maximum(points, eps)
        points = np.minimum(points, 20)

        pylab.plot(points[:, 0], points[:, 1], markers)

    r = Report()
    f = r.figure()
    for n, ur in zip(ns, resL):
        caption = str(ur)
        with f.plot('resL-%d' % n, caption=caption) as pylab:
            for n0, ur0 in zip(ns, resL):
                if n0 == n: continue
                plot_upper(pylab, ur0, 'kx')

            plot_upper(pylab, ur, 'o')

            pylab.axis((-0.1, 10.1, -0.1, 10.1))

    f = r.figure()
    for n, ur in zip(ns, resU):
        with f.plot('resU-%d' % n) as pylab:
            for n0, ur0 in zip(ns, resU):
                if n0 == n: continue
                plot_upper(pylab, ur0, 'kx')

            plot_upper(pylab, ur, 'o')
            pylab.axis((-0.1, 10.1, -0.1, 10.1))

    fn = 'out/invmult2_check3.html'
    print('writing to %s' % fn)
    r.to_html(fn)

    for urU in resU:
        for x, y in urU.minimals:
            prod = x * y
            if prod > f0:
                continue
            else:
                assert_allclose(x * y, f0)  # , (x, y, f0, x * y)
    for urL in resL:
        for x, y in urL.minimals:
            x = float(x)
            y = float(y)
            assert x * y <= f0, (x, y, f0, x * y)

    # check resU is DECREASING
    for i in range(len(resU) - 1):
        ur0 = resU[i]
        ur1 = resU[i + 1]
        try:
            UR.check_leq(ur1, ur0)
        except NotLeq:
            print('ur[%s]: %s ' % (i, UR.format(ur0)))
            print('ur[%s]: %s ' % (i + 1, UR.format(ur1)))
            raise Exception('resU is not DECREASING')

    # check resL is INCREASING
    for i in range(len(resU) - 1):
        ur0 = resL[i]
        ur1 = resL[i + 1]
        try:
            UR.check_leq(ur0, ur1)
        except NotLeq:
            print 'resL is not INCREASING'
            print('ur[%s]: %s x' % (i, UR.format(ur0)))
            print('ur[%s]: %s x ' % (i + 1, UR.format(ur1)))
            raise
            raise Exception('resL is not INCREASING')

    for ur0, ur1 in zip(resL, resU):
        UR.check_leq(ur0, ur1)