Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
def plot_different_solutions(libname, ndpname, query, out, upper=None):
    if not os.path.exists(out):
        os.makedirs(out)
    library = get_test_library(libname)
    #library.use_cache_dir(os.path.join(out, 'cache'))
    context = Context()
    ndp = library.load_ndp(ndpname, context)

    context = library._generate_context_with_hooks()
    ndp_labelled = get_labelled_version(ndp)
    dp0 = ndp_labelled.get_dp()
    if upper is not None:
        _, dpU = get_dp_bounds(dp0, nl=1, nu=upper)
        dp = dpU
    else:
        dp = dp0

    M = dp.get_imp_space()

    with open(os.path.join(out, 'ndp.txt'), 'w') as f:
        f.write(ndp.repr_long())
    with open(os.path.join(out, 'M.txt'), 'w') as f:
        f.write(M.repr_long())
    with open(os.path.join(out, 'dp.txt'), 'w') as f:
        f.write(dp.repr_long())
    with open(os.path.join(out, 'dp0.txt'), 'w') as f:
        f.write(dp0.repr_long())

    f = convert_string_query(ndp=ndp, query=query, context=context)

    report = Report()

    res = dp.solve(f)
    print('num solutions: %s' % len(res.minimals))
    for ri, r in enumerate(res.minimals):
        ms = dp.get_implementations_f_r(f, r)

        for j, m in enumerate(ms):
            imp_dict = get_imp_as_recursive_dict(M, m)
            print imp_dict

            images_paths = library.get_images_paths()
            gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=upper, nl=1)

            gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM,
                                images_paths=images_paths,
                                plotting_info=gv)

            with report.subsection('%s-%s' % (ri, j)) as rr:
                gg_figure(rr, 'figure', gg, do_png=True, do_pdf=False,
                          do_svg=False, do_dot=False)


    fn = os.path.join(out, 'solutions.html')
    print('writing to %s' % fn)
    report.to_html(fn)
Esempio n. 4
0
def dp_repr_long_labeled(data):
    ndp = get_ndp(data)
    ndp = get_labelled_version(ndp)
    try:
        dp = ndp.get_dp()
    except DPSemanticError:
        logger.warn('Could not draw dp_graph_tree_compact_labeled_')
        return []
    res1 = ('txt', 'dp_repr_long_labeled', dp.repr_long())
    return [res1]
Esempio n. 5
0
File: plot.py Progetto: kannode/mcdp
def dp_repr_long_labeled(data):
    ndp = get_ndp(data)
    ndp = get_labelled_version(ndp)
    try:
        dp = ndp.get_dp()
    except DPSemanticError:
        logger.warn('Could not draw dp_graph_tree_compact_labeled_')
        return []
    res1 = ('txt', 'dp_repr_long_labeled', dp.repr_long())
    return [res1]
Esempio n. 6
0
def dp_graph_tree_compact_labeled_(data):
    ndp = get_ndp(data)
    ndp = get_labelled_version(ndp)
    try:
        dp = ndp.get_dp()
    except DPSemanticError:
        logger.warn('Could not draw dp_graph_tree_compact_labeled_')
        return []
    
    gg = dp_graph_tree(dp, compact=True)
    return return_formats2(gg, 'dp_graph_tree_compact_labeled')
Esempio n. 7
0
def test_imp_dict_1(id_ndp, ndp):
    if '_inf' in id_ndp:  # infinite
        return

    try:
        ndp.check_fully_connected()
    except NotConnected:
        print('Skipping test_imp_dict_1 because %r not connected.' % id_ndp)
        return

    ndp_labeled = get_labelled_version(ndp)

    dp0 = ndp_labeled.get_dp()
    F = dp0.get_fun_space()
    I = dp0.get_imp_space()
    # print ndp_labeled.repr_long()
    # print dp0.repr_long()
    print('I: %s' % I.repr_long())
    

    f = list(F.get_minimal_elements())[0]
    try:
        ur = dp0.solve(f)
    except NotSolvableNeedsApprox:
        return

    imp_dict = None
    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)
            print('imp_dict: {}'.format(imp_dict))
            artifact = ndp_make(ndp, imp_dict, context)
            print('artifact: {}'.format(artifact))
            
    # Let's just do it with one
    if imp_dict is not None:

        gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=None, nl=None)

