Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/07/16 23:46:29 (7 years ago)
Author:
abeham
Message:

#2701:

  • Added MemPR for linear linkage (tabu walk still missing)
  • Added graph coloring problem
Location:
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/LinearLinkage
Files:
3 added
1 copied

Legend:

Unmodified
Added
Removed
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/LinearLinkage/SolutionModel/Univariate/UnivariateSolutionModel.cs

    r14450 r14466  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Linq;
    2524using HeuristicLab.Algorithms.MemPR.Interfaces;
    2625using HeuristicLab.Common;
    2726using HeuristicLab.Core;
    2827using HeuristicLab.Data;
    29 using HeuristicLab.Encodings.BinaryVectorEncoding;
    3028using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    31 using HeuristicLab.Random;
    3229
    33 namespace HeuristicLab.Algorithms.MemPR.Binary.SolutionModel.Univariate {
    34   [Item("Univariate solution model (binary)", "")]
     30namespace HeuristicLab.Algorithms.MemPR.LinearLinkage.SolutionModel.Univariate {
     31  [Item("Univariate solution model (linear linkage)", "")]
    3532  [StorableClass]
    36   public sealed class UnivariateModel : Item, ISolutionModel<BinaryVector> {
     33  public sealed class UnivariateModel : Item, ISolutionModel<Encodings.LinearLinkageEncoding.LinearLinkage> {
    3734    [Storable]
    38     public DoubleArray Probabilities { get; set; }
     35    public IntMatrix Frequencies { get; set; }
    3936    [Storable]
    4037    public IRandom Random { get; set; }
     38    [Storable]
     39    public IntValue Maximum { get; set; }
    4140
    4241    [StorableConstructor]
     
    4443    private UnivariateModel(UnivariateModel original, Cloner cloner)
    4544      : base(original, cloner) {
    46       Probabilities = cloner.Clone(original.Probabilities);
     45      Frequencies = cloner.Clone(original.Frequencies);
    4746      Random = cloner.Clone(original.Random);
    4847    }
    49     public UnivariateModel(IRandom random, int N) : this(random, Enumerable.Range(0, N).Select(x => 0.5).ToArray()) { }
    50     public UnivariateModel(IRandom random, double[] probabilities) {
    51       Probabilities = new DoubleArray(probabilities);
     48    public UnivariateModel(IRandom random, int[,] frequencies, int max) {
     49      Frequencies = new IntMatrix(frequencies);
    5250      Random = random;
     51      Maximum = new IntValue(max);
    5352    }
    54     public UnivariateModel(IRandom random, DoubleArray probabilties) {
    55       Probabilities = probabilties;
     53    public UnivariateModel(IRandom random, IntMatrix frequencies, int max) {
     54      Frequencies = frequencies;
    5655      Random = random;
     56      Maximum = new IntValue(max);
    5757    }
    5858
     
    6161    }
    6262
    63     public BinaryVector Sample() {
    64       var vec = new BinaryVector(Probabilities.Length);
    65       for (var i = 0; i < Probabilities.Length; i++)
    66         vec[i] = Random.NextDouble() < Probabilities[i];
    67       return vec;
     63    public Encodings.LinearLinkageEncoding.LinearLinkage Sample() {
     64      var N = Frequencies.Rows;
     65      var centroid = new Encodings.LinearLinkageEncoding.LinearLinkage(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;
    6887    }
    6988
    70     public static ISolutionModel<BinaryVector> CreateWithoutBias(IRandom random, IEnumerable<BinaryVector> population) {
    71       double[] model = null;
    72       var popSize = 0;
    73       foreach (var p in population) {
     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;
    7497        popSize++;
    75         if (model == null) model = new double[p.Length];
    76         for (var x = 0; x < model.Length; x++) {
    77           if (p[x]) model[x]++;
     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            }
    78104        }
    79       }
    80       if (model == null) throw new ArgumentException("Cannot train model from empty population.");
    81       // normalize to [0;1]
    82       var factor = 1.0 / popSize;
    83       for (var x = 0; x < model.Length; x++) {
    84         model[x] *= factor;
    85       }
    86       return new UnivariateModel(random, model);
    87     }
    88 
    89     public static ISolutionModel<BinaryVector> CreateWithRankBias(IRandom random, bool maximization, IEnumerable<BinaryVector> population, IEnumerable<double> qualities) {
    90       var popSize = 0;
    91 
    92       double[] model = null;
    93       var pop = population.Zip(qualities, (b, q) => new { Solution = b, Fitness = q });
    94       foreach (var ind in maximization ? pop.OrderBy(x => x.Fitness) : pop.OrderByDescending(x => x.Fitness)) {
    95         // from worst to best, worst solution has 1 vote, best solution N votes
    96         popSize++;
    97         if (model == null) model = new double[ind.Solution.Length];
    98         for (var x = 0; x < model.Length; x++) {
    99           if (ind.Solution[x]) model[x] += popSize;
    100         }
    101       }
    102       if (model == null) throw new ArgumentException("Cannot train model from empty population.");
    103       // normalize to [0;1]
    104       var factor = 2.0 / (popSize + 1);
    105       for (var i = 0; i < model.Length; i++) {
    106         model[i] *= factor / popSize;
    107       }
    108       return new UnivariateModel(random, model);
    109     }
    110 
    111     public static ISolutionModel<BinaryVector> CreateWithFitnessBias(IRandom random, bool maximization, IEnumerable<BinaryVector> population, IEnumerable<double> qualities) {
    112       var proportions = RandomEnumerable.PrepareProportional(qualities, true, !maximization);
    113       var factor = 1.0 / proportions.Sum();
    114       double[] model = null;
    115       foreach (var ind in population.Zip(proportions, (p, q) => new { Solution = p, Proportion = q })) {
    116         if (model == null) model = new double[ind.Solution.Length];
    117         for (var x = 0; x < model.Length; x++) {
    118           if (ind.Solution[x]) model[x] += ind.Proportion * factor;
    119         }
    120       }
    121       if (model == null) throw new ArgumentException("Cannot train model from empty population.");
    122       return new UnivariateModel(random, model);
     105      } while (iter.MoveNext());
     106      return new UnivariateModel(random, freq, popSize);
    123107    }
    124108  }
Note: See TracChangeset for help on using the changeset viewer.