Exemplo n.º 1
0
    private void Start()
    {
        ann = new ANN(numInputs, numOutputs, numHiddenLayars, numNeuronsPerHidden, alpha);

        List <double> result;

        for (int i = 0; i < 10000; i++)
        {
            sumSquareError  = 0;
            result          = Train(1, 1, 0);
            sumSquareError += Mathf.Pow((float)result[0] - 0, 2);
            result          = Train(1, 0, 1);
            sumSquareError += Mathf.Pow((float)result[0] - 1, 2);
            result          = Train(0, 1, 1);
            sumSquareError += Mathf.Pow((float)result[0] - 1, 2);
            result          = Train(0, 0, 0);
            sumSquareError += Mathf.Pow((float)result[0] - 0, 2);
        }

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

        result = Train(1, 1, 0);
        Debug.Log(" 1 1 " + result[0]);
        result = Train(1, 0, 1);
        Debug.Log(" 1 0 " + result[0]);
        result = Train(0, 1, 1);
        Debug.Log(" 0 1 " + result[0]);
        result = Train(0, 0, 0);
        Debug.Log(" 0 0 " + result[0]);
    }
 private void Start()
 {
     ///Why 4 neurons?
     ///in this case 0 between numInputs
     ann = new ANN(6, 1, 1, 4, 0.11);
     brb = ball.GetComponent <Rigidbody2D>();
 }
Exemplo n.º 3
0
    private void Start()
    {
        ann = new ANN(inputNumber, outputNumber, hiddenNumber, hidNeuronsNumber, n);

        /*
         * if (File.Exists(Application.dataPath + "/weights.txt"))
         * {
         *  LoadWeightsFromFile();
         *  exploreRate = 0.05f;
         * }
         */

        /// initialise delta intensity variables:
        intensity    = phototransistor1.intensity + phototransistor2.intensity + phototransistor3.intensity + phototransistor4.intensity;
        intensityOld = 0.0f;

        Time.timeScale = 1f;
        reLearnCounter = rlcValue;

        // initialise reset position variables:
        rb = this.GetComponent <Rigidbody>();
        vehicleStartRot = this.transform.rotation;
        vehicleStartPos = this.transform.position;
        lightStartPos   = LightSource.transform.position;
        wallStartPos    = wall.transform.position;
    }
    // Start is called before the first frame update
    void Start()
    {
        //Sets up the ANN with how many inputs, outputs, hidden layers and neurons it has.
        ann           = new ANN(6, 4, 1, 12, 0.3f);
        spyAgent      = GetComponent <NavMeshAgent>();
        spyRender     = GetComponent <Renderer>();
        startPos      = this.transform.position;
        guardAgent    = guard.GetComponent <NavMeshAgent>();
        guardStartPos = guard.transform.position;

        distance   = Vector3.Distance(guard.transform.position, this.transform.position);
        currentPos = startPos;

        if (fast)
        {
            Time.timeScale = 5.0f;
        }

        timeCheck = timer + 1;

        if (load)
        {
            LoadWeightsFromFile();
        }
    }
Exemplo n.º 5
0
    // Combine neural network of two birds
    // - get random neuron
    // - everything up to including this neuron -> get weights and bias values from one parent
    // - get everything else from the second parent
    public void Combine(ANN a1, ANN a2)
    {
        for (int i = 0; i < numHidden + 1; i++)
        {
            int point = Random.Range(0, layers[i].numNeurons);

            for (int j = 0; j < layers[i].numNeurons; j++)
            {
                if (j <= point)
                {
                    layers[i].neurons[j].bias = a1.layers[i].neurons[j].bias;
                }
                else
                {
                    layers[i].neurons[j].bias = a2.layers[i].neurons[j].bias;
                }

                for (int k = 0; k < layers[i].neurons[j].numInputs; k++)
                {
                    if (j <= point)
                    {
                        layers[i].neurons[j].weights[k] = a1.layers[i].neurons[j].weights[k];
                    }
                    else
                    {
                        layers[i].neurons[j].weights[k] = a2.layers[i].neurons[j].weights[k];
                    }
                }
            }
        }
    }
