Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK/NKBitFlipMoveEvaluator.cs @ 11140

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

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

File size: 3.2 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Encodings.BinaryVectorEncoding;
7using HeuristicLab.Parameters;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using System;
10
11namespace HeuristicLab.Problems.NK {
12
13  [Item("NK BitFlip Move Evaluator", "Evaluates a single bit flip on an NK landscape.")]
14  [StorableClass]
15  public class NKBitFlipMoveEvaluator : NKMoveEvaluator, IOneBitflipMoveOperator {
16
17    public ILookupParameter<OneBitflipMove> OneBitflipMoveParameter {
18      get { return (ILookupParameter<OneBitflipMove>)Parameters["OneBitflipMove"]; }
19    }
20    public LookupParameter<BinaryVector> MovedBinaryVectorParameter {
21      get { return (LookupParameter<BinaryVector>)Parameters["MovedBinaryVector"]; }
22    }   
23
24    private NKEvaluator NKEvaluator = new NKEvaluator();
25
26    [StorableConstructor]
27    protected NKBitFlipMoveEvaluator(bool deserializing) : base(deserializing) { }
28    protected NKBitFlipMoveEvaluator(NKBitFlipMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
29    public NKBitFlipMoveEvaluator()
30      : base() {
31      Parameters.Add(new LookupParameter<OneBitflipMove>("OneBitflipMove", "The move to evaluate."));
32      Parameters.Add(new LookupParameter<BinaryVector>("MovedBinaryVector", "The resulting binary vector after the move."));
33    }
34
35    public override IDeepCloneable Clone(Cloner cloner) {
36      return new NKBitFlipMoveEvaluator(this, cloner);
37    }
38
39    public override IOperation Apply() {
40      BinaryVector binaryVector = BinaryVectorParameter.ActualValue;
41      OneBitflipMove move = OneBitflipMoveParameter.ActualValue;
42      BoolMatrix interactions = GeneInteractionsParameter.ActualValue;
43      DoubleArray weights = WeightsParameter.ActualValue;
44      int seed = InteractionSeedParameter.ActualValue.Value;
45      double moveQuality = QualityParameter.ActualValue.Value;
46
47      List<int> affectedFitnessComponents = new List<int>();
48      for (int c = 0; c<interactions.Columns; c++)
49        if (interactions[move.Index, c])
50          affectedFitnessComponents.Add(c);
51      BinaryVector moved = new BinaryVector(binaryVector);
52      MovedBinaryVectorParameter.ActualValue = moved;
53      moved[move.Index] = !moved[move.Index];
54      if (affectedFitnessComponents.Count*2 > interactions.Columns) {
55        double[] f_i;
56        moveQuality = NKEvaluator.Evaluate(moved, interactions, weights, seed, out f_i);
57      } else {
58        long x = NKEvaluator.Encode(binaryVector);
59        long y = NKEvaluator.Encode(moved);
60        long[] g = NKEvaluator.Encode(interactions);
61        double[] w = NKEvaluator.Normalize(weights);
62        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);
65        }
66      }
67
68      if (MoveQualityParameter.ActualValue == null) MoveQualityParameter.ActualValue = new DoubleValue(moveQuality);
69      else MoveQualityParameter.ActualValue.Value = moveQuality;
70      return base.Apply();
71    }
72  }
73}
Note: See TracBrowser for help on using the repository browser.