[12483] | 1 | using System.Collections.Generic;
|
---|
| 2 | using HeuristicLab.Common;
|
---|
| 3 | using HeuristicLab.Core;
|
---|
| 4 | using HeuristicLab.Data;
|
---|
| 5 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
| 6 | using HeuristicLab.Parameters;
|
---|
| 7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 8 |
|
---|
| 9 | namespace 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 | }
|
---|