コード例 #1
0
ファイル: reports.py プロジェクト: AndreaCensi/yc1304
def report_servo1(processed, area_graphs):
    r = Report('servo1')
    
    nmap = processed['nmap']
    servo = processed['servo']
    centroid = processed['centroid']
    robot = processed['robot']
    
    with r.subsection('u', robust=False) as s:
        commands = [x['u'] for x in servo]
        vels = map(robot.debug_get_vel_from_commands, commands)
        vels = map(se2_project_from_se3, vels)
        repsec_servo1_generic_vel_field(s, 'u', centroid, nmap, vels,
                                        normalize=True, area_graphs=area_graphs)
        
    if 'u_raw' in servo[0]:
        with r.subsection('u_raw', robust=True) as s:
            commands = [x['u_raw'] for x in servo]
            vels = map(robot.debug_get_vel_from_commands, commands)
            vels = map(se2_project_from_se3, vels)
            repsec_servo1_generic_vel_field(s, 'u_raw', centroid, nmap, vels,
                                            normalize=True, area_graphs=area_graphs)

    if 'descent' in servo[0]:
        with r.subsection('descent', robust=True) as s:
            commands = [x['descent'] for x in servo]
            vels = map(robot.debug_get_vel_from_commands, commands)
            vels = map(se2_project_from_se3, vels)
            repsec_servo1_generic_vel_field(s, 'descent', centroid, nmap, vels,
                                            normalize=False, area_graphs=area_graphs)
         
    return r
コード例 #2
0
ファイル: optimization.py プロジェクト: rusi/mcdp
    def get_tree_report(self):

        r = Report()
        with r.subsection('regular') as rr:
            gg = self.draw_tree_get_tree_expand()
            gg_figure(rr, 'tree', gg, do_png=True, do_dot=False, do_svg=False)

        with r.subsection('compact') as rr:
            gg = self.draw_tree_get_tree_compact()
            gg_figure(r, 'tree', gg, do_png=True, do_dot=False, do_svg=False)
        return r
コード例 #3
0
def aer_stats_freq_meat(log, pipeline):
    events = aer_pipeline_transitions1_all(log, pipeline)

    r = Report('index')

    with r.subsection('all') as sub:
        report_band(sub, events, min_f=20.0, max_f=3000.0)

    with r.subsection('high') as sub:
        report_band(sub, events, min_f=500.0, max_f=3000.0)

    return r
コード例 #4
0
ファイル: meat.py プロジェクト: AndreaCensi/rcl
def aer_stats_freq_meat(log, pipeline):
    events = aer_pipeline_transitions1_all(log, pipeline)
    
    r = Report('index')
        
    with r.subsection('all') as sub:
        report_band(sub, events, min_f=20.0, max_f=3000.0)

    with r.subsection('high') as sub:
        report_band(sub, events, min_f=500.0, max_f=3000.0)
            
    return r
コード例 #5
0
def report_dds_geometry(id_discdds, tolerance):
    dds = get_conftools_discdds().instance(id_discdds)
    r = Report('dds_geometry-%s-%s' % (id_discdds, tolerance))
    ds = DiffeoStructure(dds, tolerance=tolerance)
    with r.subsection('display') as r:
        ds.display(r)
    
    with r.subsection('show_reduction_steps') as r:
        ds.show_reduction_steps(r, max_nsteps=5)
        
    with r.subsection('show_reduction') as r:
        ds.show_reduction(r)
        
    return r
コード例 #6
0
def report_dds_geometry(id_discdds, tolerance):
    dds = get_conftools_discdds().instance(id_discdds)
    r = Report('dds_geometry-%s-%s' % (id_discdds, tolerance))
    ds = DiffeoStructure(dds, tolerance=tolerance)
    with r.subsection('display') as r:
        ds.display(r)

    with r.subsection('show_reduction_steps') as r:
        ds.show_reduction_steps(r, max_nsteps=5)

    with r.subsection('show_reduction') as r:
        ds.show_reduction(r)

    return r
