Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/20/20 17:31:31 (5 years ago)
Author:
abeham
Message:

#2521: Worked on ResultParameter for Problem and Algorithms

  • Add ResultParameter to TSP, BinaryVectorProblem, and HillClimber
  • Refactor ResultParameter to allow presetting the ResultCollection instead of having to discover it (e.g. for use in BasicAlgorithms)
  • Unify Results property among EngineAlgorithm and BasicAlgorithm
    • There is now only a single instance which is storable
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HillClimber.cs

    r17382 r17517  
    4646    private IRandom random;
    4747
    48     private const string IterationsParameterName = "Iterations";
    49     private const string BestQualityResultName = "Best quality";
    50     private const string IterationsResultName = "Iterations";
     48    [Storable] public IFixedValueParameter<IntValue> MaximumIterationsParameter { get; private set; }
     49    [Storable] public IResultParameter<DoubleValue> BestQualityResultParameter { get; private set; }
     50    [Storable] public IResultParameter<IntValue> IterationsResultParameter { get; private set; }
    5151
    5252    public override Type ProblemType {
     
    6060    public override bool SupportsPause { get { return false; } }
    6161
    62     public IFixedValueParameter<IntValue> IterationsParameter {
    63       get { return (IFixedValueParameter<IntValue>)Parameters[IterationsParameterName]; }
     62    public int MaximumIterations {
     63      get { return MaximumIterationsParameter.Value.Value; }
     64      set { MaximumIterationsParameter.Value.Value = value; }
    6465    }
    65 
    66     public int Iterations {
    67       get { return IterationsParameter.Value.Value; }
    68       set { IterationsParameter.Value.Value = value; }
    69     }
    70 
    71     #region ResultsProperties
    72     private double ResultsBestQuality {
    73       get { return ((DoubleValue)Results[BestQualityResultName].Value).Value; }
    74       set { ((DoubleValue)Results[BestQualityResultName].Value).Value = value; }
    75     }
    76     private int ResultsIterations {
    77       get { return ((IntValue)Results[IterationsResultName].Value).Value; }
    78       set { ((IntValue)Results[IterationsResultName].Value).Value = value; }
    79     }
    80     #endregion
    8166
    8267    [StorableConstructor]
     
    8469    protected HillClimber(HillClimber original, Cloner cloner)
    8570      : base(original, cloner) {
     71      MaximumIterationsParameter = cloner.Clone(original.MaximumIterationsParameter);
     72      BestQualityResultParameter = cloner.Clone(original.BestQualityResultParameter);
     73      IterationsResultParameter = cloner.Clone(original.IterationsResultParameter);
    8674    }
    8775    public override IDeepCloneable Clone(Cloner cloner) {
     
    9280      : base() {
    9381      random = new MersenneTwister();
    94       Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "", new IntValue(100)));
     82      Parameters.Add(MaximumIterationsParameter = new FixedValueParameter<IntValue>("Maximum Iterations", "", new IntValue(100)));
     83      Parameters.Add(BestQualityResultParameter = new ResultParameter<DoubleValue>("Best Quality", "", "Results", new DoubleValue(double.NaN)));
     84      Parameters.Add(IterationsResultParameter = new ResultParameter<IntValue>("Iterations", "", "Results", new IntValue(0)));
    9585    }
    9686
    97 
    98     protected override void Initialize(CancellationToken cancellationToken) {
    99       Results.Add(new Result(BestQualityResultName, new DoubleValue(double.NaN)));
    100       Results.Add(new Result(IterationsResultName, new IntValue(0)));
    101       base.Initialize(cancellationToken);
    102     }
    10387    protected override void Run(CancellationToken cancellationToken) {
    104       while (ResultsIterations < Iterations) {
     88      while (IterationsResultParameter.ActualValue.Value < MaximumIterations) {
    10589        cancellationToken.ThrowIfCancellationRequested();
    10690
     
    11498
    11599        fitness = ImproveToLocalOptimum(Problem, solution, fitness, random);
    116         if (double.IsNaN(ResultsBestQuality) || Problem.IsBetter(fitness, ResultsBestQuality)) {
    117           ResultsBestQuality = fitness;
     100        var bestSoFar = BestQualityResultParameter.ActualValue.Value;
     101        if (double.IsNaN(bestSoFar) || Problem.IsBetter(fitness, bestSoFar)) {
     102          BestQualityResultParameter.ActualValue.Value = fitness;
    118103        }
    119104
    120         ResultsIterations++;
     105        IterationsResultParameter.ActualValue.Value++;
    121106      }
    122107    }
Note: See TracChangeset for help on using the changeset viewer.