[14420] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[14450] | 24 | using HeuristicLab.Algorithms.MemPR.Interfaces;
|
---|
[14420] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
[14544] | 28 | using HeuristicLab.Encodings.LinearLinkageEncoding;
|
---|
[14420] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
[14544] | 31 | namespace HeuristicLab.Algorithms.MemPR.Grouping.SolutionModel.Univariate {
|
---|
[14466] | 32 | [Item("Univariate solution model (linear linkage)", "")]
|
---|
[14420] | 33 | [StorableClass]
|
---|
[14544] | 34 | public sealed class UnivariateModel : Item, ISolutionModel<LinearLinkage> {
|
---|
[14420] | 35 | [Storable]
|
---|
[14466] | 36 | public IntMatrix Frequencies { get; set; }
|
---|
[14420] | 37 | [Storable]
|
---|
| 38 | public IRandom Random { get; set; }
|
---|
[14466] | 39 | [Storable]
|
---|
| 40 | public IntValue Maximum { get; set; }
|
---|
[14420] | 41 |
|
---|
| 42 | [StorableConstructor]
|
---|
| 43 | private UnivariateModel(bool deserializing) : base(deserializing) { }
|
---|
| 44 | private UnivariateModel(UnivariateModel original, Cloner cloner)
|
---|
| 45 | : base(original, cloner) {
|
---|
[14466] | 46 | Frequencies = cloner.Clone(original.Frequencies);
|
---|
[14420] | 47 | Random = cloner.Clone(original.Random);
|
---|
| 48 | }
|
---|
[14466] | 49 | public UnivariateModel(IRandom random, int[,] frequencies, int max) {
|
---|
| 50 | Frequencies = new IntMatrix(frequencies);
|
---|
[14420] | 51 | Random = random;
|
---|
[14466] | 52 | Maximum = new IntValue(max);
|
---|
[14420] | 53 | }
|
---|
[14466] | 54 | public UnivariateModel(IRandom random, IntMatrix frequencies, int max) {
|
---|
| 55 | Frequencies = frequencies;
|
---|
[14420] | 56 | Random = random;
|
---|
[14466] | 57 | Maximum = new IntValue(max);
|
---|
[14420] | 58 | }
|
---|
| 59 |
|
---|
| 60 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 61 | return new UnivariateModel(this, cloner);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[14544] | 64 | public LinearLinkage Sample() {
|
---|
[14466] | 65 | var N = Frequencies.Rows;
|
---|
[14544] | 66 | var centroid = LinearLinkage.SingleElementGroups(N);
|
---|
[14466] | 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 | }
|
---|
[14420] | 85 | }
|
---|
| 86 | }
|
---|
[14466] | 87 | return centroid;
|
---|
[14420] | 88 | }
|
---|
| 89 |
|
---|
[14544] | 90 | public static ISolutionModel<LinearLinkage> Create(IRandom random, IEnumerable<LinearLinkage> population) {
|
---|
[14466] | 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;
|
---|
[14420] | 98 | popSize++;
|
---|
[14466] | 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 | }
|
---|
[14420] | 105 | }
|
---|
[14466] | 106 | } while (iter.MoveNext());
|
---|
| 107 | return new UnivariateModel(random, freq, popSize);
|
---|
[14420] | 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|