コード例 #1
0
ファイル: Brain.cs プロジェクト: sevalper/Beat-A.N.N.-43
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("space"))
        {
            maxBalanceTime = 0;
            ResetBall();
        }

        if (Input.GetKeyDown("j"))
        {
            Debug.Log(ann.PrintWeights());
        }


        Debug.DrawRay(transform.position, this.transform.forward * visibleDistance, Color.red);
        Debug.DrawRay(transform.position, this.transform.right * visibleDistance, Color.red);
        Debug.DrawRay(transform.position, -this.transform.right * visibleDistance, Color.red);

        Debug.DrawRay(transform.position, (Quaternion.AngleAxis(-45, Vector3.up) * this.transform.right) * visibleDistance, Color.red);
        Debug.DrawRay(transform.position, (Quaternion.AngleAxis(45, Vector3.up) * -this.transform.right) * visibleDistance, Color.red);


        weightsString = ann.PrintWeights();


        //
    }
コード例 #2
0
ファイル: ANNDrive.cs プロジェクト: stasoz/AI
    IEnumerator LoadTrainingSet()
    {
        string path = Application.dataPath + "/trainingData.txt";
        string line;

        if (File.Exists(path))
        {
            int           lineCount   = File.ReadAllLines(path).Length;
            StreamReader  tdf         = File.OpenText(path);
            List <double> calcOutputs = new List <double>();
            List <double> inputs      = new List <double>();
            List <double> outputs     = new List <double>();

            for (int i = 0; i < epochs; i++)
            {
                sse = 0;
                tdf.BaseStream.Position = 0;
                string currentWeights = ann.PrintWeights();
                while ((line = tdf.ReadLine()) != null)
                {
                    string[] data      = line.Split(',');
                    float    thisError = 0;
                    if (System.Convert.ToDouble(data[5]) != 0 && System.Convert.ToDouble(data[6]) != 0)
                    {
                        inputs.Clear();
                        outputs.Clear();
                        inputs.Add(System.Convert.ToDouble(data[0]));
                        inputs.Add(System.Convert.ToDouble(data[1]));
                        inputs.Add(System.Convert.ToDouble(data[2]));
                        inputs.Add(System.Convert.ToDouble(data[3]));
                        inputs.Add(System.Convert.ToDouble(data[4]));

                        double o1 = Map(0, 1, -1, 1, System.Convert.ToSingle(data[5]));
                        outputs.Add(o1);
                        double o2 = Map(0, 1, -1, 1, System.Convert.ToSingle(data[6]));
                        outputs.Add(o2);

                        calcOutputs = ann.Train(inputs, outputs);
                        thisError   = ((Mathf.Pow((float)(outputs[0] - calcOutputs[0]), 2) +
                                        Mathf.Pow((float)(outputs[1] - calcOutputs[1]), 2))) / 2.0f;
                    }
                    sse += thisError;
                }
                trainingProgress = (float)i / (float)epochs;
                sse /= lineCount;
                if (lastSSE < sse)
                {
                    ann.LoadWeights(currentWeights);
                    ann.alpha = Mathf.Clamp((float)ann.alpha - 0.001f, 0.01f, 0.9f);
                }
                else
                {
                    ann.alpha = Mathf.Clamp((float)ann.alpha + 0.001f, 0.01f, 0.9f);
                    lastSSE   = sse;
                }
                yield return(null);
            }
        }
        trainingDone = true;
    }
