Ejemplo n.º 1
0
    private void GeneratePopulation(bool isFirst = false)
    {
        Generation++;

        if (isFirst /* || (!isFirst && GameObject.FindObjectOfType<UIManager>().bestFitLocal == 0) */)
        {
            for (int i = 0; i < CellsStartCount; i++)
            {
                var neuralNetwork = new NeuralNetwork(5, 2, new int[] { 5, 4 }, 4);
                neuralNetwork.InitializeWeight();
                AddCell().Initialize(neuralNetwork);
            }
        }
        else
        {
            IEnumerable <KeyValuePair <NeuralNetwork, float> > arr = dictionary.OrderByDescending(t => t.Value);
            var arr2 = arr.ToList();
            arr2.RemoveRange(CellsSelectionCount, arr2.Count - CellsSelectionCount);

            List <NeuralNetwork> neuralNetworks = new List <NeuralNetwork>();

            foreach (var x in arr2)
            {
                neuralNetworks.Add(x.Key);
            }

            for (int i = 0; i < neuralNetworks.Count; i++)
            {
                for (int j = 0; j < neuralNetworks.Count; j++)
                {
                    NeuralNetwork child = Genetic.CrossOver(neuralNetworks[i], neuralNetworks[j]);
                    var           save  = child.Save();
                    Genetic.Mutate(ref save.dna);
                    child = new NeuralNetwork(save);
                    AddCell().Initialize(child);
                }
            }

            dictionary.Clear();

            for (int i = 0; i < CellsToDestroy.Count; i++)
            {
                Destroy(CellsToDestroy[i].gameObject);
            }

            CellsToDestroy.Clear();
        }
        GameObject.FindObjectOfType <UIManager>().resetBestFitLocal();
    }
Ejemplo n.º 2
0
    void Update()
    {
        if (!Initialized)
        {
            return;
        }

        float foodPresence     = -1f;
        float cellPresence     = -1f;
        float poisonPresence   = -1f;
        float obstaclePresence = -1f;
        float canReproduce     = -1f;

        float turnLeft  = 0f;
        float turnRight = 0f;
        float moving    = 0f;
        float reproduce = 0f;

        Cell NearCell = null;

        RaycastHit2D hit2D;

        var baseLinePosition = transform.position + (transform.up * (GetComponent <CircleCollider2D>().radius + .1f));

        if ((hit2D = Physics2D.Raycast(baseLinePosition, transform.up, Mathf.Infinity)))
        {
            Debug.DrawLine(baseLinePosition, hit2D.point, Color.blue);

            if (hit2D.collider.tag == "Food")
            {
                foodPresence = 1f;
            }
            else if (hit2D.collider.tag == "Cell")
            {
                cellPresence = 1f;
                if (hit2D.distance < .5f)
                {
                    NearCell = hit2D.collider.transform.GetComponent <Cell>();
                }
            }
            else if (hit2D.collider.tag == "Poison")
            {
                poisonPresence = 1f;
            }
            else if (hit2D.collider.tag == "Obstacle")
            {
                obstaclePresence = 1f;
            }
            else
            {
                Debug.Log("Unknow object tag!");
            }
        }

        if (FoodStock >= 2)
        {
            canReproduce = 1f;
        }

        var Inputs = new float[] { foodPresence, cellPresence, poisonPresence, obstaclePresence, canReproduce };

        neuralNetwork.SetInput(Inputs);
        neuralNetwork.Propagate();
        var Outputs = neuralNetwork.GetOutput();

        turnLeft  = (Outputs[0] + 1f) / 2f;
        turnRight = (Outputs[1] + 1f) / 2f;
        moving    = (Outputs[2] + 1f) / 2f;
        reproduce = Outputs[3];

        if (reproduce > 0 && NearCell != null && canReproduce == 1f)
        {
            NeuralNetwork child = Genetic.CrossOver(neuralNetwork, NearCell.neuralNetwork);
            var           save  = child.Save();
            Genetic.Mutate(ref save.dna);
            child = new NeuralNetwork(save);
            GameObject.FindObjectOfType <WorldManager>().AddCell().Initialize(child);
            FoodStock -= 2;
            fitness   += 1f;
        }


        transform.Rotate(Vector3.forward * turnLeft * -80f * Time.deltaTime);
        transform.Rotate(Vector3.forward * turnRight * 80f * Time.deltaTime);
        if (moving > .5f)
        {
            transform.Translate(Vector3.up * 3f * Time.deltaTime);
        }

        //fitness += turnLeft / 100;
        //fitness += turnRight / 100;

        //fitness += moving / 100;

        life -= Time.deltaTime;

        if (life <= 0)
        {
            GameObject.FindObjectOfType <WorldManager>().RemoveCell(this.transform);
        }

        uIManager.SetLocalBestFit(fitness);
    }