记住最初的人口 [英] Remember the initial population

查看:79
本文介绍了记住最初的人口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该xml文件如何存储大量的零,如果不是解决方案,那么它是.我想保存初始种群的遗传算法.

How can the xml file and store large quantities of zero, and if it is not a solution it is. I want to save the initial population Genetic Algorithm

推荐答案

我避免使用XML,它可以保持XML结构与所保存的信息之间的联系,就像:

I''d avoid using XML, something that maintains the link between the XML structure and the information held would look like:

<?xml version="1.0"?>
<chromosome>
    <gene>7</gene>
    <gene>1</gene>
    <gene>5</gene>
    <gene>0</gene>
</chromosome>



xml比其包含的信息大得多,如果存储的是位/布尔数而不是整数,则问题变得更糟.
从个人的角度来看,由于染色体信息是如此基础,因此您可以直接以csv格式写入磁盘.



The xml is much bigger than the information it contains and problem becomes worse if you are storing bits/bools rather than ints.
Pesonally, as chromosome information is so basic, you could write directly to disk in csv format.

7,1,5,0



由于CSV格式非常简单,因此我将执行以下操作:



As the CSV format is so simple, I''d do something like this:

public class Chromosome
{
  char delimiter = ',';

  public List<int> Genes 
  {
     get;
     private set;
  }
  //....
  public override ToString()
  {
    StringBuilder sb = new StringBuilder();
    foreach(int gene in Genes)
    {
      sb.Append(gene);
      sb.Append(delimiter);
    }
    return sb.ToString().Trim(delimiter);
  }

  public void ReadString(string csv)
  {
    Genes.Clear();
    var items = string.split(delimiter);
    foreach (string item in items)
    {
       Genes.Add(int.Parse(item)); //Note this can throw if non-int passed.
    }
  }

}


我没有测试上述内容,但您应该明白这一点.然后,您需要做的就是写入文件(如果需要). Google c#streamreader和c#streamwriter获取更多信息.
我的代码的一件事是将序列化留在类中,这不是理想的解决方案,因此您应该将其分离出来.您可能要考虑的另一件事是自定义序列化程序 [


I''ve not tested the above, but you should get the idea. Then all you need do is write to file if required. Google c# streamreader and c# streamwriter for more info.
One thing about my code is that it leaves the serialization in the class, this is not an ideal solution, so you should separate it out. The other thing you could consider is a Custom Serializer[^]


这篇关于记住最初的人口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