コード例 #3
0
    private IEnumerator LoadTrainingSet()
    {
        string path = Application.dataPath + "/CarRacing/trainingData.txt";
        string line;

        if (File.Exists(path))
        {
            int           lineCount    = File.ReadAllLines(path).Length;
            StreamReader  streamReader = File.OpenText(path);
            List <double> inputs       = new List <double>();
            List <double> outputs      = new List <double>();

            for (int i = 0; i < epochs; i++)
            {
                sse = 0;
                streamReader.BaseStream.Position = 0;
                string currentWeights = ann.PrintWeights();
                while ((line = streamReader.ReadLine()) != null)
                {
                    string[] data = line.Split(',');
                    //if nothing to be learned ignore this line
                    float thisError = 0;
                    if (System.Convert.ToDouble(data[5]) != 0 && System.Convert.ToDouble(data[6]) != 0)
                    {
                        inputs.Clear();
                        outputs.Clear();
                        FillInputs(inputs, data);
                        FillOutputs(outputs, data);

                        List <double> calcOutputs = ann.Train(inputs, outputs);
                        thisError = ((Mathf.Pow((float)(outputs[0] - calcOutputs[0]), 2) +
                                      Mathf.Pow((float)(outputs[1] - calcOutputs[1]), 2))) / 2.0f;
                    }
                    sse += thisError;
                }
                trainingProgress = (float)i / (float)epochs;
                sse /= lineCount;

                CorrectTraining(currentWeights);

                yield return(null);
            }
        }
        finishedTraining = true;
        SaveWeightsToFile();
    }
コード例 #4
0
    void SaveWeightsToFile()
    {
        string       path = Application.dataPath + "/BallWeights.txt";
        StreamWriter wf   = File.CreateText(path);

        wf.WriteLine(ann.PrintWeights());
        wf.Close();
    }
コード例 #5
0
    // Use this for initialization
    void Start() {
        ann = new ANN(aNNBuilder.inputs, aNNBuilder.hidden, aNNBuilder.outputs, aNNBuilder.neuronsPerHidden, aNNBuilder.alpha, aNNBuilder.hiddenFunction, aNNBuilder.outputFunction, aNNBuilder.useWeightsFromFile, aNNBuilder.folder);
        ballStartPos = ball.transform.position;
        Time.timeScale = timeScale;

        if (aNNBuilder.useWeightsFromFile)
            ann.LoadWeightsFromFile();

        Debug.Log(ann.PrintWeights());
    }
コード例 #6
0
 // Update is called once per frame
 void Update()
 {
     if (Input.GetKey("space"))
     {
         Debug.Log(ann.PrintWeights());
     }
     if (Input.GetKey(KeyCode.Return))
     {
         ann.LoadWeights();
     }
 }
コード例 #7
0
    public void SaveWeights()
    {
        ann = bird.GetANN();

        System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\Users\Ghost\Desktop\weights.txt");
        sw.Write(ann.PrintWeights());
        sw.Close();

        System.IO.StreamWriter sw2 = new System.IO.StreamWriter(@"C:\Users\Ghost\Desktop\bias.txt");
        sw2.Write(ann.PrintBias());
        sw2.Close();
    }
    void SaveWeightsToFile()
    {
        string path = Application.dataPath + "/spyWeights.txt";

        //If file path already exists, delete and make a new one to ensure new set of weights are saved
        if (File.Exists(path))
        {
            File.Delete(path);
        }
        StreamWriter wf = File.CreateText(path);

        wf.WriteLine(ann.PrintWeights());
        wf.Close();
    }
コード例 #9
0
    public void TrainNeuralNetworkForWeightScoreUpdateFromDatabase()
    {
        ann = new ANN(6, 1, 0, 0, 0.8);

        List <double> result;

        for (int i = 0; i < 1000; i++)
        {
            for (int j = 0; j < dataNN.Names.Count; j++)
            {
                double i0 = dataNN.ScorePixelsBalance[j];
                double i1 = dataNN.ScoreUnityVisual[j];
                double i2 = dataNN.ScoreLawOfLever[j];
                double i3 = dataNN.ScoreIsolationBalance[j];

                // insert the scoreFromfeatureScore here ... cosi' only 3 score.... not need perceptiron any more.. since
                // qui stavo sbagliando dovevo fare solo 4 e poi tre in perceptron...
                // devo cambiare anche total score calculation
                // parte difficile e fare regression in tensorflowsharp... speriamo bbbenneee..

                double i4 = dataNN.ScoreNNFrontTop[j];
                double i5 = dataNN.ScoreMobileNet[j];

                double output = dataNN.JUDGE[j];

                sumSquareError  = 0;
                result          = Train(i0, i1, i2, i3, i4, i5, output);
                sumSquareError += Mathf.Pow((float)result[0] - 0, 2);
            }

            //Debug.Log("SSE: " + sumSquareError);

            string weigths = ann.PrintWeights();

            //Debug.Log(weigths);
        }
    }
