[12566] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12566] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
[7128] | 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace 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 | }
|
---|
[12569] | 38 | public ILookupParameter<BinaryVector> MovedBinaryVectorParameter {
|
---|
| 39 | get { return (ILookupParameter<BinaryVector>)Parameters["MovedBinaryVector"]; }
|
---|
[12565] | 40 | }
|
---|
[7128] | 41 |
|
---|
| 42 | [StorableConstructor]
|
---|
| 43 | protected NKBitFlipMoveEvaluator(bool deserializing) : base(deserializing) { }
|
---|
[12565] | 44 | protected NKBitFlipMoveEvaluator(NKBitFlipMoveEvaluator original, Cloner cloner)
|
---|
| 45 | : base(original, cloner) { }
|
---|
[7128] | 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;
|
---|
[12576] | 63 | int q = QParameter.ActualValue.Value;
|
---|
| 64 | double p = PParameter.ActualValue.Value;
|
---|
[7128] | 65 |
|
---|
| 66 | List<int> affectedFitnessComponents = new List<int>();
|
---|
[12565] | 67 | for (int c = 0; c < interactions.Columns; c++)
|
---|
[7128] | 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];
|
---|
[12565] | 73 | if (affectedFitnessComponents.Count * 2 > interactions.Columns) {
|
---|
[7128] | 74 | double[] f_i;
|
---|
[12576] | 75 | moveQuality = NKLandscape.Evaluate(moved, interactions, weights, seed, out f_i, q, p);
|
---|
[7128] | 76 | } else {
|
---|
[12565] | 77 | long x = NKLandscape.Encode(binaryVector);
|
---|
| 78 | long y = NKLandscape.Encode(moved);
|
---|
| 79 | long[] g = NKLandscape.Encode(interactions);
|
---|
| 80 | double[] w = NKLandscape.Normalize(weights);
|
---|
[7128] | 81 | foreach (var c in affectedFitnessComponents) {
|
---|
[12576] | 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);
|
---|
[7128] | 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 | }
|
---|