Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/18/20 18:55:08 (4 years ago)
Author:
abeham
Message:

#2521: worked on refactoring, worked a lot on binary encoding / problems

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Binary/3.3/DeceptiveStepTrapProblem.cs

    r17226 r17544  
    2222#endregion
    2323
     24using System;
    2425using HEAL.Attic;
    2526using HeuristicLab.Common;
     
    3435  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 230)]
    3536  public class DeceptiveStepTrapProblem : DeceptiveTrapProblem {
    36     [StorableConstructor]
    37     protected DeceptiveStepTrapProblem(StorableConstructorFlag _) : base(_) { }
    38     protected DeceptiveStepTrapProblem(DeceptiveStepTrapProblem original, Cloner cloner)
    39       : base(original, cloner) {
    40       RegisterParameterEvents();
    41     }
    42     public override IDeepCloneable Clone(Cloner cloner) {
    43       return new DeceptiveStepTrapProblem(this, cloner);
    44     }
    45 
    46     private const string StepSizeParameterName = "Step Size";
    47 
    48     public IFixedValueParameter<IntValue> StepSizeParameter {
    49       get { return (IFixedValueParameter<IntValue>)Parameters[StepSizeParameterName]; }
    50     }
     37    [Storable] private int offset = -1;
     38    [Storable] public IFixedValueParameter<IntValue> StepSizeParameter { get; private set; }
    5139
    5240    public int StepSize {
     
    5644
    5745    public DeceptiveStepTrapProblem() : base() {
    58       Parameters.Add(new FixedValueParameter<IntValue>(StepSizeParameterName, "", new IntValue(2)));
     46      Parameters.Add(StepSizeParameter = new FixedValueParameter<IntValue>("Step Size", "", new IntValue(2)));
     47      offset = (TrapSize - StepSize) % StepSize;
     48
    5949      RegisterParameterEvents();
    6050    }
     51
     52    protected override int TrapMaximum {
     53      get { return (offset + TrapSize) / StepSize; }
     54    }
     55
     56    protected override int Score(BinaryVector individual, int trapIndex, int trapSize) {
     57      int partial = base.Score(individual, trapIndex, trapSize);
     58      // introduce plateaus using integer division
     59      return (offset + partial) / StepSize;
     60    }
     61
     62    [StorableConstructor]
     63    protected DeceptiveStepTrapProblem(StorableConstructorFlag _) : base(_) { }
    6164
    6265    [StorableHook(HookType.AfterDeserialization)]
     
    6467      RegisterParameterEvents();
    6568    }
     69    protected DeceptiveStepTrapProblem(DeceptiveStepTrapProblem original, Cloner cloner)
     70      : base(original, cloner) {
     71      offset = original.offset;
     72      StepSizeParameter = cloner.Clone(original.StepSizeParameter);
     73
     74      RegisterParameterEvents();
     75    }
     76    public override IDeepCloneable Clone(Cloner cloner) {
     77      return new DeceptiveStepTrapProblem(this, cloner);
     78    }
    6679
    6780    private void RegisterParameterEvents() {
    68       TrapSizeParameter.Value.ValueChanged += (o, e) => { offset = -1; };
    69       StepSizeParameter.Value.ValueChanged += (o, e) => { offset = -1; };
     81      StepSizeParameter.Value.ValueChanged += StepSizeOnChanged;
    7082    }
    7183
    72 
    73     private int offset = -1;
    74     private int Offset {
    75       get {
    76         if (offset == -1) offset = (TrapSize - StepSize) % StepSize;
    77         return offset;
    78       }
     84    protected override void TrapSizeOnChanged(object sender, EventArgs e) {
     85      base.TrapSizeOnChanged(sender, e);
     86      offset = (TrapSize - StepSize) % StepSize;
    7987    }
    8088
    81     protected override int TrapMaximum {
    82       get { return (Offset + TrapSize) / StepSize; }
    83     }
    84 
    85     protected override int Score(BinaryVector individual, int trapIndex, int trapSize) {
    86       int partial = base.Score(individual, trapIndex, trapSize);
    87       // introduce plateaus using integer division
    88       return (Offset + partial) / StepSize;
     89    private void StepSizeOnChanged(object sender, EventArgs e) {
     90      offset = (TrapSize - StepSize) % StepSize;
    8991    }
    9092  }
Note: See TracChangeset for help on using the changeset viewer.