def check_coproductdp_error1(): # check error if types are different F1 = parse_poset('g') F2 = parse_poset('kg') R1 = parse_poset('J') R2 = parse_poset('V') # Same R, different F dp1 = Template(F1, R1) dp2 = Template(F2, R1) try: CoProductDP((dp1, dp2)) except ValueError as e: assert 'Cannot form' in str(e) else: raise Exception() # Same F, different R dp1 = Template(F1, R1) dp2 = Template(F1, R2) try: CoProductDP((dp1, dp2)) except ValueError as e: assert 'Cannot form' in str(e) else: raise Exception()
def check_coproductdp2(): F = parse_poset('g') R = parse_poset('J') I = parse_poset('finite_poset{a b c}') entries = [('a', 1.0, 2.0), ('b', 2.0, 4.0)] dp1 = CatalogueDP(F=F,R=R,I=I,entries=entries) # This will not be feasible for dp1 f= 3.0 r = 1000.0 try: ms = dp1.get_implementations_f_r(f,r) except NotFeasible: pass else: assert False, ms # and not for dp as well dp = CoProductDP((dp1,)) try: ms = dp.get_implementations_f_r(f,r) except NotFeasible: pass else: assert False, ms
def check_coproductdp2(): F = parse_poset('g') R = parse_poset('J') I = parse_poset('finite_poset{a b c}') entries = [('a', 1.0, 2.0), ('b', 2.0, 4.0)] dp1 = CatalogueDP(F=F, R=R, I=I, entries=entries) # This will not be feasible for dp1 f = 3.0 r = 1000.0 try: ms = dp1.get_implementations_f_r(f, r) except NotFeasible: pass else: assert False, ms # and not for dp as well dp = CoProductDP((dp1, )) try: ms = dp.get_implementations_f_r(f, r) except NotFeasible: pass else: assert False, ms
def test_series_invalid(): """ invalid argument for Series """ F1 = parse_poset('s') R1 = parse_poset('s') F2 = parse_poset('m') R2 = parse_poset('g') dp1 = Template(F1, R1) dp2 = Template(F2, R2) assert_raises(DPInternalError, Series, dp1, dp2)
def units_pixels(): # so 'pi' is forbidden as a keyword assert_raises(DPSyntaxError, parse_wrap, Keyword('pi'), 'pixels') parse_wrap(SyntaxIdentifiers.not_keyword + L('pixels'), 'pixels') parse_wrap(Syntax.pint_unit_simple, 'pixels') parse_wrap(Syntax.space_pint_unit, 'pixels') parse_wrap(Syntax.space_pint_unit, 'pixels/deg') parse_poset('pixels/deg') parse_poset('pixel/deg') parse_constant(' 1.0 pixels/deg')
def check_type_universe1(): tu = get_types_universe() R1 = parse_poset('dimensionless') R2 = parse_poset('dimensionless') assert R1 == R2 tu.check_equal(R1, R2) tu.check_leq(R1, R2) _embed1, _embed2 = tu.get_embedding(R1, R2)
def test_exc_connect_resources_to_outside(): F1 = parse_poset('s') R1 = parse_poset('s') dp = Template(F1, R1) ndp = dpwrap(dp, 'f1', 'r1') ndp_name = 'name' name2ndp = {ndp_name: ndp} connections = [] rnames = ['r1', 'notpresent'] assert_raises(ValueError, connect_resources_to_outside, name2ndp, connections, ndp_name, rnames)
def check_poset_bottom_two(): s = """ add_bottom add_bottom poset { a } """ try: parse_poset(s) except DPSemanticError as e: assert 'Poset already has the' in str(e) return assert False
def check_poset_bottom_checks(): s = """ poset { a >= b a <= b } """ try: parse_poset(s) assert False, 'Should have detected the inconsistency' except DPSemanticError as e: print str(e) return
def check_repeated_poset(): s = """ poset { a a <= b } """ parse_poset(s) s = """ poset{ a a } """ # mcdp.exceptions.DPSemanticError: Repeated element 'a'. assert_raises(DPSemanticError, parse_poset, s)
def go(): ieee_fonts_zoom3(pylab) r = Report() algos = [InvMult2.ALGO_UNIFORM, InvMult2.ALGO_VAN_DER_CORPUT] for algo in algos: InvMult2.ALGO = algo InvPlus2.ALGO = algo print('Using algorithm %s ' % algo) with r.subsection(algo) as r2: # first F = parse_poset('dimensionless') R = F dp = InvMult2(F, (R, R)) ns = [3, 4, 5, 6, 10, 15] axis = (0.0, 6.0, 0.0, 6.0) with r2.subsection('invmult2') as rr: go1(rr, ns, dp, plot_nominal_invmult, axis) # second axis = (0.0, 1.2, 0.0, 1.2) dp = InvPlus2(F, (R, R)) with r2.subsection('invplus2') as rr: go1(rr, ns, dp, plot_nominal_invplus, axis) fn = 'out-plot_approximations/report.html' print('writing to %s' % fn) r.to_html(fn)
def go(): ieee_fonts_zoom3(pylab) r = Report() algos = [InvMult2.ALGO_UNIFORM, InvMult2.ALGO_VAN_DER_CORPUT] for algo in algos: InvMult2.ALGO = algo InvPlus2.ALGO = algo print("Using algorithm %s " % algo) with r.subsection(algo) as r2: # first F = parse_poset("dimensionless") R = F dp = InvMult2(F, (R, R)) ns = [3, 4, 5, 6, 10, 15] axis = (0.0, 6.0, 0.0, 6.0) with r2.subsection("invmult2") as rr: go1(rr, ns, dp, plot_nominal_invmult, axis) # second axis = (0.0, 1.2, 0.0, 1.2) dp = InvPlus2(F, (R, R)) with r2.subsection("invplus2") as rr: go1(rr, ns, dp, plot_nominal_invplus, axis) fn = "out-plot_approximations/report.html" print("writing to %s" % fn) r.to_html(fn)
def check_add_bottom(): s = """ poset { a <= b <= c } """ P = parse_poset(s) assert len(P.elements) == 3 s = """ add_bottom poset { v1_5 v5 v6_6 } """ P = parse_poset(s) assert len(P.elements) == 4
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)
def test_right_inverse(): P = parse_poset('((J x W) x s) x (m x Hz)') coords = [(1, 1), [(0, 0, 1), (1, 0), (0, 0, 0), (0, 1)]] #print 'coords', coords i0 = get_id_indices(P) #print 'i0', i0 # compose _i0coords = compose_indices(P, i0, coords, list) #print 'i0coords', i0coords _Q, _coords2 = transform_right_inverse(P, coords, PosetProduct)
def check_poset_geq(): parse_wrap_check('a>=b>=c', Syntax.finite_poset_chain_geq) parse_wrap_check('a<=b<=c', Syntax.finite_poset_chain_leq) s = """ poset { a >= b >= c } """ P = parse_poset(s) assert len(P.elements) == 3
def test_exc_InvPlus2(): F = parse_poset('g') Rs = (parse_poset('g'), parse_poset('kg')) assert_raises(DPInternalError, InvPlus2, F, Rs) F = parse_poset('kg') Rs = (parse_poset('g'), parse_poset('g')) assert_raises(DPInternalError, InvPlus2, F, Rs)
def check_lang90(): # TODO: rename """ Multiplication Nat and Rcompunits """ # This is just a constant s = """ mcdp { unit_cost = 3 $ num_replacements = Nat: 2 requires cost >= unit_cost * num_replacements } """ dp = parse_ndp(s).get_dp() R = dp.get_res_space() assert R == parse_poset('USD'), R # This is not a constant s = """ mcdp { provides num_replacements [Nat] unit_cost = 3 $ requires cost >= unit_cost * provided num_replacements } """ dp = parse_ndp(s).get_dp() R = dp.get_res_space() assert R == parse_poset('USD'), R # This is with Rcomp s = """ mcdp { provides num_replacements [Nat] unit_cost = 3 dimensionless requires cost >= unit_cost * provided num_replacements } """ dp = parse_ndp(s).get_dp() R = dp.get_res_space() assert R == R_dimensionless, R
def check_get_names_used1(): # . L . . . . . . . \ Parallel2 % R[kg]×(R[N]×R[N]) -> R[kg]×R[W] I = PosetProduct(R[kg],PosetProduct(R[N],R[N]){actuati # on/_prod1},R[N²]) names: [('actuation', '_prod1')] # . L . . . . . . . . \ Id(R[kg]) I = R[kg] # . L . . . . . . . . \ Series: % R[N]×R[N] -> R[W] I = PosetProduct(PosetProduct(R[N],R[N]){actuation/_prod1},R[N²]) nam # es: [('actuation', '_prod1'), ('actuation', '_mult1')] # . L . . . . . . . . . \ ProductN(R[N]×R[N] -> R[N²]) named: ('actuation', '_prod1') I = PosetProduct(R[N],R[N]) # . L . . . . . . . . . \ GenericUnary(<mcdp_lang.misc_math.MultValue instance at 0x10d8dcbd8>) named: ('actuation', '_mult1 # ') I = R[N²] S1 = parse_poset('N') setattr(S1, ATTRIBUTE_NDP_RECURSIVE_NAME, ('S1',)) S2 = parse_poset('kg') setattr(S2, ATTRIBUTE_NDP_RECURSIVE_NAME, ('S2',)) S12 = PosetProduct((S1, S2)) names = get_names_used(S12) assert names == [('S1',), ('S2',)], names P = parse_poset('J x W') setattr(P, ATTRIBUTE_NDP_RECURSIVE_NAME, ('prod',)) S, _pack, _unpack = get_product_compact(P, S12) print S.__repr__() assert get_names_used(S) == [('prod',), ('S1',), ('S2',)]
def check_get_names_used1(): # . L . . . . . . . \ Parallel2 % R[kg]×(R[N]×R[N]) -> R[kg]×R[W] I = PosetProduct(R[kg],PosetProduct(R[N],R[N]){actuati # on/_prod1},R[N²]) names: [('actuation', '_prod1')] # . L . . . . . . . . \ Id(R[kg]) I = R[kg] # . L . . . . . . . . \ Series: % R[N]×R[N] -> R[W] I = PosetProduct(PosetProduct(R[N],R[N]){actuation/_prod1},R[N²]) nam # es: [('actuation', '_prod1'), ('actuation', '_mult1')] # . L . . . . . . . . . \ ProductN(R[N]×R[N] -> R[N²]) named: ('actuation', '_prod1') I = PosetProduct(R[N],R[N]) # . L . . . . . . . . . \ GenericUnary(<mcdp_lang.misc_math.MultValue instance at 0x10d8dcbd8>) named: ('actuation', '_mult1 # ') I = R[N²] att = MCDPConstants.ATTRIBUTE_NDP_RECURSIVE_NAME S1 = parse_poset('N') setattr(S1, att, ('S1', )) S2 = parse_poset('kg') setattr(S2, att, ('S2', )) S12 = PosetProduct((S1, S2)) names = get_names_used(S12) assert names == [('S1', ), ('S2', )], names P = parse_poset('J x W') setattr(P, att, ('prod', )) S, _pack, _unpack = get_product_compact(P, S12) print S.__repr__() assert get_names_used(S) == [('prod', ), ('S1', ), ('S2', )]
def check_lang84(): # TODO: rename to LF # In LF, F = parse_poset('m') LF = LowerSets(F) lf0 = F.Ls(set([])) lf1 = F.L(0.0) lf2 = F.L(5.0) lf3 = F.L(F.get_top()) # the bottom is lf3 LF.check_leq(lf3, lf2) LF.check_leq(lf3, lf1) LF.check_leq(lf3, lf0) LF.check_leq(lf2, lf1) LF.check_leq(lf2, lf0) LF.check_leq(lf1, lf0) pass
def check_lang_interval1(): parse_poset('Interval(0g, 1g)')
def check_lang_interval4(): # TODO: coporduct parse_wrap(Syntax.space_coproduct, 'coproduct(g, V)') P = parse_poset('coproduct(g, V)') print P
def check_lang_interval2(): one = 'Interval(0.0 [], 1.0 [])' rgb = " x ".join([one] * 3) print parse_poset(rgb)
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)
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)
def units_episodes(): parse_poset('episodes/day') parse_poset('episode/day')
def use_e_as_poset_element(): parse_poset('poset{ a<=b<=e }')