예제 #1
0
 def RenderState(self, world):
     self.mapRenderer.RenderObjectVisibility()
     self.mapRenderer.RenderSpaceMap()
     self.mapRenderer.RenderAgent(world.agent)
     #self.mapRenderer.RenderObjects() - only for dynamic worlds
     if Global.RenderVisibilityHistory:
         self.mapRenderer.RenderVisibilityHistory()
     else:
         self.mapRenderer.HideVisibilityHistory()
     
     self.wxCanvas.delete("infotxt")
     txt =  "Step:  " + str(world.step).zfill(6) + "\nTime:  " + Global.TimeToHumanFormat(True)
     self.txtTime = self.wxCanvas.create_text(1080, 5, text=txt, width=200, anchor=NW, tags="infotxt")
     strXY = "%.4f,%.4f" % (self.agent.x, self.agent.y)
     txt =  "Agent:  " + strXY
     nc = len(world.agent.intelligence.spaceMap.Layer.nodes)
     txt = txt + "\nEnergyLayer.nodeCount: " + str(nc)
     self.txtAgentInfo = self.wxCanvas.create_text(1300, 5, text=txt, width=200, anchor=NW, tags="infotxt")
     pa = self.agent.intelligence.processArea
     txt = "ProcessArea:\n" + "\n".join(self.agent.paText)
     self.txtPA = self.wxCanvas.create_text(1050, 50, text=txt, width=200, anchor=NW, tags="infotxt")
     ma = self.agent.intelligence.memoryArea
     txt = "MemoryArea:\n  "
     for phantom in ma.memoryPhantoms:
         txt = txt + phantom.ToString() + "\n  "  
     self.txtMA = self.wxCanvas.create_text(1050, 200, text=txt, width=400, anchor=NW, tags="infotxt")
     pf = self.agent.intelligence.perceptionField
     txt = "PerceptionField:\n  "
     for phantom in pf.environmentPhantoms:
         txt = txt + phantom.ToString() + "\n  "
     self.txtPF = self.wxCanvas.create_text(1050, 300, text=txt, width=400, anchor=NW, tags="infotxt")
     txt = "Log:\n  "
     for line in Global.logLines:
         txt = txt + line + "\n  "
     self.txtLog = self.wxCanvas.create_text(1050, 550, text=txt, width=450, anchor=NW, tags="infotxt")
     
     Global.LogData("nc", world.agent.intelligence.spaceMap.Layer.Status())