コード例 #10
0
    /// <summary>
    /// Loads the training dataset.
    /// </summary>
    /// <returns> null (Coroutine). </returns>
    private IEnumerator LoadTrainingSet()
    {
        string dataSetFilePath = Application.dataPath + dataSetFolder + dataSetFileName;

        // An instance of training (row in the dataSet).
        string instance;

        if (File.Exists(dataSetFilePath))
        {
            Debug.Log("Using training data file found at: " + dataSetFilePath);

            int          instanceCount     = File.ReadAllLines(dataSetFilePath).Length;
            StreamReader dataSetFile       = File.OpenText(dataSetFilePath);
            var          calculatedOutputs = new List <double>();
            var          inputs            = new List <double>();
            var          desiredOutputs    = new List <double>();

            for (var i = 0; i < epochs; i++)
            {
                sumSquaredError = 0;

                // Set file pointer to beginning of file.
                dataSetFile.BaseStream.Position = 0;

                string currentWeights = ann.PrintWeights();

                // Read one instance (line) at a time until the end of the dataSet.
                while ((instance = dataSetFile.ReadLine()) != null)
                {
                    // Separate each feature (column) of the current instance (row).
                    string[] features = instance.Split(',');

                    // The error we get from a particular instance.
                    // If nothing to be learned, ignore this line.
                    float thisError = 0;

                    // Ignore instances, where no user input was recorded.
                    // They provide no useful information.
                    // TODO: Fix floating point number comparison!?
                    if (System.Convert.ToDouble(features[5]) != 0 &&
                        System.Convert.ToDouble(features[6]) != 0)
                    {
                        inputs.Clear();
                        desiredOutputs.Clear();

                        // TODO: Check that training data and inputs are calculated the same way (rounding, normalizing, etc.).
                        // Assign the first five features (raycast distances) to inputs.
                        for (int j = 0; j < 5; j++)
                        {
                            inputs.Add(System.Convert.ToDouble(features[j]));
                        }

                        // Assigns the remaining two features (user input) to outputs.
                        for (int j = 5; j < 7; j++)
                        {
                            double output = Helpers.Map(0, 1, -1, 1, System.Convert.ToSingle(features[j]));
                            desiredOutputs.Add(output);
                        }

                        // Train the Neural Network.
                        calculatedOutputs = ann.Train(inputs, desiredOutputs);

                        // Calculate individual squaredErrors.
                        float output0ErrorSquared = Mathf.Pow((float)(desiredOutputs[0] - calculatedOutputs[0]), 2);
                        float output1ErrorSquared = Mathf.Pow((float)(desiredOutputs[1] - calculatedOutputs[1]), 2);
                        // Calculate averaged sum of squared errors.
                        thisError = (output0ErrorSquared + output1ErrorSquared) / 2f;
                    }
                    sumSquaredError += thisError;
                }
                // Percentage value.
                trainingProgress = (float)i / (float)epochs;

                // Calculate average sumOfSquaredErrors.
                sumSquaredError /= instanceCount;

                AdaptLearning(currentWeights);

                yield return(null);
            }
        }
        else
        {
            Debug.LogError("No training data file found at: " + dataSetFilePath);
        }
        trainingDone = true;

        if (!loadWeightsFromFile)
        {
            SaveWeightsToFile();
        }
    }
