Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/SolutionsCaching/PermutationInformation.cs @ 10007

Last change on this file since 10007 was 10007, checked in by ascheibe, 11 years ago

#1886 started working on volume calculation

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
28  public enum ProducedBy { Crossover, Mutation };
29
30  [Item("PermutationInformation", "Stores meta-information about a solution.")]
31  [StorableClass]
32  public class PermutationInformation : Item {
33    [Storable]
34    public ProducedBy ProducedBy { get; set; }
35    [Storable]
36    public List<PermutationWrapper> ParentList { get; set; }
37    [Storable]
38    public int Generation { get; set; }
39    [Storable]
40    PermutationWrapperEqualityComparer pweq;
41
42    public PermutationInformation() {
43      pweq = new PermutationWrapperEqualityComparer();
44    }
45
46    [StorableConstructor]
47    protected PermutationInformation(bool deserializing) : base(deserializing) { }
48    protected PermutationInformation(PermutationInformation original, Cloner cloner)
49      : base(original, cloner) {
50      this.ProducedBy = original.ProducedBy;
51      this.ParentList = new List<PermutationWrapper>(original.ParentList);
52      this.Generation = original.Generation;
53      this.pweq = new PermutationWrapperEqualityComparer();
54    }
55
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new PermutationInformation(this, cloner);
58    }
59
60    public override int GetHashCode() {
61      //use rotating hash, should be enough
62      //see: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
63      int h = 0;
64
65      h = (h << 4) ^ (h >> 28) ^ ProducedBy.GetHashCode();
66      h = (h << 4) ^ (h >> 28) ^ Generation.GetHashCode();
67
68      if (ParentList != null) {
69        foreach (var parent in ParentList) {
70          h = (h << 4) ^ (h >> 28) ^ pweq.GetHashCode(parent);
71        }
72      }
73
74      return h;
75    }
76
77    public override bool Equals(object obj) {
78      PermutationInformation pi = obj as PermutationInformation;
79      if (pi == null) return false;
80
81      if (ProducedBy == pi.ProducedBy && Generation == pi.Generation) {
82        if (pi.ParentList == null && ParentList == null) return true;
83        if (pi.ParentList != null && ParentList == null ||
84          pi.ParentList == null && ParentList != null) return false;
85        if (pi.ParentList.Count != ParentList.Count) return false;
86
87        foreach (var p in ParentList) {
88          if (!pi.ParentList.Contains(p)) return false;
89        }
90        return true;
91      } else {
92        return false;
93      }
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.