Пример #1
0
def test_render_env(save_new_images=False):
    np.random.seed(100)
    oEnv = RailEnv(width=10, height=10, rail_generator=empty_rail_generator(), number_of_agents=0,
                   obs_builder_object=TreeObsForRailEnv(max_depth=2))
    oEnv.reset()
    oEnv.rail.load_transition_map('env_data.tests', "test1.npy")
    oRT = rt.RenderTool(oEnv, gl="PILSVG")
    oRT.render_env(show=False)
    checkFrozenImage(oRT, "basic-env.npz", resave=save_new_images)

    oRT = rt.RenderTool(oEnv, gl="PIL")
    oRT.render_env()
    checkFrozenImage(oRT, "basic-env-PIL.npz", resave=save_new_images)
Пример #2
0
 def render_env():
     render = rendertools.RenderTool(env, gl='PILSVG')
     render.render_env()
     bg_img = Image.fromarray(render.get_image()).convert('RGB')
     img = ImageTk.PhotoImage(bg_img)
     img.img = bg_img
     return img
Пример #3
0
 def new_env(self):
     """ Tell the view to update its graphics when a new env is created.
     """
     self.oRT = rt.RenderTool(self.editor.env, gl=self.sGL)
    def test_build_graph(self):

        manuel_graph = True
        if manuel_graph:
            specs = [[(8, 0), (1, 90), (1, 90), (6, 0), (1, 90), (8, 90),
                      (0, 0)],
                     [(1, 180), (0, 0), (0, 0), (1, 0), (0, 0), (1, 0),
                      (0, 0)],
                     [(8, 270), (1, 90), (1, 90), (2, 90), (1, 90), (2, 360),
                      (0, 0)],
                     [(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (1, 0), (0, 0)],
                     [(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)]]

            rail_shape = np.array(specs).shape

            env = rail_env.RailEnv(
                width=rail_shape[1],
                height=rail_shape[0],
                rail_generator=rail_generators.
                rail_from_manual_specifications_generator(specs),
                number_of_agents=1)
        else:
            env = rail_env.RailEnv(
                width=25,
                height=15,
                rail_generator=rail_generators.sparse_rail_generator())

        observations, info = env.reset()

        tg = transition_graph.TransitionGraph(env)
        g = tg.g

        plt = igraph.Plot()
        layout = [(v['y'] + np.random.randn() * 0.125,
                   v['x'] + np.random.randn() * 0.125) for v in g.vs]

        plt.add(g,
                layout=layout,
                margin=50,
                vertex_label=list(range(len(g.vs))),
                edge_label=list(range(len(g.es))))
        plt.redraw()
        with tempfile.NamedTemporaryFile() as f:
            plt.save(f.name)
            graph_img = Image.open(f.name)

        render = rendertools.RenderTool(env, gl='PILSVG')
        render.render_env()
        bg_img = Image.fromarray(render.get_image())

        graph_img.show(
        )  #uncomment for higher res graph images to inspect labels etc.

        minx, maxx, miny, maxy = min(g.vs['x']), max(g.vs['x']), min(
            g.vs['y']), max(g.vs['y'])
        envh, envw = env.height, env.width

        w, h = bg_img.size
        graph_img = graph_img.resize((int(
            (maxy - miny) / envw * w), int((maxx - minx) / envh * h)))
        bg_img = bg_img.resize(
            (int(w / envw * (envw + 1)), int(h / envh * (envh + 1))))

        bg_img.paste(graph_img,
                     (int(w / envw * (miny + .5)), int(h / envh *
                                                       (minx + .5))),
                     graph_img)
        bg_img.show()

        #import IPython
        #IPython.embed()
        print(
            f"edges that share common resources: {transition_graph.find_edges_that_share_resource(g)}"
        )
    def test_build_graph(self):

        manuel_graph = True
        if manuel_graph:
            specs = [[(8, 0), (1, 90), (1, 90), (6, 0), (1, 90), (7, 90)],
                     [(1, 180), (0, 0), (0, 0), (1, 0), (0, 0), (0, 0)],
                     [(8, 270), (1, 90), (1, 90), (2, 90), (1, 90), (7, 90)],
                     [(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
                     [(1, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)]]

            rail_shape = np.array(specs).shape

            env = rail_env.RailEnv(
                width=rail_shape[1],
                height=rail_shape[0],
                rail_generator=rail_generators.
                rail_from_manual_specifications_generator(specs),
                number_of_agents=1)
        else:
            env = rail_env.RailEnv(
                width=25,
                height=15,
                rail_generator=rail_generators.sparse_rail_generator())

        observations, info = env.reset()

        #env = rail_env.RailEnv(
        #       width=5,
        #      height=5,
        #     rail_generator=rail_generators.complex_rail_generator())

        #obs, info = env.reset()
        #obs, info = env.reset()
        #obs, info = env.reset()

        tg = transition_graph.TransitionGraph(env)
        g = tg.g

        plt = igraph.Plot()
        layout = [(v['y'] + np.random.randn() * 0.15,
                   v['x'] + np.random.randn() * 0.15) for v in g.vs]

        plt.add(g, layout=layout)
        plt.redraw()
        with tempfile.NamedTemporaryFile() as f:
            plt.save(f.name)
            graph_img = Image.open(f.name)

        render = rendertools.RenderTool(env, gl='PILSVG')
        render.render_env()
        bg_img = Image.fromarray(render.get_image())
        graph_img = graph_img.resize(bg_img.size)

        minx, maxx, miny, maxy = min(g.vs['x']), max(g.vs['x']), min(
            g.vs['y']), max(g.vs['y'])
        envh, envw = env.height, env.width

        w, h = bg_img.size
        graph_img = graph_img.resize((int(
            (maxy - miny) / envw * w), int((maxx - minx) / envh * h)))
        bg_img = bg_img.resize(
            (int(w / envw * (envw + 1)), int(h / envh * (envh + 1))))

        bg_img.paste(graph_img,
                     (int(w / envw * (miny + .5)), int(h / envh *
                                                       (minx + .5))),
                     graph_img)
        bg_img.show()
Пример #6
0
 def new_env(self):
     """ Tell the view to update its graphics when a new env is created.
     """
     self.oRT = rt.RenderTool(self.editor.env, gl=self.sGL, show_debug=True,
         screen_height=self.xyScreen[1], screen_width=self.xyScreen[0])