コード例 #11
0
ファイル: ANNDrive.cs プロジェクト: BantamJoe/UnityML
    // Method to perform the ANN training using data collected from the player.
    IEnumerator LoadTrainingSet()
    {
        string path = Application.dataPath + "/trainingData.txt";
        string line;

        if (File.Exists(path))
        {
            int          lineCount = File.ReadAllLines(path).Length;
            StreamReader tdf       = File.OpenText(path);

            List <double> calcOutputs = new List <double>();
            List <double> inputs      = new List <double>();
            List <double> outputs     = new List <double>();

            //Loop through the epochs
            for (int i = 0; i < epochs; i++)
            {
                //set file pointer to beginning of file
                sse = 0;
                tdf.BaseStream.Position = 0;

                //Get the current weight comma separated string values from the ANN object
                string currentWeights = ann.PrintWeights();

                // Load the training data, line by line
                while ((line = tdf.ReadLine()) != null)
                {
                    string[] data = line.Split(',');
                    //if nothing to be learned ignore this line
                    float thisError = 0;

                    //We are leaving out those data where we have training labels or y (translation and rotation values) with values zero to reduce the data set.
                    if (System.Convert.ToDouble(data[5]) != 0 && System.Convert.ToDouble(data[6]) != 0) //If translation and rotation outputs in training data are not zero
                    {
                        //Clear out lists from previous row
                        inputs.Clear();
                        outputs.Clear();

                        //Add training input data to the inputs to the ANN
                        inputs.Add(System.Convert.ToDouble(data[0]));
                        inputs.Add(System.Convert.ToDouble(data[1]));
                        inputs.Add(System.Convert.ToDouble(data[2]));
                        inputs.Add(System.Convert.ToDouble(data[3]));
                        inputs.Add(System.Convert.ToDouble(data[4]));

                        //Map labels to range (0,1) for efficient training
                        double o1 = Map(0, 1, -1, 1, System.Convert.ToSingle(data[5]));
                        outputs.Add(o1);
                        double o2 = Map(0, 1, -1, 1, System.Convert.ToSingle(data[6]));
                        outputs.Add(o2);

                        //Calculated output (y-hat)
                        calcOutputs = ann.Train(inputs, outputs);
                        //Sum squared Error value: for both labels
                        thisError = ((Mathf.Pow((float)(outputs[0] - calcOutputs[0]), 2) +
                                      Mathf.Pow((float)(outputs[1] - calcOutputs[1]), 2))) / 2.0f;
                    }
                    //Add this to cumulative SSE for the epoch
                    sse += thisError;
                }

                //Percentage training to display on screen
                trainingProgress = (float)i / (float)epochs;

                // Average SSE
                sse /= lineCount;

                //If sse isn't better then reload previous set of weights and decrease alpha. This adaptive training to let the ANN move out
                // of local optima and hence find global optima.
                if (lastSSE < sse)
                {
                    ann.LoadWeights(currentWeights);
                    ann.alpha = Mathf.Clamp((float)ann.alpha - 0.001f, 0.01f, 0.9f);
                }
                else //increase alpha
                {
                    ann.alpha = Mathf.Clamp((float)ann.alpha + 0.001f, 0.01f, 0.9f);
                    lastSSE   = sse;
                }

                yield return(null); //Allow OnGUI some time to update on-screen values
            }
        }
        //Training done, save weights
        trainingDone = true;
        SaveWeightsToFile();
    }