Exemplo n.º 6
0
        public Snake()
        {
            Vector2 dims = WorldRenderer.instance.World.Dimensions;

            HeadPosition = new Vector2(dims.X / 2, dims.Y / 2);
            length       = 4;   //zmija pocinje sa duljinom 4
            BodyParts    = new Queue <Vector2>();
            brain        = new ANN(24, 18, 4);
            age          = 0;
            Fitness      = 0;
            timeLeft     = 200;
            isDead       = false;
            isTested     = false;
            //dodaj dijelove tijela
            if (HeadPosition.X < 3)
            {
                throw new Exception("zmija mora biti inicijalizirana barem na poziciji 30 da stane na ekran");
            }
            BodyParts.Enqueue(HeadPosition - new Vector2(3, 0));
            BodyParts.Enqueue(HeadPosition - new Vector2(2, 0));
            BodyParts.Enqueue(HeadPosition - new Vector2(1, 0));

            CurrentFoodUnit  = new Food();
            VelocityModifier = 1;
            TimesToGrow      = 0;
        }
Exemplo n.º 7
0
    float maxBalanceTime = 0;                                                   //record time ball is kept balanced


    // with 10 neurons in hidden layer, learns faster, but less stable
    // with 15 neurons, hard to find a balance (> 40 iter)
    // with 2 layers of 3 neurons convergence is slower. Why?
    // 2 layers of 2 neurons: convergence even slower, but trials are much more dyanmic.
    //   How to visualize these different effects?
    // How to do RL together with regularization
    void Start()
    {
        // ANN(int nI, int nO, int nH, int nPH, double a)
        ann            = new ANN(3, 2, 1, 6, 0.2f); // orig: 3,2,1,6,0.2f
        ballStartPos   = ball.transform.position;
        Time.timeScale = 5.0f;
    }
Exemplo n.º 8
0
    //states - INPUTS
    //platform x rotation
    //ball z position
    //ball velocity z

    //actions - OUTPUTS
    //rotate x direction positive
    //rotate x direction negative

    void Start()
    {
        ann            = new ANN(2, 2, 1, 6, 0.2f);
        startPos       = this.transform.position;
        Time.timeScale = 5.0f;
        rb             = this.GetComponent <Rigidbody2D>();
    }
Exemplo n.º 9
0
    void Start()
    {
        sumSquareError = 0;
        ann            = new ANN(nI: 2, nO: 1, nH: 1, nPH: 2, a: 0.8);
        List <double> result;

        for (int i = 0; i < 100000; i++)   // 100000 epochs
        {
            sumSquareError  = 0;
            result          = Train(1, 1, 0);
            sumSquareError += Mathf.Pow((float)result[0] - 0, 2);
            result          = Train(1, 0, 1);
            sumSquareError += Mathf.Pow((float)result[0] - 1, 2);
            result          = Train(0, 1, 1);
            sumSquareError += Mathf.Pow((float)result[0] - 1, 2);
            result          = Train(0, 0, 0);
            sumSquareError += Mathf.Pow((float)result[0] - 0, 2);
        }
        Debug.Log("---------------------");
        Debug.Log("Training Complete!");
        Debug.Log("Sum of squared errors: " + sumSquareError);
        Debug.Log("-(Unrounded results)-");
        result = Train(1, 1, 0);
        Debug.Log("XOR(1, 1) = " + result[0]);
        result = Train(0, 1, 1);
        Debug.Log("XOR(0, 1) = " + result[0]);
        result = Train(1, 0, 1);
        Debug.Log("XOR(1, 0) = " + result[0]);
        result = Train(0, 0, 0);
        Debug.Log("XOR(0, 0) = " + result[0]);
    }
