如何消除重复 [英] How to eliminate duplicates

查看:83
本文介绍了如何消除重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写论文,但遇到一个我无法解决的问题.我正在写关于具有稳态选择的遗传算法.
问题在于,经过一些迭代后,重复项会显示出来;我的问题是如何删除这些重复项.
如何比较对象中的某些特定元素?我尝试使用Equals()函数,但没有弄清楚如何设置她.


例如:我希望重复直到新对象与类填充中的现有对象不同:

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;

     // first part (copying genes)
     for (int i = 0; i <= recomPoint; i++)
        {
        child1.genes[i] = parent1.genes[i].Clone();
        child2.genes[i] = parent2.genes[i].Clone();
        }
     // second (inserting missing genes from other parent)
     for (int i = recomPoint + 1; i < cLen; i++)
        {
        // first child
        nadjen = false;
        for (int j = poc1; j < cLen; j++)  // search for other genes in other parent
           {
           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;
           }
       //second child;
       nadjen = false;
       for (int j = poc2; j < cLen; j++)  //search for missing genes in each other
          {
          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));
  }



代码格式从平面更改为缩进-OriginalGriff [/edit]



[edit]Code formatting changed from flat to indented - OriginalGriff[/edit]

推荐答案

让我们假设您的基因序列是一个int数组(不是二进制字符串)那么等于或==表示两个操作数都具有相同的基因.
Let us assume your gene sequence are an array of int (not a binary string) then equals or == mean that both operands has same genes then.
public class Parent
{
    public int[] genes = new int[5];
    public static bool operator ==(Parent p, Child c)
    {
        int geneCount = p.genes.Count();
        for (int i = 0; i <= geneCount; i++)
        {
            if (p.genes[i] != c.genes[i])
            {
                return false;
            }
        }
        return true;
    }
    public static bool operator !=(Parent p, Child c)
    {
        if (p == c == true)
        {
            return false;
        }
        else { return true; }
    }

    public override bool Equals(object obj)
    {
        if (obj.GetType() == typeof(Child))
        {
            Child c = (Child)obj;
            return this == c;
        }
        else
        {
            return false;
        }
    }
}
public class Child
{
    public int[] genes = new int[5];
    public static bool operator ==(Child c, Parent p)
    {
        int geneCount = c.genes.Count();
        for (int i = 0; i <= geneCount; i++)
        {
            if (c.genes[i] != p.genes[i])
            {
                return false;
            }
        }
        return true;
    }
    public static bool operator !=( Child c,Parent p)
    {
        if (c == p == true)
        {
            return false;
        }
        else { return true; }
    }
    public override bool Equals(object obj)
    {
        if (obj.GetType() == typeof(Parent))
        {
            Parent p = (Parent)obj;
            return this == p;
        }
        else
        {
            return false;
        }
    }
}




可以像
那样使用




This can be used like

Parent p = new Parent();
p.genes[0] = 1;
p.genes[1] = 2;
p.genes[2] = 3;
p.genes[3] = 4;
p.genes[4] = 5;

Child c = new Child();
c.genes[0] = 1;
c.genes[1] = 2;
c.genes[2] = 3;
c.genes[3] = 4;
c.genes[4] = 6;
MessageBox.Show((p == c).ToString());
MessageBox.Show((p != c).ToString());
MessageBox.Show((p.Equals(c)).ToString());


很遗憾,您没有提供Chromosome的定义,也没有解释什么的重复.我的东西,这是一条染色体.

您需要将所有染色体实例保存在容器中,以确保唯一性.它可以是Dictionary(一组字典键是唯一的)或HashSet.它唯一需要的功能就是唯一性,请使用System.Collections.Generic.HashSet<Chromosome>,请参见 http://msdn.microsoft.com /en-us/library/bb359438.aspx [ ^ ].

为了使您成为Chromosome类(我希望这是类,而不是结构,否则您应该在显示的代码中使用ref传递参数)以使其成为集合的元素,应确保其唯一性是由其比较操作定义.为此,请覆盖Equals(object)GetHashCode.相应地定义"=="和!="运算符也很好.哈希码是字典和哈希集的关键. Is不必是唯一的(并非总是可能的),而应具有足够的特性".为类或结构创建可接受的哈希函数的简单方法是:计算所有组件的哈希函数并将它们异或.

请参阅我的其他答案,以回应OP的新代码-更正和注释.

—SA
Unfortunately, you did not provide a definition of Chromosome and did not explain what''s is duplicated. I thing, this is a chromosome.

You need to keep all instances of the Chromosome in the container which guarantee uniqueness. It can be Dictionary (where a set of dictionary keys is unique)or HashSet. It the only feature you need is uniqueness, use System.Collections.Generic.HashSet<Chromosome>, see http://msdn.microsoft.com/en-us/library/bb359438.aspx[^].

To make you Chromosome class (I hope this is class, not structure, otherwise you should have used passing parameters by ref in the code you show) to be able to became an element of collection, you should make sure that its uniqueness is defined by its comparison operation. For this purpose, override Equals(object) and GetHashCode. Is''s also good to define "==" and "!=" operators accordingly. Hash Code is the key to dictionaries and hash sets. Is does not have to be unique (it is not always possible) but "characteristic enough". On simple way of creating an acceptable hash function for a class or structure is this: calculate hash functions for all components and XOR them together.

Please see my other Answer in response to OP''s new piece of code — corrections and notes.

—SA


这是我对OP发布的代码的改进:

This is my improvement of the code posted by OP:

public override bool Equals(object obj)
{
      if (object.ReferenceEquals(obj, null) return false;
      Chromosome chromosome = obj as Chromosome;
      if (object.ReferenceEquals(chromosome, null)) return false;
      for(int index = 0; index < chromosome.genes.Length; index++)
      {
         // why genes[index] is not integer type?
         // even if it is not, why converting ToInteger?
         // direct comparison could work
         // != is valid here
         if (this.genes[index].ToInteger() != chromosome.genes[index].ToInteger()) return false;
      }
      return true; // all corresponding genes equal
   }
}



我使用了object.ReferenceEquals,如果您修改身份规则并改善命名功能(总是一个字符或非语义名称总是不好的),它总是更好.另外,请查看我在代码中的问题,看看您是否真的为genes选择了正确的表示形式.

—SA



I used object.ReferenceEquals which is always better if you modify identity rules, and improved naming (one-character or non-semantic names are always bad). Also, look at my questions in code and see if you really choose correct presentation for genes.

—SA


这篇关于如何消除重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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