Example #1
0
        internal void generatePopul(int countOsob)
        {
            for (int i = 0; i < countOsob; i++)
            {
                int[] hrom = new int[foods.Count];
                for (int j = 0; j < foods.Count; j++)
                {
                    hrom[j] = rand.Next(0, 2);
                }



                int n = 0;
                for (int j = 0; j < foods.Count; j++)
                {
                    if (hrom[j] == 0)
                    {
                        n++;
                    }
                }
                if (n == foods.Count)
                {
                    i--;
                }
                else
                {
                    Osob osob = new Osob(hrom);

                    popul.Add(osob);
                }
            }
        }
Example #2
0
        internal void checkFoods()
        {
            countOfFood();
            foreach (var food in foods)
            {
                if (food.mainHar == 0)
                {
                    int[] hrom = new int[foods.Count];
                    for (int j = 0; j < foods.Count; j++)
                    {
                        hrom[j] = rand.Next(0, 2);
                    }
                    hrom[foods.IndexOf(food)] = 1;
                    Osob osob = new Osob(hrom);

                    popul.Add(osob);
                }
            }
        }
Example #3
0
        private void ruletka()
        {
            List <Osob> result = popul.OrderBy(u => u.mainHar).ToList();

            popul.Clear();
            double sumPrisp = 0;

            foreach (var res in result)
            {
                popul.Add(res);
                sumPrisp += res.mainHar;
            }

            int st = 0;

            foreach (var osob in popul)
            {
                osob.start  = st;
                osob.finish = osob.start + (int)(sumPrisp / osob.mainHar);
                st         += (int)(sumPrisp / osob.mainHar);
            }

            int skr = rand.Next(foods.Count);

            for (int p = 0; p < 5; p++)
            {
                int f      = rand.Next(st);
                var first  = popul.FirstOrDefault(u => u.start <= f & u.finish > f);
                int s      = rand.Next(st);
                var second = popul.FirstOrDefault(u => u.start <= s & u.finish > s);


                int[] par1 = first.hrom;
                int[] par2 = second.hrom;


                int[] hrom1 = new int[foods.Count];
                int[] hrom2 = new int[foods.Count];
                for (int j = 0; j < foods.Count; j++)
                {
                    if (j < skr)
                    {
                        hrom1[j] = par1[j];
                        hrom2[j] = par2[j];
                    }
                    else
                    {
                        hrom1[j] = par2[j];
                        hrom2[j] = par1[j];
                    }
                }
                Osob osob = new Osob(hrom1);
                childs.Add(osob);
                Osob osob1 = new Osob(hrom2);
                childs.Add(osob1);
            }


            popul.Clear();
            foreach (Osob child in childs)
            {
                popul.Add(child);
            }

            childs.Clear();
        }