- Timestamp:
- 03/22/17 16:52:36 (8 years ago)
- Location:
- branches/PerformanceComparison/HeuristicLab.Encodings.BinaryVectorEncoding
- Files:
-
- 2 added
- 4 deleted
- 2 edited
- 3 copied
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.Encodings.BinaryVectorEncoding/3.3
- Property svn:ignore
-
old new 5 5 *.vs10x 6 6 Plugin.cs 7 *.DotSettings
-
- Property svn:ignore
-
branches/PerformanceComparison/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/HeuristicLab.Encodings.BinaryVectorEncoding-3.3.csproj
r14660 r14776 99 99 </PropertyGroup> 100 100 <ItemGroup> 101 <Reference Include="HeuristicLab.Collections-3.3"> 102 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Collections-3.3.dll</HintPath> 103 <Private>False</Private> 104 </Reference> 105 <Reference Include="HeuristicLab.Common-3.3"> 106 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath> 107 <Private>False</Private> 108 </Reference> 109 <Reference Include="HeuristicLab.Core-3.3"> 110 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath> 111 <Private>False</Private> 112 </Reference> 113 <Reference Include="HeuristicLab.Data-3.3"> 114 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Data-3.3.dll</HintPath> 115 <Private>False</Private> 116 </Reference> 117 <Reference Include="HeuristicLab.Operators-3.3"> 118 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Operators-3.3.dll</HintPath> 119 <Private>False</Private> 120 </Reference> 121 <Reference Include="HeuristicLab.Optimization.Operators-3.3"> 122 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Optimization.Operators-3.3.dll</HintPath> 123 <Private>False</Private> 124 </Reference> 125 <Reference Include="HeuristicLab.Parameters-3.3"> 126 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Parameters-3.3.dll</HintPath> 127 <Private>False</Private> 128 </Reference> 129 <Reference Include="HeuristicLab.Persistence-3.3"> 130 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Persistence-3.3.dll</HintPath> 131 <Private>False</Private> 132 </Reference> 133 <Reference Include="HeuristicLab.PluginInfrastructure-3.3"> 134 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath> 135 <Private>False</Private> 136 </Reference> 101 137 <Reference Include="System" /> 102 138 <Reference Include="System.Core"> … … 153 189 <Compile Include="Properties\AssemblyInfo.cs" /> 154 190 <Compile Include="ShakingOperators\BinaryVectorShakingOperator.cs" /> 155 </ItemGroup> 156 <ItemGroup> 157 <ProjectReference Include="..\..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj"> 158 <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project> 159 <Name>HeuristicLab.Collections-3.3</Name> 160 <Private>False</Private> 161 </ProjectReference> 162 <ProjectReference Include="..\..\HeuristicLab.Common\3.3\HeuristicLab.Common-3.3.csproj"> 163 <Project>{A9AD58B9-3EF9-4CC1-97E5-8D909039FF5C}</Project> 164 <Name>HeuristicLab.Common-3.3</Name> 165 <Private>False</Private> 166 </ProjectReference> 167 <ProjectReference Include="..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj"> 168 <Project>{C36BD924-A541-4A00-AFA8-41701378DDC5}</Project> 169 <Name>HeuristicLab.Core-3.3</Name> 170 <Private>False</Private> 171 </ProjectReference> 172 <ProjectReference Include="..\..\HeuristicLab.Data\3.3\HeuristicLab.Data-3.3.csproj"> 173 <Project>{BBAB9DF5-5EF3-4BA8-ADE9-B36E82114937}</Project> 174 <Name>HeuristicLab.Data-3.3</Name> 175 <Private>False</Private> 176 </ProjectReference> 177 <ProjectReference Include="..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj"> 178 <Project>{23DA7FF4-D5B8-41B6-AA96-F0561D24F3EE}</Project> 179 <Name>HeuristicLab.Operators-3.3</Name> 180 <Private>False</Private> 181 </ProjectReference> 182 <ProjectReference Include="..\..\HeuristicLab.Optimization.Operators\3.3\HeuristicLab.Optimization.Operators-3.3.csproj"> 183 <Project>{25087811-F74C-4128-BC86-8324271DA13E}</Project> 184 <Name>HeuristicLab.Optimization.Operators-3.3</Name> 185 <Private>False</Private> 186 </ProjectReference> 191 <Compile Include="SolutionModel\Univariate\UnivariateModelTrainer.cs" /> 192 <Compile Include="SolutionModel\Univariate\UnivariateModel.cs" /> 193 <Compile Include="SolutionModel\SolutionSamplingOperator.cs" /> 194 <Compile Include="SolutionModel\Univariate\UnivariateModelTrainingOperator.cs" /> 195 </ItemGroup> 196 <ItemGroup> 187 197 <ProjectReference Include="..\..\HeuristicLab.Optimization\3.3\HeuristicLab.Optimization-3.3.csproj"> 188 198 <Project>{14AB8D24-25BC-400C-A846-4627AA945192}</Project> 189 199 <Name>HeuristicLab.Optimization-3.3</Name> 190 <Private>False</Private>191 </ProjectReference>192 <ProjectReference Include="..\..\HeuristicLab.Parameters\3.3\HeuristicLab.Parameters-3.3.csproj">193 <Project>{56F9106A-079F-4C61-92F6-86A84C2D84B7}</Project>194 <Name>HeuristicLab.Parameters-3.3</Name>195 <Private>False</Private>196 </ProjectReference>197 <ProjectReference Include="..\..\HeuristicLab.Persistence\3.3\HeuristicLab.Persistence-3.3.csproj">198 <Project>{102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}</Project>199 <Name>HeuristicLab.Persistence-3.3</Name>200 <Private>False</Private>201 </ProjectReference>202 <ProjectReference Include="..\..\HeuristicLab.PluginInfrastructure\3.3\HeuristicLab.PluginInfrastructure-3.3.csproj">203 <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project>204 <Name>HeuristicLab.PluginInfrastructure-3.3</Name>205 200 <Private>False</Private> 206 201 </ProjectReference> -
branches/PerformanceComparison/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/SolutionModel/Univariate/UnivariateModel.cs
r14685 r14776 20 20 #endregion 21 21 22 using System;23 using System.Collections.Generic;24 22 using System.Linq; 25 using HeuristicLab.Algorithms.MemPR.Interfaces;26 23 using HeuristicLab.Common; 27 24 using HeuristicLab.Core; 28 25 using HeuristicLab.Data; 29 using HeuristicLab. Encodings.BinaryVectorEncoding;26 using HeuristicLab.Optimization; 30 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Random;32 28 33 namespace HeuristicLab. Algorithms.MemPR.Binary.SolutionModel.Univariate{29 namespace HeuristicLab.Encodings.BinaryVectorEncoding.SolutionModel { 34 30 [Item("Univariate solution model (binary)", "")] 35 31 [StorableClass] … … 67 63 return vec; 68 64 } 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 votes96 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 }124 65 } 125 66 } -
branches/PerformanceComparison/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/SolutionModel/Univariate/UnivariateModelTrainer.cs
r14685 r14776 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Algorithms.MemPR.Interfaces;24 using System.Linq; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Encodings.BinaryVectorEncoding;27 26 28 namespace HeuristicLab.Algorithms.MemPR.Binary.SolutionModel.Univariate { 29 public enum ModelBiasOptions { Rank, Fitness } 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 } 30 59 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; 40 138 } 41 139 } 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 } 45 144 } 46 145 }
Note: See TracChangeset
for help on using the changeset viewer.