Ejemplo n.º 1
0
    void findBestCar()
    {
        for (int i = 1; i < CarsList.Length; i++)
        {
            AICarSensors testCar = CarsList[i].GetComponent <AICarSensors>();

            if (testCar.getBestCheckpoint() > bestCheckpoint)
            {
                bestAI.replaceValues(testCar.GetComponent <ValuesStorage>());
                bestCheckpoint = testCar.getBestCheckpoint();
                leastDistance  = testCar.getDistanceToNextCheckpoint();
                //print("Test car best checkpoint :" + testCar.getBestCheckpoint());
                //print("Record holder best checkpoint :" + currentlyBestCar.getBestCheckpoint());
            }
            else if (testCar.getBestCheckpoint() == bestCheckpoint)
            {
                if (testCar.getDistanceToNextCheckpoint() < leastDistance)
                {
                    bestAI.replaceValues(testCar.GetComponent <ValuesStorage>());
                    bestCheckpoint = testCar.getBestCheckpoint();
                    leastDistance  = testCar.getDistanceToNextCheckpoint();
                    //print("Test car best checkpoint :" + testCar.getBestCheckpoint());
                    //print("Record holder best checkpoint :" + currentlyBestCar.getBestCheckpoint());
                }
            }
        }
    }
Ejemplo n.º 2
0
    private void FixedUpdate()
    {
        startdelay -= Time.deltaTime;
        if (startdelay < 0)
        {
            //reset the values in the nodes to 0 every frame
            for (int i = 0; i < 8; i++)
            {
                layer2NodeValues[i] = 0;
                if (i < 5)
                {
                    triggerValues[i] = 0;
                }
            }
            AICarSensors inputs = gameObject.GetComponent <AICarSensors>();

            /// Check the first layer for nodes that fired, then perform additions if they did
            for (int i = 0; i < 8; i++)
            {
                layer2NodeValues[i] += layer1Node1lines[i] * inputs.ForwardDist();
                layer2NodeValues[i] += layer1Node2lines[i] * inputs.FrontLeftDist();
                layer2NodeValues[i] += layer1Node3lines[i] * inputs.FrontRightDist();
                layer2NodeValues[i] += layer1Node4lines[i] * inputs.FrontDownDist();
                layer2NodeValues[i] += layer1Node5lines[i] * inputs.LeftDist();
                layer2NodeValues[i] += layer1Node6lines[i] * inputs.RightDist();
                layer2NodeValues[i] += layer1Node7lines[i] * inputs.BackLeftDist();
                layer2NodeValues[i] += layer1Node8lines[i] * inputs.BackRightDist();
            }

            //apply a sigmoid function to the node values in order to lock them between 0 and 1
            for (int i = 0; i < 8; i++)
            {
                layer2NodeValues[i] = sigmoid(layer2NodeValues[i]);
            }

            ///End of the first layer firing its nodes

            ///Start second layer firing its nodes if they surpass the threshholds

            for (int i = 0; i < 5; i++)
            {
                triggerValues[i] += layer2Node1lines[i] * layer2NodeValues[0];
                triggerValues[i] += layer2Node2lines[i] * layer2NodeValues[1];
                triggerValues[i] += layer2Node3lines[i] * layer2NodeValues[2];
                triggerValues[i] += layer2Node4lines[i] * layer2NodeValues[3];
                triggerValues[i] += layer2Node5lines[i] * layer2NodeValues[4];
                triggerValues[i] += layer2Node6lines[i] * layer2NodeValues[5];
                triggerValues[i] += layer2Node7lines[i] * layer2NodeValues[6];
                triggerValues[i] += layer2Node8lines[i] * layer2NodeValues[7];
            }
            //apply a sigmoid function to the node values in order to lock them between 0 and 1
            for (int i = 0; i < 5; i++)
            {
                triggerValues[i] = sigmoid(triggerValues[i]);
            }
            ///End layer 2 nodes firing


            ///perform the action that the neural net tells the car to perform
            AIDrivingActions controls = GetComponent <AIDrivingActions>();

            float bestvalue    = triggerValues[0];
            int   bestvaluenum = 0;

            for (int i = 1; i < 5; i++)
            {
                if (triggerValues[i] > bestvalue)
                {
                    bestvalue    = triggerValues[i];
                    bestvaluenum = i;
                }
            }

            switch (bestvaluenum)
            {
            case 0:
                controls.TurnLeft();
                break;

            case 1:
                controls.TurnRight();
                break;

            case 2:
                controls.Accelerate();
                break;

            case 3:
                controls.Decelerate();
                break;

            case 4:
                controls.Jump();
                break;
            }
            ///End of the AI performing its Job
        }
    }