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;
|
---|
24 | using HeuristicLab.Algorithms.MemPR.Interfaces;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Algorithms.MemPR.LinearLinkage.SolutionModel.Univariate {
|
---|
31 | [Item("Univariate solution model (linear linkage)", "")]
|
---|
32 | [StorableClass]
|
---|
33 | public sealed class UnivariateModel : Item, ISolutionModel<Encodings.LinearLinkageEncoding.LinearLinkage> {
|
---|
34 | [Storable]
|
---|
35 | public IntMatrix Frequencies { get; set; }
|
---|
36 | [Storable]
|
---|
37 | public IRandom Random { get; set; }
|
---|
38 | [Storable]
|
---|
39 | public IntValue Maximum { get; set; }
|
---|
40 |
|
---|
41 | [StorableConstructor]
|
---|
42 | private UnivariateModel(bool deserializing) : base(deserializing) { }
|
---|
43 | private UnivariateModel(UnivariateModel original, Cloner cloner)
|
---|
44 | : base(original, cloner) {
|
---|
45 | Frequencies = cloner.Clone(original.Frequencies);
|
---|
46 | Random = cloner.Clone(original.Random);
|
---|
47 | }
|
---|
48 | public UnivariateModel(IRandom random, int[,] frequencies, int max) {
|
---|
49 | Frequencies = new IntMatrix(frequencies);
|
---|
50 | Random = random;
|
---|
51 | Maximum = new IntValue(max);
|
---|
52 | }
|
---|
53 | public UnivariateModel(IRandom random, IntMatrix frequencies, int max) {
|
---|
54 | Frequencies = frequencies;
|
---|
55 | Random = random;
|
---|
56 | Maximum = new IntValue(max);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
60 | return new UnivariateModel(this, cloner);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public Encodings.LinearLinkageEncoding.LinearLinkage Sample() {
|
---|
64 | var N = Frequencies.Rows;
|
---|
65 | var centroid = Encodings.LinearLinkageEncoding.LinearLinkage.SingleElementGroups(N);
|
---|
66 | var dict = new Dictionary<int, int>();
|
---|
67 | for (var i = N - 1; i >= 0; i--) {
|
---|
68 | centroid[i] = i; // default be a cluster of your own
|
---|
69 | for (var j = i + 1; j < N; j++) {
|
---|
70 | // try to find a suitable link
|
---|
71 | if (Maximum.Value * Random.NextDouble() < Frequencies[i, j]) {
|
---|
72 | int pred;
|
---|
73 | if (dict.TryGetValue(j, out pred)) {
|
---|
74 | int tmp, k = pred;
|
---|
75 | while (dict.TryGetValue(k, out tmp)) {
|
---|
76 | if (k == tmp) break;
|
---|
77 | k = tmp;
|
---|
78 | }
|
---|
79 | centroid[i] = k;
|
---|
80 | } else centroid[i] = j;
|
---|
81 | dict[centroid[i]] = i;
|
---|
82 | break;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 | return centroid;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public static ISolutionModel<Encodings.LinearLinkageEncoding.LinearLinkage> Create(IRandom random, IEnumerable<Encodings.LinearLinkageEncoding.LinearLinkage> population) {
|
---|
90 | var iter = population.GetEnumerator();
|
---|
91 | if (!iter.MoveNext()) throw new ArgumentException("Cannot create solution model from empty population.");
|
---|
92 | var popSize = 1;
|
---|
93 | var N = iter.Current.Length;
|
---|
94 | var freq = new int[N, N];
|
---|
95 | do {
|
---|
96 | var current = iter.Current;
|
---|
97 | popSize++;
|
---|
98 | foreach (var g in current.GetGroups()) {
|
---|
99 | for (var i = 0; i < g.Count - 1; i++)
|
---|
100 | for (var j = i + 1; j < g.Count; j++) {
|
---|
101 | freq[g[i], g[j]]++;
|
---|
102 | freq[g[j], g[i]]++;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | } while (iter.MoveNext());
|
---|
106 | return new UnivariateModel(random, freq, popSize);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|