예제 #1
0
파일: basic.py 프로젝트: AndreaCensi/mcdp
def check_posets_misc1():
    try:
        poset_check_chain(Nat(), [2, 1])
    except ValueError:
        pass
    else:
        assert False

    try:
        check_minimal([2, 1], Nat())
    except ValueError:
        pass
    else:
        assert False

    try:
        check_maximal([2, 1], Nat())
    except ValueError:
        pass
    else:
        assert False
예제 #2
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)
예제 #3
0
def invmultL_solve_options(F, R, f, n, algo):
    """ Returns a set of points that are *below* r1*r2 = f """
    from .dp_inv_mult import InvMult2
    assert algo in [InvMult2.ALGO_UNIFORM, InvMult2.ALGO_VAN_DER_CORPUT]

    if f == 0.0:
        return set([(0.0, 0.0)])

    if is_top(F, f):
        mcdp_dev_warning('FIXME Need much more thought about this')
        top1 = R[0].get_top()
        top2 = R[1].get_top()
        s = set([(top1, top2)])
        return s

    if algo == InvMult2.ALGO_UNIFORM:
        if n == 1:
            points = [(0.0, 0.0)]
        elif n == 2:
            points = [(0.0, 0.0)]
        else:
            pu = sorted(samplec(n - 1, f), key=lambda _: _[0])
            assert len(pu) == n - 1, (len(pu), n - 1)
            nu = len(pu)

            points = set()
            points.add((0.0, pu[0][1]))
            points.add((pu[-1][0], 0.0))
            for i in range(nu - 1):
                p = (pu[i][0], pu[i + 1][1])
                points.add(p)

    elif algo == InvMult2.ALGO_VAN_DER_CORPUT:

        if n == 1:
            points = set([(0.0, 0.0)])
        else:
            x1, x2 = generate_exp_van_der_corput_sequence(n=n - 1, C=f)
            pu = zip(x1, x2)
            assert len(pu) == n - 1, pu

            if do_extra_checks():
                check_minimal(pu, R)

            nu = len(pu)
            points = []
            points.append((0.0, pu[0][1]))

            for i in range(nu - 1):
                p = (pu[i][0], pu[i + 1][1])
                points.append(p)

            points.append((pu[-1][0], 0.0))

            points = set(points)
    else:  # pragma: no cover
        assert False

    assert len(points) == n, (n, len(points), points)

    return points
예제 #4
0
def invmultL_solve_options(F, R, f, n, algo):
    """ Returns a set of points that are *below* r1*r2 = f """
    from .dp_inv_mult import InvMult2
    assert algo in [InvMult2.ALGO_UNIFORM, InvMult2.ALGO_VAN_DER_CORPUT]
    
    if f == 0.0:
        return set([(0.0, 0.0)])

    if is_top(F, f):
        mcdp_dev_warning('FIXME Need much more thought about this')
        top1 = R[0].get_top()
        top2 = R[1].get_top()
        s = set([(top1, top2)])
        return s

    if algo == InvMult2.ALGO_UNIFORM:
        if n == 1:
            points = [(0.0, 0.0)]
        elif n == 2:
            points = [(0.0, 0.0)]
        else:
            pu = sorted(samplec(n - 1, f), key=lambda _: _[0])
            assert len(pu) == n - 1, (len(pu), n - 1)
            nu = len(pu)

            points = set()
            points.add((0.0, pu[0][1]))
            points.add((pu[-1][0], 0.0))
            for i in range(nu - 1):
                p = (pu[i][0], pu[i + 1][1])
                points.add(p)

    elif algo == InvMult2.ALGO_VAN_DER_CORPUT:

        if n == 1:
            points = set([(0.0, 0.0)])
        else:
            x1, x2 = generate_exp_van_der_corput_sequence(n=n - 1, C=f)
            pu = zip(x1, x2)
            assert len(pu) == n - 1, pu

            if do_extra_checks():
                check_minimal(pu, R)

            nu = len(pu)
            points = []
            points.append((0.0, pu[0][1]))

            for i in range(nu - 1):
                p = (pu[i][0], pu[i + 1][1])
                points.append(p)

            points.append((pu[-1][0], 0.0))

            points = set(points)
    else: # pragma: no cover
        assert False

    assert len(points) == n, (n, len(points), points)

    return points
예제 #5
0
파일: invmult2_tests.py 프로젝트: rusi/mcdp
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)