Exemple #1
0
    def main():

        win = turtle.Screen()
        win.bgcolor('black')
        win.setup(width=800, height=600)
        win.tracer(0, 0)
        win.listen()

        Application.rootWindow = win.getcanvas().master

        # TODO:
        # Lets add a population of agents, initialized to
        # random positions, with their own network.
        # Maybe we can see if competition changes anything
        # Also consider adding a pool of targets
        # and give each agent the information about the
        # closest available target.
        # agent = Agent(14, 6)
        agent = Agent(21, 9)
        target = Target()

        Application.debug = Entity()
        Application.spikes = Entity()

        controls = Text(200, 230, Application.controlText)
        counter = Text(200, 210, 'Target found 0 times.')

        Application.debug.shapesize(0.5, 0.5)
        Application.spikes.shapesize(0.5, 0.5)

        agent.setTarget(target)

        win.onkey(Application.registerClose, 'q')
        win.onkey(Application.enableSynapseDebug, 's')
        win.onkey(partial(Application.writeSynapses, agent), 'd')
        win.onkey(partial(Application.readSynapses, agent), 'l')
        win.onkey(partial(Application.setSpeed, 3), '3')
        win.onkey(partial(Application.setSpeed, 2), '2')
        win.onkey(partial(Application.setSpeed, 1), '1')
        win.onkey(partial(Application.enableNoisy, agent.net), 'n')
        win.onkey(agent.clear, 'c')
        win.onclick(target.goto)

        prev = time.clock_gettime(time.CLOCK_MONOTONIC)
        cur = prev

        debugAccum = 0.0
        updateAccum = 0.0

        # NOTE: We want this to be separate from realtime
        # because the agent should still get a reward if we
        # change the speed setting.
        agentRewardAccum = 1.0

        foundCount = 0

        while Application.run:

            # Reset update accum when we change
            # the speed, so that we instantly
            # do the next update at the correct
            # time interval
            if Application.speedChanged is True:
                Application.speedChanged = False
                updateAccum = 0.0

            cur = time.clock_gettime(time.CLOCK_MONOTONIC)
            delta = cur - prev
            debugAccum += delta
            updateAccum += delta

            Application.spikes.clearstamps()

            if debugAccum >= 1.0:
                if Application.synapseDebug:
                    Application.drawSynapseDebug(agent.net)
                debugAccum -= 1.0

            Application.drawSpikeDebug(agent.net)

            if agent.distance(target) < 30.0:
                target.onCollision()
                agent.reward(1 + agentRewardAccum)
                agentRewardAccum = 1.0
                agent.clear()

                foundCount += 1
                counter.setText('Target found ' + str(foundCount) + ' times.')

            # Agent did not find the taret in time
            # So we decrease the synaptic weights
            if agentRewardAccum <= 0:
                agent.reward(0.99)
                agentRewardAccum = 1.0

            screenSize = agent.getscreen().screensize()
            agentX = agent.xcor()
            agentY = agent.ycor()
            agentX = agentX if agentX < screenSize[0] - 30 else -agentX + 30
            agentX = agentX if agentX > -screenSize[0] + 30 else -agentX - 30
            agentY = agentY if agentY < screenSize[1] - 30 else -agentY + 30
            agentY = agentY if agentY > -screenSize[1] + 30 else -agentY - 30
            agent.penup()
            agent.goto(agentX, agentY)
            agent.pendown()

            if updateAccum >= Application.updateDelta:
                agent.update()
                updateAccum -= Application.updateDelta
                agentRewardAccum -= 0.00008  # Update reward

            win.update()
            prev = cur

        turtle.bye()