Beispiel #1
0
    def __init__(self):
        ShowBase.__init__(self)

        # Override defaults
        self.disableMouse()
        self.setBackgroundColor(VBase3(160, 200, 150) / 255.0)
        self.setFrameRateMeter(True)

        # Lights
        dlight = DirectionalLight("dlight")
        dlnp = self.render.attachNewNode(dlight)
        dlnp.setHpr(180.0, -70.0, 0)
        self.render.setLight(dlnp)

        alight = AmbientLight("alight")
        alnp = self.render.attachNewNode(alight)
        alight.setColor(VBase4(0.4, 0.4, 0.4, 1))
        self.render.setLight(alnp)

        # Collision traverser
        self.cTrav = CollisionTraverser("collisionTraverser")
        #self.cTrav.showCollisions(self.render)

        # Collision handlers
        self.carCollisionHandler = CollisionHandlerEvent()
        self.carCollisionHandler.addInPattern("%fn-into-%in")

        # Camera controls
        self.cameraController = CameraController(self, 400, math.pi / 4.0,
                                                 math.pi / 4.0)
        #self.cameraController = CameraController(self, 300, -math.pi, math.pi / 4.0)

        # Load the track
        #self.track = self.loader.loadModel("models/trackMotegi")
        self.track = self.loader.loadModel("models/trackValencia")
        checkpointsCollision = self.track.find("checkpoints").node()
        checkpointsCollision.setIntoCollideMask(BitMask32(0xF0))
        self.numCheckpoints = checkpointsCollision.getNumSolids()
        self.track.reparentTo(self.render)

        # Load the car
        #self.car = KeyboardCar(self)
        self.car = NeuralNetworkCar(self)
        self.cameraController.follow(self.car.getNodePath())
        #self.cameraController.setTarget(self.car.getNodePath())

        # Reposition the car
        #self.car.getNodePath().setH(180.0)

        # Register car collisions with track
        self.cTrav.addCollider(self.car.carCollider, self.carCollisionHandler)
        self.accept("carCollider-into-trackCollision", self.car.onCrash)
        self.accept("carCollider-into-checkpoints", self.car.onCheckpoint)

        # State logger
        self.loggingActive = False
        self.log = []
        self.accept("l", self.toggleStateLogger)
Beispiel #2
0
    def __init__(self):
        ShowBase.__init__(self)

        # Override defaults
        self.disableMouse()
        self.setBackgroundColor(0.7, 0.7, 0.7)
        self.setFrameRateMeter(True)

        # Lights
        dlight = DirectionalLight("dlight")
        dlnp = self.render.attachNewNode(dlight)
        dlnp.setHpr(180.0, -70.0, 0)
        self.render.setLight(dlnp)

        alight = AmbientLight("alight")
        alnp = self.render.attachNewNode(alight)
        alight.setColor(VBase4(0.4, 0.4, 0.4, 1))
        self.render.setLight(alnp)

        # Collisions
        self.cTrav = CollisionTraverser("collisionTraverser")
        self.cTrav.showCollisions(self.render)

        # Camera controls
        self.cameraController = CameraController(self, 200, math.pi / 4.0,
                                                 math.pi / 4.0)
        #self.cameraController = CameraController(self, 200, -math.pi, math.pi / 4.0)

        # Load the track
        self.track = self.loader.loadModel("models/trackMotegi")
        self.track.reparentTo(self.render)

        # Load the car
        self.car = KeyboardController(self)
