private void FixedUpdate()
    {
        if (!sleep)
        {
            Feelers_RayGenerator feelerNum = this.GetComponentInChildren <Feelers_RayGenerator>();
            float[] inputs = new float[size + 3 + (outputs - 2)]; // initialised size of inputs as the num of feelers + 2 vars(speed and angle),inputs from Output and bias
            int     j      = 0;
            for (int i = 0; i < feelerNum.feelerDists.Length; i++)
            {
                inputs[i] = feelerNum.feelerDists[i];
                j++;
            }
            // j is to keep track of the counter used to add to the inputs list
            int io = 0; // to iterate through the outputs, with the first 2 items passed as input to the car
            for (int i = j; i < j + outInput.Length; i++)
            {
                inputs[i] = outInput[io];
                ++io;
            }
            inputs[inputs.GetLength(0) - 3] = m_Car.CurrentSpeed;
            inputs[inputs.GetLength(0) - 2] = m_Car.CurrentSteerAngle;
            inputs[inputs.GetLength(0) - 1] = -1;//bias value


            Feedforward(inputs);
            //Get outputs to be used for recurrent NN
            for (int i = 2; i < outputs; i++)
            {
                outInput[i - 2] = getOutputs()[i];
            }
            // pass the input to the car!
            steering = getOutputs()[0];
            throttle = getOutputs()[1];
            float h = getOutputs()[0];
            float v = getOutputs()[1];
            m_Car.Move(h, v, v, 0f);
        }
        else
        {
            m_Car.Move(0, 0, 0, 0 /*h, v, v, 0f*/);
            m_Car.gameObject.GetComponent <Rigidbody>().angularVelocity = Vector3.zero;
            m_Car.gameObject.GetComponent <Rigidbody>().velocity        = Vector3.zero;
            m_Car.Move(0, 0, 0, 0 /*h, v, v, 0f*/);
        }
    }
    void Awake()
    {
        m_Car = GetComponent <CarController>();
        Feelers_RayGenerator feelerNum = this.GetComponentInChildren <Feelers_RayGenerator>();

        size     = feelerNum.feelerDists.GetLength(0);
        inputs   = size + 3 + (outputs - 2); //bias, carspeed and angle included in input layer, and the inputs for the recurrentNN from output
        layers   = hiddenLayers + 2;         // total layers including input and output layers
        weights  = new List <float[][]>();   //weight initialisation
        neurons  = new List <List <float> >();
        outInput = new float[outputs - 2];

        // Assign Values to neurons
        for (int i = 0; i < layers; i++)
        {
            float[][]    layerWeights;
            List <float> layer     = new List <float>();
            int          layerSize = getSizeLayer(i);
            if (i != hiddenLayers + 1) // checking that not the last layer (it is + 1, rather than + 2 because i is zero-based)
            {
                layerWeights = new float[layerSize][];
                int nextSize = getSizeLayer(i + 1); // size of the next layer
                for (int j = 0; j < layerSize; j++)
                {
                    layerWeights[j] = new float[nextSize];
                    for (int k = 0; k < nextSize; k++)
                    {
                        layerWeights[j][k] = getRandom();
                    }
                }
                weights.Add(layerWeights);
            }
            //What is this for? Geoff = Think it stores the Neuron's value (Input/lastestResultantFNet)
            for (int j = 0; j < layerSize; j++)
            {
                layer.Add(0);
            }
            neurons.Add(layer);
        }
    }
Esempio n. 3
0
    // Start is called before the first frame update
    void Start()
    {
        // Open logging files
        if (logOutputs)
        {
            lastPrintedDist = -1;
            startTime       = Time.time;
            Feelers_RayGenerator feelerSettings = this.gameObject.transform.GetChild(0).gameObject.GetComponent <Feelers_RayGenerator>();
            NeuralNetwork        NN             = this.gameObject.GetComponent <NeuralNetwork>();
            // Initialize naming conventions (open/create all the files) add a time stamp line to each
            if (folderToSaveTo.Length != 0)
            {
                folderToSaveTo = folderToSaveTo + "/";
            }
            string filepath = String.Format("{0}{5}-Log(Feelr#={1}len{2};#recur={3};#hid={4})"
                                            , folderToSaveTo, feelerSettings.feelerDists.Length, feelerSettings.feelerLength, NN.outputs - 2, NN.hLayer_size, customLabel);

            outputStream = new StreamWriter("Assets/logs/" + filepath + ".txt", true);
            outputStream.WriteLine("/////////////////// START ///////////////////");
            outputStream.WriteLine("///// " + DateTime.Now);
            outputStream.WriteLine("///// " + filepath);
        }
    }
    // Create genes(weights) property and instantiate and position cars
    void Start()
    {
        // Initialize some variables
        curGeneration = 0;
        leadCounter   = 0;

        // Open logging files
        if (logOutputs && sessionStartTime == 0)
        {
            sessionStartTime = Time.time;
            Feelers_RayGenerator feelerSettings = cars.carPopulation[0].transform.GetChild(0).gameObject.GetComponent <Feelers_RayGenerator>();
            NeuralNetwork        NN             = cars.carPopulation[0].GetComponent <NeuralNetwork>();
            // Initialize naming conventions (open/create all the files) add a time stamp line to each
            if (folderToSaveTo.Length != 0)
            {
                folderToSaveTo = folderToSaveTo + "/";
            }
            string filepath = String.Format("{0}Log(Feelr#={1}len{2};NN-w={3}socC={4}cognC={5};#recur={6};#hid={7})"
                                            , folderToSaveTo, feelerSettings.feelerDists.Length, feelerSettings.feelerLength, w, socialConst, cognitiveConst, NN.outputs - 2, NN.hLayer_size);

            outputStream = new StreamWriter("Assets/logs/" + filepath + ".txt", true);
            outputStream.WriteLine("/////////////////// START ///////////////////");
            outputStream.WriteLine("///// " + DateTime.Now);
            outputStream.WriteLine("///// " + filepath);
        }


        //Initialize Velocity Vectors to Zero
        ParticleVelocityVectors = new List <List <float[][]> >();
        for (int x = 0; x < POPULATION_SIZE; x++)
        {
            ParticleVelocityVectors.Add(InitializeParticleVelocityToZero());
        }

        // Can't evaluate fitness until raced at least once...
        // So can build that in on Update()
    }