コード例 #1
0
class Ant_Frame(Generic_Main_Frame):

    def __init__(self, Parent,NbAgents):
        #frame with some additional ant capabilities
        Generic_Main_Frame.__init__(self,Parent,self.oneStep,Wtitle='Ants')
        self.startGround()
        self.LastTimeStep = 0
        self.Counter = 0
        #create population of agents
        self.Pop = [Ant('A%d' % IdNb) for IdNb in range(NbAgents)]
        self.PopSize = NbAgents
        self.Moves = 0  # counts the number of times agents have moved
        t = Thread(target=self.redraw)
        t.start()
          
    def startGround(self):
        """ the ground is a 2-D space representing the field where ants wander
        """
        self.Ground = Ground(self, Toric=True)
        self.Ground.scaleX = self.Ground.scaleY = LandSize   # Logical coordinates
        self.Ground.W = self.Ground.H = LandWindowSize      # Physical coordinates
        self.Ground.configure(width=self.Ground.W, height=self.Ground.H)
        self.Ground.pack(expand=Tkinter.YES,fill=Tkinter.BOTH)  # the window shows on the scren
           
    def oneStep(self):
        # this function is called back after each simulation step
        Land.evaporate()
        
        for agent in self.Pop:
            agent.moves()
        
        self.Moves += 1
        return 0
    
    def redraw(self):
        while 1:
            # the landscape is entirely redrawn
            self.Ground.erase() # supposedly destroys all objects on ground
            
            self.displayNodes(Land.Nodes)
            self.displayPheromons(Land.EdgesWithPheromons)
            sleep(1)
    

    def displayPheromons(self, edges):
        for edge in edges:
            coord1 = edge[0]
            coord2 = edge[1]
            self.Ground.create_line(coord1[0], coord1[1], coord2[0],coord2[1], fill="red", dash=(4, 4))

    def displayNodes(self, Nodes):
        for node in Nodes:
            coord = node.Position
            self.Ground.create_rectangle(coord[0]-2, coord[1]-2, coord[0]+2, coord[1]+2,outline='black',fill='gray50')
コード例 #2
0
class Ant_Frame(Generic_Main_Frame):
    def __init__(self, Parent, NbAgents):
        # frame with some additional ant capabilities
        Generic_Main_Frame.__init__(self, Parent, self.oneStep, Wtitle='Ants')
        self.startGround()
        self.LastTimeStep = 0
        self.Counter = 0
        # create population of agents
        self.Pop = [Ant('A%d' % IdNb) for IdNb in range(NbAgents)]
        self.PopSize = NbAgents
        self.Moves = 0  # counts the number of times agents have moved
        t = Thread(target=self.redraw)
        t.start()

    def startGround(self):
        """ the ground is a 2-D space representing the field where ants wander
        """
        self.Ground = Ground(self, Toric=True)
        self.Ground.scaleX = self.Ground.scaleY = LandSize  # Logical coordinates
        self.Ground.W = self.Ground.H = LandWindowSize  # Physical coordinates
        self.Ground.configure(width=self.Ground.W, height=self.Ground.H)
        self.Ground.pack(expand=Tkinter.YES, fill=Tkinter.BOTH)  # the window shows on the scren

    def oneStep(self):
        # this function is called back after each simulation step
        Land.evaporate()

        for agent in self.Pop:
            agent.moves()

        self.Moves += 1
        return 0

    def redraw(self):
        while 1:
            # the landscape is entirely redrawn
            self.Ground.erase()  # supposedly destroys all objects on ground

            self.displayNodes(Land.Nodes)
            self.displayPheromons(Land.EdgesWithPheromons)
            sleep(1)

    def displayPheromons(self, edges):
        for edge in edges:
            coord1 = edge[0]
            coord2 = edge[1]
            self.Ground.create_line(coord1[0], coord1[1], coord2[0], coord2[1], fill="red", dash=(4, 4))

    def displayNodes(self, Nodes):
        for node in Nodes:
            coord = node.Position
            self.Ground.create_rectangle(coord[0] - 2, coord[1] - 2, coord[0] + 2, coord[1] + 2, outline='black',
                                         fill='gray50')