Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Algorithms.MemPR/3.3/LinearLinkage/SolutionModel/Univariate/UnivariateSolutionModel.cs @ 14695

Last change on this file since 14695 was 14544, checked in by abeham, 8 years ago

#2701:

  • LLE: Added equality comparer
  • MemPR:
    • Added GPR to learn about heuristic performance
    • Changed Breeding to do more exhaustive search on crossover
    • Added Delinking separately to Relinking
    • Rewrote d/relinking for LLE
    • Reduce usage of local search
    • Renamed TabuWalk to AdaptiveWalk
    • Rewrote adaptive walk for binary problems
    • Renamed LLE namespace to Grouping to avoid namespace clashes
File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
23using System.Collections.Generic;
24using HeuristicLab.Algorithms.MemPR.Interfaces;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.LinearLinkageEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.MemPR.Grouping.SolutionModel.Univariate {
32  [Item("Univariate solution model (linear linkage)", "")]
33  [StorableClass]
34  public sealed class UnivariateModel : Item, ISolutionModel<LinearLinkage> {
35    [Storable]
36    public IntMatrix Frequencies { get; set; }
37    [Storable]
38    public IRandom Random { get; set; }
39    [Storable]
40    public IntValue Maximum { get; set; }
41
42    [StorableConstructor]
43    private UnivariateModel(bool deserializing) : base(deserializing) { }
44    private UnivariateModel(UnivariateModel original, Cloner cloner)
45      : base(original, cloner) {
46      Frequencies = cloner.Clone(original.Frequencies);
47      Random = cloner.Clone(original.Random);
48    }
49    public UnivariateModel(IRandom random, int[,] frequencies, int max) {
50      Frequencies = new IntMatrix(frequencies);
51      Random = random;
52      Maximum = new IntValue(max);
53    }
54    public UnivariateModel(IRandom random, IntMatrix frequencies, int max) {
55      Frequencies = frequencies;
56      Random = random;
57      Maximum = new IntValue(max);
58    }
59
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new UnivariateModel(this, cloner);
62    }
63
64    public LinearLinkage Sample() {
65      var N = Frequencies.Rows;
66      var centroid = LinearLinkage.SingleElementGroups(N);
67      var dict = new Dictionary<int, int>();
68      for (var i = N - 1; i >= 0; i--) {
69        centroid[i] = i; // default be a cluster of your own
70        for (var j = i + 1; j < N; j++) {
71          // try to find a suitable link
72          if (Maximum.Value * Random.NextDouble() < Frequencies[i, j]) {
73            int pred;
74            if (dict.TryGetValue(j, out pred)) {
75              int tmp, k = pred;
76              while (dict.TryGetValue(k, out tmp)) {
77                if (k == tmp) break;
78                k = tmp;
79              }
80              centroid[i] = k;
81            } else centroid[i] = j;
82            dict[centroid[i]] = i;
83            break;
84          }
85        }
86      }
87      return centroid;
88    }
89
90    public static ISolutionModel<LinearLinkage> Create(IRandom random, IEnumerable<LinearLinkage> population) {
91      var iter = population.GetEnumerator();
92      if (!iter.MoveNext()) throw new ArgumentException("Cannot create solution model from empty population.");
93      var popSize = 1;
94      var N = iter.Current.Length;
95      var freq = new int[N, N];
96      do {
97        var current = iter.Current;
98        popSize++;
99        foreach (var g in current.GetGroups()) {
100          for (var i = 0; i < g.Count - 1; i++)
101            for (var j = i + 1; j < g.Count; j++) {
102              freq[g[i], g[j]]++;
103              freq[g[j], g[i]]++;
104            }
105        }
106      } while (iter.MoveNext());
107      return new UnivariateModel(random, freq, popSize);
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.