Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/FixedDataAnalysisAlgorithm.cs @ 12912

Last change on this file since 12912 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 4.3 KB
RevLine 
[5617]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5617]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[6466]23using System.Threading;
24using System.Threading.Tasks;
[5617]25using HeuristicLab.Common;
26using HeuristicLab.Optimization;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [StorableClass]
32  public abstract class FixedDataAnalysisAlgorithm<T> : Algorithm,
33    IDataAnalysisAlgorithm<T>,
34    IStorableContent
35    where T : class, IDataAnalysisProblem {
36    public string Filename { get; set; }
37
38    #region Properties
39    public override Type ProblemType {
40      get { return typeof(T); }
41    }
42    public new T Problem {
43      get { return (T)base.Problem; }
44      set { base.Problem = value; }
45    }
46    [Storable]
47    private ResultCollection results;
48    public override ResultCollection Results {
49      get { return results; }
50    }
51    #endregion
[6466]52
[5661]53    private DateTime lastUpdateTime;
[5617]54
55    [StorableConstructor]
56    protected FixedDataAnalysisAlgorithm(bool deserializing) : base(deserializing) { }
57    protected FixedDataAnalysisAlgorithm(FixedDataAnalysisAlgorithm<T> original, Cloner cloner)
58      : base(original, cloner) {
59      results = cloner.Clone(original.Results);
60    }
61    public FixedDataAnalysisAlgorithm()
62      : base() {
63      results = new ResultCollection();
64    }
65
66    public override void Prepare() {
67      if (Problem != null) base.Prepare();
68      results.Clear();
69      OnPrepared();
70    }
71
72    public override void Start() {
73      base.Start();
[5661]74      var cancellationTokenSource = new CancellationTokenSource();
75
[5617]76      OnStarted();
[5661]77      Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
78      task.ContinueWith(t => {
79        try {
80          t.Wait();
81        }
82        catch (AggregateException ex) {
83          try {
84            ex.Flatten().Handle(x => x is OperationCanceledException);
85          }
86          catch (AggregateException remaining) {
87            if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
88            else OnExceptionOccurred(remaining);
89          }
90        }
91        cancellationTokenSource.Dispose();
92        cancellationTokenSource = null;
93        OnStopped();
94      });
95    }
96    private void Run(object state) {
97      CancellationToken cancellationToken = (CancellationToken)state;
[9343]98      lastUpdateTime = DateTime.UtcNow;
[5661]99      System.Timers.Timer timer = new System.Timers.Timer(250);
100      timer.AutoReset = true;
101      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
102      timer.Start();
[5649]103      try {
104        Run();
105      }
106      finally {
[5661]107        timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
108        timer.Stop();
[9343]109        ExecutionTime += DateTime.UtcNow - lastUpdateTime;
[5649]110      }
[5661]111
112      cancellationToken.ThrowIfCancellationRequested();
[5617]113    }
[5661]114    protected abstract void Run();
[5617]115    #region Events
116    protected override void OnProblemChanged() {
117      Problem.Reset += new EventHandler(Problem_Reset);
118      base.OnProblemChanged();
119    }
[5661]120    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
121      System.Timers.Timer timer = (System.Timers.Timer)sender;
122      timer.Enabled = false;
[9343]123      DateTime now = DateTime.UtcNow;
[5661]124      ExecutionTime += now - lastUpdateTime;
125      lastUpdateTime = now;
126      timer.Enabled = true;
127    }
[5617]128    #endregion
129
130  }
131}
Note: See TracBrowser for help on using the repository browser.