Exemplo n.º 10
0
    void Start()
    {
        ann = new ANN(2, 1, 1, 2, 0.8);

        List <double> result;

        for (int i = 0; i < 5000; i++)
        {
            sumSquareError  = 0;
            result          = Train(1, 1, 0);
            sumSquareError += Mathf.Pow((float)result[0] - 0, 2);
            result          = Train(1, 0, 1);
            sumSquareError += Mathf.Pow((float)result[0] - 1, 2);
            result          = Train(0, 1, 1);
            sumSquareError += Mathf.Pow((float)result[0] - 1, 2);
            result          = Train(0, 0, 0);
            sumSquareError += Mathf.Pow((float)result[0] - 0, 2);
        }
        Debug.Log("SSE: " + sumSquareError);

        result = Train(1, 1, 0);
        Debug.Log(" 1 1 " + result[0]);
        result = Train(1, 0, 1);
        Debug.Log(" 1 0 " + result[0]);
        result = Train(0, 1, 1);
        Debug.Log(" 0 1 " + result[0]);
        result = Train(0, 0, 0);
        Debug.Log(" 0 0 " + result[0]);
    }
Exemplo n.º 11
0
    // Use this for initialization
    void Start()
    {
        ann = new ANN(2, 1, 1, 2, 0.8);
        for (int i = 0; i < 1000; i++)
        {
            sunSquareError  = 0;
            result          = Train(0, 0, 0);
            sunSquareError += Mathf.Pow((float)result[0] - 0, 2);
            result          = Train(1, 0, 1);
            sunSquareError += Mathf.Pow((float)result[0] - 1, 2);
            result          = Train(0, 1, 1);
            sunSquareError += Mathf.Pow((float)result[0] - 1, 2);
            result          = Train(1, 1, 0);
            sunSquareError += Mathf.Pow((float)result[0] - 0, 2);
        }
        Debug.Log("sun squared error: " + sunSquareError);

        result = Train(0, 0, 0);
        Debug.Log("0 0: " + result[0]);

        result = Train(0, 1, 1);
        Debug.Log("0 1: " + result[0]);

        result = Train(1, 0, 1);
        Debug.Log("1 0: " + result[0]);

        result = Train(1, 1, 0);
        Debug.Log("1 1: " + result[0]);
    }
Exemplo n.º 12
0
    double sumSquareError = 0;     //How closely the model fits the data

    // Use this for initialization
    void Start()
    {
        ann = new ANN(2, 1, 1, 2, 0.8);

        //Stores the result of each line in the training set
        List <double> result;

        //Loop over the epochs
        for (int i = 0; i < 1000; i++)
        {
            sumSquareError = 0;

            //Training set for the XOR operation
            result          = Train(1, 1, 0);
            sumSquareError += Mathf.Pow((float)result[0] - 0, 2);            //desired result is 0
            result          = Train(1, 0, 1);
            sumSquareError += Mathf.Pow((float)result[0] - 1, 2);            //desired result is 1
            result          = Train(0, 1, 1);
            sumSquareError += Mathf.Pow((float)result[0] - 1, 2);
            result          = Train(0, 0, 0);
            sumSquareError += Mathf.Pow((float)result[0] - 0, 2);
        }
        //Print out the final sum of squared errors after training epochs
        Debug.Log("SSE: " + sumSquareError);

        //Run and test the neural network
        result = Train(1, 1, 0);
        Debug.Log(" 1 1 " + result[0]);
        result = Train(1, 0, 1);
        Debug.Log(" 1 0 " + result[0]);
        result = Train(0, 1, 1);
        Debug.Log(" 0 1 " + result[0]);
        result = Train(0, 0, 0);
        Debug.Log(" 0 0 " + result[0]);
    }