コード例 #12
0
    private void QLearning()
    {
        /// Counting timer
        resetTimer--;

        qs.Clear();
        qs = SoftMax(ann.CalcOutput(states));

        float maxQ      = qs.Max();
        int   maxQIndex = qs.ToList().IndexOf(maxQ);

        /// Counting exploring output values
        exploreRate = Mathf.Clamp(exploreRate - exploreDecay, minExploreRate, maxExploreRate);
        if (Random.Range(0, 100) < exploreRate)
        {
            maxQIndex = Random.Range(0, 4);
        }

        /// Moving the vehicle using output values
        /// Move forward
        if (maxQIndex == 0)
        {
            rb.AddForce(this.transform.forward * Mathf.Clamp(qs[maxQIndex], 0f, 1f) * 300f);
        }

        if (maxQIndex == 1)
        {
            rb.AddForce(this.transform.forward * Mathf.Clamp(qs[maxQIndex], 0f, 1f) * -300f);
        }

        /// Turning
        if (maxQIndex == 2)
        {
            this.transform.Rotate(0, Mathf.Clamp(qs[maxQIndex], 0f, 1f) * 2f, 0, 0);
        }

        if (maxQIndex == 3)
        {
            this.transform.Rotate(0, Mathf.Clamp(qs[maxQIndex], 0f, 1f) * -2f, 0, 0);
        }

        RewardFunction();

        /// Setting replay memory
        Replay lastMemory = new Replay(states, reward);

        Rewards.Add(reward);

        if (replayMemory.Count > mCapacity)
        {
            replayMemory.RemoveAt(0);
        }

        replayMemory.Add(lastMemory);

        /// Training through replay memory
        if (collisionFail || resetTimer == 0 || win || backFail)
        {
            List <float> QOld = new List <float>();
            List <float> QNew = new List <float>();

            for (int i = replayMemory.Count - 1; i >= 0; i--)
            {
                List <float> toutputsOld = new List <float>();                                      // List of actions at time [t] (present)
                List <float> toutputsNew = new List <float>();                                      // List of actions at time [t + 1] (future)
                toutputsOld = SoftMax(ann.CalcOutput(replayMemory[i].states));                      // Action in time [t] is equal to NN output for [i] step states in replay memory

                float maxQOld = toutputsOld.Max();                                                  // maximum Q value at [i] step is equal to maximum Q value in the list of actions in time [t]
                int   action  = toutputsOld.ToList().IndexOf(maxQOld);                              // number of action (in list of actions at time [t]) with maximum Q value is setted
                QOld.Add(toutputsOld[action]);

                float feedback;
                if (i == replayMemory.Count - 1)                                                    // if it's the end of replay memory or if by any reason it's the end of the sequence (in this case
                {                                                                                   // it's collision fail, timer reset and getting into the source of light) then the
                    feedback = replayMemory[i].reward;                                              // feedback (new reward) is equal to the reward in [i] replay memory, because it's the end of the
                }                                                                                   // sequence and there's no event after to count Bellman's equation

                else
                {
                    toutputsNew = SoftMax(ann.CalcOutput(replayMemory[i + 1].states));              // otherwise the action at time [t + 1] is equal to NN output for [i + 1] step states
                    maxQ        = toutputsNew.Max();                                                // in replay memory and then feedback is equal to the Bellman's Equation
                    feedback    = (replayMemory[i].reward +
                                   discount * maxQ);
                }
                QNew.Add(feedback);

                if (save == true)
                {
                    SaveToFile(QOld, QNew, Rewards, "QValues");
                }

                float thisError = 0f;
                currentWeights = ann.PrintWeights();

                toutputsOld[action] = feedback;                                                     // then the action at time [t] with max Q value (the best action) is setted as counted feedback
                List <float> calcOutputs = ann.Train(replayMemory[i].states, toutputsOld);          // value and it's used to train NN for [i] state
                for (int j = 0; j < calcOutputs.Count; j++)
                {
                    thisError += (Mathf.Pow((toutputsOld[j] - calcOutputs[j]), 2));
                }
                thisError = thisError / calcOutputs.Count;
                sse      += thisError;
            }
            sse /= replayMemory.Count;

            if (lastRewardSum < Rewards.Sum())
            {
                //ann.LoadWeights(currentWeights);
                ann.eta = Mathf.Clamp(ann.eta - 0.001f, 0.1f, 0.4f);
            }
            else
            {
                ann.eta       = Mathf.Clamp(ann.eta + 0.001f, 0.1f, 0.4f);
                lastRewardSum = Rewards.Sum();
            }

            replayMemory.Clear();
            ResetVehicle();
            Rewards.Clear();
        }
    }