コード例 #7
0
ファイル: plot_approximations.py プロジェクト: rusi/mcdp
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)
コード例 #8
0
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)
コード例 #9
0
ファイル: consistency_test.py プロジェクト: ajaycharan/diffeo
def test_consistency_uncertainty():
    print 'here'
    pass
    contracts.disable_all()
    symdds = 'sym-dpchain1-120'
    print('instancing dds %s' % symdds)
    dds = get_conftools_discdds().instance(symdds)
    shape = dds.get_shape()
    d1f = dds.actions[0].get_diffeo2d_forward()
    d1b = dds.actions[0].get_diffeo2d_backward()
    
    fb = Diffeomorphism2D.compose(d1f, d1b)
    bf = Diffeomorphism2D.compose(d1b, d1f)
    identity = Diffeomorphism2D.identity(shape)
    print Diffeomorphism2D.distance_L2_infow(d1f, identity)
    print Diffeomorphism2D.distance_L2_infow(d1b, identity)
    print Diffeomorphism2D.distance_L2_infow(fb, identity)
    print Diffeomorphism2D.distance_L2_infow(bf, identity)

    action = dds.actions[0]
    action2 = consistency_based_uncertainty(action, None)

    r = Report(symdds)
    r.text('symdds', symdds)
    with r.subsection('action') as sub:
        action.display(sub)
    with r.subsection('action2') as sub:
        action2.display(sub)
#         
#     with r.subsection('misc') as sub:
#         d = d1f.get_discretized_diffeo()
#         f = sub.figure()
#         f.array_as_image('d0', d[:, :, 0])
#         f.array_as_image('d1', d[:, :, 1])
#         
        
#     with r.subsection('d1f') as sub:
#         d1f.display(sub)
#     with r.subsection('d1b') as sub:
#         d1b.display(sub)
# 
#     with r.subsection('fb') as sub:
#         fb.display(sub)
#     with r.subsection('bf') as sub:
#         bf.display(sub)
    
    r.to_html('test_consistency_uncertainty.html')
コード例 #10
0
ファイル: test1.py プロジェクト: AndreaCensi/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)
            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)
コード例 #11
0
ファイル: meat.py プロジェクト: AndreaCensi/rcl
def aer_stats_events_meat(log):
#    events = collect_all(aer_load_log_generic(log))
    events = aer_raw_events_from_file_all(log)
    hist = aer_histogram(events)
    _, coords = md_argmax(hist)

    r = Report('index')    
    with r.subsection('sub') as sub:
        report_for_one(sub, events, coords)
    return r
コード例 #12
0
ファイル: meat.py プロジェクト: kpykc/rcl
def aer_stats_events_meat(log):
    #    events = collect_all(aer_load_log_generic(log))
    events = aer_raw_events_from_file_all(log)
    hist = aer_histogram(events)
    _, coords = md_argmax(hist)

    r = Report('index')
    with r.subsection('sub') as sub:
        report_for_one(sub, events, coords)
    return r
コード例 #13
0
ファイル: report_agent_imp.py プロジェクト: afcarl/tmdp
def report_agent(res, pomdp):
    agent = res['agent']

    r = Report()

    f = r.figure()

    p_p0 = pomdp.get_start_dist_dist()
    for i, (p0, _) in enumerate(p_p0.items()):
        with f.plot('p0-%d' % i) as pylab:
            pomdp.display_state_dist(pylab, p0)
            turn_all_axes_off(pylab)

    with r.subsection('states') as sub:
        agent.report_states(sub)

    with r.subsection('transitions') as sub:
        agent.report_transitions(sub)

    return r
コード例 #14
0
ファイル: ddsl_show_stream.py プロジェクト: ajaycharan/diffeo
def make_stream_report(id_stream, nsamples):
    config = get_diffeo2ddslearn_config()
    stream = config.streams.instance(id_stream)
    r = Report(id_stream)
    data = itertools.islice(stream.read_all(), nsamples)

    for i, log_item in enumerate(data):
        with r.subsection('log_item%d' % i) as sub:
            log_item.display(sub)

    return r
