7
Answers

How to eliminate duplicates; Genetic Algorithm

Mato Milic

Mato Milic

13y
2.1k
1
I was writing my thesis and got to a problem that i can't solve. I am writing about Genetic Algorithms with steady-state selection.
The problem is that after some iteration duplicates shows up; my question is how to remove these duplicates.
How can i compare some specific elements from an object? I tried with the function Equals() but didn't figured out how to set her up.


Example: i want that this repeats until the new object isn't the same as existing objects in class population:


public void permutationRecombination(Chromosome parent1, Chromosome parent2, Chromosome child1, Chromosome child2)
  {
  int cLen = parent1.genes.Length;


  do{
  int recomPoint = ga.rng.Next(cLen - 1);
  int poc1 = 0, poc2 = 0;
  bool nadjen;

  // prvi dio (kopiranje gena)
  for (int i = 0; i <= recomPoint; i++)
  {
  child1.genes[i] = parent1.genes[i].Clone();
  child2.genes[i] = parent2.genes[i].Clone();
  }
  // drugi dio (umetanje gena koji nedostaju redoslijedom iz drugog roditelja)
  for (int i = recomPoint + 1; i < cLen; i++)
  {
  // prvo dijete
  nadjen = false;
  for (int j = poc1; j < cLen; j++)  // trazi preostale gene iz jednog u drugom
  {
  for (int k = recomPoint + 1; k < cLen; k++)
  {
  if (parent2.genes[j].ToInteger() == parent1.genes[k].ToInteger()) //trazi
  {
  child1.genes[i] = parent2.genes[j].Clone();
  poc1 = j + 1;
  nadjen = true;
  }
  }
  if (nadjen) break;
  }
  //drugo dijete;
  nadjen = false;
  for (int j = poc2; j < cLen; j++)  // trazi preostale gene iz jednog u drugom
  {
  for (int k = recomPoint + 1; k < cLen; k++)
  {
  if (parent1.genes[j].ToInteger() == parent2.genes[k].ToInteger()) //trazi
  {
  child2.genes[i] = parent1.genes[j].Clone();
  poc2 = j + 1;
  nadjen = true;
  }
  }
  if (nadjen) break;
  }
  }
  if(child1.genes==parent1.genes) MessageBox.Show("postoje jednaki");
  } while (child1.Equals(parent1) || child2.Equals(parent2) || child1.Equals(parent2) || child2.Equals(parent1));
  }



Answers (7)