コード例 #13
0
    IEnumerator LoadTrainingSet()
    {
        string path = ann.GetPath("trainingData");
        string line;

        if (File.Exists(path))
        {
            int          lineCount = File.ReadAllLines(path).Length;
            StreamReader tdf       = File.OpenText(path);

            List <double> calcOutputs = new List <double>();
            List <double> inputs      = new List <double>();
            List <double> outputs     = new List <double>();

            for (int i = 0; i < epochs; i++)
            {
                // set file pointer to beginning of file
                sse = 0;
                tdf.BaseStream.Position = 0;
                string currentWeights = ann.PrintWeights();
                while ((line = tdf.ReadLine()) != null)
                {
                    string[] data = line.Split(',');
                    // if nothing to be learned ignore this line
                    float thisError = 0;
                    if (System.Convert.ToDouble(data[5]) != 0 && System.Convert.ToDouble(data[6]) != 0)
                    {
                        inputs.Clear();
                        outputs.Clear();
                        inputs.Add(System.Convert.ToDouble(data[0]));
                        inputs.Add(System.Convert.ToDouble(data[1]));
                        inputs.Add(System.Convert.ToDouble(data[2]));
                        inputs.Add(System.Convert.ToDouble(data[3]));
                        inputs.Add(System.Convert.ToDouble(data[4]));

                        double out1 = Utils.Map(0, 1, -1, 1, System.Convert.ToSingle(data[5]));
                        double out2 = Utils.Map(0, 1, -1, 1, System.Convert.ToSingle(data[6]));
                        outputs.Add(out1);
                        outputs.Add(out2);

                        calcOutputs = ann.Train(inputs, outputs);
                        thisError   = ((Mathf.Pow((float)(outputs[0] - calcOutputs[0]), 2) +
                                        Mathf.Pow((float)(outputs[1] - calcOutputs[1]), 2))) / 2.0f;
                    }
                    sse += thisError;
                }
                trainingProgress = ((float)i / (float)epochs) * 100;
                sse /= lineCount;

                if (lastSSE < sse)   // if sse isnt better reload old one and decrease alpha
                {
                    ann.LoadWeights(currentWeights);
                    ann.alpha = Mathf.Clamp((float)ann.alpha - 0.001f, 0.01f, 0.9f);
                }
                else     // increase alpha
                {
                    ann.alpha = Mathf.Clamp((float)ann.alpha + 0.001f, 0.01f, 0.9f);
                    lastSSE   = sse;
                }
                yield return(null);
            }
            tdf.Close();
        }
        trainingDone = true;
        ann.SaveWeightsToFile();
    }
コード例 #14
0
    IEnumerator LoadTrainingSet()
    {
        string path = Application.dataPath + "/trainingData.txt";
        string line;

        if (File.Exists(path))
        {
            int           lineCount   = File.ReadAllLines(path).Length;
            StreamReader  tdf         = File.OpenText(path);
            List <double> calcOutputs = new List <double>();
            List <double> inputs      = new List <double>();
            List <double> outputs     = new List <double>();

            for (int i = 0; i < epochs; i++)
            {
                //set file pointer to beginning of file
                sse = 0;
                tdf.BaseStream.Position = 0;
                string currentWeights = ann.PrintWeights();
                while ((line = tdf.ReadLine()) != null)
                {
                    string[] data = line.Split(',');
                    //if nothing to be learned ignore this line
                    float thisError = 0;
                    if (Convert.ToDouble(data[5]) != 0 && Convert.ToDouble(data[6]) != 0)   //if the data had user input (active keys pressed)
                    {
                        inputs.Clear();
                        outputs.Clear();
                        inputs.Add(Convert.ToDouble(data[0]));
                        inputs.Add(Convert.ToDouble(data[1]));
                        inputs.Add(Convert.ToDouble(data[2]));
                        inputs.Add(Convert.ToDouble(data[3]));
                        inputs.Add(Convert.ToDouble(data[4]));

                        double o1 = Map(0, 1, -1, 1, Convert.ToSingle(data[5]));
                        outputs.Add(o1);
                        double o2 = Map(0, 1, -1, 1, Convert.ToSingle(data[6]));
                        outputs.Add(o2);

                        calcOutputs = ann.Train(inputs, outputs);
                        thisError   = ((Mathf.Pow((float)(outputs[0] - calcOutputs[0]), 2) + Mathf.Pow((float)(outputs[1] - calcOutputs[1]), 2))) / 2.0f;
                    }
                    sse += thisError;
                }
                trainingProgress = (float)i / (float)epochs;
                sse /= lineCount;

                //if sse isn't better then reload previous set of weights
                //and decrease alpha
                if (lastSSE < sse)
                {
                    ann.LoadWeights(currentWeights);
                    ann.alpha = Mathf.Clamp((float)ann.alpha - 0.001f, 0.01f, 0.9f);
                }
                else
                {
                    ann.alpha = Mathf.Clamp((float)ann.alpha + 0.001f, 0.01f, 0.9f);
                }

                yield return(null);
            }
        }
        trainingDone = true;
        SaveWeightsToFile();
    }