Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/31/18 18:14:33 (7 years ago)
Author:
abeham
Message:

#1614:

  • Changed performance measure to stopwatch instead of datetime for precision reasons
Location:
branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure/Algorithms/ContextAlgorithm.cs

    r15574 r15700  
    2121
    2222using System;
     23using System.Diagnostics;
     24using System.Linq;
    2325using System.Threading;
    2426using HeuristicLab.Analysis;
     
    98100      set { stopAtBestKnownQualityParameter.Value.Value = value; }
    99101    }
     102
     103    private Stopwatch stopwatch;
     104    [Storable]
     105    private TimeSpan elapsed;
     106    [Storable]
     107    private IndexedDataTable<double> qualityPerClock;
     108    [Storable]
     109    private IndexedDataRow<double> firstHitGraph;
    100110
    101111    [StorableConstructor]
     
    109119      maximumRuntimeParameter = cloner.Clone(original.maximumRuntimeParameter);
    110120      stopAtBestKnownQualityParameter = cloner.Clone(original.stopAtBestKnownQualityParameter);
     121      elapsed = original.elapsed;
     122      qualityPerClock = cloner.Clone(original.qualityPerClock);
     123      firstHitGraph = cloner.Clone(original.firstHitGraph);
     124      if (context != null)
     125        context.BestQualityChanged += ContextOnBestQualityChanged;
    111126    }
    112127    protected ContextAlgorithm()
     
    123138      context = new TContext();
    124139      context.Scope.Variables.Add(new Variable("Results", Results));
     140      context.BestQualityChanged += ContextOnBestQualityChanged;
    125141
    126142      IExecutionContext ctxt = null;
     
    133149      context.EvaluatedSolutions = 0;
    134150      context.BestQuality = double.NaN;
     151
     152      qualityPerClock = new IndexedDataTable<double>("Quality Per Clock");
     153      firstHitGraph = new IndexedDataRow<double>("First-hit Graph");
     154      qualityPerClock.Rows.Add(firstHitGraph);
     155      Results.Add(new Result("QualityPerClock", qualityPerClock));
     156
     157      elapsed = TimeSpan.Zero;
     158      stopwatch = Stopwatch.StartNew();
     159    }
     160
     161    protected override void Run(CancellationToken cancellationToken) {
     162      if (stopwatch == null || !stopwatch.IsRunning) stopwatch = Stopwatch.StartNew();
     163    }
     164
     165    private void ContextOnBestQualityChanged(object sender, EventArgs e) {
     166      if (double.IsNaN(Context.BestQuality)) return;
     167      var newEntry = Tuple.Create((elapsed + stopwatch.Elapsed).TotalSeconds, Context.BestQuality);
     168
     169      if (firstHitGraph.Values.Count == 0) {
     170        firstHitGraph.Values.Add(newEntry); // record the first data
     171        firstHitGraph.Values.Add(Tuple.Create(newEntry.Item1, newEntry.Item2)); // last entry records max number of evaluations
     172        return;
     173      }
     174
     175      var improvement = firstHitGraph.Values.Last().Item2 != newEntry.Item2;
     176      if (improvement) {
     177        firstHitGraph.Values[firstHitGraph.Values.Count - 1] = newEntry; // record the improvement
     178        firstHitGraph.Values.Add(Tuple.Create(newEntry.Item1, newEntry.Item2)); // last entry records max number of evaluations
     179      } else {
     180        firstHitGraph.Values[firstHitGraph.Values.Count - 1] = Tuple.Create(newEntry.Item1, newEntry.Item2);
     181      }
    135182    }
    136183
    137184    public override void Prepare() {
    138       context = null;
     185      if (context != null) {
     186        context.BestQualityChanged -= ContextOnBestQualityChanged;
     187        context = null;
     188      }
    139189      base.Prepare();
     190    }
     191
     192    protected override void OnPaused() {
     193      base.OnPaused();
     194      elapsed += stopwatch.Elapsed;
     195      stopwatch.Reset();
     196      firstHitGraph.Values[firstHitGraph.Values.Count - 1] = Tuple.Create(elapsed.TotalSeconds, Context.BestQuality);
     197    }
     198
     199    protected override void OnStopped() {
     200      base.OnStopped();
     201      if (stopwatch.IsRunning) {
     202        elapsed += stopwatch.Elapsed;
     203        stopwatch.Reset();
     204        firstHitGraph.Values[firstHitGraph.Values.Count - 1] = Tuple.Create(elapsed.TotalSeconds, Context.BestQuality);
     205      }
     206    }
     207
     208    [StorableHook(HookType.AfterDeserialization)]
     209    private void AfterDeserialization() {
     210      if (context != null) context.BestQualityChanged += ContextOnBestQualityChanged;
    140211    }
    141212
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure/Contexts/BasicContext.cs

    r15616 r15700  
    6969    public double BestQuality {
    7070      get { return bestQuality.Value.Value; }
    71       set { bestQuality.Value.Value = value; }
     71      set {
     72        if (bestQuality.Value.Value == value) return;
     73        bestQuality.Value.Value = value;
     74        OnBestQualityChanged();
     75      }
    7276    }
    7377
     
    142146    }
    143147
     148    public event EventHandler BestQualityChanged;
     149    private void OnBestQualityChanged() {
     150      BestQualityChanged?.Invoke(this, EventArgs.Empty);
     151    }
     152
    144153    #region IExecutionContext members
    145154    IAtomicOperation IExecutionContext.CreateOperation(IOperator op) {
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure/Interfaces.cs

    r15572 r15700  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Core;
    2324
     
    3334    int EvaluatedSolutions { get; set; }
    3435    double BestQuality { get; set; }
     36
     37    event EventHandler BestQualityChanged;
    3538  }
    3639
Note: See TracChangeset for help on using the changeset viewer.