Exemplo n.º 13
0
    // Start is called before the first frame update
    void Start()
    {
        this.ann = new ANN(2, 1, 1, 2, 0.8);

        List <double> result;

        // 1000 epochs
        for (int i = 0; i < 10000; i++)
        {
            this.sumSquareError = 0;
            result = Train(1, 1, 0, true);
            this.sumSquareError += Mathf.Pow((float)result[0] - 0, 2);
            result = Train(1, 0, 1, true);
            this.sumSquareError += Mathf.Pow((float)result[0] - 1, 2);
            result = Train(0, 1, 1, true);
            this.sumSquareError += Mathf.Pow((float)result[0] - 1, 2);
            result = Train(0, 0, 0, true);
            this.sumSquareError += Mathf.Pow((float)result[0] - 0, 2);
        }
        Debug.Log("SSE: " + (float)sumSquareError);

        result = Train(1, 1, 0, false);
        Debug.Log(" 1 1 (0)" + result[0]);
        result = Train(1, 0, 1, false);
        Debug.Log(" 1 0 (1)" + result[0]);
        result = Train(0, 1, 1, false);
        Debug.Log(" 0 1 (1)" + result[0]);
        result = Train(0, 0, 0, false);
        Debug.Log(" 0 0 (0)" + result[0]);
    }
    double sumSquareError = 0; //sumsq is how closely model fixes the data it is fed

    // Start is called before the first frame update
    void Start()
    {
        ann = new ANN(2, 1, 1, 2, 0.8);

        List <double> results;

        for (int i = 0; i < 200000; i++)
        {
            sumSquareError = 0;
            //XOR operation
            results         = Train(0, 0, 0);
            sumSquareError += Mathf.Pow((float)results[0] - 0, 2);
            results         = Train(0, 1, 1);
            sumSquareError += Mathf.Pow((float)results[0] - 1, 2);
            results         = Train(1, 0, 1);
            sumSquareError += Mathf.Pow((float)results[0] - 1, 2);
            results         = Train(1, 1, 0);
            sumSquareError += Mathf.Pow((float)results[0] - 0, 2);
        }
        Debug.Log("SSE: " + sumSquareError); //should be near to zero

        results = Train(0, 0, 0);
        Debug.Log("0 0 " + results[0]);
        results = Train(0, 1, 1);
        Debug.Log("0 1 " + results[0]);
        results = Train(1, 0, 1);
        Debug.Log("1 0 " + results[0]);
        results = Train(1, 1, 0);
        Debug.Log("1 1 " + results[0]);
    }
Exemplo n.º 15
0
        /// <summary>
        /// Constructs a new AnnDialog.
        /// </summary>
        public AnnDialog(ref Klu Klu)
        {
            InitializeComponent();

            _Klu = Klu;

            #region Initialize ANN stuff
            _ANN           = new ANN();
            _ANN.NumLayers = 3;
            _ANN.SetNumNeurons(0, 38);
            _ANN.SetNumNeurons(1, 6);
            _ANN.SetNumNeurons(2, 7);

            // Bind certain labels to ANN stuff
            AnnNumLayers.DataContext = _ANN;

            // Bind DataGrid to ANN-DataSet now
            _DataSet = new DataSet("HiddenLayer");
            _DataSet.Tables.Add("HiddenLayerTable");
            uint tmp = 0;
            _DataSet.Tables[0].Columns.Add("Neurons", tmp.GetType());
            tmp = 6;
            _DataSet.Tables[0].Rows.Add(tmp);
            HiddenLayerDataGrid.DataContext = _DataSet.Tables[0];
            #endregion
        }
Exemplo n.º 16
0
    void Start()
    {
        ann = new ANN(2, 1, 1, 2, 0.8);

        List <double> result;

        for (int i = 0; i < 5000; i++)
        {
            sumSquareError  = 0;
            result          = Train(1, 1, 0);
            sumSquareError += Mathf.Pow((float)result[0] - 0, 2);            // idk what happened here. Need to check the end of 2nd video
            result          = Train(1, 0, 1);
            sumSquareError += Mathf.Pow((float)result[0] - 1, 2);
            result          = Train(0, 1, 1);
            sumSquareError += Mathf.Pow((float)result[0] - 1, 2);
            result          = Train(0, 0, 0);
            sumSquareError += Mathf.Pow((float)result[0] - 0, 2);
        }
        Debug.Log("SSE: " + sumSquareError);         //if error near the 1, so we need more iterations or change values

        result = Train(1, 1, 0);
        Debug.Log(" 1 1 " + result[0]);
        result = Train(1, 0, 1);
        Debug.Log(" 1 0 " + result[0]);
        result = Train(0, 1, 1);
        Debug.Log(" 0 1 " + result[0]);         // u can change it to printf SUMBELLA!1
        result = Train(0, 0, 0);
        Debug.Log(" 0 0 " + result[0]);
    }
