0

I'm working with dictionaries and the most problem that they are referenced to each other. I need to create a deep clone (smth like this it's named).

I use population as initial dictionary where I store graphs, then I need to take elements from Population, change them, write to childPopulation then some calculations and changed elements becomes in scores. Finnaly I need to merge population and scores dictionaries.

The problem is that when I change elements from population they changed there ofcause. I need some how to create a copy of population whil will be not referenced with initial.

        population = new Dictionary<AdjacencyGraph<Figure, TaggedEdge<Figure, double>>, double>();
        addElement(p1, initialGraph);
        Run(initialGraph, p1);
        double scoreBest = RMSECalc();
        population.Add(initialGraph, scoreBest);
        while (scoreBest >= 1.0)
        {
            childPopulation = new List<AdjacencyGraph<Figure, TaggedEdge<Figure, double>>>();
            var parentList = new Dictionary<AdjacencyGraph<Figure, TaggedEdge<Figure, double>>, double>();

            foreach (var parent in population)
            {
                childPopulation.Add(Mutate(parent.Key));
            }
            GetBestSolution(childPopulation);
            scores = scores.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
            if (scores.First().Value < scoreBest)
                scoreBest = scores.First().Value;
            scores.ToList().ForEach(x => population.Add(x.Key, x.Value));
            population.Take(50);
            scores = new Dictionary<AdjacencyGraph<Figure, TaggedEdge<Figure, double>>, double>();
            childPopulation = new List<AdjacencyGraph<Figure, TaggedEdge<Figure, double>>>();
        }
  • Have you considered using structs instead of reference types? – Eduard Malakhov Feb 05 '17 at 11:10
  • One option for doing deep clones is to do a binary serialization and deserialization. Otherwise you have to write your own code for doing the cloning.http://stackoverflow.com/questions/78536/deep-cloning-objects – juharr Feb 05 '17 at 11:11
  • The problem is that I cannot use serialization cause Adjacency graph is not mention as serializable. And I also can not fix the source code. Same as I can not use interface IClonable – Дима Федюков Feb 05 '17 at 13:30

0 Answers0