Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
Posted
Updated 9-Nov-12 5:16am
v2
Comments
OriginalGriff 9-Nov-12 10:50am    
I'm sure English is not your native language, but it is the default language for this site.
In English, your question makes no sense at all.
Please, either try to find a better translation of your question to English, or find a site in your own native language, as they may be able to help you better than we can!
Use the "Improve question" widget to edit your question and provide better information.
Sergey Alexandrovich Kryukov 9-Nov-12 11:38am    
If a person wants to store "large quantities of zero", learning English more will hardly help... :-)
--SA
Ritwesh 9-Nov-12 11:49am    
Perfectly agreed. I wish I could give you a +5 here :-). Still one might try learning. Hope these links help:
http://www.learnenglish.de
http://www.learn-english-online.org
OriginalGriff 9-Nov-12 11:55am    
I dunno, I have an infinite capacity of WOM fitted to this PC - it can hold *huge* quantities of zero without any strain. :laugh:
Sergey Alexandrovich Kryukov 9-Nov-12 11:58am    
Yes, you can. I'm talking about someone who can see a problem storing it and needs an advice on it... :-)
--SA

1 solution

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

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


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


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

C#
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.
    }
  }

}

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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900