def test_imp_space_1(): ndp0 = parse_ndp(""" mcdp { actuation = instance mcdp { # actuators need to provide this lift provides lift [N] # and will require power requires power [W] # simple model: quadratic c = 0.002 W/N^2 power >= lift * lift * c a = instance catalogue { provides a [N] one | 10N } a.a >= 0N } requires power for actuation provides lift using actuation } """) assert isinstance(ndp0, CompositeNamedDP) ndp0_labeled = get_labelled_version(ndp0) _ndp = ndp0 dp = ndp0_labeled.get_dp() # print ndp.repr_long() # print dp.repr_long() f = 0.0 R = dp.get_res_space() ur = dp.solve(f) I = dp.get_imp_space() assert isinstance(I, SpaceProduct) print(getattr(I, ATTRIBUTE_NDP_RECURSIVE_NAME, 'no attr')) print('I: %s' % I) print('get_names_used: %s' % get_names_used(I)) for r in ur.minimals: print('r = %s' % R.format(r)) imps = dp.get_implementations_f_r(f, r) print('imps: %s' % imps) for imp in imps: I.belongs(imp) imp_dict = get_imp_as_recursive_dict(I, imp) print('imp dict: %r' % imp_dict) assert set(imp_dict) == set(['_res_power', '_fun_lift', 'actuation']), imp_dict found = set(imp_dict['actuation']) expected = set(['_mult1', 'a', '_res_power', '_c', '_prod1', '_fun_lift', '_join_fname1']) assert_equal(expected, found) context = {} artifact = ndp_make(ndp0, imp_dict, context) print('artifact: %s' % artifact)
def test_imp_space_1(): ndp0 = parse_ndp(""" mcdp { actuation = instance mcdp { # actuators need to provide this lift provides lift [N] # and will require power requires power [W] # simple model: quadratic c = 0.002 W/N^2 power >= lift * lift * c a = instance catalogue { provides a [N] one | 10N } a.a >= 0N } requires power for actuation provides lift using actuation } """) assert isinstance(ndp0, CompositeNamedDP) ndp0_labeled = get_labelled_version(ndp0) _ndp = ndp0 dp = ndp0_labeled.get_dp() # print ndp.repr_long() # print dp.repr_long() f = 0.0 R = dp.get_res_space() ur = dp.solve(f) I = dp.get_imp_space() assert isinstance(I, SpaceProduct) print(getattr(I, MCDPConstants.ATTRIBUTE_NDP_RECURSIVE_NAME, 'no attr')) print('I: %s' % I) print('get_names_used: %s' % get_names_used(I)) for r in ur.minimals: print('r = %s' % R.format(r)) imps = dp.get_implementations_f_r(f, r) print('imps: %s' % imps) for imp in imps: I.belongs(imp) imp_dict = get_imp_as_recursive_dict(I, imp) print('imp dict: %r' % imp_dict) assert set(imp_dict) == set(['_res_power', '_fun_lift', 'actuation']), imp_dict found = set(imp_dict['actuation']) expected = set(['_mult1', 'a', '_res_power', '_c', '_prod1', '_fun_lift', '_join_fname1']) assert_equal(expected, found) context = {} artifact = ndp_make(ndp0, imp_dict, context) print('artifact: %s' % artifact)
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_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 test_imp_dict_2_makecanonical(id_ndp, ndp0): """ This one also canonicalizes. """ if '_inf' in id_ndp: # infinite return if not isinstance(ndp0, CompositeNamedDP): print('skipping because not CompositeNamedDP: %s' % type(ndp0).__name__) return try: ndp0.check_fully_connected() except NotConnected: print('skipping because not connected') return ndp_labeled = get_labelled_version(ndp0) _ndp = cndp_makecanonical(ndp0) dp0 = ndp_labeled.get_dp() F = dp0.get_fun_space() I = dp0.get_imp_space() assert isinstance(I, SpaceProduct) # print ndp.repr_long() print('I: %s' % I) print('get_names_used: %s' % get_names_used(I)) f = list(F.get_minimal_elements())[0] try: ur = dp0.solve(f) except NotSolvableNeedsApprox: return for r in ur.minimals: imps = dp0.get_implementations_f_r(f, r) for imp in imps: I.belongs(imp) context = {} imp_dict = get_imp_as_recursive_dict(I, imp) artifact = ndp_make(ndp0, imp_dict, context) print('artifact: %s' % artifact)
def test_imp_dict_2_makecanonical(id_ndp, ndp0): """ This one also canonicalizes. """ if '_inf' in id_ndp: # infinite return if not isinstance(ndp0, CompositeNamedDP): print('skipping because not CompositeNamedDP: %s' % type(ndp0).__name__) return try: ndp0.check_fully_connected() except NotConnected: print('skipping because not connected') return ndp_labeled = get_labelled_version(ndp0) _ndp = cndp_makecanonical(ndp0) dp0 = ndp_labeled.get_dp() F = dp0.get_fun_space() I = dp0.get_imp_space() assert isinstance(I, SpaceProduct) # print ndp.repr_long() print('I: %s' % I) print('get_names_used: %s' % get_names_used(I)) f = list(F.get_minimal_elements())[0] try: ur = dp0.solve(f) except NotSolvableNeedsApprox: return for r in ur.minimals: imps = dp0.get_implementations_f_r(f, r) for imp in imps: I.belongs(imp) context = {} imp_dict = get_imp_as_recursive_dict(I, imp) artifact = ndp_make(ndp0, imp_dict, context) print('artifact: %s' % artifact)
def test_imp_space_2(): ndp0 = parse_ndp(""" addmake(root: code mocdp.comp.tests.test_imp_space.make_root) mcdp { a = instance addmake(root: code mocdp.comp.tests.test_imp_space.make_a) mcdp { a2 = instance addmake(root: code mocdp.comp.tests.test_imp_space.make_a2) catalogue { provides capacity [J] requires mass [g] model1 | 1 J | 200 g model2 | 2 J | 300 g } provides capacity using a2 requires mass >= 10g + a2.mass } b = instance addmake(root: code mocdp.comp.tests.test_imp_space.make_b) catalogue { provides capacity [J] requires mass [g] model3 | 1 J | 200 g model4 | 2 J | 300 g } provides capacity <= a.capacity + b.capacity requires mass >= a.mass + b.mass } """) assert isinstance(ndp0, CompositeNamedDP) ndp_labeled = get_labelled_version(ndp0) ndp_canonical = cndp_makecanonical(ndp_labeled) dp0 = ndp_canonical.get_dp() # print dp0.repr_long() dp, _ = get_dp_bounds(dp0, 5, 5) f = 0.0 R = dp.get_res_space() ur = dp.solve(f) I = dp.get_imp_space() assert isinstance(I, SpaceProduct) print('I: %s' % I) print('get_names_used: %s' % get_names_used(I)) for r in ur.minimals: print('r = %s' % R.format(r)) imps = dp.get_implementations_f_r(f, r) print('imps: %s' % imps) for imp in imps: I.belongs(imp) imp_dict = get_imp_as_recursive_dict(I, imp) # , ignore_hidden=False) print('imp dict: %r' % imp_dict) assert set(imp_dict) == set(['a', 'b', '_sum1', '_invplus1', '_fun_capacity', '_res_mass']), imp_dict assert set(imp_dict['a']) == set(['_plus1', 'a2', '_fun_capacity', '_res_mass' ]), imp_dict['a'] context = {} artifact = ndp_make(ndp0, imp_dict, context) print('artifact: %s' % artifact)
def test_imp_space_2(): ndp0 = parse_ndp(""" addmake(root: code mcdp_comp_tests.test_imp_space.make_root) mcdp { a = instance addmake(root: code mcdp_comp_tests.test_imp_space.make_a) mcdp { a2 = instance addmake(root: code mcdp_comp_tests.test_imp_space.make_a2) catalogue { provides capacity [J] requires mass [g] model1 | 1 J | 200 g model2 | 2 J | 300 g } provides capacity using a2 requires mass >= 10g + a2.mass } b = instance addmake(root: code mcdp_comp_tests.test_imp_space.make_b) catalogue { provides capacity [J] requires mass [g] model3 | 1 J | 200 g model4 | 2 J | 300 g } provides capacity <= a.capacity + b.capacity requires mass >= a.mass + b.mass } """) assert isinstance(ndp0, CompositeNamedDP) ndp_labeled = get_labelled_version(ndp0) ndp_canonical = cndp_makecanonical(ndp_labeled) dp0 = ndp_canonical.get_dp() # print dp0.repr_long() dp, _ = get_dp_bounds(dp0, 5, 5) f = 0.0 R = dp.get_res_space() ur = dp.solve(f) I = dp.get_imp_space() assert isinstance(I, SpaceProduct) print('I: %s' % I) print('get_names_used: %s' % get_names_used(I)) for r in ur.minimals: print('r = %s' % R.format(r)) imps = dp.get_implementations_f_r(f, r) print('imps: %s' % imps) for imp in imps: I.belongs(imp) imp_dict = get_imp_as_recursive_dict(I, imp) # , ignore_hidden=False) print('imp dict: %r' % imp_dict) assert set(imp_dict) == set(['a', 'b', '_sum1', '_invplus1', '_fun_capacity', '_res_mass']), imp_dict assert set(imp_dict['a']) == set(['_plus1', 'a2', '_fun_capacity', '_res_mass' ]), imp_dict['a'] context = {} artifact = ndp_make(ndp0, imp_dict, context) print('artifact: %s' % artifact)