Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/22/17 16:52:36 (7 years ago)
Author:
abeham
Message:

#2457: working on MemPR integration

Location:
branches/PerformanceComparison/HeuristicLab.Encodings.BinaryVectorEncoding
Files:
2 added
4 deleted
1 edited
3 copied
1 moved

Legend:

Unmodified
Added
Removed
  • branches/PerformanceComparison/HeuristicLab.Encodings.BinaryVectorEncoding/3.3

    • Property svn:ignore
      •  

        old new  
        55*.vs10x
        66Plugin.cs
         7*.DotSettings
  • branches/PerformanceComparison/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/SolutionModel/Univariate/UnivariateModel.cs

    r14685 r14776  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    2422using System.Linq;
    25 using HeuristicLab.Algorithms.MemPR.Interfaces;
    2623using HeuristicLab.Common;
    2724using HeuristicLab.Core;
    2825using HeuristicLab.Data;
    29 using HeuristicLab.Encodings.BinaryVectorEncoding;
     26using HeuristicLab.Optimization;
    3027using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    31 using HeuristicLab.Random;
    3228
    33 namespace HeuristicLab.Algorithms.MemPR.Binary.SolutionModel.Univariate {
     29namespace HeuristicLab.Encodings.BinaryVectorEncoding.SolutionModel {
    3430  [Item("Univariate solution model (binary)", "")]
    3531  [StorableClass]
     
    6763      return vec;
    6864    }
    69 
    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) {
    74         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]++;
    78         }
    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 = Util.Auxiliary.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);
    123     }
    12465  }
    12566}
  • branches/PerformanceComparison/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/SolutionModel/Univariate/UnivariateModelTrainer.cs

    r14685 r14776  
    2222using System;
    2323using System.Collections.Generic;
    24 using HeuristicLab.Algorithms.MemPR.Interfaces;
     24using System.Linq;
    2525using HeuristicLab.Core;
    26 using HeuristicLab.Encodings.BinaryVectorEncoding;
    2726
    28 namespace HeuristicLab.Algorithms.MemPR.Binary.SolutionModel.Univariate {
    29   public enum ModelBiasOptions { Rank, Fitness }
     27namespace HeuristicLab.Encodings.BinaryVectorEncoding.SolutionModel {
     28  public static class UnivariateModelTrainer {
     29    /// <summary>
     30    /// Creates a univariate sampling model out of a ranked population.
     31    /// The first solution in <paramref name="rankedPopulation"/> is the
     32    /// highest influential one, the last solution has the least influence.
     33    /// </summary>
     34    /// <param name="random">The model is stochastic and will use this RNG for sampling.</param>
     35    /// <param name="maximization">Whether higher or lower qualities are better</param>
     36    /// <param name="population">The population that is ranked from highest influential to least influential, e.g. best to worst.</param>
     37    /// <param name="qualities">The solution quality of the respective solution.</param>
     38    /// <returns>The sampling model which is created with the given population.</returns>
     39    public static UnivariateModel TrainWithRankBias(IRandom random, bool maximization, IEnumerable<BinaryVector> population, IEnumerable<double> qualities) {
     40      var popSize = 0;
     41      double[] model = null;
     42      var pop = population.Zip(qualities, (b, q) => new { Solution = b, Fitness = q });
     43      foreach (var ind in maximization ? pop.OrderBy(x => x.Fitness) : pop.OrderByDescending(x => x.Fitness)) {
     44        // from worst to best, worst solution has 1 vote, best solution N votes
     45        popSize++;
     46        if (model == null) model = new double[ind.Solution.Length];
     47        for (var x = 0; x < model.Length; x++) {
     48          if (ind.Solution[x]) model[x] += popSize;
     49        }
     50      }
     51      if (model == null) throw new ArgumentException("Cannot train model from empty population.");
     52      // normalize to [0;1]
     53      var factor = 2.0 / (popSize + 1);
     54      for (var i = 0; i < model.Length; i++) {
     55        model[i] *= factor / popSize;
     56      }
     57      return new UnivariateModel(random, model);
     58    }
    3059
    31   public static class Trainer {
    32     public static ISolutionModel<BinaryVector> TrainBiased(ModelBiasOptions modelBias, IRandom random, bool maximization, IEnumerable<BinaryVector> population, IEnumerable<double> qualities) {
    33       switch (modelBias) {
    34         case ModelBiasOptions.Rank:
    35           return UnivariateModel.CreateWithRankBias(random, maximization, population, qualities);
    36         case ModelBiasOptions.Fitness:
    37           return UnivariateModel.CreateWithFitnessBias(random, maximization, population, qualities);
    38         default:
    39           throw new InvalidOperationException(string.Format("Unknown bias option {0}", modelBias));
     60    /// <summary>
     61    /// Creates a univariate sampling model out of solutions and their given fitness.
     62    /// The best solution's influence is proportional to its fitness, while the worst
     63    /// solution does not have an influence at all (except if the fitness of worst and
     64    /// best are equal).
     65    /// </summary>
     66    /// <param name="random">The model is stochastic and makes use of this RNG instance for sampling.</param>
     67    /// <param name="maximization">Whether higher fitness values are better or lower ones.</param>
     68    /// <param name="population">The solutions that will be used to create the model.</param>
     69    /// <param name="qualities">The solutions' associated qualities.</param>
     70    /// <returns>The sampling model which is created with the given population.</returns>
     71    public static UnivariateModel TrainWithFitnessBias(IRandom random, bool maximization, IEnumerable<BinaryVector> population, IEnumerable<double> qualities) {
     72      var proportions = PrepareProportional(qualities, true, !maximization);
     73      var factor = 1.0 / proportions.Sum();
     74      double[] model = null;
     75      foreach (var ind in population.Zip(proportions, (p, q) => new { Solution = p, Proportion = q })) {
     76        if (model == null) model = new double[ind.Solution.Length];
     77        for (var x = 0; x < model.Length; x++) {
     78          if (ind.Solution[x]) model[x] += ind.Proportion * factor;
     79        }
     80      }
     81      if (model == null) throw new ArgumentException("Cannot train model from empty population.");
     82      return new UnivariateModel(random, model);
     83    }
     84
     85    /// <summary>
     86    /// Creates a univariate sampling model out of solutions. Each of the solutions
     87    /// has the same influence on the model.
     88    /// </summary>
     89    /// <param name="random">The model is stochastic and will make use of this RNG instance.</param>
     90    /// <param name="population">The solutions that are used to create the model.</param>
     91    /// <returns>The model created from the population.</returns>
     92    public static UnivariateModel TrainUnbiased(IRandom random, IEnumerable<BinaryVector> population) {
     93      double[] model = null;
     94      var popSize = 0;
     95      foreach (var p in population) {
     96        popSize++;
     97        if (model == null) model = new double[p.Length];
     98        for (var x = 0; x < model.Length; x++) {
     99          if (p[x]) model[x]++;
     100        }
     101      }
     102      if (model == null) throw new ArgumentException("Cannot train model from empty population.");
     103      // normalize to [0;1]
     104      var factor = 1.0 / popSize;
     105      for (var x = 0; x < model.Length; x++) {
     106        model[x] *= factor;
     107      }
     108      return new UnivariateModel(random, model);
     109    }
     110
     111    // TODO: make PrepareProportional public in EnumerableExtensions
     112    private static double[] PrepareProportional(IEnumerable<double> weights, bool windowing, bool inverseProportional) {
     113      double maxValue = double.MinValue, minValue = double.MaxValue;
     114      double[] valueArray = weights.ToArray();
     115
     116      for (int i = 0; i < valueArray.Length; i++) {
     117        if (valueArray[i] > maxValue) maxValue = valueArray[i];
     118        if (valueArray[i] < minValue) minValue = valueArray[i];
     119      }
     120      if (minValue == maxValue) {  // all values are equal
     121        for (int i = 0; i < valueArray.Length; i++) {
     122          valueArray[i] = 1.0;
     123        }
     124      } else {
     125        if (windowing) {
     126          if (inverseProportional) InverseProportionalScale(valueArray, maxValue);
     127          else ProportionalScale(valueArray, minValue);
     128        } else {
     129          if (minValue < 0.0) throw new InvalidOperationException("Proportional selection without windowing does not work with values < 0.");
     130          if (inverseProportional) InverseProportionalScale(valueArray, 2 * maxValue);
     131        }
     132      }
     133      return valueArray;
     134    }
     135    private static void ProportionalScale(double[] values, double minValue) {
     136      for (int i = 0; i < values.Length; i++) {
     137        values[i] = values[i] - minValue;
    40138      }
    41139    }
    42 
    43     public static ISolutionModel<BinaryVector> TrainUnbiased(IRandom random, IEnumerable<BinaryVector> population) {
    44       return UnivariateModel.CreateWithoutBias(random, population);
     140    private static void InverseProportionalScale(double[] values, double maxValue) {
     141      for (int i = 0; i < values.Length; i++) {
     142        values[i] = maxValue - values[i];
     143      }
    45144    }
    46145  }
Note: See TracChangeset for help on using the changeset viewer.