Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK/NKEvaluatator.cs @ 10879

Last change on this file since 10879 was 7128, checked in by epitzer, 13 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

File size: 5.9 KB
Line 
1
2using System;
3using System.Security.Cryptography;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Encodings.BinaryVectorEncoding;
8using HeuristicLab.Operators;
9using HeuristicLab.Parameters;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11using System.Threading;
12namespace HeuristicLab.Problems.NK {
13
14  [Item("NK Landscape Evaluator", "Evaluates 'points' on an NK Landscape.")]
15  [StorableClass]
16  public class NKEvaluator : SingleSuccessorOperator, INKEvaluator {
17
18    #region Parameters
19
20    public ILookupParameter<DoubleValue> QualityParameter {
21      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
22    }
23    public LookupParameter<DoubleArray> F_iParameter {
24      get { return (LookupParameter<DoubleArray>)Parameters["F_i"]; }
25    }
26    public ILookupParameter<BinaryVector> BinaryVectorParameter {
27      get { return (ILookupParameter<BinaryVector>)Parameters["BinaryVector"]; }
28    }
29    public LookupParameter<BoolMatrix> GeneInteractionsParameter {
30      get { return (LookupParameter<BoolMatrix>)Parameters["GeneInteractions"]; }
31    }
32    public LookupParameter<IntValue> InteractionSeedParameter {
33      get { return (LookupParameter<IntValue>)Parameters["InteractionSeed"]; }
34    }
35    public LookupParameter<DoubleArray> WeightsParameter {
36      get { return (LookupParameter<DoubleArray>)Parameters["Weights"]; }
37    }
38    public ValueLookupParameter<BoolValue> StoreComponentsParameter {
39      get { return (ValueLookupParameter<BoolValue>)Parameters["StoreComponents"]; }
40    }
41
42    #endregion
43
44    #region ParameterValues
45    private double Quality {
46      set { QualityParameter.ActualValue = new DoubleValue(value); }
47    }
48    private BinaryVector BinaryVector {
49      get { return BinaryVectorParameter.ActualValue; }
50    }
51    private BoolMatrix GeneInteractions {
52      get { return GeneInteractionsParameter.ActualValue; }
53    }
54    private int InteractionSeed {
55      get { return InteractionSeedParameter.ActualValue.Value; }
56    }
57    private DoubleArray Weights {
58      get { return WeightsParameter.ActualValue; }
59    }
60    private bool StoreComponents {
61      get {
62        IParameter param;
63        return Parameters.TryGetValue("StoreComponents", out param)
64          && ((ValueLookupParameter<BoolValue>)param).ActualValue.Value == true;
65      }
66    }
67    #endregion
68
69    private ThreadLocal<HashAlgorithm> hashAlgorithm;
70
71    [StorableConstructor]
72    protected NKEvaluator(bool deserializing) : base(deserializing) {
73      hashAlgorithm = new ThreadLocal<HashAlgorithm>(() => HashAlgorithm.Create("MD5"));
74    }
75    protected NKEvaluator(NKEvaluator original, Cloner cloner) : base(original, cloner) {
76      hashAlgorithm = new ThreadLocal<HashAlgorithm>(() => HashAlgorithm.Create("MD5"));
77    }
78    public NKEvaluator() {
79      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the OneMax solution."));
80      Parameters.Add(new LookupParameter<DoubleArray>("F_i", "Fitness component functions."));
81      Parameters.Add(new LookupParameter<BinaryVector>("BinaryVector", "The solution given in path representation which should be evaluated."));
82      Parameters.Add(new LookupParameter<BoolMatrix>("GeneInteractions", "Matrix indicating gene interactions."));
83      Parameters.Add(new LookupParameter<IntValue>("InteractionSeed", "Seed used for randomly generting gene interactions."));
84      Parameters.Add(new LookupParameter<DoubleArray>("Weights", "The weights for the component functions. If shorter, will be repeated."));
85      Parameters.Add(new ValueLookupParameter<BoolValue>("StoreComponents", "Inject values of component functions into the scope (F_i)", new BoolValue(false)));
86      hashAlgorithm = new ThreadLocal<HashAlgorithm>(() => HashAlgorithm.Create("MD5"));
87    }
88
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new NKEvaluator(this, cloner);
91    }
92
93    public long Hash(long x) {
94      return BitConverter.ToInt64(hashAlgorithm.Value.ComputeHash(BitConverter.GetBytes(x), 0, 8), 0);
95    }
96
97    public double F_i(long x, long i, long g_i, long seed) {
98      return Math.Abs((double)Hash((x & g_i) ^ Hash(g_i ^ Hash(i ^ seed))))/long.MaxValue;
99    }
100
101    public double F(long x, long[] g, double[] w, long seed, ref double[] f_i) {
102      double value = 0;
103      for (int i = 0; i<g.Length; i++) {
104        f_i[i] = F_i(x, i, g[i], seed);
105        value += w[i%w.Length] * f_i[i];
106      }
107      return value;
108    }
109
110    public long Encode(BinaryVector v) {
111      long x = 0;
112      for (int i = 0; i<64 && i<v.Length; i++) {
113        x |= (v[i] ? (long)1 : (long)0) << i;
114      }
115      return x;
116    }
117
118    public static long[] Encode(BoolMatrix m) {
119      long[] x = new long[m.Columns];
120      for (int c = 0; c<m.Columns; c++) {
121        x[c] = 0;
122        for (int r = 0; r<64 && r<m.Rows; r++) {
123          x[c] |= (m[r, c] ? (long)1 : (long)0) << r;
124        }
125      }
126      return x;
127    }
128
129    public static double[] Normalize(DoubleArray weights) {
130      double sum = 0;
131      double[] w = new double[weights.Length];
132      foreach (var v in weights) {
133        sum += Math.Abs(v);
134      }
135      for (int i = 0; i<weights.Length; i++) {
136        w[i] = Math.Abs(weights[i])/sum;
137      }
138      return w;
139    }
140
141    public sealed override IOperation Apply() {
142      double[] f_i;
143      Quality = Evaluate(BinaryVector, GeneInteractions, Weights, InteractionSeed, out f_i);
144      if (StoreComponents)
145        F_iParameter.ActualValue = new DoubleArray(f_i);
146      return base.Apply();
147    }
148
149    public double Evaluate(BinaryVector vector, BoolMatrix interactions, DoubleArray weights, int seed, out double[] f_i) {
150      long x = Encode(vector);
151      long[] g = Encode(interactions);
152      double[] w = Normalize(weights);
153      f_i = new double[interactions.Columns];
154      return F(x, g, w, (long)seed, ref f_i);
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.