コード例 #15
0
def make_stream_report(id_stream, nsamples):
    config = get_diffeo2ddslearn_config()
    stream = config.streams.instance(id_stream)
    r = Report(id_stream)
    data = itertools.islice(stream.read_all(), nsamples)
    
    for i, log_item in enumerate(data):
        with r.subsection('log_item%d' % i) as sub:
            log_item.display(sub)
         
    return r
コード例 #16
0
def report_ndp1(ndp):
    r = Report()

    gg = gvgen_from_ndp(ndp)
    gg_figure(r, 'graph', gg)

    styles = ['greenred', 'clean', 'greenredsym']
    for style in styles:
        with r.subsection(style) as r2:
            gg = gvgen_from_ndp(ndp, style=style)
            gg_figure(r2, 'graph', gg)

    return r
コード例 #17
0
ファイル: report_agent_imp.py プロジェクト: AndreaCensi/tmdp
def report_agent(res, pomdp):
    agent = res['agent']


    r = Report()

    f = r.figure()

    p_p0 = pomdp.get_start_dist_dist()
    for i, (p0, _) in enumerate(p_p0.items()):
        with f.plot('p0-%d' % i) as pylab:
            pomdp.display_state_dist(pylab, p0)
            turn_all_axes_off(pylab)

    with r.subsection('states') as sub:
        agent.report_states(sub)

    with r.subsection('transitions') as sub:
        agent.report_transitions(sub)


    return r
コード例 #18
0
ファイル: report.py プロジェクト: AndreaCensi/mcdp
def report_ndp1(ndp):
    r = Report()

    gg = gvgen_from_ndp(ndp)
    gg_figure(r, 'graph', gg)

    styles = ['greenred', 'clean', 'greenredsym']
    for style in styles:
        with r.subsection(style) as r2:
            gg = gvgen_from_ndp(ndp, style=style)
            gg_figure(r2, 'graph', gg)

    return r
コード例 #19
0
ファイル: reports.py プロジェクト: AndreaCensi/yc1304
def report_raw_display(processed):
    r = Report('raw_display')
    f = r.figure()

    centroid = processed['centroid']
    nmap = processed['nmap']
    xy = processed['nmap'].get_R2_points() 

    caption = "Raw trajectory and selected points"
    with f.plot('sparse_xy', caption=caption) as pylab:
        xy = np.array(xy)
        pylab.plot(xy[:, 0], xy[:, 1], 'k+')
        pylab.plot(centroid[0], centroid[1], 'go')
        nmap.plot_points(pylab)
        pylab.axis('equal')

    with r.subsection('nmap') as n:
        processed['nmap'].display(n) 

    return r
コード例 #20
0
ファイル: reports.py プロジェクト: AndreaCensi/yc1304
def report_servo_details(servo_agent, processed, nsamples=6):
    r = Report('servo_details')

    nmap = processed['nmap']
    y_goal = processed['y_goal']
    robot = processed['robot']

    for i in range(nsamples):
        j = np.random.randint(len(nmap.data))
        y0 = nmap.get_observations_at(j)
        servo = processed['servo'][j]
        with r.subsection('sample%s' % i) as r_i:
            f = r.figure(cols=4)
              
            with f.plot('map') as pylab:
                nmap.plot_points(pylab)
                vel = robot.debug_get_vel_from_commands(servo['u'])
                nmap.plot_vel_at_index(pylab, j, se2_project_from_se3(vel), length=0.05)

            with r_i.subsection('query') as rq:
                servo_agent.display_query(rq, observations=y0, goal=y_goal)
    
    return r