예제 #2
0
    def RenderToFile(self, world, filename, layers=[]):
        if "info" in layers:
            im = Image.new("RGB", (1500, 1020), (255, 2555, 255))
        else:
            im = Image.new("RGB", (1100, 1100), (255, 2555, 255))
        draw = ImageDraw.Draw(im)

        if "vh" in layers:
            for vObj in self.map.visibilityHistory:
                intensity = 255 - int(
                    255 * vObj.visibility * 1.0 / self.map.visibilityMaxEver)
                color = (intensity, intensity, intensity)
                x = (vObj.x -
                     Global.VisibilityHistoryArea / 2) * self.zoom + 10
                y = (vObj.y -
                     Global.VisibilityHistoryArea / 2) * self.zoom + 10
                length = Global.VisibilityHistoryArea * self.zoom
                draw.rectangle([x, y, x + length, y + length],
                               fill=color,
                               outline=None)

        for edge in self.map.edges:
            draw.line([
                self.zoom * edge.start.x + 10, self.zoom * edge.start.y + 10,
                self.zoom * edge.end.x + 10, self.zoom * edge.end.y + 10
            ],
                      fill=(0, 0, 0))
        if "wp" in layers:
            for wayPoint in self.map.wayPoints:
                x = wayPoint.x * self.zoom - 2 + 10
                y = wayPoint.y * self.zoom - 2 + 10
                draw.rectangle([x, y, x + 4, y + 4],
                               fill=(0, 0, 0),
                               outline=None)

        if "agent" in layers:
            lastPoint = self.map.agentMoves[0]
            points = self.map.agentMoves[1:]
            points.append(Point(self.agent.newX, self.agent.newY))
            for point in points:
                draw.line([
                    self.zoom * lastPoint.x + 10, self.zoom * lastPoint.y + 10,
                    self.zoom * point.x + 10, self.zoom * point.y + 10
                ],
                          fill=(255, 200, 200))
                lastPoint = point
            x = self.agent.x * self.zoom - 5 + 10
            y = self.agent.y * self.zoom - 5 + 10
            draw.rectangle([x, y, x + 10, y + 10],
                           fill=(255, 0, 0),
                           outline=None)
            agStart = (self.agent.dirAngle - pi / 2)
            for vc in self.agent.viewCones:
                if vc.angle == pi:
                    x = self.agent.x * self.zoom - round(
                        vc.distance * self.zoom) + 10
                    y = self.agent.y * self.zoom - round(
                        vc.distance * self.zoom) + 10
                    draw.ellipse([
                        x, y, x + round(2 * vc.distance * self.zoom),
                        y + round(2 * vc.distance * self.zoom)
                    ],
                                 outline=(255, 0, 0))
                else:
                    start = agStart + vc.angle
                    if start < 0: start = 2 * pi + start
                    if start > 2 * pi: start = start - 2 * pi
                    start = int(360 - (180.0 * start / pi))
                    end = agStart - vc.angle
                    if end < 0: end = 2 * pi + end
                    end = int(360 - (180.0 * end / pi))
                    x = int(self.agent.x * self.zoom -
                            round(vc.distance * self.zoom) + 10)
                    y = int(self.agent.y * self.zoom -
                            round(vc.distance * self.zoom) + 10)
                    draw.pieslice([
                        x, y, x + int(2 * vc.distance * self.zoom),
                        y + int(2 * vc.distance * self.zoom)
                    ],
                                  start,
                                  end,
                                  outline=(255, 0, 0))

        spaceMap = self.agent.intelligence.spaceMap
        elayer = spaceMap.Layer
        if "eps" in layers:
            for ep in elayer.energyPoints:
                x = ep.x * self.zoom  #- 10 + 10
                y = ep.y * self.zoom  #- 10 + 10
                draw.ellipse([x, y, x + 20, y + 20],
                             fill=None,
                             outline=(0, 200, 0))
        if "ovh" in layers:
            for obj in self.map.objects:
                x = obj.x * self.zoom - 5 + 10
                y = obj.y * self.zoom - 5 + 10
                intensity = 255 - int(255 * obj.trainHistory * 1.0 / max(
                    Global.MinPositiveNumber, spaceMap.maxTrained))
                color = (intensity, intensity, intensity)
                draw.rectangle([x, y, x + 10, y + 10],
                               fill=color,
                               outline=(0, 0, 0))
        elif "ov" in layers:
            for obj in self.map.objects:
                x = obj.x * self.zoom - 5 + 10
                y = obj.y * self.zoom - 5 + 10
                if obj.visibility > 0:
                    if obj.type.name == "Waypoint":
                        draw.rectangle([x, y, x + 10, y + 10],
                                       fill=(100, 150, 170),
                                       outline=(0, 200, 0))
                    else:
                        draw.rectangle([x, y, x + 10, y + 10],
                                       fill=(100, 150, 255),
                                       outline=None)
                else:
                    if obj.type.name == "Waypoint":
                        draw.rectangle([x, y, x + 10, y + 10],
                                       fill=(0, 0, 170),
                                       outline=(0, 200, 0))
                    else:
                        draw.rectangle([x, y, x + 10, y + 10],
                                       fill=(0, 0, 255),
                                       outline=None)
        else:
            for obj in self.map.objects:
                x = obj.x * self.zoom - 5 + 10
                y = obj.y * self.zoom - 5 + 10
                if obj.type.name == "Waypoint":
                    draw.rectangle([x, y, x + 10, y + 10],
                                   fill=(0, 0, 170),
                                   outline=(0, 200, 0))
                else:
                    draw.rectangle([x, y, x + 10, y + 10],
                                   fill=(0, 0, 255),
                                   outline=None)

        for place in elayer.places:
            if place.parent == None: continue
            x = (place.x - place.range) * self.zoom + 10
            y = (place.y - place.range) * self.zoom + 10
            x2 = (place.x + place.range) * self.zoom + 10
            y2 = (place.y + place.range) * self.zoom + 10
            draw.ellipse([x, y, x2, y2], fill=None, outline=(0, 0, 128))
            x = place.x * self.zoom - 5 + 10
            y = place.y * self.zoom - 5 + 10
            draw.rectangle([x, y, x + 10, y + 10],
                           fill=None,
                           outline=(0, 0, 128))
            for node in place.nodes:
                draw.line([
                    x + 5, y + 5, self.zoom * node.x + 10,
                    self.zoom * node.y + 10
                ],
                          fill=(0, 0, 128))

        for node in elayer.nodes:
            x = node.x * self.zoom - 2 + 10
            y = node.y * self.zoom - 2 + 10
            draw.rectangle([x, y, x + 4, y + 4],
                           fill=(0, 180, 0),
                           outline=None)

        if "ovh" in layers:
            for obj in self.map.objects:
                x = obj.x * self.zoom - 5 + 10
                y = obj.y * self.zoom - 5 + 10
                draw.text([x + 5, y + 10],
                          str(obj.trainHistory),
                          font=self.font,
                          fill=(0, 0, 0))

        if "info" in layers:
            draw.text([1080, 5],
                      "Step:  " + str(world.step).zfill(6),
                      font=self.font,
                      fill=(0, 0, 0))
            draw.text([1080, 20],
                      "Time:  " + Global.TimeToHumanFormat(True),
                      font=self.font,
                      fill=(0, 0, 0))
            strXY = "%.4f,%.4f" % (self.agent.x, self.agent.y)
            draw.text([1300, 5],
                      "Agent:  " + strXY,
                      font=self.font,
                      fill=(0, 0, 0))
            draw.text([1300, 20],
                      "EnergyLayer.nodeCount: " + str(len(elayer.nodes)),
                      font=self.font,
                      fill=(0, 0, 0))
            txt = self.agent.paText
            draw.text([1050, 50],
                      "ProcessArea:",
                      font=self.font,
                      fill=(0, 0, 0))
            ypos = 50
            for t in txt:
                ypos = ypos + 14
                draw.text([1050, ypos], t, font=self.font, fill=(0, 0, 0))
            ma = self.agent.intelligence.memoryArea
            draw.text([1050, 200],
                      "MemoryArea:",
                      font=self.font,
                      fill=(0, 0, 0))
            ypos = 200
            for phantom in ma.memoryPhantoms:
                ypos = ypos + 15
                draw.text([1050, ypos],
                          " " + phantom.ToString(),
                          font=self.font,
                          fill=(0, 0, 0))
            pf = self.agent.intelligence.perceptionField
            draw.text([1050, 300],
                      "PerceptionField:",
                      font=self.font,
                      fill=(0, 0, 0))
            ypos = 300
            for phantom in pf.environmentPhantoms:
                ypos = ypos + 15
                draw.text([1050, ypos],
                          " " + phantom.ToString(),
                          font=self.font,
                          fill=(0, 0, 0))
            draw.text([1050, 500], "Log:", font=self.font, fill=(0, 0, 0))
            ypos = 500
            for line in Global.logLines:
                ypos = ypos + 15
                draw.text([1050, ypos],
                          "  " + line,
                          font=self.font,
                          fill=(0, 0, 0))
        else:
            draw.text([10, 1020],
                      "Step: " + str(world.step).zfill(6),
                      font=self.font,
                      fill=(0, 0, 0))
            draw.text([400, 1020],
                      "Time: " + Global.TimeToHumanFormat(True),
                      font=self.font,
                      fill=(0, 0, 0))

        im.save(filename, "PNG")