Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Permutation/SolutionModel/Univariate/UnivariateAbsoluteModel.cs @ 14450

Last change on this file since 14450 was 14450, checked in by abeham, 7 years ago

#2701: working on MemPR implementation

File size: 3.7 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Algorithms.MemPR.Interfaces;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Random;
31
32namespace HeuristicLab.Algorithms.MemPR.Permutation.SolutionModel.Univariate {
33  [Item("Univariate solution model (Permutation.Absolute)", "")]
34  [StorableClass]
35  public sealed class UnivariateAbsoluteModel : Item, ISolutionModel<Encodings.PermutationEncoding.Permutation> {
36    [Storable]
37    public IntMatrix Probabilities { get; set; }
38    [Storable]
39    public IRandom Random { get; set; }
40
41    [StorableConstructor]
42    private UnivariateAbsoluteModel(bool deserializing) : base(deserializing) { }
43    private UnivariateAbsoluteModel(UnivariateAbsoluteModel original, Cloner cloner)
44      : base(original, cloner) {
45      Probabilities = cloner.Clone(original.Probabilities);
46      Random = cloner.Clone(original.Random);
47    }
48    public UnivariateAbsoluteModel(IRandom random, int[,] probabilities) {
49      Probabilities = new IntMatrix(probabilities);
50      Random = random;
51    }
52    public UnivariateAbsoluteModel(IRandom random, IntMatrix probabilties) {
53      Probabilities = probabilties;
54      Random = random;
55    }
56
57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new UnivariateAbsoluteModel(this, cloner);
59    }
60
61    public Encodings.PermutationEncoding.Permutation Sample() {
62      var N = Probabilities.Rows;
63      var child = new Encodings.PermutationEncoding.Permutation(PermutationTypes.Absolute, N);
64      var indices = Enumerable.Range(0, N).Shuffle(Random).ToList();
65      var values = Enumerable.Range(0, N).Shuffle(Random).ToList();
66      for (var i = N - 1; i > 0; i--) {
67        var nextIndex = indices[i];
68        var total = 0.0;
69        for (var v = 0; v < values.Count; v++) {
70          total += Probabilities[nextIndex, values[v]] + 1.0 / N;
71        }
72        var ball = Random.NextDouble() * total;
73        for (var v = 0; v < values.Count; v++) {
74          ball -= Probabilities[nextIndex, values[v]] + 1.0 / N;
75          if (ball <= 0.0) {
76            child[nextIndex] = values[v];
77            values.RemoveAt(v);
78            indices.RemoveAt(i);
79            break;
80          }
81        }
82      }
83      child[indices[0]] = values[0];
84      return child;
85    }
86
87    public static UnivariateAbsoluteModel Create(IRandom random, IList<Encodings.PermutationEncoding.Permutation> pop, int N) {
88      var model = new int[N, N];
89      for (var i = 0; i < pop.Count; i++) {
90        for (var j = 0; j < N; j++) {
91          model[j, pop[i][j]]++;
92        }
93      }
94      return new UnivariateAbsoluteModel(random, model);
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.