コード例 #21
0
def report(res):

    r = Report()

    dataL = res['dataL']
    dataU = res['dataU']

    what_to_plot_res = dict(total_cost="USD", total_mass='kg')
    what_to_plot_fun = dict(endurance="hour", extra_payload="g")

    queries = dataL['queries']
    endurance = [q['endurance'] for q in queries]

    def get_value(data, field):
        for res in data['results']:
            a = to_numpy_array({field: 'kg'}, res)

            if len(a):
                a = min(a[field])
            else:
                a = None
            yield a

    from matplotlib import pylab
    ieee_fonts_zoom3(pylab)

    markers = dict(markeredgecolor='none',
                   markerfacecolor='black',
                   markersize=6,
                   marker='o')
    LOWER2 = dict(color='orange', linewidth=4, linestyle='-', clip_on=False)
    UPPER2 = dict(color='purple', linewidth=4, linestyle='-', clip_on=False)
    LOWER2.update(markers)
    UPPER2.update(markers)
    color_resources = '#700000'
    color_functions = '#007000'

    fig = dict(figsize=(4.5, 4))

    with r.plot('total_mass', **fig) as pylab:
        ieee_spines_zoom3(pylab)
        total_massL = np.array(list(get_value(dataL, 'total_mass')))
        total_massU = np.array(list(get_value(dataU, 'total_mass')))
        print endurance
        print total_massL, total_massU
        pylab.plot(endurance, total_massL, **LOWER2)
        pylab.plot(endurance, total_massU, **UPPER2)
        set_axis_colors(pylab, color_functions, color_resources)
        pylab.xlabel('endurance [hours]')
        pylab.ylabel('total_mass [kg]')

    return r

    print('Plotting lower')
    with r.subsection('lower') as rL:
        plot_all_directions(rL,
                            queries=dataL['queries'],
                            results=dataL['results'],
                            what_to_plot_res=what_to_plot_res,
                            what_to_plot_fun=what_to_plot_fun)

    print('Plotting upper')
    with r.subsection('upper') as rU:
        plot_all_directions(rU,
                            queries=dataU['queries'],
                            results=dataU['results'],
                            what_to_plot_res=what_to_plot_res,
                            what_to_plot_fun=what_to_plot_fun)

    return r
コード例 #22
0
ファイル: test1.py プロジェクト: 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)
コード例 #23
0
ファイル: drone_unc1.py プロジェクト: AndreaCensi/mcdp
def report(res):

    r = Report()

    dataL = res['dataL']
    dataU = res['dataU']

    what_to_plot_res = dict(total_cost="USD", total_mass='kg')
    what_to_plot_fun = dict(endurance="hour", extra_payload="g")

    queries = dataL['queries']
    endurance = [q['endurance'] for q in queries]

    def get_value(data, field):
        for res in data['results']:
            a = to_numpy_array({field: 'kg'}, res)

            if len(a):
                a = min(a[field])
            else:
                a = None
            yield a


    from matplotlib import pylab
    ieee_fonts_zoom3(pylab)


    markers = dict(markeredgecolor='none', markerfacecolor='black', markersize=6,
                   marker='o')
    LOWER2 = dict(color='orange', linewidth=4, linestyle='-', clip_on=False)
    UPPER2 = dict(color='purple', linewidth=4, linestyle='-', clip_on=False)
    LOWER2.update(markers)
    UPPER2.update(markers)
    color_resources = '#700000'
    color_functions = '#007000'


    fig = dict(figsize=(4.5, 4))

    with r.plot('total_mass', **fig) as pylab:
        ieee_spines_zoom3(pylab)
        total_massL = np.array(list(get_value(dataL, 'total_mass')))
        total_massU = np.array(list(get_value(dataU, 'total_mass')))
        print endurance
        print total_massL, total_massU
        pylab.plot(endurance, total_massL, **LOWER2)
        pylab.plot(endurance, total_massU, **UPPER2)
        set_axis_colors(pylab, color_functions, color_resources)
        pylab.xlabel('endurance [hours]')
        pylab.ylabel('total_mass [kg]')

    return r


    print('Plotting lower')
    with r.subsection('lower') as rL:
        plot_all_directions(rL,
                            queries=dataL['queries'],
                            results=dataL['results'],
                            what_to_plot_res=what_to_plot_res,
                            what_to_plot_fun=what_to_plot_fun)
    
    print('Plotting upper')
    with r.subsection('upper') as rU:
        plot_all_directions(rU,
                            queries=dataU['queries'],
                            results=dataU['results'],
                            what_to_plot_res=what_to_plot_res,
                            what_to_plot_fun=what_to_plot_fun)

    return r