#         images_paths = []  # library.get_images_paths()
        from mcdp_report.gdc import STYLE_GREENREDSYM
        gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM,
                            plotting_info=gv)

        from reprep import Report
        from mcdp_report.gg_utils import gg_figure
        report = Report()
        gg_figure(report, 'figure', gg, do_png=True, do_pdf=False, do_svg=False, do_dot=False)
        fn = os.path.join('out', 'test_imp_dict_1', '%s.html' % id_ndp)
        print('written to %s' % fn)
        report.to_html(fn)
Esempio n. 8
0
File: plot.py Progetto: kannode/mcdp
def dp_graph_tree_compact_labeled_(data):
    ndp = get_ndp(data)
    ndp = get_labelled_version(ndp)
    try:
        dp = ndp.get_dp()
    except DPSemanticError:
        logger.warn('Could not draw dp_graph_tree_compact_labeled_')
        return []

    gg = dp_graph_tree(dp, compact=True)
    return return_formats2(gg, 'dp_graph_tree_compact_labeled')
Esempio n. 9
0
def test_imp_dict_1(id_ndp, ndp):
    if '_inf' in id_ndp:  # infinite
        return

    try:
        ndp.check_fully_connected()
    except NotConnected:
        print('Skipping test_imp_dict_1 because %r not connected.' % id_ndp)
        return

    ndp_labeled = get_labelled_version(ndp)

    dp0 = ndp_labeled.get_dp()
    F = dp0.get_fun_space()
    I = dp0.get_imp_space()
    # print ndp_labeled.repr_long()
    # print dp0.repr_long()
    print('I: %s' % I.repr_long())
    

    f = list(F.get_minimal_elements())[0]
    try:
        ur = dp0.solve(f)
    except NotSolvableNeedsApprox:
        return

    imp_dict = None
    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)
            print('imp_dict: {}'.format(imp_dict))
            artifact = ndp_make(ndp, imp_dict, context)
            print('artifact: {}'.format(artifact))
            
    # Let's just do it with one
    if imp_dict is not None:

        gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=None, nl=None)

        images_paths = []  # library.get_images_paths()
        from mcdp_report.gdc import STYLE_GREENREDSYM
        gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM, images_paths=images_paths,
                            plotting_info=gv)

        from reprep import Report
        from mcdp_report.gg_utils import gg_figure
        report = Report()
        gg_figure(report, 'figure', gg, do_png=True, do_pdf=False, do_svg=False, do_dot=False)
        fn = os.path.join('out', 'test_imp_dict_1', '%s.html' % id_ndp)
        print('written to %s' % fn)
        report.to_html(fn)
Esempio n. 10
0
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)
Esempio n. 11
0
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)
Esempio n. 12
0
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)
Esempio n. 13
0
def ndp_repr_long_labeled(data):
    ndp = get_ndp(data)
    ndp = get_labelled_version(ndp)
    res1 = ('txt', 'ndp_repr_long_labeled', ndp.repr_long())
    return [res1]
Esempio n. 14
0
File: plot.py Progetto: kannode/mcdp
def ndp_repr_long_labeled(data):
    ndp = get_ndp(data)
    ndp = get_labelled_version(ndp)
    res1 = ('txt', 'ndp_repr_long_labeled', ndp.repr_long())
    return [res1]
