Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15603


Ignore:
Timestamp:
01/12/18 13:33:23 (6 years ago)
Author:
abeham
Message:

#1614: Implemented changed behavior to measure execution time (cf. #2869)

Location:
branches/GeneralizedQAP
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • branches/GeneralizedQAP/HeuristicLab.Optimization.Views/3.3/IOptimizerView.Designer.cs

    r14185 r15603  
    4545    /// </summary>
    4646    private void InitializeComponent() {
     47      this.components = new System.ComponentModel.Container();
    4748      System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(IOptimizerView));
    4849      this.startButton = new System.Windows.Forms.Button();
     
    5253      this.stopButton = new System.Windows.Forms.Button();
    5354      this.resetButton = new System.Windows.Forms.Button();
     55      this.executionTimer = new System.Windows.Forms.Timer(this.components);
    5456      ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).BeginInit();
    5557      this.SuspendLayout();
     
    132134      this.resetButton.Click += new System.EventHandler(this.resetButton_Click);
    133135      //
     136      // executionTimer
     137      //
     138      this.executionTimer.Interval = 200;
     139      this.executionTimer.Tick += new System.EventHandler(this.executionTimer_Tick);
     140      //
    134141      // IOptimizerView
    135142      //
     
    166173    protected System.Windows.Forms.Button pauseButton;
    167174    protected System.Windows.Forms.Button stopButton;
     175    protected System.Windows.Forms.Timer executionTimer;
    168176    protected System.Windows.Forms.Button resetButton;
    169177
  • branches/GeneralizedQAP/HeuristicLab.Optimization.Views/3.3/IOptimizerView.cs

    r15367 r15603  
    4444      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
    4545      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
    46       Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
    4746      Content.Prepared -= new EventHandler(Content_Prepared);
    4847      Content.Started -= new EventHandler(Content_Started);
     
    5554      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
    5655      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
    57       Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
    5856      Content.Prepared += new EventHandler(Content_Prepared);
    5957      Content.Started += new EventHandler(Content_Started);
     
    6462    protected override void OnContentChanged() {
    6563      base.OnContentChanged();
    66       if (Content == null) {
    67         executionTimeTextBox.Text = "-";
    68       } else {
     64      if (Content != null) {
    6965        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
    70         executionTimeTextBox.Text = Content.ExecutionTime.ToString();
    7166      }
     67      UpdateExecutionTime();
    7268    }
    7369
     
    9086      else {
    9187        nameTextBox.Enabled = infoLabel.Enabled = true;
     88        UpdateExecutionTime();
    9289        ReadOnly = Locked = false;
    9390        SetEnabledStateOfExecutableButtons();
     
    9895        Invoke(new EventHandler(Content_Started), sender, e);
    9996      else {
     97        executionTimer.Start();
    10098        nameTextBox.Enabled = infoLabel.Enabled = false;
    10199        ReadOnly = Locked = true;
     
    107105        Invoke(new EventHandler(Content_Paused), sender, e);
    108106      else {
     107        executionTimer.Stop();
     108        UpdateExecutionTime();
    109109        nameTextBox.Enabled = infoLabel.Enabled = true;
    110110        ReadOnly = Locked = false;
     
    116116        Invoke(new EventHandler(Content_Stopped), sender, e);
    117117      else {
     118        executionTimer.Stop();
     119        UpdateExecutionTime();
    118120        nameTextBox.Enabled = infoLabel.Enabled = true;
    119121        ReadOnly = Locked = false;
     
    121123      }
    122124    }
    123     protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
    124       if (InvokeRequired)
    125         Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
    126       else
    127         executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
    128     }
    129125    protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
    130126      if (InvokeRequired)
    131127        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
    132       else
     128      else {
     129        executionTimer.Stop();
     130        UpdateExecutionTime();
    133131        ErrorHandling.ShowErrorDialog(this, e.Value);
     132      }
    134133    }
    135134    #endregion
     
    147146    protected virtual void resetButton_Click(object sender, EventArgs e) {
    148147      Content.Prepare(false);
     148    }
     149    private void executionTimer_Tick(object sender, EventArgs e) {
     150      UpdateExecutionTime();
     151    }
     152
     153    private void UpdateExecutionTime() {
     154      executionTimeTextBox.Text = (Content == null ? "-" : Content.ExecutionTime.ToString());
    149155    }
    150156    #endregion
  • branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/Algorithm.cs

    r15287 r15603  
    6464      }
    6565    }
    66 
    67     [Storable]
    68     private TimeSpan executionTime;
    69     public TimeSpan ExecutionTime {
    70       get { return executionTime; }
    71       protected set {
    72         executionTime = value;
    73         OnExecutionTimeChanged();
    74       }
    75     }
     66   
     67    public abstract TimeSpan ExecutionTime { get; }
    7668
    7769    public virtual Type ProblemType {
     
    133125      : base() {
    134126      executionState = ExecutionState.Stopped;
    135       executionTime = TimeSpan.Zero;
    136127      storeAlgorithmInEachRun = false;
    137128      runsCounter = 0;
     
    141132      : base(name) {
    142133      executionState = ExecutionState.Stopped;
    143       executionTime = TimeSpan.Zero;
    144134      storeAlgorithmInEachRun = false;
    145135      runsCounter = 0;
     
    149139      : base(name, parameters) {
    150140      executionState = ExecutionState.Stopped;
    151       executionTime = TimeSpan.Zero;
    152141      storeAlgorithmInEachRun = false;
    153142      runsCounter = 0;
     
    157146      : base(name, description) {
    158147      executionState = ExecutionState.Stopped;
    159       executionTime = TimeSpan.Zero;
    160148      storeAlgorithmInEachRun = false;
    161149      runsCounter = 0;
     
    165153      : base(name, description, parameters) {
    166154      executionState = ExecutionState.Stopped;
    167       executionTime = TimeSpan.Zero;
    168155      storeAlgorithmInEachRun = false;
    169156      runsCounter = 0;
     
    181168      if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
    182169      executionState = original.executionState;
    183       executionTime = original.executionTime;
    184170      problem = cloner.Clone(original.problem);
    185171      storeAlgorithmInEachRun = original.storeAlgorithmInEachRun;
     
    259245      if (handler != null) handler(this, EventArgs.Empty);
    260246    }
    261     public event EventHandler ExecutionTimeChanged;
    262     protected virtual void OnExecutionTimeChanged() {
    263       EventHandler handler = ExecutionTimeChanged;
    264       if (handler != null) handler(this, EventArgs.Empty);
    265     }
    266247    public event EventHandler ProblemChanged;
    267248    protected virtual void OnProblemChanged() {
     
    276257    public event EventHandler Prepared;
    277258    protected virtual void OnPrepared() {
    278       ExecutionTime = TimeSpan.Zero;
    279259      foreach (IStatefulItem statefulObject in this.GetObjectGraphObjects(new HashSet<object>() { Runs }).OfType<IStatefulItem>()) {
    280260        statefulObject.InitializeState();
     
    347327    }
    348328    #endregion
     329
     330    [Obsolete("Deprecate, does nothing, needs to be removed from IExecutable")]
     331    public event EventHandler ExecutionTimeChanged;
    349332  }
    350333}
  • branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs

    r15367 r15603  
    2121
    2222using System;
    23 using System.Linq;
    2423using System.Threading;
    2524using HeuristicLab.Common;
     
    3231
    3332    private bool pausePending;
    34     private DateTime lastUpdateTime;
     33    private bool executionTimeActive;
    3534
    3635    public string Filename { get; set; }
     
    5251    }
    5352
     53    [Storable]
     54    private TimeSpan executionTime;
     55    private DateTime startDate;
     56    public override TimeSpan ExecutionTime {
     57      get { return executionTime + (executionTimeActive ? DateTime.UtcNow - startDate : TimeSpan.Zero); }
     58    }
     59
    5460    [StorableConstructor]
    5561    protected BasicAlgorithm(bool deserializing) : base(deserializing) { }
     
    5864      results = cloner.Clone(original.Results);
    5965      initialized = original.initialized;
     66      executionTime = original.executionTime;
    6067    }
    6168    protected BasicAlgorithm()
    6269      : base() {
    6370      results = new ResultCollection();
     71      executionTime = TimeSpan.Zero;
    6472    }
    6573
    6674    public override void Prepare() {
     75      executionTime = TimeSpan.Zero;
    6776      if (Problem == null) return;
    6877      base.Prepare();
     
    7382
    7483    public override void Start(CancellationToken cancellationToken) {
     84      startDate = DateTime.UtcNow;
     85      executionTimeActive = true;
    7586      base.Start(cancellationToken);
    7687      CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
     
    117128    private void Run(object state) {
    118129      CancellationToken cancellationToken = (CancellationToken)state;
    119       lastUpdateTime = DateTime.UtcNow;
    120       System.Timers.Timer timer = new System.Timers.Timer(250);
    121       timer.AutoReset = true;
    122       timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    123       timer.Start();
    124       try {
    125         if (!initialized)
    126           Initialize(cancellationToken);
    127         initialized = true;
    128         Run(cancellationToken);
    129       }
    130       finally {
    131         timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
    132         timer.Stop();
    133         ExecutionTime += DateTime.UtcNow - lastUpdateTime;
    134       }
     130      if (!initialized)
     131        Initialize(cancellationToken);
     132      initialized = true;
     133      Run(cancellationToken);
    135134    }
    136135
     
    138137    protected abstract void Run(CancellationToken cancellationToken);
    139138
    140     #region Events
    141     private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
    142       System.Timers.Timer timer = (System.Timers.Timer)sender;
    143       timer.Enabled = false;
    144       DateTime now = DateTime.UtcNow;
    145       ExecutionTime += now - lastUpdateTime;
    146       lastUpdateTime = now;
    147       timer.Enabled = true;
     139    protected override void OnPaused() {
     140      executionTime += DateTime.UtcNow - startDate;
     141      executionTimeActive = false;
     142      base.OnPaused();
    148143    }
    149     #endregion
    150144
     145    protected override void OnStopped() {
     146      if (executionTimeActive) { // don't do if going from pause to stop
     147        executionTime += DateTime.UtcNow - startDate;
     148        executionTimeActive = false;
     149      }
     150      base.OnStopped();
     151    }
    151152  }
    152153}
  • branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/EngineAlgorithm.cs

    r15287 r15603  
    7171    }
    7272
     73    public override TimeSpan ExecutionTime {
     74      get { return engine.ExecutionTime; }
     75    }
     76
    7377    public override ResultCollection Results {
    7478      get {
     
    195199    private void RegisterEngineEvents() {
    196200      Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
    197       Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);
    198201      Engine.Paused += new EventHandler(Engine_Paused);
    199202      Engine.Prepared += new EventHandler(Engine_Prepared);
     
    203206    private void DeregisterEngineEvents() {
    204207      Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
    205       Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);
    206208      Engine.Paused -= new EventHandler(Engine_Paused);
    207209      Engine.Prepared -= new EventHandler(Engine_Prepared);
     
    211213    private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
    212214      OnExceptionOccurred(e.Value);
    213     }
    214     private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {
    215       ExecutionTime = Engine.ExecutionTime;
    216215    }
    217216    private void Engine_Paused(object sender, EventArgs e) {
  • branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/HeuristicOptimizationAlgorithm.cs

    r14185 r15603  
    2121
    2222using System;
     23using System.Threading;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
     
    3738    }
    3839
    39     protected HeuristicOptimizationAlgorithm() : base() { }
    40     protected HeuristicOptimizationAlgorithm(string name) : base(name) { }
    41     protected HeuristicOptimizationAlgorithm(string name, ParameterCollection parameters) : base(name, parameters) { }
    42     protected HeuristicOptimizationAlgorithm(string name, string description) : base(name, description) { }
    43     protected HeuristicOptimizationAlgorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { }
     40    [Storable]
     41    private TimeSpan executionTime;
     42    private DateTime startDate;
     43    public override TimeSpan ExecutionTime {
     44      get { return executionTime + (ExecutionState == ExecutionState.Started ? DateTime.UtcNow - startDate : TimeSpan.Zero); }
     45    }
     46
     47    protected HeuristicOptimizationAlgorithm() : base() {
     48      executionTime = TimeSpan.Zero;
     49    }
     50    protected HeuristicOptimizationAlgorithm(string name) : base(name) {
     51      executionTime = TimeSpan.Zero;
     52    }
     53    protected HeuristicOptimizationAlgorithm(string name, ParameterCollection parameters) : base(name, parameters) {
     54      executionTime = TimeSpan.Zero;
     55    }
     56    protected HeuristicOptimizationAlgorithm(string name, string description) : base(name, description) {
     57      executionTime = TimeSpan.Zero;
     58    }
     59    protected HeuristicOptimizationAlgorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) {
     60      executionTime = TimeSpan.Zero;
     61    }
    4462
    4563    [StorableConstructor]
    4664    protected HeuristicOptimizationAlgorithm(bool deserializing) : base(deserializing) { }
    47     protected HeuristicOptimizationAlgorithm(HeuristicOptimizationAlgorithm original, Cloner cloner) : base(original, cloner) { }
     65    protected HeuristicOptimizationAlgorithm(HeuristicOptimizationAlgorithm original, Cloner cloner)
     66      : base(original, cloner) {
     67      executionTime = original.executionTime;
     68    }
     69
     70    public override void Prepare() {
     71      executionTime = TimeSpan.Zero;
     72      base.Prepare();
     73    }
     74
     75    public override void Start(CancellationToken cancellationToken) {
     76      startDate = DateTime.UtcNow;
     77      base.Start(cancellationToken);
     78    }
    4879
    4980    #region Events
     
    6293    protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
    6394    #endregion
     95
     96    protected override void OnPaused() {
     97      var elapsed = DateTime.UtcNow - startDate;
     98      executionTime += elapsed;
     99      base.OnPaused();
     100    }
     101
     102    protected override void OnStopped() {
     103      if (ExecutionState == ExecutionState.Started) {
     104        var elapsed = DateTime.UtcNow - startDate;
     105        executionTime += elapsed;
     106      }
     107      base.OnStopped();
     108    }
    64109  }
    65110}
  • branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/MetaOptimizers/BatchRun.cs

    r15287 r15603  
    6868      }
    6969    }
    70 
     70   
    7171    [Storable]
    7272    private TimeSpan executionTime;
    7373    public TimeSpan ExecutionTime {
    74       get {
    75         if ((Optimizer != null) && (Optimizer.ExecutionState != ExecutionState.Stopped))
    76           return executionTime + Optimizer.ExecutionTime;
    77         else
    78           return executionTime;
    79       }
    80       private set {
    81         executionTime = value;
    82         OnExecutionTimeChanged();
    83       }
    84     }
    85 
    86     [Storable]
    87     private TimeSpan runsExecutionTime;
     74      get { return executionTime + (Optimizer?.ExecutionTime ?? TimeSpan.Zero); }
     75    }
    8876
    8977    [Storable]
     
    174162      executionState = ExecutionState.Stopped;
    175163      executionTime = TimeSpan.Zero;
    176       runsExecutionTime = TimeSpan.Zero;
    177164      repetitions = 10;
    178165      repetitionsCounter = 0;
     
    184171      executionState = ExecutionState.Stopped;
    185172      executionTime = TimeSpan.Zero;
    186       runsExecutionTime = TimeSpan.Zero;
    187173      repetitions = 10;
    188174      repetitionsCounter = 0;
     
    193179      executionState = ExecutionState.Stopped;
    194180      executionTime = TimeSpan.Zero;
    195       runsExecutionTime = TimeSpan.Zero;
    196181      repetitions = 10;
    197182      repetitionsCounter = 0;
     
    209194      executionState = original.executionState;
    210195      executionTime = original.executionTime;
    211       runsExecutionTime = original.runsExecutionTime;
    212196      optimizer = cloner.Clone(original.optimizer);
    213197      repetitions = original.repetitions;
     
    234218        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
    235219      if (Optimizer != null) {
    236         ExecutionTime = TimeSpan.Zero;
     220        executionTime = TimeSpan.Zero;
    237221        RepetitionsCounter = 0;
    238222        if (clearRuns) runs.Clear();
     
    297281      if (handler != null) handler(this, EventArgs.Empty);
    298282    }
     283    [Obsolete("Deprecated needs to be removed")]
    299284    public event EventHandler ExecutionTimeChanged;
    300     private void OnExecutionTimeChanged() {
    301       EventHandler handler = ExecutionTimeChanged;
    302       if (handler != null) handler(this, EventArgs.Empty);
    303     }
    304285    public event EventHandler OptimizerChanged;
    305286    private void OnOptimizerChanged() {
     
    353334    private void RegisterOptimizerEvents() {
    354335      optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Optimizer_ExceptionOccurred);
    355       optimizer.ExecutionTimeChanged += new EventHandler(Optimizer_ExecutionTimeChanged);
    356336      optimizer.Paused += new EventHandler(Optimizer_Paused);
    357337      optimizer.Prepared += new EventHandler(Optimizer_Prepared);
     
    364344    private void DeregisterOptimizerEvents() {
    365345      optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Optimizer_ExceptionOccurred);
    366       optimizer.ExecutionTimeChanged -= new EventHandler(Optimizer_ExecutionTimeChanged);
    367346      optimizer.Paused -= new EventHandler(Optimizer_Paused);
    368347      optimizer.Prepared -= new EventHandler(Optimizer_Prepared);
     
    376355      OnExceptionOccurred(e.Value);
    377356    }
    378     private void Optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
    379       OnExecutionTimeChanged();
    380     }
    381357    private void Optimizer_Paused(object sender, EventArgs e) {
    382358      if (ExecutionState == ExecutionState.Started) {
     
    386362    private void Optimizer_Prepared(object sender, EventArgs e) {
    387363      if (batchRunAction == BatchRunAction.Prepare || ExecutionState == ExecutionState.Stopped) {
    388         ExecutionTime = TimeSpan.Zero;
    389         runsExecutionTime = TimeSpan.Zero;
    390364        RepetitionsCounter = 0;
    391365        OnPrepared();
     
    398372    private void Optimizer_Stopped(object sender, EventArgs e) {
    399373      RepetitionsCounter++;
    400       ExecutionTime += runsExecutionTime;
    401       runsExecutionTime = TimeSpan.Zero;
    402374
    403375      if (batchRunAction == BatchRunAction.Stop) OnStopped();
     
    434406    private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    435407      if (Optimizer != null) Optimizer.Runs.RemoveRange(e.OldItems);
    436       foreach (IRun run in e.Items) {
     408      executionTime += SumExecutionTimes(e.OldItems);
     409      executionTime += SumExecutionTimes(e.Items);
     410    }
     411    private void Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
     412      executionTime += SumExecutionTimes(e.Items);
     413    }
     414    private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
     415      if (Optimizer != null) Optimizer.Runs.RemoveRange(e.Items);
     416      executionTime -= SumExecutionTimes(e.Items);
     417    }
     418    private TimeSpan SumExecutionTimes(IEnumerable<IRun> runs) {
     419      var sum = TimeSpan.Zero;
     420      foreach (IRun run in runs) {
    437421        IItem item;
    438422        run.Results.TryGetValue("Execution Time", out item);
    439         TimeSpanValue executionTime = item as TimeSpanValue;
    440         if (executionTime != null) ExecutionTime += executionTime.Value;
    441       }
    442     }
    443     private void Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    444       foreach (IRun run in e.Items) {
    445         IItem item;
    446         run.Results.TryGetValue("Execution Time", out item);
    447         TimeSpanValue executionTime = item as TimeSpanValue;
    448         if (executionTime != null) {
    449           if (Optimizer.ExecutionState == ExecutionState.Started)
    450             runsExecutionTime += executionTime.Value;
    451           else
    452             ExecutionTime += executionTime.Value;
    453         }
    454       }
    455     }
    456     private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    457       if (Optimizer != null) Optimizer.Runs.RemoveRange(e.Items);
     423        TimeSpanValue exTime = item as TimeSpanValue;
     424        if (exTime != null) sum += exTime.Value;
     425      }
     426      return sum;
    458427    }
    459428    #endregion
  • branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs

    r15339 r15603  
    7070    private TimeSpan executionTime;
    7171    public TimeSpan ExecutionTime {
    72       get { return executionTime; }
    73       private set {
    74         executionTime = value;
    75         OnExecutionTimeChanged();
    76       }
     72      get { return executionTime + Optimizers.Aggregate(TimeSpan.Zero, (a, x) => a + x.ExecutionTime); }
    7773    }
    7874
     
    249245      if (handler != null) handler(this, EventArgs.Empty);
    250246    }
     247    [Obsolete("deprecated needs to be removed")]
    251248    public event EventHandler ExecutionTimeChanged;
    252     private void OnExecutionTimeChanged() {
    253       EventHandler handler = ExecutionTimeChanged;
    254       if (handler != null) handler(this, EventArgs.Empty);
    255     }
    256249    public event EventHandler Prepared;
    257250    private void OnPrepared() {
     
    334327    private void RegisterOptimizerEvents(IOptimizer optimizer) {
    335328      optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
    336       optimizer.ExecutionTimeChanged += new EventHandler(optimizer_ExecutionTimeChanged);
    337329      optimizer.Paused += new EventHandler(optimizer_Paused);
    338330      optimizer.Prepared += new EventHandler(optimizer_Prepared);
     
    345337    private void DeregisterOptimizerEvents(IOptimizer optimizer) {
    346338      optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
    347       optimizer.ExecutionTimeChanged -= new EventHandler(optimizer_ExecutionTimeChanged);
    348339      optimizer.Paused -= new EventHandler(optimizer_Paused);
    349340      optimizer.Prepared -= new EventHandler(optimizer_Prepared);
     
    360351      lock (locker)
    361352        OnExceptionOccurred(e.Value);
    362     }
    363     private void optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
    364       // only wait for maximally 100ms to acquire lock, otherwise return and don't update the execution time
    365       var success = Monitor.TryEnter(locker, 100);
    366       if (!success) return;
    367       try {
    368         ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime);
    369       } finally {
    370         Monitor.Exit(locker);
    371       }
    372353    }
    373354    private void optimizer_Paused(object sender, EventArgs e) {
  • branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/MetaOptimizers/TimeLimitRun.cs

    r15287 r15603  
    214214
    215215    private void Initialize() {
     216      timer = new System.Timers.Timer();
     217      timer.Interval = 50;
     218      timer.Elapsed += Timer_Elapsed;
    216219      if (algorithm != null) RegisterAlgorithmEvents();
    217220      snapshotTimes.ItemsAdded += snapshotTimes_Changed;
     
    255258    }
    256259
     260    private System.Timers.Timer timer;
     261
     262    private void Timer_Elapsed(object sender, EventArgs e) {
     263      if (Algorithm.ExecutionState == ExecutionState.Started) {
     264        if (snapshotTimesIndex < SnapshotTimes.Count && ExecutionTime >= SnapshotTimes[snapshotTimesIndex]
     265          && !pausedForSnapshot) {
     266          pausedForSnapshot = true;
     267          Algorithm.Pause();
     268        }
     269        if (ExecutionTime >= MaximumExecutionTime && !pausedForTermination) {
     270          pausedForTermination = true;
     271          if (!pausedForSnapshot) Algorithm.Pause();
     272        }
     273      }
     274    }
     275
    257276    #region Events
    258277    protected override void OnNameChanged() {
     
    273292      if (handler != null) handler(this, EventArgs.Empty);
    274293    }
     294    [Obsolete("Deprecated needs to be removed")]
    275295    public event EventHandler ExecutionTimeChanged;
    276     private void OnExecutionTimeChanged() {
    277       var handler = ExecutionTimeChanged;
    278       if (handler != null) handler(this, EventArgs.Empty);
    279     }
    280296    public event EventHandler Prepared;
    281297    private void OnPrepared() {
     
    285301    public event EventHandler Started;
    286302    private void OnStarted() {
     303      timer.Start();
    287304      var handler = Started;
    288305      if (handler != null) handler(this, EventArgs.Empty);
     
    290307    public event EventHandler Paused;
    291308    private void OnPaused() {
     309      timer.Stop();
    292310      var handler = Paused;
    293311      if (handler != null) handler(this, EventArgs.Empty);
     
    295313    public event EventHandler Stopped;
    296314    private void OnStopped() {
     315      timer.Stop();
    297316      var handler = Stopped;
    298317      if (handler != null) handler(this, EventArgs.Empty);
     
    308327    private void RegisterAlgorithmEvents() {
    309328      algorithm.ExceptionOccurred += Algorithm_ExceptionOccurred;
    310       algorithm.ExecutionTimeChanged += Algorithm_ExecutionTimeChanged;
    311329      algorithm.ExecutionStateChanged += Algorithm_ExecutionStateChanged;
    312330      algorithm.Paused += Algorithm_Paused;
     
    317335    private void DeregisterAlgorithmEvents() {
    318336      algorithm.ExceptionOccurred -= Algorithm_ExceptionOccurred;
    319       algorithm.ExecutionTimeChanged -= Algorithm_ExecutionTimeChanged;
    320337      algorithm.ExecutionStateChanged -= Algorithm_ExecutionStateChanged;
    321338      algorithm.Paused -= Algorithm_Paused;
     
    326343    private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
    327344      OnExceptionOccurred(e.Value);
    328     }
    329     private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
    330       if (snapshotTimesIndex < SnapshotTimes.Count && ExecutionTime >= SnapshotTimes[snapshotTimesIndex]
    331         && !pausedForSnapshot) {
    332         pausedForSnapshot = true;
    333         Algorithm.Pause();
    334       }
    335       if (ExecutionTime >= MaximumExecutionTime && !pausedForTermination) {
    336         pausedForTermination = true;
    337         if (!pausedForSnapshot) Algorithm.Pause();
    338       }
    339       OnExecutionTimeChanged();
    340345    }
    341346    private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
Note: See TracChangeset for help on using the changeset viewer.