コード例 #24
0
ファイル: solve_meat.py プロジェクト: AndreaCensi/mcdp
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)
コード例 #25
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)
コード例 #26
0
def opt_basic_1_long():
    libnames = ['actuation']
    library = get_test_library2(libnames)

    outdir = 'out/opt_basic_1'

    library.use_cache_dir(os.path.join(outdir, 'cache'))

    options = library.list_spec(SPEC_MODELS)
    options.remove('RigidBodyAssignID')
    options.remove('YoubotBaseBattery')
    options.remove('YoubotBaseComputer')
    options.remove('AdafruitDCHatCore')

    # XXX: this should not be case-sensitive
    #     options.remove('raspberryPI2')  # does not compile
    #     options.remove('DaguChassis')
    options.remove('IRobotCreate')
    options.remove('YoubotBase')
    options.remove('duckiebot1')
    options.remove('duckiebot1_flatten')
    print('libraries: %s' % libnames)
    print('options: %s' % options)

    initial_string = """
        mcdp {    
            provides motion [`Motion]
            
            assign_id = instance abstract `RigidBodyAssignID
            
            add_budget = instance abstract mcdp {    
                provides budget1 [USD]
                provides budget2 [USD]
                provides budget3 [USD]
                provides budget4 [USD]
                provides budget5 [USD]
                provides budget6 [USD]
            
                requires budget [USD]
            
                required budget >= (
                    provided budget1 + 
                    provided budget2 +
                    provided budget3 +
                    provided budget4 +
                    provided budget5 +   
                    provided budget6 
                )
            }
            
            requires budget >= budget required by add_budget
            requires ac [`AC_charging]
        }
    """

    poset = library.parse_poset

    flabels, F0s, f0s = zip(*(('motion', poset('`Motion'), ('rb1', 0.1,
                                                            600.0)), ))

    rlabels, R0s, r0s = zip(*(
        ('ac', poset('`AC_Charging'), (('AC_power', 'TypeA', 'v110', 'f50',
                                        200.0), 3 * 3600.0)),
        ('budget', poset('USD'), 1000.0),
    ))

    initial = library.parse_ndp(initial_string)

    opt = Optimization(library=library,
                       options=options,
                       flabels=flabels,
                       F0s=F0s,
                       f0s=f0s,
                       rlabels=rlabels,
                       R0s=R0s,
                       r0s=r0s,
                       initial=initial)

    i = 0
    maxit = 500
    out_draw_tree = os.path.join(outdir, 'optim_tree')
    if os.path.exists(out_draw_tree):
        shutil.rmtree(out_draw_tree)

    while not opt.is_done() and i <= maxit:
        opt.step()
        opt.print_status()
        opt.draw_tree(out_draw_tree)
        i += 1

    opt.print_status()

    r = Report()

    for i, state in enumerate(opt.done):
        ndp = state.get_current_ndp()
        plot_ndp(r, 'done%d' % i, ndp, library)

    for i, state in enumerate(opt.states):
        ndp = state.get_current_ndp()
        with r.subsection('open%d' % i) as s:
            plot_ndp(s, 'open%d' % i, ndp, library)
            msg = 'ur: %s' % state.ur
            msg += '\n num_connection_options: %s' % state.num_connection_options
            s.text('info', msg)

    for i, state in enumerate(opt.abandoned):
        with r.subsection('abandoned%d' % i) as s:
            ndp = state.get_current_ndp()
            plot_ndp(s, 'abandoned%d' % i, ndp, library)
            msg = getattr(state, 'msg', '(no message)')

            msg += '\n ur: %s' % state.ur
            msg += '\n num_connection_options: %s' % state.num_connection_options
            s.text('info', msg)

    fn = os.path.join(outdir, 'opt_basic_1.html')
    print(fn)
    r.to_html(fn)