Esempio n. 15
0
def solve_main(
    logger,
    config_dirs,
    maindir,
    cache_dir,
    model_name,
    lower,
    upper,
    out_dir,
    max_steps,
    query_strings,
    intervals,
    _exp_advanced,
    expect_nres,
    imp,
    expect_nimp,
    plot,
    do_movie,
    # expect_res=None,
    expect_res,  # @UnusedVariable
    make,
):

    if out_dir is None:
        out = solve_get_output_dir(prefix="out/out")
    else:
        out = out_dir

    logger.info("Using output dir %r" % out)

    librarian = Librarian()
    logger.info("Looking for libraries in %s..." % config_dirs)
    for e in config_dirs:
        librarian.find_libraries(e)
    logger.info("Found %d libraries." % len(librarian.get_libraries()))

    library = librarian.get_library_by_dir(maindir)
    if cache_dir is not None:
        library.use_cache_dir(cache_dir)

    ndp = library.load_ndp(model_name)
    basename = model_name

    if make or (plot and imp):
        ndp_labelled = get_labelled_version(ndp)
    else:
        ndp_labelled = ndp

    basename, dp = solve_get_dp_from_ndp(basename=basename, ndp=ndp_labelled, lower=lower, upper=upper)

    F = dp.get_fun_space()
    R = dp.get_res_space()
    UR = UpperSets(R)

    query = " ".join(query_strings)
    c = library.parse_constant(query)
    tu = get_types_universe()
    try:
        tu.check_leq(c.unit, F)
    except NotLeq as e:
        msg = "The value given cannot be converted to functionality space."
        raise_wrapped(UserError, e, msg, unit=c.unit, F=F, compact=True)
    fg = express_value_in_isomorphic_space(c.unit, c.value, F)

    logger.info("query: %s" % F.format(fg))

    tracer = Tracer(logger=logger)
    res, trace = solve_meat_solve(tracer, ndp, dp, fg, intervals, max_steps, _exp_advanced)

    nres = len(res.minimals)

    if expect_nres is not None:
        if nres != expect_nres:
            msg = "Found wrong number of resources"
            raise_desc(ExpectationsNotMet, msg, expect_nres=expect_nres, nres=nres)

    if imp:
        M = dp.get_imp_space()
        nimplementations = 0
        for r in res.minimals:
            ms = dp.get_implementations_f_r(fg, r)
            nimplementations += len(ms)
            s = "r = %s " % R.format(r)
            for j, m in enumerate(ms):
                # print('m = %s' % str(m))
                s += "\n  implementation %d of %d: m = %s " % (j + 1, len(ms), M.format(m))

                if make:
                    imp_dict = get_imp_as_recursive_dict(M, m)  # , ignore_hidden=False)
                    print("imp dict: %r" % imp_dict)
                    context = {}
                    artifact = ndp_make(ndp, imp_dict, context)

                    print("artifact: %s" % artifact)

            tracer.log(s)

        if expect_nimp is not None:
            if expect_nimp != nimplementations:
                msg = "Found wrong number of implementations"
                raise_desc(ExpectationsNotMet, msg, expect_nimp=expect_nimp, nimplementations=nimplementations)

    #     if expect_res is not None:
    #         value = interpret_string(expect_res)
    #         tracer.log('value: %s' % value)
    #         res_expected = value.value
    #         tu = get_types_universe()
    #         # If it's a tuple of two elements, then we assume it's upper/lower bounds
    #         if isinstance(value.unit, PosetProduct):
    #             subs = value.unit.subs
    #             assert len(subs) == 2, subs
    #
    #             lower_UR_expected, upper_UR_expected = subs
    #             lower_res_expected, upper_res_expected = value.value
    #
    #             lower_bound = tu.get_embedding(lower_UR_expected, UR)[0](lower_res_expected)
    #             upper_bound = tu.get_embedding(upper_UR_expected, UR)[0](upper_res_expected)
    #
    #             tracer.log('lower: %s <= %s' % (UR.format(lower_bound), UR.format(res)))
    #             tracer.log('upper: %s <= %s' % (UR.format(upper_bound), UR.format(res)))
    #
    #             UR.check_leq(lower_bound, res)
    #             UR.check_leq(res, upper_bound)
    #         else:
    #             # only one element: equality
    #             UR_expected = value.unit
    #             tu.check_leq(UR_expected, UR)
    #             A_to_B, _B_to_A = tu.get_embedding(UR_expected, UR)
    #
    #             res_expected_f = A_to_B(res_expected)
    #             try:
    #                 UR.check_equal(res, res_expected_f)
    #             except NotEqual as e:
    #                 raise_wrapped(ExpectationsNotMet, e, 'res is different',
    #                               res=res, res_expected=res_expected, compact=True)

    if plot:
        r = Report()
        if _exp_advanced:
            from mcdp_report.generic_report_utils import generic_report

            generic_report(r, dp, trace, annotation=None, axis0=(0, 0, 0, 0))
        else:
            f = r.figure()
            from mcdp_report.generic_report_utils import generic_plot

            generic_plot(f, space=UR, value=res)
            from mcdp_report.generic_report_utils import generic_report_trace

            generic_report_trace(r, ndp, dp, trace, out, do_movie=do_movie)

        out_html = os.path.join(out, "report.html")
        logger.info("writing to %r" % out_html)
        r.to_html(out_html)

    if plot and imp:
        from mcdp_report_ndp_tests.test1 import GetValues
        from mcdp_report.gg_ndp import gvgen_from_ndp
        from mcdp_report.gdc import STYLE_GREENREDSYM
        from mcdp_report.gg_utils import gg_figure

        M = dp.get_imp_space()

        report_solutions = Report()
        for i, r in enumerate(res.minimals):
            ms = dp.get_implementations_f_r(fg, r)
            for j, m in enumerate(ms):

                imp_dict = get_imp_as_recursive_dict(M, m)
                images_paths = library.get_images_paths()
                gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=upper, nl=1)

                setattr(ndp, "_hack_force_enclose", True)

                with report_solutions.subsection("sol-%s-%s" % (i, j)) as rr:
                    # Left right
                    gg = gvgen_from_ndp(
                        ndp=ndp, style=STYLE_GREENREDSYM, images_paths=images_paths, plotting_info=gv, direction="LR"
                    )

                    gg_figure(rr, "figure", gg, do_png=True, do_pdf=True, do_svg=False, do_dot=False)

                    # Top-bottom
                    gg = gvgen_from_ndp(
                        ndp=ndp, style=STYLE_GREENREDSYM, images_paths=images_paths, plotting_info=gv, direction="TB"
                    )

                    gg_figure(rr, "figure2", gg, do_png=True, do_pdf=True, do_svg=False, do_dot=False)

        out_html = os.path.join(out, "report_solutions.html")
        logger.info("writing to %r" % out_html)
        report_solutions.to_html(out_html)
