Changeset 12576 for branches/FitnessLandscapeAnalysis
- Timestamp:
- 07/02/15 15:36:04 (9 years ago)
- Location:
- branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK/NKBitFlipMoveEvaluator.cs
r12569 r12576 61 61 int seed = InteractionSeedParameter.ActualValue.Value; 62 62 double moveQuality = QualityParameter.ActualValue.Value; 63 int q = QParameter.ActualValue.Value; 64 double p = PParameter.ActualValue.Value; 63 65 64 66 List<int> affectedFitnessComponents = new List<int>(); … … 71 73 if (affectedFitnessComponents.Count * 2 > interactions.Columns) { 72 74 double[] f_i; 73 moveQuality = NKLandscape.Evaluate(moved, interactions, weights, seed, out f_i );75 moveQuality = NKLandscape.Evaluate(moved, interactions, weights, seed, out f_i, q, p); 74 76 } else { 75 77 long x = NKLandscape.Encode(binaryVector); … … 78 80 double[] w = NKLandscape.Normalize(weights); 79 81 foreach (var c in affectedFitnessComponents) { 80 moveQuality -= w[c % w.Length] * NKLandscape.F_i(x, c, g[c], seed);81 moveQuality += w[c % w.Length] * NKLandscape.F_i(y, c, g[c], seed);82 moveQuality -= w[c%w.Length]*NKLandscape.F_i(x, c, g[c], seed, q, p); 83 moveQuality += w[c%w.Length]*NKLandscape.F_i(y, c, g[c], seed, q, p); 82 84 } 83 85 } -
branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK/NKLandscape.cs
r12573 r12576 49 49 } 50 50 public IValueParameter<IntValue> SeedParameter { 51 get { return (IValueParameter<IntValue>)Parameters[" Seed"]; }51 get { return (IValueParameter<IntValue>)Parameters["ProblemSeed"]; } 52 52 } 53 53 public IValueParameter<IntValue> InteractionSeedParameter { … … 59 59 public IValueParameter<IntValue> NrOfFitnessComponentsParameter { 60 60 get { return (IValueParameter<IntValue>)Parameters["NrOfFitnessComponents"]; } 61 } 62 public IValueParameter<IntValue> QParameter { 63 get { return (IValueParameter<IntValue>)Parameters["Q"]; } 64 } 65 public IValueParameter<DoubleValue> PParameter { 66 get { return (IValueParameter<DoubleValue>)Parameters["P"]; } 61 67 } 62 68 public IValueParameter<DoubleArray> WeightsParameter { … … 93 99 get { return WeightsInitializerParameter.Value; } 94 100 } 101 public int Q { get { return QParameter.Value.Value; } } 102 public double P { get { return PParameter.Value.Value; } } 95 103 public IntValue Seed { 96 104 get { return SeedParameter.Value; } … … 103 111 [ThreadStatic] 104 112 private static HashAlgorithm hashAlgorithm; 113 114 [ThreadStatic] 115 private static HashAlgorithm hashAlgorithmP; 105 116 106 117 public static HashAlgorithm HashAlgorithm { … … 113 124 } 114 125 126 public static HashAlgorithm HashAlgorithmP { 127 get { 128 if (hashAlgorithmP == null) { 129 hashAlgorithmP = HashAlgorithm.Create("SHA1"); 130 } 131 return hashAlgorithmP; 132 } 133 } 134 115 135 [StorableConstructor] 116 136 private NKLandscape(bool deserializing) : base(deserializing) { } … … 125 145 126 146 Parameters.Add(new ValueParameter<BoolMatrix>("GeneInteractions", "Every column gives the participating genes for each fitness component")); 127 Parameters.Add(new ValueParameter<IntValue>(" Seed", "The seed used for the random number generator.", new IntValue(0)));147 Parameters.Add(new ValueParameter<IntValue>("ProblemSeed", "The seed used for the random number generator.", new IntValue(0))); 128 148 random.Reset(Seed.Value); 129 149 … … 131 151 Parameters.Add(new ValueParameter<IntValue>("NrOfFitnessComponents", "Number of fitness component functions. (nr of columns in the interaction column)", new IntValue(10))); 132 152 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))); 153 Parameters.Add(new ValueParameter<IntValue>("Q", "Number of allowed fitness values in the (virutal) random table, or zero", new IntValue(0))); 154 Parameters.Add(new ValueParameter<DoubleValue>("P", "Probability of any entry in the (virtual) random table being zero", new DoubleValue(0))); 133 155 Parameters.Add(new ValueParameter<DoubleArray>("Weights", "The weights for the component functions. If shorted, will be repeated.", new DoubleArray(new[] { 1.0 }))); 134 156 Parameters.Add(new OptionalConstrainedValueParameter<IInteractionInitializer>("InteractionInitializer", "Initialize interactions within the component functions.")); … … 224 246 225 247 #region Evaluation function 226 public static long Hash(long x) { 227 return BitConverter.ToInt64(HashAlgorithm.ComputeHash(BitConverter.GetBytes(x), 0, 8), 0); 228 } 229 230 public static double F_i(long x, long i, long g_i, long seed) { 231 return Math.Abs((double)Hash((x & g_i) ^ Hash(g_i ^ Hash(i ^ seed)))) / long.MaxValue; 232 } 233 234 public static double F(long x, long[] g, double[] w, long seed, ref double[] f_i) { 248 public static long Hash(long x, HashAlgorithm hashAlg) { 249 return BitConverter.ToInt64(hashAlg.ComputeHash(BitConverter.GetBytes(x), 0, 8), 0); 250 } 251 252 public static double F_i(long x, long i, long g_i, long seed, int q, double p) { 253 var hash = new Func<long,long>(y => Hash(y, HashAlgorithm)); 254 var fi = Math.Abs((double)hash((x & g_i) ^ hash(g_i ^ hash(i ^ seed)))) / long.MaxValue; 255 if (q > 0) { fi = Math.Round(fi*q)/q; } 256 if (p > 0) { 257 hash = y => Hash(y, HashAlgorithmP); 258 var r = Math.Abs((double)hash((x & g_i) ^ hash(g_i ^ hash(i ^ seed)))) / long.MaxValue; 259 fi = (r <= p) ? 0 : fi; 260 } 261 return fi; 262 } 263 264 public static double F(long x, long[] g, double[] w, long seed, ref double[] f_i, int q, double p) { 235 265 double value = 0; 236 266 for (int i = 0; i < g.Length; i++) { 237 f_i[i] = F_i(x, i, g[i], seed );267 f_i[i] = F_i(x, i, g[i], seed, q, p); 238 268 value += w[i % w.Length] * f_i[i]; 239 269 } … … 272 302 } 273 303 274 public static double Evaluate(BinaryVector vector, BoolMatrix interactions, DoubleArray weights, int seed, out double[] f_i ) {304 public static double Evaluate(BinaryVector vector, BoolMatrix interactions, DoubleArray weights, int seed, out double[] f_i, int q, double p) { 275 305 long x = Encode(vector); 276 306 long[] g = Encode(interactions); 277 307 double[] w = Normalize(weights); 278 308 f_i = new double[interactions.Columns]; 279 return F(x, g, w, (long)seed, ref f_i );309 return F(x, g, w, (long)seed, ref f_i, q, p); 280 310 } 281 311 282 312 public override double Evaluate(BinaryVector vector, IRandom random) { 283 313 double[] f_i;//not used atm 284 double quality = Evaluate(vector, GeneInteractions, Weights, InteractionSeed.Value, out f_i );314 double quality = Evaluate(vector, GeneInteractions, Weights, InteractionSeed.Value, out f_i, Q, P); 285 315 return quality; 286 316 } -
branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK/NKMoveEvaluator.cs
r12569 r12576 52 52 get { return (ILookupParameter<DoubleArray>)Parameters["Weights"]; } 53 53 } 54 public ILookupParameter<IntValue> QParameter { get { return (ILookupParameter<IntValue>)Parameters["Q"]; } } 55 public ILookupParameter<DoubleValue> PParameter { get { return (ILookupParameter<DoubleValue>)Parameters["P"]; } } 54 56 55 57 [StorableConstructor] … … 64 66 Parameters.Add(new LookupParameter<IntValue>("InteractionSeed", "Seed used for randomly generting gene interactions.")); 65 67 Parameters.Add(new LookupParameter<DoubleArray>("Weights", "The weights for the component functions. If shorter, will be repeated.")); 68 Parameters.Add(new LookupParameter<IntValue>("Q", "Number of allowed fitness values in the (virutal) random table, or zero")); 69 Parameters.Add(new LookupParameter<DoubleValue>("P", "Probability of any entry in the (virtual) random table being zero")); 66 70 } 67 71 }
Note: See TracChangeset
for help on using the changeset viewer.