Exemplo n.º 17
0
    private void Start()
    {
        model = new ANN(2, 1, 1, 2, 0.8d);
        List <double> result;

        for (int i = 0; i < epochs; i++)
        {
            sumSquareError = 0;
            //input 1, input 2, expected output for an XOR
            result          = Train(1, 1, 0);
            sumSquareError += Mathf.Pow((float)result[0], 2);
            result          = Train(1, 0, 1);
            sumSquareError += Mathf.Pow((float)result[0] - 1, 2);
            result          = Train(0, 1, 1);
            sumSquareError += Mathf.Pow((float)result[0] - 1, 2);
            result          = Train(0, 0, 0);
            sumSquareError += Mathf.Pow((float)result[0], 2);
        }

        Debug.Log("Sum square error: " + sumSquareError);

        //predict not spereated from "Train" but this is effectively predicting
        result = Train(1, 1, 0);
        Debug.Log("1 1 = " + result);
        result = Train(1, 0, 1);
        Debug.Log("1 0 = " + result);
        result = Train(0, 1, 1);
        Debug.Log("0 1 = " + result);
        result = Train(0, 0, 0);
        Debug.Log("0 0 = " + result);
    }
Exemplo n.º 18
0
    // Start is called before the first frame update
    void Start()
    {
        Time.timeScale = 1f;

        this.ann           = new ANN(6, 1, 1, 4, 0.01);
        this.ballRigidBody = this.ball.GetComponent <Rigidbody2D>();
    }
Exemplo n.º 19
0
    float maxBalanceTime = 0;                                                                                                   //record time ball is kept balanced

//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------

    // Use this for initialization
    void Start()
    {
        nn = new ANN(3, 1, 2, 5, 0.2f);
        // nn = new ANN(3, 1, 2, 6, 0.2f); //TOTRY
        poleStartPosition = pole.transform.position;
        Debug.Log("Pole start position: " + poleStartPosition);
        Time.timeScale = 1.0f;
    }
Exemplo n.º 20
0
 // Start is called before the first frame update
 void Start()
 {
     ann            = new ANN(2, 2, 1, 6, 0.2f);
     rb             = GetComponent <Rigidbody2D>();
     robotAccess    = GetComponent <RobotController>();
     theGameManager = FindObjectOfType <GameManager>();
     Time.timeScale = 5.0f;
     wait           = false;
 }
Exemplo n.º 21
0
 private void Start()
 {
     episode           = 0;
     cumulative_reward = 0;
     Time.timeScale    = 3f;
     ann      = new ANN(4, 2, 1, 7, 0.4f);
     myBody   = GetComponent <Rigidbody2D>();
     startPos = transform.localPosition;
 }
    // 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());
    }
Exemplo n.º 23
0
 void Start()
 {
     paddleMinY     = 8.8f;
     paddleMaxY     = 17.4f;
     paddleMaxSpeed = 15;
     numSaved       = 0;
     numMissed      = 0;
     brb            = ball.GetComponent <Rigidbody2D>();
     ann            = new ANN(6, 1, 1, 4, 0.11); // 6 inputs, 1 output, 1 hidden layer, 4 neurons, 0.11 alpha
 }
Exemplo n.º 24
0
    public MonsterBrain(int input, int output, List <int> hiddenLayer, double alpha, float discount)
    {
        this.discount = discount;

        // Setting ANN
        ann = new ANN(input, output, hiddenLayer, alpha);

        states = new List <double>();
        qs     = new List <double>();
    }