Esempio n. 16
0
def solve_main(
        logger,
        config_dirs,
        maindir,
        cache_dir,
        model_name,
        lower,
        upper,
        out_dir,
        max_steps,
        query_strings,
        intervals,
        _exp_advanced,
        expect_nres,
        imp,
        expect_nimp,
        plot,
        do_movie,

        # expect_res=None,
        expect_res,  # @UnusedVariable
        make):

    if out_dir is None:
        out = solve_get_output_dir(prefix='out/out')
    else:
        out = out_dir

    logger.info('Using output dir %r' % out)

    librarian = Librarian()
    logger.info('Looking for libraries in %s...' % config_dirs)
    for e in config_dirs:
        librarian.find_libraries(e)
    logger.info('Found %d libraries.' % len(librarian.get_libraries()))

    library = librarian.get_library_by_dir(maindir)
    if cache_dir is not None:
        library.use_cache_dir(cache_dir)

    ndp = library.load_ndp(model_name)
    basename = model_name

    if make or (plot and imp):
        ndp_labelled = get_labelled_version(ndp)
    else:
        ndp_labelled = ndp

    basename, dp = solve_get_dp_from_ndp(basename=basename,
                                         ndp=ndp_labelled,
                                         lower=lower,
                                         upper=upper)

    F = dp.get_fun_space()
    R = dp.get_res_space()
    UR = UpperSets(R)

    query = " ".join(query_strings)
    c = library.parse_constant(query)
    tu = get_types_universe()
    try:
        tu.check_leq(c.unit, F)
    except NotLeq as e:
        msg = 'The value given cannot be converted to functionality space.'
        raise_wrapped(UserError, e, msg, unit=c.unit, F=F, compact=True)
    fg = express_value_in_isomorphic_space(c.unit, c.value, F)

    logger.info('query: %s' % F.format(fg))

    tracer = Tracer(logger=logger)
    res, trace = solve_meat_solve_ftor(tracer, ndp, dp, fg, intervals,
                                       max_steps, _exp_advanced)

    nres = len(res.minimals)

    if expect_nres is not None:
        if nres != expect_nres:
            msg = 'Found wrong number of resources'
            raise_desc(ExpectationsNotMet,
                       msg,
                       expect_nres=expect_nres,
                       nres=nres)

    if imp:
        M = dp.get_imp_space()
        nimplementations = 0
        for r in res.minimals:
            ms = dp.get_implementations_f_r(fg, r)
            nimplementations += len(ms)
            s = 'r = %s ' % R.format(r)
            for j, m in enumerate(ms):
                # print('m = %s' % str(m))
                s += "\n  implementation %d of %d: m = %s " % (j + 1, len(ms),
                                                               M.format(m))

                if make:
                    imp_dict = get_imp_as_recursive_dict(
                        M, m)  # , ignore_hidden=False)
                    print('imp dict: %r' % imp_dict)
                    context = {}
                    artifact = ndp_make(ndp, imp_dict, context)

                    print('artifact: %s' % artifact)

            tracer.log(s)

        if expect_nimp is not None:
            if expect_nimp != nimplementations:
                msg = 'Found wrong number of implementations'
                raise_desc(ExpectationsNotMet,
                           msg,
                           expect_nimp=expect_nimp,
                           nimplementations=nimplementations)


