Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17517


Ignore:
Timestamp:
04/20/20 17:31:31 (4 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
Location:
branches/2521_ProblemRefactoring
Files:
10 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    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Clients.OKB/3.3/RunCreation/EmptyAlgorithm.cs

    r17226 r17517  
    2121
    2222using System;
     23using HEAL.Attic;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
    25 using HeuristicLab.Optimization;
    26 using HEAL.Attic;
    2726using HeuristicLab.PluginInfrastructure;
    2827
     
    4140    }
    4241
    43     private ResultCollection results;
    44     public override Optimization.ResultCollection Results {
    45       get { return results; }
    46     }
    47 
    4842    #region Persistence Properties
    4943    [Storable(Name = "ExceptionMessage")]
     
    5650    [StorableConstructor]
    5751    private EmptyAlgorithm(StorableConstructorFlag _) : base(_) {
    58       this.results = new ResultCollection();
    5952    }
    6053    private EmptyAlgorithm(EmptyAlgorithm original, Cloner cloner)
    6154      : base(original, cloner) {
    6255      this.exceptionMessage = original.exceptionMessage;
    63       this.results = new ResultCollection();
    6456    }
    6557    public EmptyAlgorithm()
    6658      : base() {
    67       results = new ResultCollection();
    6859    }
    6960    public EmptyAlgorithm(string exceptionMessage)
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorProblem.cs

    r17229 r17517  
    3333  [StorableType("2F6FEB34-BD19-47AF-9484-7F48565C0C43")]
    3434  public abstract class BinaryVectorProblem : SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> {
     35    [Storable] public IResultParameter<BinaryVector> BestSolutionParameter { get; private set; }
     36
    3537    public int Length {
    3638      get { return Encoding.Length; }
     
    4749    protected BinaryVectorProblem(BinaryVectorProblem original, Cloner cloner)
    4850      : base(original, cloner) {
     51      BestSolutionParameter = cloner.Clone(original.BestSolutionParameter);
    4952      RegisterEventHandlers();
    5053    }
     
    5356    protected BinaryVectorProblem(BinaryVectorEncoding encoding) : base(encoding) {
    5457      EncodingParameter.ReadOnly = true;
     58      BestSolutionParameter = new ResultParameter<BinaryVector>("Best Solution", "The best solution.");
     59      Parameters.Add(BestSolutionParameter);
    5560
    5661      Operators.Add(new HammingSimilarityCalculator());
     
    6570      base.Analyze(vectors, qualities, results, random);
    6671      var best = GetBestSolution(vectors, qualities);
    67 
    68       results.AddOrUpdateResult("Best Solution", (IItem)best.Item1.Clone());
     72      BestSolutionParameter.ActualValue = (BinaryVector)best.Item1.Clone();
    6973    }
    7074
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Algorithms/Algorithm.cs

    r17513 r17517  
    9595    }
    9696
    97     public abstract ResultCollection Results { get; }
     97    [Storable]
     98    private readonly ResultCollection results = new ResultCollection();
     99    public ResultCollection Results {
     100      get { return results; }
     101    }
    98102
    99103    [Storable]
     
    185189      storeAlgorithmInEachRun = original.storeAlgorithmInEachRun;
    186190      runsCounter = original.runsCounter;
     191      results = cloner.Clone(original.Results);
    187192      runs = cloner.Clone(original.runs);
    188193      Initialize();
     
    197202      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
    198203        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
     204      results.Clear();
    199205    }
    200206    public void Prepare(bool clearRuns) {
     
    286292    public event EventHandler Started;
    287293    protected virtual void OnStarted() {
     294      foreach (var param in Parameters.Concat(Problem.Parameters).OfType<IResultParameter>())
     295        param.ResultCollection = results;
    288296      ExecutionState = ExecutionState.Started;
    289297      EventHandler handler = Started;
     
    292300    public event EventHandler Paused;
    293301    protected virtual void OnPaused() {
     302      foreach (var param in Parameters.Concat(Problem.Parameters).OfType<IResultParameter>())
     303        param.ResultCollection = null;
    294304      ExecutionState = ExecutionState.Paused;
    295305      EventHandler handler = Paused;
     
    311321        }
    312322      } finally {
     323        foreach (var param in Parameters.Concat(Problem.Parameters).OfType<IResultParameter>())
     324          param.ResultCollection = null;
    313325        ExecutionState = ExecutionState.Stopped;
    314326        EventHandler handler = Stopped;
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs

    r17226 r17517  
    2222using System;
    2323using System.Threading;
     24using HEAL.Attic;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
    26 using HEAL.Attic;
    2727
    2828namespace HeuristicLab.Optimization {
     
    4242    [Storable]
    4343    private bool initialized;
    44     [Storable]
    45     private readonly ResultCollection results;
    46     public override ResultCollection Results {
    47       get { return results; }
    48     }
    4944
    5045    private CancellationTokenSource cancellationTokenSource;
     
    5853    protected BasicAlgorithm(BasicAlgorithm original, Cloner cloner)
    5954      : base(original, cloner) {
    60       results = cloner.Clone(original.Results);
    6155      initialized = original.initialized;
    6256    }
    6357    protected BasicAlgorithm()
    6458      : base() {
    65       results = new ResultCollection();
    6659    }
    6760
     
    6962      if (Problem == null) return;
    7063      base.Prepare();
    71       results.Clear();
    7264      initialized = false;
    7365      OnPrepared();
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Algorithms/EngineAlgorithm.cs

    r17513 r17517  
    7676    }
    7777
    78     public override ResultCollection Results {
    79       get {
    80         return (ResultCollection)globalScope.Variables["Results"].Value;
    81       }
    82     }
    83 
    8478    protected EngineAlgorithm()
    8579      : base() {
    8680      globalScope = new Scope("Global Scope");
    87       globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
     81      globalScope.Variables.Add(new Variable("Results", Results));
    8882      operatorGraph = new OperatorGraph();
    8983      Initialize();
     
    9286      : base(name) {
    9387      globalScope = new Scope("Global Scope");
    94       globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
     88      globalScope.Variables.Add(new Variable("Results", Results));
    9589      operatorGraph = new OperatorGraph();
    9690      Initialize();
     
    9993      : base(name, parameters) {
    10094      globalScope = new Scope("Global Scope");
    101       globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
     95      globalScope.Variables.Add(new Variable("Results", Results));
    10296      operatorGraph = new OperatorGraph();
    10397      Initialize();
     
    106100      : base(name, description) {
    107101      globalScope = new Scope("Global Scope");
    108       globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
     102      globalScope.Variables.Add(new Variable("Results", Results));
    109103      operatorGraph = new OperatorGraph();
    110104      Initialize();
     
    113107      : base(name, description, parameters) {
    114108      globalScope = new Scope("Global Scope");
    115       globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
     109      globalScope.Variables.Add(new Variable("Results", Results));
    116110      operatorGraph = new OperatorGraph();
    117111      Initialize();
     
    160154      base.Prepare();
    161155      globalScope.Clear();
    162       globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
     156      globalScope.Variables.Add(new Variable("Results", Results));
    163157
    164158      if ((engine != null) && (operatorGraph.InitialOperator != null)) {
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Results/IResultParameter.cs

    r17226 r17517  
    2828  public interface IResultParameter : ILookupParameter {
    2929    string ResultCollectionName { get; set; }
     30    ResultCollection ResultCollection { get; set; }
     31
    3032    event EventHandler ResultCollectionNameChanged;
    3133  }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Results/ResultParameter.cs

    r17257 r17517  
    6060    }
    6161
     62    [Storable]
     63    public ResultCollection ResultCollection { get; set; }
     64
    6265    [StorableConstructor]
    6366    private ResultParameter(StorableConstructorFlag _) : base(_) { }
     
    6669      resultCollectionName = original.resultCollectionName;
    6770      defaultValue = cloner.Clone(original.defaultValue);
     71      ResultCollection = cloner.Clone(original.ResultCollection);
    6872    }
    6973    public override IDeepCloneable Clone(Cloner cloner) {
     
    8993
    9094    protected override IItem GetActualValue() {
    91       ResultCollection results;
    92       if (CachedActualValue != null) {
    93         results = CachedActualValue as ResultCollection;
    94         if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection not found.");
    95       } else {
    96         var tmp = ResultCollectionName;
    97         // verifyType has to be disabled, because the ResultCollection may not be identical to the generic type of the parameter
    98         results = GetValue(ExecutionContext, ref tmp) as ResultCollection;
    99         if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection with name " + tmp + " not found.");
    100         CachedActualValue = results;
     95      ResultCollection results = ResultCollection;
     96      if (results == null) {
     97        if (CachedActualValue != null) {
     98          results = CachedActualValue as ResultCollection;
     99          if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection not found.");
     100        } else {
     101          var tmp = ResultCollectionName;
     102          // verifyType has to be disabled, because the ResultCollection may not be identical to the generic type of the parameter
     103          results = GetValue(ExecutionContext, ref tmp) as ResultCollection;
     104          if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection with name " + tmp + " not found.");
     105          CachedActualValue = results;
     106        }
    101107      }
    102108
    103109      IResult result;
    104110      if (!results.TryGetValue(ActualName, out result)) {
    105         if (DefaultValue == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): Result not found and no default value specified.");
     111        if (DefaultValue == null) return null;
    106112        result = ItemDescription == Description ? new Result(ActualName, (T)DefaultValue.Clone()) : new Result(ActualName, Description, (T)DefaultValue.Clone());
    107113        results.Add(result);
     
    116122
    117123    protected override void SetActualValue(IItem value) {
    118       ResultCollection results;
    119       if (CachedActualValue != null) {
    120         results = CachedActualValue as ResultCollection;
    121         if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection not found.");
    122       } else {
    123         var tmp = ResultCollectionName;
    124         results = GetValue(ExecutionContext, ref tmp) as ResultCollection;
    125         if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection with name " + tmp + " not found.");
    126         CachedActualValue = results;
     124      ResultCollection results = ResultCollection;
     125      if (results == null) {
     126        if (CachedActualValue != null) {
     127          results = CachedActualValue as ResultCollection;
     128          if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection not found.");
     129        } else {
     130          var tmp = ResultCollectionName;
     131          results = GetValue(ExecutionContext, ref tmp) as ResultCollection;
     132          if (results == null) throw new InvalidOperationException("ResultParameter (" + ActualName + "): ResultCollection with name " + tmp + " not found.");
     133          CachedActualValue = results;
     134        }
    127135      }
    128136
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Scheduling/3.3/JobShopSchedulingProblem.cs

    r17462 r17517  
    107107      set { ObjectiveParameter.Value.Value = value; }
    108108    }
     109
     110    public new IScheduleEncoding Encoding {
     111      get { return base.Encoding; }
     112      set { base.Encoding = value; }
     113    }
    109114    #endregion
    110115
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/TSP.cs

    r17382 r17517  
    4747    [Storable] public IValueParameter<ITSPData> TSPDataParameter { get; private set; }
    4848    [Storable] public IValueParameter<ITSPSolution> BestKnownSolutionParameter { get; private set; }
     49    [Storable] public IResultParameter<ITSPSolution> BestTSPSolutionParameter { get; private set; }
    4950
    5051    public ITSPData TSPData {
     
    6465      TSPDataParameter = cloner.Clone(original.TSPDataParameter);
    6566      BestKnownSolutionParameter = cloner.Clone(original.BestKnownSolutionParameter);
     67      BestTSPSolutionParameter = cloner.Clone(original.BestTSPSolutionParameter);
    6668    }
    6769
     
    7072      Parameters.Add(TSPDataParameter = new ValueParameter<ITSPData>("TSPData", "The main parameters of the TSP."));
    7173      Parameters.Add(BestKnownSolutionParameter = new OptionalValueParameter<ITSPSolution>("BestKnownSolution", "The best known solution."));
    72 
     74      Parameters.Add(BestTSPSolutionParameter = new ResultParameter<ITSPSolution>("Best TSP Solution", "The best so far solution found."));
     75     
    7376      TSPData = new EuclideanTSPData();
    7477      Encoding.Length = TSPData.Cities;
     
    112115      }
    113116
    114       IResult bestSolutionResult;
    115       if (results.TryGetValue("Best TSP Solution", out bestSolutionResult)) {
    116         var bestSolution = bestSolutionResult.Value as ITSPSolution;
    117         if (bestSolution == null || Maximization && bestSolution.TourLength.Value < qualities[i]
    118           || !Maximization && bestSolution.TourLength.Value > qualities[i]) {
    119           bestSolutionResult.Value = TSPData.GetSolution(solutions[i], qualities[i]);
    120         }
    121       } else results.Add(new Result("Best TSP Solution", TSPData.GetSolution(solutions[i], qualities[i])));
     117      var bestSolution = BestTSPSolutionParameter.ActualValue;
     118      if (bestSolution == null || IsBetter(qualities[i], bestSolution.TourLength.Value)) {
     119        BestTSPSolutionParameter.ActualValue = TSPData.GetSolution(solutions[i], qualities[i]);
     120      }
    122121    }
    123122
Note: See TracChangeset for help on using the changeset viewer.