Exemplo n.º 25
0
    void ResetVehicle()
    {
        if (reLearnCounter == 0)
        {
            if (reward > 0f)
            {
                rewardCounter++;
            }
            else
            {
                reLearnCounter = rlcValue;

                ann = new ANN(inputNumber, outputNumber, hiddenNumber, hidNeuronsNumber, n);
                this.transform.position        = vehicleStartPos;
                this.transform.rotation        = vehicleStartRot;
                rb.velocity                    = new Vector3(0f, 0f, 0f);
                rb.angularVelocity             = new Vector3(0f, 0f, 0f);
                LightSource.transform.position = lightStartPos;
                wall.transform.position        = wallStartPos;
                wall.transform.Rotate(0f, 0f, 0f, 0f);
                wall.transform.localScale = new Vector3(15f, 30f, 4f);
                Debug.Log("---------------------------------------RESTART!------------------------------------------");
            }

            if (rewardCounter > 15)
            {
                SaveWeightsToFile();
                Debug.Log("------------------------------------WEIGHTS_SAVED!-------------------------------------");
                Debug.Break();
            }
        }
        else
        {
            reLearnCounter--;
        }

        this.transform.position        = vehicleStartPos + new Vector3(Random.Range(-20, 20), 0, (Random.Range(-5, 5)));
        this.transform.rotation        = vehicleStartRot;
        rb.velocity                    = new Vector3(0f, 0f, 0f);
        rb.angularVelocity             = new Vector3(0f, 0f, 0f);
        LightSource.transform.position = lightStartPos + new Vector3(Random.Range(-20, 20), 0, (Random.Range(-20, 10)));
        wall.transform.position        = wallStartPos + new Vector3(Random.Range(-10, 10), 0, 0);
        wall.transform.Rotate(0f, Random.Range(-180f, 180f), 0f, 0f);
        wall.transform.localScale = new Vector3(Random.Range(10f, 20f), 30f, 4f);
        //deltaCounter = 20f;
        intensityOld  = 0.0f;
        win           = false;
        collisionFail = false;
        backFail      = false;
        resetCounter++;
        Debug.Log(Round(resetCounter / rlcValue) + "." + resetCounter + ".Total reward: " + Rewards.Sum());
        reward     = 0;
        resetTimer = 500;
        //intensityOld = 100f;
    }
Exemplo n.º 26
0
    void LoadWeightsFromFile(ANN ann)
    {
        string       path = Application.dataPath + "/weights.txt";
        StreamReader wf   = File.OpenText(path);

        if (File.Exists(path))
        {
            string line = wf.ReadLine();
            ann.LoadWeights(line);
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        // Constructor
        // Rule of thumb: A good starting number of neurons is a number between zero and the number of inputs.
        // Six inputs, one output, one hidden layer, four neurons, learning rate
        ann = new ANN(6, 1, 1, 4, 0.05);  // 0.11 learning rate worked well
        // ann = new ANN(6, 1, 1, 4, 0.001);  // 0.001 the ANN with this learning rate performed very poorly

        // Now, capture the rigid body on the ball to get the ball speed to feed to the neural network
        brb = ball.GetComponent <Rigidbody2D>();
    }
Exemplo n.º 28
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();
    }
 private void Start()
 {
     ann = new ANN(5, 2, 1, 10, 0.5);
     if (loadWeightsFromFile)
     {
         LoadWeightsFromFile();
         finishedTraining = true;
     }
     else
     {
         StartCoroutine(LoadTrainingSet());
     }
 }
 // Use this for initialization
 void Start()
 {
     ann = new ANN(5, 2, 1, 10, 0.5);
     if (loadFromFile)
     {
         LoadWeightsFromFile();
         trainingDone = true;
     }
     else
     {
         StartCoroutine(LoadTrainingSet());
     }
 }
Exemplo n.º 31
0
 public ANNTrainer(ANN nn)
 {
     this.nn = nn;
     this.score = 0;
 }