#     if expect_res is not None:
#         value = interpret_string(expect_res)
#         tracer.log('value: %s' % value)
#         res_expected = value.value
#         tu = get_types_universe()
#         # If it's a tuple of two elements, then we assume it's upper/lower bounds
#         if isinstance(value.unit, PosetProduct):
#             subs = value.unit.subs
#             assert len(subs) == 2, subs
#
#             lower_UR_expected, upper_UR_expected = subs
#             lower_res_expected, upper_res_expected = value.value
#
#             lower_bound = tu.get_embedding(lower_UR_expected, UR)[0](lower_res_expected)
#             upper_bound = tu.get_embedding(upper_UR_expected, UR)[0](upper_res_expected)
#
#             tracer.log('lower: %s <= %s' % (UR.format(lower_bound), UR.format(res)))
#             tracer.log('upper: %s <= %s' % (UR.format(upper_bound), UR.format(res)))
#
#             UR.check_leq(lower_bound, res)
#             UR.check_leq(res, upper_bound)
#         else:
#             # only one element: equality
#             UR_expected = value.unit
#             tu.check_leq(UR_expected, UR)
#             A_to_B, _B_to_A = tu.get_embedding(UR_expected, UR)
#
#             res_expected_f = A_to_B(res_expected)
#             try:
#                 UR.check_equal(res, res_expected_f)
#             except NotEqual as e:
#                 raise_wrapped(ExpectationsNotMet, e, 'res is different',
#                               res=res, res_expected=res_expected, compact=True)

    if plot:
        r = Report()
        if _exp_advanced:
            from mcdp_report.generic_report_utils import generic_report
            generic_report(r, dp, trace, annotation=None, axis0=(0, 0, 0, 0))
        else:
            f = r.figure()
            from mcdp_report.generic_report_utils import generic_plot
            generic_plot(f, space=UR, value=res)
            from mcdp_report.generic_report_utils import generic_report_trace
            generic_report_trace(r, ndp, dp, trace, out, do_movie=do_movie)

        out_html = os.path.join(out, 'report.html')
        logger.info('writing to %r' % out_html)
        r.to_html(out_html)

    if plot and imp:
        from mcdp_report_ndp_tests.test1 import GetValues
        from mcdp_report.gg_ndp import gvgen_from_ndp
        from mcdp_report.gdc import STYLE_GREENREDSYM
        from mcdp_report.gg_utils import gg_figure
        M = dp.get_imp_space()

        report_solutions = Report()
        for i, r in enumerate(res.minimals):
            ms = dp.get_implementations_f_r(fg, r)
            for j, m in enumerate(ms):

                imp_dict = get_imp_as_recursive_dict(M, m)
                images_paths = library.get_images_paths()
                image_source = ImagesFromPaths(images_paths)
                gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=upper, nl=1)

                setattr(ndp, '_hack_force_enclose', True)

                with report_solutions.subsection('sol-%s-%s' % (i, j)) as rr:
                    # Left right
                    gg = gvgen_from_ndp(ndp=ndp,
                                        style=STYLE_GREENREDSYM,
                                        image_source=image_source,
                                        plotting_info=gv,
                                        direction='LR')

                    gg_figure(rr,
                              'figure',
                              gg,
                              do_png=True,
                              do_pdf=True,
                              do_svg=False,
                              do_dot=False)

                    # Top-bottom
                    gg = gvgen_from_ndp(ndp=ndp,
                                        style=STYLE_GREENREDSYM,
                                        image_source=image_source,
                                        plotting_info=gv,
                                        direction='TB')

                    gg_figure(rr,
                              'figure2',
                              gg,
                              do_png=True,
                              do_pdf=True,
                              do_svg=False,
                              do_dot=False)

        out_html = os.path.join(out, 'report_solutions.html')
        logger.info('writing to %r' % out_html)
        report_solutions.to_html(out_html)
