Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.NK/3.3/NKBitFlipMoveEvaluator.cs @ 15604

Last change on this file since 15604 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.BinaryVectorEncoding;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.NK {
31
32  [Item("NK BitFlip Move Evaluator", "Evaluates a single bit flip on an NK landscape.")]
33  [StorableClass]
34  public class NKBitFlipMoveEvaluator : NKMoveEvaluator, IOneBitflipMoveOperator {
35    public ILookupParameter<OneBitflipMove> OneBitflipMoveParameter {
36      get { return (ILookupParameter<OneBitflipMove>)Parameters["OneBitflipMove"]; }
37    }
38    public ILookupParameter<BinaryVector> MovedBinaryVectorParameter {
39      get { return (ILookupParameter<BinaryVector>)Parameters["MovedBinaryVector"]; }
40    }
41
42    [StorableConstructor]
43    protected NKBitFlipMoveEvaluator(bool deserializing) : base(deserializing) { }
44    protected NKBitFlipMoveEvaluator(NKBitFlipMoveEvaluator original, Cloner cloner)
45      : base(original, cloner) { }
46    public NKBitFlipMoveEvaluator()
47      : base() {
48      Parameters.Add(new LookupParameter<OneBitflipMove>("OneBitflipMove", "The move to evaluate."));
49      Parameters.Add(new LookupParameter<BinaryVector>("MovedBinaryVector", "The resulting binary vector after the move."));
50    }
51
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new NKBitFlipMoveEvaluator(this, cloner);
54    }
55
56    public override IOperation Apply() {
57      BinaryVector binaryVector = BinaryVectorParameter.ActualValue;
58      OneBitflipMove move = OneBitflipMoveParameter.ActualValue;
59      BoolMatrix interactions = GeneInteractionsParameter.ActualValue;
60      DoubleArray weights = WeightsParameter.ActualValue;
61      int seed = InteractionSeedParameter.ActualValue.Value;
62      double moveQuality = QualityParameter.ActualValue.Value;
63      int q = QParameter.ActualValue.Value;
64      double p = PParameter.ActualValue.Value;
65
66      List<int> affectedFitnessComponents = new List<int>();
67      for (int c = 0; c < interactions.Columns; c++)
68        if (interactions[move.Index, c])
69          affectedFitnessComponents.Add(c);
70      BinaryVector moved = new BinaryVector(binaryVector);
71      MovedBinaryVectorParameter.ActualValue = moved;
72      moved[move.Index] = !moved[move.Index];
73      if (affectedFitnessComponents.Count * 2 > interactions.Columns) {
74        double[] f_i;
75        moveQuality = NKLandscape.Evaluate(moved, interactions, weights, seed, out f_i, q, p);
76      } else {
77        long x = NKLandscape.Encode(binaryVector);
78        long y = NKLandscape.Encode(moved);
79        long[] g = NKLandscape.Encode(interactions);
80        double[] w = NKLandscape.Normalize(weights);
81        foreach (var c in affectedFitnessComponents) {
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);
84        }
85      }
86
87      if (MoveQualityParameter.ActualValue == null) MoveQualityParameter.ActualValue = new DoubleValue(moveQuality);
88      else MoveQualityParameter.ActualValue.Value = moveQuality;
89      return base.Apply();
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.