// Use this for initialization
 void Start()
 {
     genNOindicator = GetComponentInChildren <Text> ();
     ptr            = -1;
     finished       = false;
     popSIze        = 100;
     Debug.Log("starting");
     startObj         = new population(popSIze);
     bestgenesholder1 = new List <int[]> ();
     bestgenesholder1 = startObj.bestgenesholder;
     startInatantiating();
 }
Ejemplo n.º 2
0
    //void Start
    void Start()
    {
        inputsTO_NN = new float[InputCountANN];
        layers      = new int[] { InputCountANN, 9, 9, 8 };

        net = new NeuralNetwork(layers);

        string[] g = net.GetAllWeightNN();

        float[] DF = new float[7];
        DF[0] = 0; DF[1] = 100; DF[2] = 0; DF[3] = 0; DF[4] = 0; DF[5] = 0; DF[6] = 0;

        //creamos nueva pobacion y pasamos los pesos de la ANN y los datos para el calculo de fitness
        p = new population(g, DF);
    }
Ejemplo n.º 3
0
        //Мутация
        public void Mutation(population parent)
        {
            double Pm = 2 / (numberOfIndividuals * 1.0);

            for (int i = 0; i < parent.Population.Count; i++)
            {
                for (int j = 0; j < parent.Population[i].Genes.Count(); j++)
                {
                    double p = random.NextDouble();
                    if (Pm > p)
                    {
                        parent.Population[i].Genes[j] += random.Next(-5, 11);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        //отбор при помощи турнинрой селекции с турниром (размер турнира в переменной)
        public void choice(population parent, population selectionparent)
        {
            Random     rand = new Random();
            population parentForSelection = new population();

            for (int i = 0; i < numberOfIndividuals / 2; i++)
            {
                int    theBest       = 0;
                double theBestresult = 50000;
                for (int j = 0; j < sizeTourney; j++)
                {
                    int in1 = rand.Next(0, numberOfIndividuals);
                    if (absolutlifitnessPersent(parent.Population[in1], model) < theBestresult)
                    {
                        theBestresult = absolutlifitnessPersent(parent.Population[in1], model);
                        theBest       = in1;
                    }
                }
                parentForSelection.Population.Add(parent.Population[theBest]);
                selectionparent.Population.Add(parent.Population[theBest]);
            }
            parent.Population.Clear();
            individual child1;
            individual child2;

            while (parent.Population.Count < numberOfIndividuals)
            {
                int i = rand.Next(0, numberOfIndividuals / 2);
                int j = rand.Next(0, numberOfIndividuals / 2);
                if (p > rand.NextDouble())
                {
                    crossingover(parentForSelection.Population[i], parentForSelection.Population[j], child1 = new individual(), child2 = new individual());
                }
                else
                {
                    child1       = new individual();
                    child2       = new individual();
                    child1.Genes = parentForSelection.Population[i].Genes;
                    child2.Genes = parentForSelection.Population[j].Genes;
                }
                parent.Population.Add(child1);
                parent.Population.Add(child2);
            }
        }
 RunGA(population, iterations, Crosseover, crossoverProbability, fitness, mutation, selection);
Ejemplo n.º 6
0
        private void button_win_Click(object sender, EventArgs e)
        {
            //данные с формы
            numberOfIndividuals = Convert.ToInt32(PopulationNumber.Text);
            sizeTourney         = Convert.ToInt32(textBox_sizeTourney.Text);

            //храним родителей, родителей для селекции и потомство
            parents             = new population();
            parentsForSelection = new population();
            children            = new population();

            Random rnd = new Random();

            //Эталонная особь
            model.Genes = new int[] { 96, 96, 159 };
            //Генерация начальной популяции
            population FirstPopulation = new population();

            for (int i = 0; i < numberOfIndividuals; i++)
            {
                individual curentIndividuals = new individual();
                curentIndividuals.Genes = new int[] { rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256) };
                FirstPopulation.Population.Add(curentIndividuals);
            }
            bool individualIsfind = false;
            int  iter             = 0;

            listBox_number.Items.Clear();
            while (!individualIsfind)
            {
                double theBestreslt = 50000;
                for (int i = 0; i < FirstPopulation.Population.Count; i++)
                {
                    double rez = absolutlifitnessPersent(FirstPopulation.Population[i], model);
                    if (rez < theBestreslt)
                    {
                        theBestreslt = rez;
                    }
                    if (absolutlifitnessPersent(FirstPopulation.Population[i], model) == 0)
                    {
                        individualIsfind = true;
                        int number = i;
                        break;
                    }
                }
                rezult.Add(theBestreslt);
                if (!individualIsfind)
                {
                    iter++;
                    foreach (var item in FirstPopulation.Population)
                    {
                        parents.Population.Add(item);
                    }
                    listBox_number.Items.Add(iter);
                    choice(FirstPopulation, parentsForSelection);
                    foreach (var item in FirstPopulation.Population)
                    {
                        children.Population.Add(item);
                    }


                    Mutation(FirstPopulation);
                }
            }
            Form2 graph = new Form2(rezult, iter);

            graph.Show();
        }