Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/05/10 03:17:25 (14 years ago)
Author:
swagner
Message:

Continued work on algorithm batch processing (#947).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimization/3.3/BatchRun.cs

    r3261 r3265  
    2222using System;
    2323using System.Drawing;
     24using HeuristicLab.Common;
    2425using HeuristicLab.Core;
    2526using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3233  [Creatable("Testing & Analysis")]
    3334  [StorableClass]
    34   public sealed class BatchRun : NamedItem {
     35  public sealed class BatchRun : NamedItem, IExecutable {
    3536    public override Image ItemImage {
    3637      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; }
     38    }
     39
     40    [Storable]
     41    private ExecutionState executionState;
     42    public ExecutionState ExecutionState {
     43      get { return executionState; }
     44      private set {
     45        if (executionState != value) {
     46          executionState = value;
     47          OnExecutionStateChanged();
     48        }
     49      }
     50    }
     51
     52    [Storable]
     53    private TimeSpan executionTime;
     54    public TimeSpan ExecutionTime {
     55      get {
     56        if ((Algorithm != null) && (Algorithm.ExecutionState != ExecutionState.Stopped))
     57          return executionTime + Algorithm.ExecutionTime;
     58        else
     59          return executionTime;
     60      }
     61      private set {
     62        executionTime = value;
     63        OnExecutionTimeChanged();
     64      }
    3765    }
    3866
     
    6896          repetitions = value;
    6997          OnRepetitionsChanged();
     98          if ((runs.Count < repetitions) && (Algorithm != null) && (Algorithm.ExecutionState == ExecutionState.Stopped))
     99            Prepare(false);
    70100        }
    71101      }
     
    78108    }
    79109
    80     [Storable]
    81     private TimeSpan executionTime;
    82     public TimeSpan ExecutionTime {
    83       get { return executionTime; }
    84       private set {
    85         if (executionTime != value) {
    86           executionTime = value;
    87           OnExecutionTimeChanged();
    88         }
    89       }
    90     }
    91 
    92     private bool running;
    93     public bool Running {
    94       get { return running; }
    95       private set {
    96         if (running != value) {
    97           running = value;
    98           OnRunningChanged();
    99         }
    100       }
    101     }
    102 
    103     public bool Finished {
    104       get { return ((Algorithm == null) || (Algorithm.Finished && (runs.Count >= repetitions))); }
    105     }
    106 
    107     private bool canceled;
     110    private bool stopPending;
    108111
    109112    public BatchRun()
    110113      : base() {
     114      executionState = ExecutionState.Stopped;
     115      executionTime = TimeSpan.Zero;
    111116      repetitions = 10;
    112117      runs = new RunCollection();
     118      stopPending = false;
     119    }
     120    public BatchRun(string name) : base(name) {
     121      executionState = ExecutionState.Stopped;
    113122      executionTime = TimeSpan.Zero;
    114     }
    115     public BatchRun(string name) : base(name) {
    116123      repetitions = 10;
    117124      runs = new RunCollection();
     125      stopPending = false;
     126    }
     127    public BatchRun(string name, string description) : base(name, description) {
     128      executionState = ExecutionState.Stopped;
    118129      executionTime = TimeSpan.Zero;
    119     }
    120     public BatchRun(string name, string description) : base(name, description) {
    121130      repetitions = 10;
    122131      runs = new RunCollection();
    123       executionTime = TimeSpan.Zero;
     132      stopPending = false;
    124133    }
    125134
    126135    public override IDeepCloneable Clone(Cloner cloner) {
    127136      BatchRun clone = (BatchRun)base.Clone(cloner);
     137      clone.executionState = executionState;
     138      clone.executionTime = executionTime;
    128139      clone.Algorithm = (IAlgorithm)cloner.Clone(algorithm);
    129140      clone.repetitions = repetitions;
    130141      clone.runs = (RunCollection)cloner.Clone(runs);
    131       clone.executionTime = executionTime;
    132       clone.running = running;
    133       clone.canceled = canceled;
     142      clone.stopPending = stopPending;
    134143      return clone;
    135144    }
    136145
    137146    public void Prepare() {
    138       executionTime = TimeSpan.Zero;
    139       runs.Clear();
    140       if (Algorithm != null) Algorithm.Prepare();
    141       OnPrepared();
     147      Prepare(true);
     148    }
     149    public void Prepare(bool clearRuns) {
     150      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
     151        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
     152      if (Algorithm != null) {
     153        if (clearRuns) {
     154          ExecutionTime = TimeSpan.Zero;
     155          runs.Clear();
     156        }
     157        Algorithm.Prepare();
     158      }
    142159    }
    143160    public void Start() {
    144       if (Algorithm != null) {
    145         OnStarted();
    146         Running = true;
    147         canceled = false;
    148         Algorithm.Start();
    149       }
     161      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
     162        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
     163      if (Algorithm != null) Algorithm.Start();
     164    }
     165    public void Pause() {
     166      if (ExecutionState != ExecutionState.Started)
     167        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
     168      if (Algorithm != null) Algorithm.Pause();
    150169    }
    151170    public void Stop() {
    152       if (Algorithm != null) {
    153         canceled = true;
    154         Algorithm.Stop();
    155       }
     171      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
     172        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
     173      stopPending = true;
     174      if (Algorithm != null) Algorithm.Stop();
    156175    }
    157176
    158177    #region Events
     178    public event EventHandler ExecutionStateChanged;
     179    private void OnExecutionStateChanged() {
     180      EventHandler handler = ExecutionStateChanged;
     181      if (handler != null) handler(this, EventArgs.Empty);
     182    }
     183    public event EventHandler ExecutionTimeChanged;
     184    private void OnExecutionTimeChanged() {
     185      EventHandler handler = ExecutionTimeChanged;
     186      if (handler != null) handler(this, EventArgs.Empty);
     187    }
    159188    public event EventHandler AlgorithmChanged;
    160189    private void OnAlgorithmChanged() {
    161       if (AlgorithmChanged != null)
    162         AlgorithmChanged(this, EventArgs.Empty);
     190      EventHandler handler = AlgorithmChanged;
     191      if (handler != null) handler(this, EventArgs.Empty);
    163192    }
    164193    public event EventHandler RepetitionsChanged;
    165194    private void OnRepetitionsChanged() {
    166       if (RepetitionsChanged != null)
    167         RepetitionsChanged(this, EventArgs.Empty);
    168     }
    169     public event EventHandler ExecutionTimeChanged;
    170     private void OnExecutionTimeChanged() {
    171       if (ExecutionTimeChanged != null)
    172         ExecutionTimeChanged(this, EventArgs.Empty);
    173     }
    174     public event EventHandler RunningChanged;
    175     private void OnRunningChanged() {
    176       if (RunningChanged != null)
    177         RunningChanged(this, EventArgs.Empty);
     195      EventHandler handler = RepetitionsChanged;
     196      if (handler != null) handler(this, EventArgs.Empty);
    178197    }
    179198    public event EventHandler Prepared;
    180199    private void OnPrepared() {
    181       if (Prepared != null)
    182         Prepared(this, EventArgs.Empty);
     200      ExecutionState = ExecutionState.Prepared;
     201      EventHandler handler = Prepared;
     202      if (handler != null) handler(this, EventArgs.Empty);
    183203    }
    184204    public event EventHandler Started;
    185205    private void OnStarted() {
    186       if (Started != null)
    187         Started(this, EventArgs.Empty);
     206      ExecutionState = ExecutionState.Started;
     207      EventHandler handler = Started;
     208      if (handler != null) handler(this, EventArgs.Empty);
     209    }
     210    public event EventHandler Paused;
     211    private void OnPaused() {
     212      ExecutionState = ExecutionState.Paused;
     213      EventHandler handler = Paused;
     214      if (handler != null) handler(this, EventArgs.Empty);
    188215    }
    189216    public event EventHandler Stopped;
    190217    private void OnStopped() {
    191       if (Stopped != null)
    192         Stopped(this, EventArgs.Empty);
    193     }
    194     public event EventHandler<HeuristicLab.Common.EventArgs<Exception>> ExceptionOccurred;
     218      ExecutionState = ExecutionState.Stopped;
     219      EventHandler handler = Stopped;
     220      if (handler != null) handler(this, EventArgs.Empty);
     221    }
     222    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
    195223    private void OnExceptionOccurred(Exception exception) {
    196       if (ExceptionOccurred != null)
    197         ExceptionOccurred(this, new HeuristicLab.Common.EventArgs<Exception>(exception));
     224      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
     225      if (handler != null) handler(this, new EventArgs<Exception>(exception));
    198226    }
    199227
    200228    private void RegisterAlgorithmEvents() {
    201       algorithm.ExceptionOccurred += new EventHandler<HeuristicLab.Common.EventArgs<Exception>>(Algorithm_ExceptionOccurred);
     229      algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
     230      algorithm.ExecutionTimeChanged += new EventHandler(Algorithm_ExecutionTimeChanged);
     231      algorithm.Paused += new EventHandler(Algorithm_Paused);
     232      algorithm.Prepared += new EventHandler(Algorithm_Prepared);
    202233      algorithm.Started += new EventHandler(Algorithm_Started);
    203234      algorithm.Stopped += new EventHandler(Algorithm_Stopped);
    204235    }
    205236    private void DeregisterAlgorithmEvents() {
    206       algorithm.ExceptionOccurred -= new EventHandler<HeuristicLab.Common.EventArgs<Exception>>(Algorithm_ExceptionOccurred);
     237      algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
     238      algorithm.ExecutionTimeChanged -= new EventHandler(Algorithm_ExecutionTimeChanged);
     239      algorithm.Paused -= new EventHandler(Algorithm_Paused);
     240      algorithm.Prepared -= new EventHandler(Algorithm_Prepared);
    207241      algorithm.Started -= new EventHandler(Algorithm_Started);
    208242      algorithm.Stopped -= new EventHandler(Algorithm_Stopped);
    209243    }
    210244
    211     private void Algorithm_ExceptionOccurred(object sender, HeuristicLab.Common.EventArgs<Exception> e) {
     245    private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
    212246      OnExceptionOccurred(e.Value);
    213247    }
     248    private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
     249      OnExecutionTimeChanged();
     250    }
     251    private void Algorithm_Paused(object sender, EventArgs e) {
     252      OnPaused();
     253    }
     254    private void Algorithm_Prepared(object sender, EventArgs e) {
     255      OnPrepared();
     256    }
    214257    private void Algorithm_Started(object sender, EventArgs e) {
    215       if (!Running) {
    216         OnStarted();
    217         Running = true;
    218         canceled = false;
    219       }
     258      stopPending = false;
     259      OnStarted();
    220260    }
    221261    private void Algorithm_Stopped(object sender, EventArgs e) {
    222       if (!canceled) {
    223         ExecutionTime += Algorithm.ExecutionTime;
    224         runs.Add(new Run("Run " + Algorithm.ExecutionTime.ToString(), Algorithm));
     262      ExecutionTime += Algorithm.ExecutionTime;
     263      runs.Add(new Run("Run " + Algorithm.ExecutionTime.ToString(), Algorithm));
     264      OnStopped();
     265
     266      if (!stopPending && (runs.Count < repetitions)) {
    225267        Algorithm.Prepare();
    226 
    227         if (runs.Count < repetitions)
    228           Algorithm.Start();
    229         else {
    230           Running = false;
    231           OnStopped();
    232         }
    233       } else {
    234         canceled = false;
    235         Running = false;
    236         OnStopped();
     268        Algorithm.Start();
    237269      }
    238270    }
Note: See TracChangeset for help on using the changeset viewer.