// Update is called once per frame
    void Update()
    {
        GeneratePipes();

        if (pipes.Count > 0 && pipes[0] != null)
        {
            for (int i = 0; i < birds.Count; i++)
            {
                RedeNeural birdNN = birds[i].GetComponent <Player>().nn;

                double distX = pipes[0].transform.position.x - birds[i].transform.position.x;
                double distY = pipes[0].transform.position.y - birds[i].transform.position.y;

                double[] input  = new double[2];
                double[] output = new double[1];

                input[0] = distX;
                input[1] = distY;

                if (birdNN.predict(input)[0] > 0.5d)
                {
                    birds[i].GetComponent <Player>().Jump();
                }
            }
        }
    }
 void Awake()
 {
     // nn = new RedeNeural(2, 6, 1);
     nn          = gameObject.AddComponent <RedeNeural>();
     rb          = GetComponent <Rigidbody2D>();
     gameManager = GameObject.FindGameObjectWithTag("GameManager").GetComponent <GameManager>();
 }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            RedeNeural redeNeural = new RedeNeural(1, 3, 5);

            int[] input = { 1, 2 };
            redeNeural.FeedFoward(input);
        }
    private void RespawnBirds()
    {
        for (int i = 0; i < birds.Count; i++)
        {
            bool canMutate = true;

            birds[i].transform.position = new Vector3(UnityEngine.Random.Range(-7f, -4f), UnityEngine.Random.Range(-2f, 2f), 0);
            birds[i].GetComponent <SpriteRenderer>().enabled   = true;
            birds[i].GetComponent <CircleCollider2D>().enabled = true;
            birds[i].GetComponent <Player>().enabled           = true;
            birds[i].GetComponent <Player>().distance          = 0;
            birds[i].GetComponent <Player>().score             = 0;
            birds[i].GetComponent <Player>().fitness           = 0;

            for (int j = 0; j < bestBirds.Count; j++)
            {
                if (birds[i] == bestBirds[j])
                {
                    canMutate = false;
                }
            }

            if (canMutate)
            {
                RedeNeural birdNN = birds[i].GetComponent <Player>().nn;

                float rnd = UnityEngine.Random.Range(0f, 100f);

                if (bestBirdsNN[0])
                {
                    birdNN = Instantiate(bestBirdsNN[0]);
                }

                if (rnd < 5f)
                {
                    // Mutation
                    birdNN.weights_ih = Matrix.mutation(bestBirdsNN[0].weights_ih, bestBirdsNN[1].weights_ih);
                    birdNN.weights_ho = Matrix.mutation(bestBirdsNN[0].weights_ho, bestBirdsNN[1].weights_ho);
                }
                else if (rnd < 10f)
                {
                    // Crossover
                    birdNN.weights_ih = Matrix.crossover(bestBirdsNN[0].weights_ih, bestBirdsNN[1].weights_ih);
                    birdNN.weights_ho = Matrix.crossover(bestBirdsNN[0].weights_ho, bestBirdsNN[1].weights_ho);
                }

                birds[i].GetComponent <Player>().nn = birdNN;
                // Debug.Log(birdNN.weights_ho.data[0][0]);
                // Debug.Log(bestBirdsNN[0].weights_ho.data[0][0]);
            }
        }
    }
Ejemplo n.º 5
0
    // Start is called before the first frame update
    void Start()
    {
        var timeA = DateTime.Now;
        var rede  = new RedeNeural(6, 4, 2);
        var arr   = new double[] { 1, 2, 3, 4, 5, 6 };

        rede.feedFoward(arr);
        var timeB = DateTime.Now;

        var time = timeB - timeA;

        UnityEngine.Debug.Log(time);
    }
    private void GenerateBirds()
    {
        for (int i = 0; i < 100; i++)
        {
            GameObject bird = Instantiate(birdPrefab);

            RedeNeural birdNN = bird.GetComponent <Player>().nn;
            birdNN.weights_ih.randomize();
            birdNN.weights_ho.randomize();
            birdNN.bias_ih.randomize();
            birdNN.bias_ho.randomize();

            bird.transform.parent   = birdsTransform;
            bird.transform.position = new Vector3(UnityEngine.Random.Range(-8f, -5f), UnityEngine.Random.Range(-2f, 2f), 0);
            birds.Add(bird);
        }
    }
Ejemplo n.º 7
0
 void Start()
 {
     car     = GetComponent <CarBehaviour>();
     network = new RedeNeural(car.distanceSensors.Length, car.distanceSensors.Length - 1, 2);
     Sensors = new double[car.distanceSensors.Length];
 }