Beispiel #3
0
class SmartCar(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)

        # Override defaults
        self.disableMouse()
        self.setBackgroundColor(VBase3(160, 200, 150) / 255.0)
        self.setFrameRateMeter(True)

        # Lights
        dlight = DirectionalLight("dlight")
        dlnp = self.render.attachNewNode(dlight)
        dlnp.setHpr(180.0, -70.0, 0)
        self.render.setLight(dlnp)

        alight = AmbientLight("alight")
        alnp = self.render.attachNewNode(alight)
        alight.setColor(VBase4(0.4, 0.4, 0.4, 1))
        self.render.setLight(alnp)

        # Collision traverser
        self.cTrav = CollisionTraverser("collisionTraverser")
        #self.cTrav.showCollisions(self.render)

        # Collision handlers
        self.carCollisionHandler = CollisionHandlerEvent()
        self.carCollisionHandler.addInPattern("%fn-into-%in")

        # Camera controls
        self.cameraController = CameraController(self, 250, math.pi / 4.0, math.pi / 4.0)
        #self.cameraController = CameraController(self, 300, -math.pi, math.pi / 4.0)

        # Load the track
        self.track = self.loader.loadModel("models/trackMotegi")
        checkpointsCollision = self.track.find("checkpoints").node()
        checkpointsCollision.setIntoCollideMask(BitMask32(0xF0))
        self.numCheckpoints = checkpointsCollision.getNumSolids()
        self.track.reparentTo(self.render)

        # Load the car
        #self.car = KeyboardCar(self)
        self.car = NeuralNetworkCar(self)
        self.cameraController.follow(self.car.getNodePath())
        #self.cameraController.setTarget(self.car.getNodePath())

        # Reposition the car
        #self.car.getNodePath().setH(180.0)

        # Register car collisions with track
        self.cTrav.addCollider(self.car.carCollider, self.carCollisionHandler)
        self.accept("carCollider-into-trackCollision", self.car.onCrash)
        self.accept("carCollider-into-checkpoints", self.car.onCheckpoint)

        # State logger
        self.loggingActive = False
        self.log = []
        self.accept("l", self.toggleStateLogger)

    def toggleStateLogger(self):
        if not self.loggingActive:
            print "LOG: started."
            self.taskMgr.add(self.stateLogger, "StateLoggerTask")
        else:
            self.taskMgr.remove("StateLoggerTask")
            self.writeLog("data/log.csv")
        self.loggingActive = not self.loggingActive

    def stateLogger(self, task):
        row = [self.car.speed, self.car.steerAngle]
        row.extend(self.car.sensorDistances)
        row.append(float(self.car.arrowUpDown))
        row.append(float(self.car.arrowLeftDown))
        row.append(float(self.car.arrowRightDown))
        self.log.append(row)
        return task.again

    def writeLog(self, filename):
        with open(filename, 'wb') as file:
            wr = csv.writer(file)
            wr.writerows(self.log)
        print "LOG: written."
    def __init__(self):
        ShowBase.__init__(self)

        # Override defaults
        self.disableMouse()
        self.setBackgroundColor(VBase3(160, 200, 150) / 255.0)
        self.setFrameRateMeter(True)

        # Lights
        dlight = DirectionalLight("dlight")
        dlnp = self.render.attachNewNode(dlight)
        dlnp.setHpr(180.0, -70.0, 0)
        self.render.setLight(dlnp)

        alight = AmbientLight("alight")
        alnp = self.render.attachNewNode(alight)
        alight.setColor(VBase4(0.4, 0.4, 0.4, 1))
        self.render.setLight(alnp)

        # Collision traverser
        self.cTrav = CollisionTraverser("collisionTraverser")

        # Collision handlers
        self.carCollisionHandler = CollisionHandlerEvent()
        self.carCollisionHandler.addInPattern("%fn-into-%in")

        # Camera controls
        self.cameraController = CameraController(self, 400, math.pi / 4.0,
                                                 math.pi / 4.0)

        # Load the track
        self.track = self.loader.loadModel("models/trackMotegi")
        #self.track = self.loader.loadModel("models/trackValencia")
        checkpointsCollision = self.track.find("checkpoints").node()
        checkpointsCollision.setIntoCollideMask(BitMask32(0xF0))
        self.numCheckpoints = checkpointsCollision.getNumSolids()
        self.track.reparentTo(self.render)

        # Neuroevolution
        self.generationCount = 0
        self.generationSize = 20
        self.cars = []
        for i in range(self.generationSize):
            car = NeuroevolutionCar(self, i)
            # Register car collisions with track
            self.cTrav.addCollider(car.carCollider, self.carCollisionHandler)
            self.accept("carCollider{}-into-trackCollision".format(i),
                        car.onCrash)
            self.accept("carCollider{}-into-checkpoints".format(i),
                        car.onCheckpoint)
            self.cars.append(car)

        # Run learning task
        self.taskMgr.add(self.neuroevolution, "NeuroevolutionTask")

        # DEBUG
        self.txtGen = OnscreenText(text='',
                                   pos=(0.0, -0.04),
                                   scale=0.05,
                                   align=TextNode.ALeft,
                                   fg=(1, 1, 1, 1),
                                   bg=(0, 0, 0, .4))
        self.txtGen.reparentTo(self.a2dTopLeft)
