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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Algorithms.MemPR.Interfaces;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Random;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Algorithms.MemPR.Permutation.SolutionModel.Univariate {
|
---|
33 | [Item("Univariate solution model (Permutation.Relative)", "")]
|
---|
34 | [StorableClass]
|
---|
35 | public sealed class UnivariateRelativeModel : Item, ISolutionModel<Encodings.PermutationEncoding.Permutation> {
|
---|
36 | [Storable]
|
---|
37 | public IntMatrix Probabilities { get; set; }
|
---|
38 |
|
---|
39 | [Storable]
|
---|
40 | public IRandom Random { get; set; }
|
---|
41 |
|
---|
42 | [Storable]
|
---|
43 | public PermutationTypes PermutationType { get; set; }
|
---|
44 |
|
---|
45 | [StorableConstructor]
|
---|
46 | private UnivariateRelativeModel(bool deserializing) : base(deserializing) { }
|
---|
47 | private UnivariateRelativeModel(UnivariateRelativeModel original, Cloner cloner)
|
---|
48 | : base(original, cloner) {
|
---|
49 | Probabilities = cloner.Clone(original.Probabilities);
|
---|
50 | Random = cloner.Clone(original.Random);
|
---|
51 | PermutationType = original.PermutationType;
|
---|
52 | }
|
---|
53 | public UnivariateRelativeModel(IRandom random, int[,] probabilities, PermutationTypes permutationType) {
|
---|
54 | Probabilities = new IntMatrix(probabilities);
|
---|
55 | Random = random;
|
---|
56 | PermutationType = permutationType;
|
---|
57 | }
|
---|
58 | public UnivariateRelativeModel(IRandom random, IntMatrix probabilties, PermutationTypes permutationType) {
|
---|
59 | Probabilities = probabilties;
|
---|
60 | Random = random;
|
---|
61 | PermutationType = permutationType;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
65 | return new UnivariateRelativeModel(this, cloner);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public Encodings.PermutationEncoding.Permutation Sample() {
|
---|
69 | var N = Probabilities.Rows;
|
---|
70 | var next = Random.Next(N);
|
---|
71 | var child = new Encodings.PermutationEncoding.Permutation(PermutationType, N);
|
---|
72 | child[0] = next;
|
---|
73 | var open = Enumerable.Range(0, N).Where(x => x != next).Shuffle(Random).ToList();
|
---|
74 | for (var i = 1; i < N - 1; i++) {
|
---|
75 | var total = 0.0;
|
---|
76 | for (var j = 0; j < open.Count; j++) {
|
---|
77 | total += Probabilities[next, open[j]] + 1.0 / N;
|
---|
78 | }
|
---|
79 | var ball = Random.NextDouble() * total;
|
---|
80 | for (var j = 0; j < open.Count; j++) {
|
---|
81 | ball -= Probabilities[next, open[j]] + 1.0 / N;
|
---|
82 | if (ball <= 0.0) {
|
---|
83 | child[i] = open[j];
|
---|
84 | next = open[j];
|
---|
85 | open.RemoveAt(j);
|
---|
86 | break;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | child[N - 1] = open[0];
|
---|
91 | return child;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public static UnivariateRelativeModel CreateDirected(IRandom random, IList<Encodings.PermutationEncoding.Permutation> pop, int N) {
|
---|
95 | var model = new int[N, N];
|
---|
96 | for (var i = 0; i < pop.Count; i++) {
|
---|
97 | for (var j = 0; j < N - 1; j++) {
|
---|
98 | for (var k = j + 1; k < N; k++) {
|
---|
99 | model[pop[i][j], pop[i][k]]++;
|
---|
100 | }
|
---|
101 | model[pop[i][N - 1], pop[i][0]]++;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | return new UnivariateRelativeModel(random, model, PermutationTypes.RelativeDirected);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public static UnivariateRelativeModel CreateUndirected(IRandom random, IList<Encodings.PermutationEncoding.Permutation> pop, int N) {
|
---|
108 | var model = new int[N, N];
|
---|
109 | for (var i = 0; i < pop.Count; i++) {
|
---|
110 | for (var j = 0; j < N - 1; j++) {
|
---|
111 | for (var k = j + 1; k < N; k++) {
|
---|
112 | model[pop[i][j], pop[i][k]]++;
|
---|
113 | model[pop[i][k], pop[i][j]]++;
|
---|
114 | }
|
---|
115 | model[pop[i][0], pop[i][N - 1]]++;
|
---|
116 | model[pop[i][N - 1], pop[i][0]]++;
|
---|
117 | }
|
---|
118 | }
|
---|
119 | return new UnivariateRelativeModel(random, model, PermutationTypes.RelativeUndirected);
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|