Esempio n. 1
0
File: GA.cs Progetto: uwmyuan/ICITN
        }//获得当前种群的最优适应度

        public void Initializer(ref GAChromosome GAChro)
        {
            int    flag1 = 0;
            int    flag2 = 1;
            double flag3;

            while (flag2 == 1)
            {
                for (int i = 0; i < p; i++)
                {
                    flag3 = m_Random.NextDouble();
                    if (flag3 > 0.5 && flag1 != r)
                    {
                        GAChro.Add(1);
                        flag1++;
                    }
                    else
                    {
                        GAChro.Add(0);
                    }
                }
                if (flag1 == r)
                {
                    flag2 = 0;
                }
                else
                {
                    GAChro.Clear();
                    flag1 = 0;
                }
            }
        }//染色体初始化
Esempio n. 2
0
File: GA.cs Progetto: uwmyuan/ICITN
        }//交叉

        public void Mutation(ref GAChromosome chromose)
        {
            int index = ((int)m_Random.Next(0, p));
            int count = ((int)m_Random.Next(0, p - index));

            chromose.Reverse(index, count);
        }//变异
Esempio n. 3
0
File: GA.cs Progetto: uwmyuan/ICITN
        }//变异

        public int[] output()
        {
            int[]        r     = new int[p];
            GAChromosome GAChr = this.GetBestChromosome();

            for (int i = 0; i < p; i++)
            {
                r[i] = GAChr[i];
            }
            return(r);
        }
Esempio n. 4
0
File: GA.cs Progetto: uwmyuan/ICITN
 public void Initialize(Parameter Par)
 {
     //PopulationSizeCalculate();
     PopulationSize = 100;
     if (PopulationSize >= 50)
     {
         ApplyElitism = true;
     }
     for (int i = 0; i < PopulationSize; i++)//PopulationSize为染色体数
     {
         GAChromosome newParent = new GAChromosome(Par);
         this.Initializer(ref newParent);
         newParent.FitnessCalculate();
         m_thisGeneration.Add(newParent);
     }
     TotalFitness.Clear();
     RankPopulation(m_thisGeneration);
     TotalFitnessRecord();
 }//初始化种群
Esempio n. 5
0
File: GA.cs Progetto: uwmyuan/ICITN
        }//染色体初始化

        public void Initializer1(ref GAChromosome GAChro)
        {
            List <int> ones = new List <int>();

            ones.Clear();
            while (ones.Count < p)
            {
                int  temp = m_Random.Next(0, n);
                bool flag = true;
                for (int i = 0; i < ones.Count; i++)
                {
                    if (temp == ones[i])
                    {
                        flag = false;
                        continue;
                    }
                }
                if (flag)
                {
                    ones.Add(temp);
                }
            }
            GAChro.Clear();
            for (int i = 0; i < n; i++)
            {
                bool flag = true;
                for (int j = 0; j < ones.Count; j++)
                {
                    if (ones[j] == i)
                    {
                        GAChro.Add(1);
                        ones.Remove(i);
                        flag = false;
                    }
                }
                if (flag)
                {
                    GAChro.Add(0);
                }
            }
        }
