// Start is called before the first frame update
 void Start()
 {
     col            = GetComponent <Collider>();
     rb             = GetComponent <Rigidbody>();
     dna_obj        = GetComponent <dna>();
     popM           = GameObject.FindGameObjectWithTag("Population Manager").GetComponent <population_manager>();
     rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezeRotationX;
 }
 public void setDNA(dna nD)
 {
     this.weights_1 = copy_matrix(nD.getWeights1());
     this.weights_2 = copy_matrix(nD.getWeights2());
     this.weights_3 = copy_matrix(nD.getWeights3());
     this.bias1     = copy_matrix(nD.getb1());
     this.bias2     = copy_matrix(nD.getb2());
     this.bias3     = copy_matrix(nD.getb3());
 }
    public dna crossover_DNA(dna parent_b)
    {
        Matrix <float> w1 = copy_matrix(parent_b.getWeights1());
        Matrix <float> w2 = copy_matrix(parent_b.getWeights2());
        Matrix <float> w3 = copy_matrix(parent_b.getWeights3());
        Matrix <float> b1 = copy_matrix(parent_b.getb1());
        Matrix <float> b2 = copy_matrix(parent_b.getb2());
        Matrix <float> b3 = copy_matrix(parent_b.getb3());

        Matrix <float> new_w1 = copy_matrix(weights_1);

        for (int i = 0; i < (this.weights_1.RowCount * this.weights_1.ColumnCount) / 2; i++)
        {
            int randI = Random.Range(0, this.weights_1.RowCount);
            int randJ = Random.Range(0, this.weights_1.ColumnCount);
            new_w1[randI, randJ] = w1[randI, randJ];
        }

        Matrix <float> new_w2 = copy_matrix(weights_2);

        for (int i = 0; i < (this.weights_2.RowCount * this.weights_2.ColumnCount) / 2; i++)
        {
            int randI = Random.Range(0, this.weights_2.RowCount);
            int randJ = Random.Range(0, this.weights_2.ColumnCount);
            new_w2[randI, randJ] = w2[randI, randJ];
        }

        Matrix <float> new_w3 = copy_matrix(weights_3);

        for (int i = 0; i < (this.weights_3.RowCount * this.weights_3.ColumnCount) / 2; i++)
        {
            int randI = Random.Range(0, this.weights_3.RowCount);
            int randJ = Random.Range(0, this.weights_3.ColumnCount);
            new_w3[randI, randJ] = w3[randI, randJ];
        }

        Matrix <float> new_b1 = copy_matrix(bias1);

        for (int i = 0; i < (this.bias1.RowCount * this.bias1.ColumnCount) / 2; i++)
        {
            int randI = Random.Range(0, this.bias1.RowCount);
            new_b1[randI, 0] = b1[randI, 0];
        }

        Matrix <float> new_b2 = copy_matrix(bias2);

        for (int i = 0; i < (this.bias2.RowCount * this.bias2.ColumnCount) / 2; i++)
        {
            int randI = Random.Range(0, this.bias2.RowCount);
            new_b2[randI, 0] = b2[randI, 0];
        }

        Matrix <float> new_b3 = copy_matrix(bias3);

        for (int i = 0; i < (this.bias3.RowCount * this.bias3.ColumnCount) / 2; i++)
        {
            int randI = Random.Range(0, this.bias3.RowCount);
            new_b3[randI, 0] = b3[randI, 0];
        }

        dna new_dna = new dna();

        new_dna.setDNA(new_w1, new_w2, new_w3, new_b1, new_b2, new_b3);
        return(new_dna);
    }
Exemple #4
0
 Console.WriteLine(MedianString(dna, k));
    public void reset_day()
    {
        move_cam = false;
        List <float> fit = new List <float>();
        float        sum = 0;

        for (int i = 0; i < num_org; i++)
        {
            fit.Add(organisms[i].getFitness());
            sum += fit[i];
        }
        fit.Sort();
        organisms_sort();
        for (int i = 0; i < fit.Count; i++)
        {
            if (i == 0)
            {
                fit[i] = fit[i] / sum;
            }
            else
            {
                fit[i] = (fit[i] / sum) + fit[i - 1];
            }
        }

        List <dna> tempO = new List <dna>();

        for (int i = 0; i < num_org; i++)
        {
            dna parent_a = new dna();
            dna parent_b = new dna();

            //int a_rand = Random.Range(0,num_org);
            //int b_rand = Random.Range(0,num_org);

            while (parent_a == null || parent_b == null)
            {
                float a_rand = Random.Range(0.0f, 1.0f);
                float b_rand = Random.Range(0.0f, 1.0f);


                for (int j = 0; j < fit.Count - 1; j++)
                {
                    if (a_rand < fit[j + 1] && a_rand >= fit[j])
                    {
                        parent_a = organisms[j + 1];
                        //Debug.Log(a_rand + " Organism Selected: "+ (j+1));
                    }

                    if (b_rand < fit[j + 1] && b_rand >= fit[j])
                    {
                        parent_b = organisms[j + 1];
                    }
                }
            }


            /*
             * while(a_randT >  fit[a_rand])
             * {
             *  if(a_randT <  fit[a_rand] )
             *  {
             *      parent_a = organisms[a_rand];
             *  }
             *  a_rand = Random.Range(0,num_org);
             *  a_randT = Random.Range(0.0f,1.0f);
             * }
             * while(b_randT >  fit[b_rand])
             * {
             *  if(b_randT <  fit[b_rand] )
             *  {
             *      parent_b = organisms[b_rand];
             *  }
             *  b_rand = Random.Range(0,num_org);
             *  b_randT = Random.Range(0.0f,1.0f);
             * }
             */

            //parent_a = organisms[organisms.Count-1];
            //parent_b = organisms[organisms.Count-2];

            tempO.Add(parent_a.crossover_DNA(parent_b));
            if (i == 0 || i == 1)
            {
                organisms[i].initialize_DNA();
            }
            else
            {
                organisms[i].setDNA(tempO[i]);
            }
            organisms[i].mutate_DNA();
            organisms[i].transform.position = start_location.position;
            organisms[i].transform.rotation = Quaternion.identity;
            organisms[i].setAlive(true);
            organisms[i].setFit(0);
        }
        cam.transform.position = new Vector3(start_location.position.x, cam.transform.position.y, start_location.position.z);
        running_time           = 0;
        current_day           += 1;
        num_dead   = 0;
        start_time = Time.time;
        move_cam   = true;
    }