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 System.Linq;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 |
|
---|
27 | namespace 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 | }
|
---|
59 |
|
---|
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;
|
---|
138 | }
|
---|
139 | }
|
---|
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 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|