Esempio n. 6
0
File: GA.cs Progetto: uwmyuan/ICITN
        public void CrossOver(GAChromosome Dad, GAChromosome Mum, ref GAChromosome child1, ref GAChromosome child2)
        {
            int        countDad = 0, countMum = 0;
            List <int> crosspoint = new List <int>();

            for (int i = 0; i < p; i++)
            {
                if (Dad[i] == 1)
                {
                    countDad++;
                }
                if (Mum[i] == 1)
                {
                    countMum++;
                }
                if (countDad == countMum)
                {
                    crosspoint.Add(i);
                }
                ;
            }
            switch (Crosstype = 1)
            {
            //一点交叉
            case 1:
            {
                int count = crosspoint.Count;
                int j     = ((int)m_Random.Next(0, count));
                int index = (int)crosspoint[j];
                for (int k = 0; k < p; k++)
                {
                    if (k <= index)
                    {
                        child1.Add(Dad[k]);
                        child2.Add(Mum[k]);
                    }
                    else
                    {
                        child1.Add(Mum[k]);
                        child2.Add(Dad[k]);
                    }
                }
                break;
            }

            //多点交叉
            case 2:
            {
                for (int i = 0; i < crosspoint.Count; i++)
                {
                    if (m_Random.NextDouble() > 0.5)
                    {
                        for (int k = crosspoint[i]; k < crosspoint[i + 1]; k++)
                        {
                            child1.Add(Dad[k]);
                            child2.Add(Mum[k]);
                        }
                    }
                    else
                    {
                        for (int k = crosspoint[i]; k < crosspoint[i + 1]; k++)
                        {
                            child1.Add(Mum[k]);
                            child2.Add(Dad[k]);
                        }
                    }
                }
                break;
            }
            }
        }//交叉
Esempio n. 7
0
File: GA.cs Progetto: uwmyuan/ICITN
        public GAChromosome GetBestChromosome()
        {
            GAChromosome bestChromosome = m_thisGeneration[0];

            return(bestChromosome);
        }//获得当前种群的最优适应度
Esempio n. 8
0
File: GA.cs Progetto: uwmyuan/ICITN
        }//对染色体按适应度降序排序

        public void CreateNextGeneration(Parameter Par)
        {
            m_nextGeneration.Clear();
            GAChromosome bestChromo = null;

            if (this.ApplyElitism) //最优秀适应度最好是否直接选择加入到新一代群体
            {
                bestChromo = m_thisGeneration[0];
            }//取出最优染色体(已排序)
            for (int i = 0; i < this.PopulationSize; i += 2)
            {
                //Step1选择
                int iDadParent = 0;
                int iMumParent = 0;
                if (this.Selection == SelectionType.Tournment)//竞争法
                {
                    iDadParent = TournamentSelection();
                    iMumParent = TournamentSelection();
                }
                else if (this.Selection == SelectionType.Roullette)//轮赌法
                {
                    iDadParent = RoulletteSelection();
                    iMumParent = RoulletteSelection();
                }
                GAChromosome Dad    = (GAChromosome)m_thisGeneration[iDadParent];
                GAChromosome Mum    = (GAChromosome)m_thisGeneration[iMumParent];
                GAChromosome child1 = new GAChromosome(Par);
                GAChromosome child2 = new GAChromosome(Par);
                //Step2交叉
                if (m_Random.NextDouble() < this.CrossRate)
                {
                    CrossOver(Dad, Mum, ref child1, ref child2);
                }
                else
                {
                    child1 = Dad;
                    child2 = Mum;
                }
                //Step3变异
                if (m_Random.NextDouble() < this.MutationRate)
                {
                    this.Mutation(ref child1);
                    this.Mutation(ref child2);
                }
                //Step4计算适应度
                child1.FitnessCalculate();
                child2.FitnessCalculate();
                m_nextGeneration.Add(Dad);
                m_nextGeneration.Add(Mum);
                m_nextGeneration.Add(child1);
                m_nextGeneration.Add(child2);
            }
            if (null != bestChromo)
            {
                m_nextGeneration.Add(bestChromo); //最优的染色体插入子代群体中
            }
            RankPopulation(m_nextGeneration);     //按照适应度对染色体排序
            m_thisGeneration.Clear();             //用新一代替换当前群体
            for (int j = 0; j < this.PopulationSize; j++)
            {
                m_thisGeneration.Add(m_nextGeneration[j]);
            }
            TotalFitnessRecord();

            //using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\BestFitness_RUE.txt", true))
            //{
            //    file.WriteLine(m_thisGeneration[0].Fitness.ToString());
            //}
            this.GenerationNum++;//进化代计数
        }//产生下一代种群