Esempio n. 17
0
File: test1.py Progetto: rusi/mcdp
def plot_different_solutions(libname, ndpname, query, out, upper=None):
    if not os.path.exists(out):
        os.makedirs(out)
    library = get_test_library(libname)
    #library.use_cache_dir(os.path.join(out, 'cache'))
    context = Context()
    ndp = library.load_ndp(ndpname, context)

    context = library._generate_context_with_hooks()
    ndp_labelled = get_labelled_version(ndp)
    dp0 = ndp_labelled.get_dp()
    if upper is not None:
        _, dpU = get_dp_bounds(dp0, nl=1, nu=upper)
        dp = dpU
    else:
        dp = dp0

    M = dp.get_imp_space()

    with open(os.path.join(out, 'ndp.txt'), 'w') as f:
        f.write(ndp.repr_long())
    with open(os.path.join(out, 'M.txt'), 'w') as f:
        f.write(M.repr_long())
    with open(os.path.join(out, 'dp.txt'), 'w') as f:
        f.write(dp.repr_long())
    with open(os.path.join(out, 'dp0.txt'), 'w') as f:
        f.write(dp0.repr_long())

    f = convert_string_query(ndp=ndp, query=query, context=context)

    report = Report()

    res = dp.solve(f)
    print('num solutions: %s' % len(res.minimals))
    for ri, r in enumerate(res.minimals):
        ms = dp.get_implementations_f_r(f, r)

        for j, m in enumerate(ms):
            imp_dict = get_imp_as_recursive_dict(M, m)
            logger.info(imp_dict)

            images_paths = library.get_images_paths()
            image_source = ImagesFromPaths(images_paths)
            gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=upper, nl=1)

            gg = gvgen_from_ndp(ndp=ndp,
                                style=STYLE_GREENREDSYM,
                                image_source=image_source,
                                plotting_info=gv)

            with report.subsection('%s-%s' % (ri, j)) as rr:
                gg_figure(rr,
                          'figure',
                          gg,
                          do_png=True,
                          do_pdf=False,
                          do_svg=False,
                          do_dot=False)

    fn = os.path.join(out, 'solutions.html')
    print('writing to %s' % fn)
    report.to_html(fn)
Esempio n. 18
0
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)