1

I need an alternate for a dictionary, because duplicates are needed. The reason for that is: I need to search for the best route. To do that I am creating a population with 20 individuals for example. Each individual has its own route and to each route is a fitness calculated. To be able to sort the route by the fitness I am creating a dictionary. Now I am iterating over generations while sorting the dictionary and adding new routes to it every iteration. However the dictionary is deleting duplicates, which should not be done because the code might throw the best route multiple times.

I already read something about a lookup, and a linked list but don't really have knowledge about it. Or maybe a tuple? Anyone who knows better what might help?

This is my code though: Well its not the hole code just showing the dictionary to avoid misunderstanding.

List<List<Point3d>> currentGeneration = new List<List<Point3d>>(cGP.Count);
cGP.ForEach((item) => {currentGeneration.Add(new List<Point3d>(item));});

List<double> currentFitness = cGF.ToList();

Dictionary<List<Point3d>, double> dictionary = new Dictionary<List<Point3d>, double>();
foreach(List<Point3d> individual in currentGeneration)
{
 foreach(double individualsFitness in currentFitness)
 {
  if(!dictionary.ContainsKey(individual))
  {
   if(!dictionary.ContainsValue(individualsFitness))
   {
    dictionary.Add(individual, individualsFitness);
   }
  }
 }
}
N.Y
  • 2,849
  • 2
  • 31
  • 46
Meliss
  • 123
  • 1
  • 8
  • You can use a list of Key-Value pairs (`List>`). However, performance will scale more poorly. If this is not a problem for you (i.e. you are not using large datasets) then this is the easiest answer. – RB. Oct 07 '16 at 16:27
  • To increase Benchmark performance... You might also consider using `GetEnumerator` opposed to `foreach` [https://www.dotnetperls.com/dictionary-getenumerator](https://www.dotnetperls.com/dictionary-getenumerator) – Chef_Code Oct 07 '16 at 17:26
  • Does this answer your question? [Duplicate keys in .NET dictionaries?](https://stackoverflow.com/questions/146204/duplicate-keys-in-net-dictionaries) – riQQ Jan 07 '21 at 20:16

2 Answers2

4

I need an alternate for a dictionary, because duplicates are needed

See Lookup. This is essentially a dictionary that allows duplicates.

From the documentation:

Represents a collection of keys each mapped to one or more values.

The difference is that a Dictionary maps keys to single values, whereas a Lookup maps keys to collections of values.

You can create an instance of a Lookup by calling ToLookup on an object that implements IEnumerable.

Igor
  • 55,253
  • 10
  • 80
  • 149
  • I wrote this: ILookup, double> lookup = new ILookup, double>(); , but the compiler says that I can't create an instance of the abstract class. Want to add 'currentGeneration' as keys and 'currentFitness' as values like I did with the dictionary, but how do I do that? Only like this?: var lookup = currentGeneration.ToLookup(item => item.Key); lookup = currentFitness.ToLookup(item => item.Value); – Meliss Oct 08 '16 at 17:54
  • And what do you think about List? Or sortedList? – Meliss Oct 08 '16 at 18:54
0

Ok, I already found what I was searching for.

List<KeyValuePair> does not delete duplicates and it is able to sort on place by value or by key, both are possible.

And here is my code for those who are new with programming like myself:

List<List<Point3d>> handoverPopulation = createPopulation(pts, p);
List<double> handoverFitness = calculateFitness(handoverPopulation, p0);

List<KeyValuePair<List<Point3d>, double>> list = new List<KeyValuePair<List<Point3d>, double>>();
for(int i = 0; i < handoverFitness.Count; i++)
{
 list.Add(new KeyValuePair<List<Point3d>, double>(handoverPopulation[i], handoverFitness[i]));
}
list.Sort((x, y) => x.Value.CompareTo(y.Value));

Yeeyy happy :)

Meliss
  • 123
  • 1
  • 8