遗传算法

昵称不能为空噢噢噢噢 2011-09-01 11:25:21
来点C#代码瞧瞧
...全文
183 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangbudao 2013-08-09
  • 打赏
  • 举报
回复
IGeneticStrategy对象是什么东西
果-果 2011-09-21
  • 打赏
  • 举报
回复

//
//class Population
//
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace SGA.SingleGA
{
  /*
  群体类,实现了种群初始化,有交叉,变异行为,选择等行为。同时统计种群的平均适应度,最大适应度,适应度之和。
  */
  public class Population : CollectionBase,ICloneable,IDisposable
  {
    private IGeneticStrategy _iGeneticStrategy;
    public Population(IGeneticStrategy iGeneticStrategy) { this._iGeneticStrategy = iGeneticStrategy; }
    public Population() { }
    public void Init(int iMax)
    {
      Random rd = new Random();
      for (int i = 0; i < iMax; i++)
      {
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < 22; j++)
        {
          sb.Append(rd.Next(0,2));
        }
        this.List.Add(new Individual(sb.ToString()));
      }
    }
    public double MaxFitness
    {
      get
      {
        double dmax = double.MinValue;
        for (int i = 0; i < List.Count; i++)
        {
          if ((List[i] as Individual).Fitness > dmax) dmax = (List[i] as Individual).Fitness;
        }
        return dmax;
      }
    }
    public double AverageFitness
    {
      get
      {
        return SumFitness / List.Count;
      }
    }
    public double SumFitness
    {
      get
      {
        double dSum = 0;
        for (int i = 0; i < List.Count; i++)
        {
          dSum += (List[i] as Individual).Fitness;
        }
        return dSum;
      }
    }
    public IGeneticStrategy GeneticStrategy
    {
      get { return this._iGeneticStrategy; }
      set { this._iGeneticStrategy = value; }
    }
    public Population Select()
    {
      if (_iGeneticStrategy == null) return null;
      return _iGeneticStrategy.Select(this);
    }
    public void Crossover()
    {
      if (_iGeneticStrategy == null) return;
      _iGeneticStrategy.Crossover(this);
    }
    public void Mutation()
    {
      if (_iGeneticStrategy == null) return;
      _iGeneticStrategy.Mutation(this);
    }
    public Individual this[int index]
    {
      get { return (Individual)this.List[index]; }
      set { this.List[index] = value; }
    }
    public void Add(Individual ind)
    {
      this.List.Add(ind);
    }
    public int Indexof(Individual ind)
    {
      return this.List.IndexOf(ind);
    }
    public void Print()
    {
      for (int i = 0; i < List.Count; i++)
      {
        Console.WriteLine("第{0}个体fit={2} {1}", i.ToString(), (List[i] as Individual).Variable.ToString(), (List[i] as Individual).Fitness);
      }
    }
    #region ICloneable 成员
    public object Clone()
    {
      Population pop = new Population(this.GeneticStrategy);
      for (int i = 0; i < this.List.Count; i++)
        pop.List.Add(this.List[i]);
      return pop;
    }
    #endregion
    #region IDisposable 成员
    public void Dispose()
    {
      _iGeneticStrategy = null;
    }
    #endregion
  }
}
//
//Individual
//
using System;
using System.Collections.Generic;
using System.Text;
namespace SGA.SingleGA
{
  public class Individual : ICloneable
  {
    private string _gene;
    private double _fitness = double.MinValue;
    public static IFitness _calFit = new OneDFitness();
    public string Gene
    {
      get { return _gene; }
      set
      {
        _gene = value;
        _fitness = _calFit.Fitness(Variable);
      }
    }
    public double Fitness
    {
      get { return _fitness; }
    }
    public double Variable
    {
      get { return Coder.ToReal(_gene, -1.0, 2.0); }
    }
    public Individual()
    {
    }
    public Individual(string sGene)
    {
      Gene = sGene;
    }
    public Individual(string sGene,IFitness calFit)
    {
      _calFit = calFit;
      this.Gene = sGene;
    }
    //public IFitness CalFit
    //{
    //  get { return this._calFit; }
    //  set
    //  {
    //    this._calFit = value;
    //    _fitness = _calFit.Fitness(Coder.ToReal(_gene, -1.0, 2.0));
    //  }
    //}
    public int ToMetrication()
    {
      return Convert.ToInt32(Gene, 2);
    }
    public static int operator * (Individual ind1,Individual ind2)
    {
      Random rd = new Random();
      int iStart = rd.Next(0, ind1.Gene.Length-2);
      int iLast = rd.Next(iStart+1,ind1.Gene.Length-1);
      while (ind1.Gene.Substring(iStart, iLast - iStart) == ind2.Gene.Substring(iStart, iLast - iStart))
      {
        iStart = rd.Next(0, ind1.Gene.Length - 2);
        iLast = rd.Next(iStart + 1, ind1.Gene.Length - 1);
      }
      StringBuilder sbGene1 = new StringBuilder();
      sbGene1.Append(ind1.Gene.Substring(0, iStart));
      sbGene1.Append(ind2.Gene.Substring(iStart, iLast-iStart));
      sbGene1.Append(ind1.Gene.Substring(iLast));
      StringBuilder sbGene2 = new StringBuilder();
      sbGene2.Append(ind2.Gene.Substring(0, iStart));
      sbGene2.Append(ind1.Gene.Substring(iStart,iLast-iStart));
      sbGene2.Append(ind2.Gene.Substring(iLast));
      ind1.Gene = sbGene1.ToString();
      ind2.Gene = sbGene2.ToString();
      return iLast - iStart;
    }
    public int Crossover(Individual ind)
    {
      return this * ind;
    }
    public int Mutation()
    {
      Random rd = new Random();
      int iPos = rd.Next(0, this.Gene.Length-1);
      StringBuilder sb = new StringBuilder(this.Gene);
      sb[iPos] = sb[iPos] == '0' ? '1' : '0';
      this.Gene = sb.ToString();
      return iPos;
    }
    public override string ToString()
    {
      return this.Gene;
    }
    public override bool Equals(object obj)
    {
      return base.Equals(obj);
    }
    public override int GetHashCode()
    {
      return base.GetHashCode();
    }
    #region ICloneable 成员
    public object Clone()
    {
      return new Individual(this.Gene);
    }
    #endregion
  }
}



