/// <summary> /// Ген. Алгоритм /// </summary> /// <param name="CountSpecies">К-во особей</param> /// <param name="_Count_Genes">К-во генов</param> /// <param name="_Weight_Genes">Стоимость</param> /// <param name="_Price_Genes">Вес</param> public cGenAlg(int CountSpecies, int _Count_Genes, int[] _Weight_Genes, int[] _Price_Genes, int _MaxPrice, int _Count_Parent, int _Count_iteration) { Population = new cSpecies[CountSpecies]; Weight_Genes = _Weight_Genes; Price_Genes = _Price_Genes; Count_Genes = _Count_Genes; Count_Parent = _Count_Parent; MaxPrice = _MaxPrice; Count_iteration = _Count_iteration; Max = new int[Count_iteration]; Average = new int[Count_iteration]; //Создаем первую популяцию New_GenAlg(CountSpecies); for (int i = 0; i < Count_iteration; i++) { Get_parents(Population, Count_Parent); Get_new_population(Parent); Max[i] = Population[0].Weight; Average[i] = _Average / Population.Count(); } Sort(Population); ResultChild = Population[0]; }
public cSpecies Mutation(cSpecies _Species) { cSpecies Species = _Species; Random rnd = new Random(); int i = rnd.Next(_Species.Gene.Count()); Species.Gene[i] = (Species.Gene[i]) ? false : true; return(Species); }
public void Get_Weight_Price(cSpecies Species, int[] _Weight, int[] _Price) { Species.Weight = 0; Species.Price = 0; for (int i = 0; i < Species.Gene.Count(); i++) { Species.Weight += (Species.Gene[i]) ? _Weight[i]: 0; Species.Price += (Species.Gene[i]) ? _Price[i] : 0; } }
public cSpecies New_Species(int Count, int[] _Weight, int[] _Price) { cSpecies Species = new cSpecies(Count); Random rnd = new Random(); for (int i = 0; i < Count; i++) { Species.Gene[i] = (rnd.Next(0, 2) == 1) ? true : false; } Get_Weight_Price(Species, _Weight, _Price); return(Species); }
/// <summary> /// Создать первую популяцию /// </summary> /// <param name="CountSpecies"></param> private void New_GenAlg(int CountSpecies) { cSpecies Species = new cSpecies(Count_Genes); for (int i = 0; i < CountSpecies; i++) { do { Species = Species.New_Species(Count_Genes, Weight_Genes, Price_Genes); if (Species.Price <= MaxPrice) { Population[i] = Species; } } while (Population[i] == null); Thread.Sleep(20); //Костыли подъехали } }
private void Get_new_population(cSpecies[] Parent) { Random rnd = new Random(); cSpecies[] New_Population = new cSpecies[Population.Count()]; _Average = 0; for (int i = 0; i < Population.Count(); i++) { do { //Выбираем случайных родителей cSpecies Parent1 = Parent[rnd.Next(0, Count_Parent)]; cSpecies Parent2 = Parent[rnd.Next(1, Count_Parent)]; cSpecies Child = new cSpecies(Count_Genes); //Собираем новые гены for (int j = 0; j < Count_Genes; j++) { if (j % 2 == 0) { Child.Gene[j] = Parent1.Gene[j]; } else { Child.Gene[j] = Parent2.Gene[j]; } } int r = rnd.Next(0, 100); if (r < 10) { Child = Child.Mutation(Child); } Child.Get_Weight_Price(Child, Weight_Genes, Price_Genes); if (Child.Price <= MaxPrice) { New_Population[i] = Child; } } while (New_Population[i] == null); _Average += New_Population[i].Weight; } Population = New_Population; }