Beispiel #5
0
    def __init__(self):
        ShowBase.__init__(self)

        # Override defaults
        self.disableMouse()
        self.setBackgroundColor(VBase3(160, 200, 150) / 255.0)
        self.setFrameRateMeter(True)

        # Lights
        dlight = DirectionalLight("dlight")
        dlnp = self.render.attachNewNode(dlight)
        dlnp.setHpr(180.0, -70.0, 0)
        self.render.setLight(dlnp)

        alight = AmbientLight("alight")
        alnp = self.render.attachNewNode(alight)
        alight.setColor(VBase4(0.4, 0.4, 0.4, 1))
        self.render.setLight(alnp)

        # Collision traverser
        self.cTrav = CollisionTraverser("collisionTraverser")
        # self.cTrav.showCollisions(self.render)

        # Collision handlers
        self.carCollisionHandler = CollisionHandlerEvent()
        self.carCollisionHandler.addInPattern("%fn-into-%in")

        # Camera controls
        self.cameraController = CameraController(self, 600, math.radians(45),
                                                 math.radians(60))

        # Load the track
        self.track = self.loader.loadModel("models/trackMotegi")
        # self.track = self.loader.loadModel("models/trackValencia")
        checkpointsCollision = self.track.find("checkpoints").node()
        checkpointsCollision.setIntoCollideMask(BitMask32(0xF0))
        self.numCheckpoints = checkpointsCollision.getNumSolids()
        self.track.reparentTo(self.render)

        # Enemy cars
        enemyColor = LColor(204 / 255.0, 72 / 255.0, 63 / 255.0, 1.0)
        self.enemyPos = [(-150, -110, 60), (113, -233, 45), (-255, -264, 20),
                         (-364, -237, -90), (-204, -46, 230)]

        self.enemyCars = []
        for pos in self.enemyPos:
            car = NeuralNetworkCar(self)
            car.getNodePath().setColor(enemyColor)
            car.getNodePath().setX(pos[0])
            car.getNodePath().setY(pos[1])
            car.getNodePath().setH(pos[2])
            self.enemyCars.append(car)

        # Neuroevolution
        # self.loggingActive = False
        self.inputLayerSize = 9
        self.hiddenLayer1Size = 5
        self.hiddenLayer2Size = 5
        self.numLabels = 2
        self.thetaSizes = [(self.hiddenLayer1Size, self.inputLayerSize + 1),
                           (self.hiddenLayer2Size, self.hiddenLayer1Size + 1),
                           (self.numLabels, self.hiddenLayer2Size + 1)]

        self.generationSize = 15
        self.weightInit = 0.12
        self.replaceRatio = 0.02
        self.scaleRatio = 0.02
        self.addRatio = 0.02

        self.generationCount = 1
        self.cars = []
        for i in range(self.generationSize):
            car = NeuroevolutionCar(self, i, self.inputLayerSize)
            # Register car collisions with track
            self.cTrav.addCollider(car.carCollider, self.carCollisionHandler)
            self.accept("carCollider{}-into-carCollider".format(i),
                        car.onCrash)
            self.accept("carCollider{}-into-trackCollision".format(i),
                        car.onCrash)
            self.accept("carCollider{}-into-checkpoints".format(i),
                        car.onCheckpoint)
            self.cars.append(car)

        # Initial generation
        np.random.seed(0)
        for i in range(self.generationSize):
            theta1 = self.randWeights(self.thetaSizes[0], self.weightInit)
            theta2 = self.randWeights(self.thetaSizes[1], self.weightInit)
            theta3 = self.randWeights(self.thetaSizes[2], self.weightInit)
            self.cars[i].startSimulation(theta1, theta2, theta3)

        # Run learning task
        self.taskMgr.add(self.neuroevolution, "NeuroevolutionTask")

        # DEBUG
        self.accept("l", self.logGeneration)
        self.txtGen = OnscreenText(text='',
                                   pos=(0.0, -0.04),
                                   scale=0.05,
                                   align=TextNode.ALeft,
                                   fg=(1, 1, 1, 1),
                                   bg=(0, 0, 0, .4))
        self.txtGen.reparentTo(self.a2dTopLeft)