遗传算法是通过模拟生物进化而进行数据寻优的一种进化算法。主要过程是初始种群的产生,选择,交叉,变异,循环迭代,直至出现最优解。本程序两个主要类,种群类与个体类。定义进化策略接口,计算适应度策略接口。进化策略接口实现三个行为,交叉,变异,选择,其中,进化策略接口可以加上自己的实现。大致实现如上:
threenewbee 2011-09-01
  • 打赏
  • 举报
回复
遗传算法并不是一个算法实现的名称,它代表一种类型的算法。
所以没有具体的问题,谈不上什么代码。
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lidandanyi 的回复:]
这个,学过,不知道你要的代码
[/Quote]要啊358429109@qq.com
小依 2011-09-01
  • 打赏
  • 举报
回复
这个,学过,不知道你要的代码
  • 打赏
  • 举报
回复
永动机不可能,相对论很难说,进化论有点可能
threenewbee 2011-09-01
  • 打赏
  • 举报
回复
最近csdn的“民科”一族经常出没。特点就是对计算机原理都缺乏了解,基本啥概念没有。了解几个名词,总是探讨一些自认为很高深很学术的问题。类似什么某某农民研究出了永动机,某某中学生推翻了相对论,某某工人修正了进化论等等。
  • 打赏
  • 举报
回复
那就来个具体的应用吧呵呵
threenewbee 2011-09-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hhqsy 的回复:]
那近似的也行嘛,难道这些算法不能用代码表达?
[/Quote]

我的意思是说,除非你提出一个具体的问题,否则讨论遗传算法没有意义。

比如说,我们求解一个非线性的多元方程。直接求解比较困难。
我们可以用遗传算法。这是一个具体问题。

算法是,我们设定两个个初始解集(基因),这个方程就是一个评价函数,我们将这两个解集拆开,各取一半(分裂、交配),得到2个新解集,然后我们改变解集(变异),重新代入方程,淘汰最差的解集(优胜劣汰),继续这个过程,直到找到近似最优解。

再比如,我们进行模型辨识,比如说我们有一个人口健康的统计数据,每个人的慢性疾病、身体特征指标,现在给定一个人的体检结果,要求评估他的身体状况,那么用于这个问题的遗传算法就完全不同。
petrie 2011-09-01
  • 打赏
  • 举报
回复
。。。
  • 打赏
  • 举报
回复
那近似的也行嘛,难道这些算法不能用代码表达?

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