Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NKNew/NKBitFlipMoveEvaluator.cs @ 12531

Last change on this file since 12531 was 12483, checked in by ascheibe, 9 years ago

#2306 added a new project for NK landscapes and ported it to BasicProblem

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