Free cookie consent management tool by TermsFeed Policy Generator

Changeset 12576


Ignore:
Timestamp:
07/02/15 15:36:04 (9 years ago)
Author:
epitzer
Message:

#2306 Implement NKq and NKp landscape (and fix Seed parameter clash with Algorithm's seed)

Location:
branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK/NKBitFlipMoveEvaluator.cs

    r12569 r12576  
    6161      int seed = InteractionSeedParameter.ActualValue.Value;
    6262      double moveQuality = QualityParameter.ActualValue.Value;
     63      int q = QParameter.ActualValue.Value;
     64      double p = PParameter.ActualValue.Value;
    6365
    6466      List<int> affectedFitnessComponents = new List<int>();
     
    7173      if (affectedFitnessComponents.Count * 2 > interactions.Columns) {
    7274        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);
    7476      } else {
    7577        long x = NKLandscape.Encode(binaryVector);
     
    7880        double[] w = NKLandscape.Normalize(weights);
    7981        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);
    8284        }
    8385      }
  • branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK/NKLandscape.cs

    r12573 r12576  
    4949    }
    5050    public IValueParameter<IntValue> SeedParameter {
    51       get { return (IValueParameter<IntValue>)Parameters["Seed"]; }
     51      get { return (IValueParameter<IntValue>)Parameters["ProblemSeed"]; }
    5252    }
    5353    public IValueParameter<IntValue> InteractionSeedParameter {
     
    5959    public IValueParameter<IntValue> NrOfFitnessComponentsParameter {
    6060      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"]; }
    6167    }
    6268    public IValueParameter<DoubleArray> WeightsParameter {
     
    9399      get { return WeightsInitializerParameter.Value; }
    94100    }
     101    public int Q { get { return QParameter.Value.Value; } }
     102    public double P { get { return PParameter.Value.Value; } }
    95103    public IntValue Seed {
    96104      get { return SeedParameter.Value; }
     
    103111    [ThreadStatic]
    104112    private static HashAlgorithm hashAlgorithm;
     113
     114    [ThreadStatic]
     115    private static HashAlgorithm hashAlgorithmP;
    105116
    106117    public static HashAlgorithm HashAlgorithm {
     
    113124    }
    114125
     126    public static HashAlgorithm HashAlgorithmP {
     127      get {
     128        if (hashAlgorithmP == null) {
     129          hashAlgorithmP = HashAlgorithm.Create("SHA1");
     130        }
     131        return hashAlgorithmP;
     132      }
     133    }
     134
    115135    [StorableConstructor]
    116136    private NKLandscape(bool deserializing) : base(deserializing) { }
     
    125145
    126146      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)));
    128148      random.Reset(Seed.Value);
    129149
     
    131151      Parameters.Add(new ValueParameter<IntValue>("NrOfFitnessComponents", "Number of fitness component functions. (nr of columns in the interaction column)", new IntValue(10)));
    132152      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)));
    133155      Parameters.Add(new ValueParameter<DoubleArray>("Weights", "The weights for the component functions. If shorted, will be repeated.", new DoubleArray(new[] { 1.0 })));
    134156      Parameters.Add(new OptionalConstrainedValueParameter<IInteractionInitializer>("InteractionInitializer", "Initialize interactions within the component functions."));
     
    224246
    225247    #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) {
    235265      double value = 0;
    236266      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);
    238268        value += w[i % w.Length] * f_i[i];
    239269      }
     
    272302    }
    273303
    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) {
    275305      long x = Encode(vector);
    276306      long[] g = Encode(interactions);
    277307      double[] w = Normalize(weights);
    278308      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);
    280310    }
    281311
    282312    public override double Evaluate(BinaryVector vector, IRandom random) {
    283313      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);
    285315      return quality;
    286316    }
  • branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK/NKMoveEvaluator.cs

    r12569 r12576  
    5252      get { return (ILookupParameter<DoubleArray>)Parameters["Weights"]; }
    5353    }
     54    public ILookupParameter<IntValue> QParameter { get { return (ILookupParameter<IntValue>)Parameters["Q"]; } }
     55    public ILookupParameter<DoubleValue> PParameter { get { return (ILookupParameter<DoubleValue>)Parameters["P"]; } }
    5456
    5557    [StorableConstructor]
     
    6466      Parameters.Add(new LookupParameter<IntValue>("InteractionSeed", "Seed used for randomly generting gene interactions."));
    6567      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"));
    6670    }
    6771  }
Note: See TracChangeset for help on using the changeset viewer.