- Timestamp:
- 07/01/15 15:17:28 (10 years ago)
- Location:
- branches/FitnessLandscapeAnalysis
- Files:
-
- 3 deleted
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.sln
r12483 r12565 15 15 EndProject 16 16 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VRPProblemAnalyzer", "VRPProblemAnalyzer\VRPProblemAnalyzer.csproj", "{683C29F1-9A43-4998-B58A-C477B86B7A64}" 17 EndProject18 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Problems.NKNew", "HeuristicLab.Problems.NKNew\HeuristicLab.Problems.NKNew.csproj", "{56D80626-06F1-49AE-A911-3F3A20888968}"19 17 EndProject 20 18 Global … … 100 98 {683C29F1-9A43-4998-B58A-C477B86B7A64}.Release|x86.ActiveCfg = Release|x86 101 99 {683C29F1-9A43-4998-B58A-C477B86B7A64}.Release|x86.Build.0 = Release|x86 102 {56D80626-06F1-49AE-A911-3F3A20888968}.Debug|Any CPU.ActiveCfg = Debug|Any CPU103 {56D80626-06F1-49AE-A911-3F3A20888968}.Debug|Any CPU.Build.0 = Debug|Any CPU104 {56D80626-06F1-49AE-A911-3F3A20888968}.Debug|x64.ActiveCfg = Debug|x64105 {56D80626-06F1-49AE-A911-3F3A20888968}.Debug|x64.Build.0 = Debug|x64106 {56D80626-06F1-49AE-A911-3F3A20888968}.Debug|x86.ActiveCfg = Debug|x86107 {56D80626-06F1-49AE-A911-3F3A20888968}.Debug|x86.Build.0 = Debug|x86108 {56D80626-06F1-49AE-A911-3F3A20888968}.Release|Any CPU.ActiveCfg = Release|Any CPU109 {56D80626-06F1-49AE-A911-3F3A20888968}.Release|Any CPU.Build.0 = Release|Any CPU110 {56D80626-06F1-49AE-A911-3F3A20888968}.Release|x64.ActiveCfg = Release|x64111 {56D80626-06F1-49AE-A911-3F3A20888968}.Release|x64.Build.0 = Release|x64112 {56D80626-06F1-49AE-A911-3F3A20888968}.Release|x86.ActiveCfg = Release|x86113 {56D80626-06F1-49AE-A911-3F3A20888968}.Release|x86.Build.0 = Release|x86114 100 EndGlobalSection 115 101 GlobalSection(SolutionProperties) = preSolution -
branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK/HeuristicLab.Problems.NK.csproj
r11626 r12565 123 123 <Reference Include="HeuristicLab.Encodings.BinaryVectorEncoding-3.3"> 124 124 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Encodings.BinaryVectorEncoding-3.3.dll</HintPath> 125 <Private>False</Private> 125 126 </Reference> 126 127 <Reference Include="HeuristicLab.Operators-3.3"> … … 138 139 <Reference Include="HeuristicLab.PluginInfrastructure-3.3"> 139 140 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath> 141 </Reference> 142 <Reference Include="HeuristicLab.Problems.Binary-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 143 <SpecificVersion>False</SpecificVersion> 144 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Problems.Binary-3.3.dll</HintPath> 140 145 </Reference> 141 146 <Reference Include="HeuristicLab.Random-3.3"> … … 160 165 <Compile Include="InteractionInitializers\SortedRandomInteractionsInitializer.cs" /> 161 166 <Compile Include="InteractionInitializers\IInteractionInitializer.cs" /> 162 <Compile Include="INKEvaluator.cs" />163 167 <Compile Include="INKMoveEvaluator.cs" /> 168 <Compile Include="NKLandscape.cs" /> 164 169 <Compile Include="WeightInitializers\ExponentialDistributionWeightsInitializer.cs" /> 165 170 <Compile Include="WeightInitializers\ReverseLinearWeightsInitializer.cs" /> … … 169 174 <Compile Include="WeightInitializers\IWeightsInitializer.cs" /> 170 175 <Compile Include="NKBitFlipMoveEvaluator.cs" /> 171 <Compile Include="NKEvaluatator.cs" />172 <Compile Include="NKLandscape.cs" />173 176 <Compile Include="HeuristicLabProblemsNKPlugin.cs" /> 174 177 <Compile Include="NKMoveEvaluator.cs" /> -
branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK/NKBitFlipMoveEvaluator.cs
r7128 r12565 1 1 using System.Collections.Generic; 2 using System.Linq;3 2 using HeuristicLab.Common; 4 3 using HeuristicLab.Core; … … 7 6 using HeuristicLab.Parameters; 8 7 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 9 using System;10 8 11 9 namespace HeuristicLab.Problems.NK { … … 20 18 public LookupParameter<BinaryVector> MovedBinaryVectorParameter { 21 19 get { return (LookupParameter<BinaryVector>)Parameters["MovedBinaryVector"]; } 22 } 23 24 private NKEvaluator NKEvaluator = new NKEvaluator(); 20 } 25 21 26 22 [StorableConstructor] 27 23 protected NKBitFlipMoveEvaluator(bool deserializing) : base(deserializing) { } 28 protected NKBitFlipMoveEvaluator(NKBitFlipMoveEvaluator original, Cloner cloner) : base(original, cloner) { } 24 25 protected NKBitFlipMoveEvaluator(NKBitFlipMoveEvaluator original, Cloner cloner) 26 : base(original, cloner) { } 29 27 public NKBitFlipMoveEvaluator() 30 28 : base() { … … 46 44 47 45 List<int> affectedFitnessComponents = new List<int>(); 48 for (int c = 0; c <interactions.Columns; c++)46 for (int c = 0; c < interactions.Columns; c++) 49 47 if (interactions[move.Index, c]) 50 48 affectedFitnessComponents.Add(c); … … 52 50 MovedBinaryVectorParameter.ActualValue = moved; 53 51 moved[move.Index] = !moved[move.Index]; 54 if (affectedFitnessComponents.Count *2 > interactions.Columns) {52 if (affectedFitnessComponents.Count * 2 > interactions.Columns) { 55 53 double[] f_i; 56 moveQuality = NK Evaluator.Evaluate(moved, interactions, weights, seed, out f_i);54 moveQuality = NKLandscape.Evaluate(moved, interactions, weights, seed, out f_i); 57 55 } else { 58 long x = NK Evaluator.Encode(binaryVector);59 long y = NK Evaluator.Encode(moved);60 long[] g = NK Evaluator.Encode(interactions);61 double[] w = NK Evaluator.Normalize(weights);56 long x = NKLandscape.Encode(binaryVector); 57 long y = NKLandscape.Encode(moved); 58 long[] g = NKLandscape.Encode(interactions); 59 double[] w = NKLandscape.Normalize(weights); 62 60 foreach (var c in affectedFitnessComponents) { 63 moveQuality -= w[c %w.Length] * NKEvaluator.F_i(x, c, g[c], seed);64 moveQuality += w[c %w.Length] * NKEvaluator.F_i(y, c, g[c], seed);61 moveQuality -= w[c % w.Length] * NKLandscape.F_i(x, c, g[c], seed); 62 moveQuality += w[c % w.Length] * NKLandscape.F_i(y, c, g[c], seed); 65 63 } 66 64 } -
branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK/NKLandscape.cs
r8172 r12565 1 1 using System; 2 using System.Collections.Generic;3 2 using System.Linq; 3 using System.Security.Cryptography; 4 4 using HeuristicLab.Common; 5 5 using HeuristicLab.Core; 6 6 using HeuristicLab.Data; 7 7 using HeuristicLab.Encodings.BinaryVectorEncoding; 8 using HeuristicLab.Optimization;9 8 using HeuristicLab.Parameters; 10 9 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 11 10 using HeuristicLab.PluginInfrastructure; 11 using HeuristicLab.Problems.Binary; 12 12 using HeuristicLab.Problems.NK.WeightInitializers; 13 13 using HeuristicLab.Random; … … 18 18 [Creatable("Problems")] 19 19 [StorableClass] 20 public sealed class NKLandscape : SingleObjectiveHeuristicOptimizationProblem<INKEvaluator, IBinaryVectorCreator>, IStorableContent { 21 22 public string Filename { get; set; } 23 24 #region Parameter Properties 25 public ValueParameter<IntValue> LengthParameter { 26 get { return (ValueParameter<IntValue>)Parameters["Length"]; } 27 } 20 public sealed class NKLandscape : BinaryProblem { 21 public override bool Maximization { 22 get { return false; } 23 } 24 25 #region Parameters 28 26 public ValueParameter<BoolMatrix> GeneInteractionsParameter { 29 27 get { return (ValueParameter<BoolMatrix>)Parameters["GeneInteractions"]; } … … 50 48 51 49 #region Properties 52 public IntValue Length {53 get { return LengthParameter.Value; }54 set { LengthParameter.Value = value; }55 }56 50 public IInteractionInitializer InteractionInitializer { 57 51 get { return InteractionInitializerParameter.Value; } 58 52 } 59 [Storable(AllowOneWay = true)] 60 private List<IOperator> operators { 61 set { 62 Operators.AddRange(value); 63 } 64 } 65 #endregion 66 67 private static MersenneTwister random = new MersenneTwister(); 53 public BoolMatrix GeneInteractions { 54 get { return GeneInteractionsParameter.Value; } 55 } 56 public DoubleArray Weights { 57 get { return WeightsParameter.Value; } 58 } 59 public IntValue InteractionSeed { 60 get { return InteractionSeedParameter.Value; } 61 } 62 #endregion 63 64 [ThreadStatic] 65 private static MersenneTwister random; 66 public static MersenneTwister Random { 67 get { 68 if (random == null) { 69 random = new MersenneTwister(); 70 } 71 return random; 72 } 73 } 74 75 [ThreadStatic] 76 private static HashAlgorithm hashAlgorithm; 77 78 public static HashAlgorithm HashAlgorithm { 79 get { 80 if (hashAlgorithm == null) { 81 hashAlgorithm = HashAlgorithm.Create("MD5"); 82 } 83 return hashAlgorithm; 84 } 85 } 68 86 69 87 [StorableConstructor] … … 71 89 private NKLandscape(NKLandscape original, Cloner cloner) 72 90 : base(original, cloner) { 73 AttachEventHandlers();91 RegisterEventHandlers(); 74 92 } 75 93 public NKLandscape() 76 94 : base() { 77 Parameters.Add(new ValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(10)));78 95 Parameters.Add(new ValueParameter<BoolMatrix>("GeneInteractions", "Every column gives the participating genes for each fitness component")); 79 Parameters.Add(new ValueParameter<IntValue>("InteractionSeed", "The seed used for the hash function to generate interaction tables.", new IntValue( random.Next())));96 Parameters.Add(new ValueParameter<IntValue>("InteractionSeed", "The seed used for the hash function to generate interaction tables.", new IntValue(Random.Next()))); 80 97 Parameters.Add(new ValueParameter<IntValue>("NrOfFitnessComponents", "Number of fitness component functions. (nr of columns in the interaction column)", new IntValue(10))); 81 98 Parameters.Add(new ValueParameter<IntValue>("NrOfInteractions", "Number of genes interacting with each other. (nr of True values per column in the interaction matrix)", new IntValue(3))); … … 84 101 Parameters.Add(new OptionalConstrainedValueParameter<IWeightsInitializer>("WeightsInitializer", "Operator to initialize weights distribution")); 85 102 86 SolutionCreator = new RandomBinaryVectorCreator();87 Evaluator = new NKEvaluator();88 89 103 InitializeInteractionInitializerParameter(); 90 104 InitializeWeightsInitializerParameter(); 91 105 92 ParameterizeSolutionCreator();93 ParameterizeEvaluator();94 95 106 InitializeOperators(); 96 AttachEventHandlers();107 RegisterEventHandlers(); 97 108 InitializeInteractions(); 98 109 } … … 115 126 116 127 #region Events 117 protected override void OnSolutionCreatorChanged() { 118 SolutionCreator.BinaryVectorParameter.ActualNameChanged += SolutionCreator_BinaryVectorParameter_ActualNameChanged; 119 ParameterizeSolutionCreator(); 120 ParameterizeEvaluator(); 121 ParameterizeAnalyzer(); 122 ParameterizeOperators(); 123 base.OnSolutionCreatorChanged(); 124 } 125 private void SolutionCreator_BinaryVectorParameter_ActualNameChanged(object sender, EventArgs e) { 126 ParameterizeEvaluator(); 127 ParameterizeAnalyzer(); 128 ParameterizeOperators(); 129 } 130 protected override void OnEvaluatorChanged() { 131 ParameterizeEvaluator(); 132 ParameterizeAnalyzer(); 133 base.OnEvaluatorChanged(); 134 } 135 void LengthParameter_ValueChanged(object sender, EventArgs e) { 136 ParameterizeSolutionCreator(); 137 LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged); 138 BestKnownQualityParameter.Value.Value = Length.Value; 139 } 140 void Length_ValueChanged(object sender, EventArgs e) { 141 NrOfFitnessComponentsParameter.Value = new IntValue(Length.Value); 142 } 143 void OneBitflipMoveParameter_ActualNameChanged(object sender, EventArgs e) { 144 string name = ((ILookupParameter<OneBitflipMove>)sender).ActualName; 145 foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) { 146 op.OneBitflipMoveParameter.ActualName = name; 147 } 128 protected override void LengthParameter_ValueChanged(object sender, EventArgs e) { 129 BestKnownQualityParameter.Value = new DoubleValue(Length); 130 NrOfFitnessComponentsParameter.Value = new IntValue(Length); 148 131 } 149 132 #endregion … … 152 135 [StorableHook(HookType.AfterDeserialization)] 153 136 private void AfterDeserialization() { 154 if (!Parameters.ContainsKey("InteractionInitializer")) { 155 Parameters.Add(new OptionalConstrainedValueParameter<IInteractionInitializer>("InteractionInitializer", "Initialized the interaction between bits and fitness components")); 156 InitializeInteractionInitializerParameter(); 157 } 158 if (!Parameters.ContainsKey("WeightsInitializer")) { 159 Parameters.Add(new OptionalConstrainedValueParameter<IWeightsInitializer>("WeightsInitializer", "Operator to initialize weights distribution")); 160 InitializeWeightsInitializerParameter(); 161 } 162 AttachEventHandlers(); 163 } 164 165 private void AttachEventHandlers() { 166 SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged); 167 LengthParameter.ValueChanged += new EventHandler(LengthParameter_ValueChanged); 168 LengthParameter.Value.ValueChanged += new EventHandler(Length_ValueChanged); 137 RegisterEventHandlers(); 138 } 139 140 private void RegisterEventHandlers() { 169 141 NrOfInteractionsParameter.ValueChanged += InteractionParameterChanged; 170 142 NrOfInteractionsParameter.Value.ValueChanged += InteractionParameterChanged; 171 143 NrOfFitnessComponentsParameter.ValueChanged += InteractionParameterChanged; 172 144 NrOfFitnessComponentsParameter.Value.ValueChanged += InteractionParameterChanged; 173 InteractionInitializerParameter.ValueChanged += new EventHandler(InteractionInitializerParameter_ValueChanged);174 WeightsInitializerParameter.ValueChanged += new EventHandler(WeightsInitializerParameter_ValueChanged);145 InteractionInitializerParameter.ValueChanged += InteractionInitializerParameter_ValueChanged; 146 WeightsInitializerParameter.ValueChanged += WeightsInitializerParameter_ValueChanged; 175 147 } 176 148 … … 187 159 } 188 160 189 private void ParameterizeSolutionCreator() {190 SolutionCreator.LengthParameter.ActualName = LengthParameter.Name;191 }192 193 private void ParameterizeEvaluator() {194 if (Evaluator is NKEvaluator)195 ((NKEvaluator)Evaluator).BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;196 }197 private void ParameterizeAnalyzer() {198 //BestOneMaxSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;199 //BestOneMaxSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;200 //BestOneMaxSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;201 //BestOneMaxSolutionAnalyzer.ResultsParameter.ActualName = "Results";202 }203 204 161 private void InitializeOperators() { 205 ParameterizeAnalyzer(); 206 foreach (IBinaryVectorOperator op in ApplicationManager.Manager.GetInstances<IBinaryVectorOperator>()) { 207 if (!(op is ISingleObjectiveMoveEvaluator) || (op is INKMoveEvaluator)) { 208 Operators.Add(op); 209 } 210 } 211 ParameterizeOperators(); 212 InitializeMoveGenerators(); 213 } 214 private void InitializeMoveGenerators() { 215 foreach (IOneBitflipMoveOperator op in Operators.OfType<IOneBitflipMoveOperator>()) { 216 if (op is IMoveGenerator) { 217 op.OneBitflipMoveParameter.ActualNameChanged += new EventHandler(OneBitflipMoveParameter_ActualNameChanged); 218 } 219 } 220 } 221 private void ParameterizeOperators() { 222 foreach (IBinaryVectorCrossover op in Operators.OfType<IBinaryVectorCrossover>()) { 223 op.ParentsParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName; 224 op.ChildParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName; 225 } 226 foreach (IBinaryVectorManipulator op in Operators.OfType<IBinaryVectorManipulator>()) { 227 op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName; 228 } 229 foreach (IBinaryVectorMoveOperator op in Operators.OfType<IBinaryVectorMoveOperator>()) { 230 op.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName; 231 } 162 NKBitFlipMoveEvaluator nkEvaluator = new NKBitFlipMoveEvaluator(); 163 Encoding.ConfigureOperator(nkEvaluator); 164 Operators.Add(nkEvaluator); 232 165 } 233 166 … … 235 168 if (InteractionInitializer != null) 236 169 GeneInteractionsParameter.Value = InteractionInitializer.InitializeInterations( 237 Length .Value,170 Length, 238 171 NrOfFitnessComponentsParameter.Value.Value, 239 NrOfInteractionsParameter.Value.Value, 240 random); 172 NrOfInteractionsParameter.Value.Value, Random); 241 173 } 242 174 … … 249 181 #endregion 250 182 183 #region Evaluation function 184 public static long Hash(long x) { 185 return BitConverter.ToInt64(HashAlgorithm.ComputeHash(BitConverter.GetBytes(x), 0, 8), 0); 186 } 187 188 public static double F_i(long x, long i, long g_i, long seed) { 189 return Math.Abs((double)Hash((x & g_i) ^ Hash(g_i ^ Hash(i ^ seed)))) / long.MaxValue; 190 } 191 192 public static double F(long x, long[] g, double[] w, long seed, ref double[] f_i) { 193 double value = 0; 194 for (int i = 0; i < g.Length; i++) { 195 f_i[i] = F_i(x, i, g[i], seed); 196 value += w[i % w.Length] * f_i[i]; 197 } 198 return value; 199 } 200 201 public static long Encode(BinaryVector v) { 202 long x = 0; 203 for (int i = 0; i < 64 && i < v.Length; i++) { 204 x |= (v[i] ? (long)1 : (long)0) << i; 205 } 206 return x; 207 } 208 209 public static long[] Encode(BoolMatrix m) { 210 long[] x = new long[m.Columns]; 211 for (int c = 0; c < m.Columns; c++) { 212 x[c] = 0; 213 for (int r = 0; r < 64 && r < m.Rows; r++) { 214 x[c] |= (m[r, c] ? (long)1 : (long)0) << r; 215 } 216 } 217 return x; 218 } 219 220 public static double[] Normalize(DoubleArray weights) { 221 double sum = 0; 222 double[] w = new double[weights.Length]; 223 foreach (var v in weights) { 224 sum += Math.Abs(v); 225 } 226 for (int i = 0; i < weights.Length; i++) { 227 w[i] = Math.Abs(weights[i]) / sum; 228 } 229 return w; 230 } 231 232 public static double Evaluate(BinaryVector vector, BoolMatrix interactions, DoubleArray weights, int seed, out double[] f_i) { 233 long x = Encode(vector); 234 long[] g = Encode(interactions); 235 double[] w = Normalize(weights); 236 f_i = new double[interactions.Columns]; 237 return F(x, g, w, (long)seed, ref f_i); 238 } 239 240 public override double Evaluate(BinaryVector vector, IRandom random) { 241 double[] f_i;//not used atm 242 double quality = Evaluate(vector, GeneInteractions, Weights, InteractionSeed.Value, out f_i); 243 return quality; 244 } 245 #endregion 251 246 } 252 247 }
Note: See TracChangeset
for help on using the changeset viewer.