Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/04/10 05:22:47 (14 years ago)
Author:
swagner
Message:

Continued work on algorithm batch processing (#947).

Location:
trunk/sources/HeuristicLab.Optimization/3.3
Files:
5 edited

Legend:

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

    r3261 r3262  
    3636    public override Image ItemImage {
    3737      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 { return executionTime; }
     56      protected set {
     57        executionTime = value;
     58        OnExecutionTimeChanged();
     59      }
    3860    }
    3961
     
    6890    public abstract ResultCollection Results { get; }
    6991
    70     public abstract TimeSpan ExecutionTime { get; }
    71 
    72     private bool running;
    73     public bool Running {
    74       get { return running; }
    75       protected set {
    76         if (running != value) {
    77           running = value;
    78           OnRunningChanged();
    79         }
    80       }
    81     }
    82 
    83     public abstract bool Finished { get; }
    84 
    85     private bool canceled;
    86     protected bool Canceled {
    87       get { return canceled; }
    88       private set {
    89         if (canceled != value) {
    90           canceled = value;
    91           OnCanceledChanged();
    92         }
    93       }
    94     }
    95 
    96     protected Algorithm() : base() { }
    97     protected Algorithm(string name) : base(name) { }
    98     protected Algorithm(string name, ParameterCollection parameters) : base(name, parameters) { }
    99     protected Algorithm(string name, string description) : base(name, description) { }
    100     protected Algorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { }
     92    protected Algorithm()
     93      : base() {
     94      executionState = ExecutionState.Stopped;
     95      executionTime = TimeSpan.Zero;
     96    }
     97    protected Algorithm(string name)
     98      : base(name) {
     99      executionState = ExecutionState.Stopped;
     100      executionTime = TimeSpan.Zero;
     101    }
     102    protected Algorithm(string name, ParameterCollection parameters)
     103      : base(name, parameters) {
     104      executionState = ExecutionState.Stopped;
     105      executionTime = TimeSpan.Zero;
     106    }
     107    protected Algorithm(string name, string description)
     108      : base(name, description) {
     109      executionState = ExecutionState.Stopped;
     110      executionTime = TimeSpan.Zero;
     111    }
     112    protected Algorithm(string name, string description, ParameterCollection parameters)
     113      : base(name, description, parameters) {
     114      executionState = ExecutionState.Stopped;
     115      executionTime = TimeSpan.Zero;
     116    }
    101117
    102118    public override IDeepCloneable Clone(Cloner cloner) {
    103119      Algorithm clone = (Algorithm)base.Clone(cloner);
     120      clone.executionState = executionState;
     121      clone.executionTime = executionTime;
    104122      clone.Problem = (IProblem)cloner.Clone(problem);
    105       clone.running = running;
    106       clone.canceled = canceled;
    107123      return clone;
    108124    }
    109125
    110     public void Prepare() {
    111       OnPrepared();
    112     }
    113     public void Start() {
    114       OnStarted();
    115       Running = true;
    116       Canceled = false;
    117     }
    118     public void Stop() {
    119       Canceled = true;
     126    public virtual void Prepare() {
     127      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
     128        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
     129      ExecutionTime = TimeSpan.Zero;
     130    }
     131    public virtual void Start() {
     132      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
     133        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
     134    }
     135    public virtual void Pause() {
     136      if (ExecutionState != ExecutionState.Started)
     137        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
     138    }
     139    public virtual void Stop() {
     140      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
     141        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
    120142    }
    121143
     
    130152
    131153    #region Events
     154    public event EventHandler ExecutionStateChanged;
     155    protected virtual void OnExecutionStateChanged() {
     156      EventHandler handler = ExecutionStateChanged;
     157      if (handler != null) handler(this, EventArgs.Empty);
     158    }
     159    public event EventHandler ExecutionTimeChanged;
     160    protected virtual void OnExecutionTimeChanged() {
     161      EventHandler handler = ExecutionTimeChanged;
     162      if (handler != null) handler(this, EventArgs.Empty);
     163    }
    132164    public event EventHandler ProblemChanged;
    133165    protected virtual void OnProblemChanged() {
    134       if (ProblemChanged != null)
    135         ProblemChanged(this, EventArgs.Empty);
    136     }
    137     public event EventHandler ExecutionTimeChanged;
    138     protected virtual void OnExecutionTimeChanged() {
    139       if (ExecutionTimeChanged != null)
    140         ExecutionTimeChanged(this, EventArgs.Empty);
    141     }
    142     public event EventHandler RunningChanged;
    143     protected virtual void OnRunningChanged() {
    144       if (RunningChanged != null)
    145         RunningChanged(this, EventArgs.Empty);
     166      EventHandler handler = ProblemChanged;
     167      if (handler != null) handler(this, EventArgs.Empty);
    146168    }
    147169    public event EventHandler Prepared;
    148170    protected virtual void OnPrepared() {
    149       if (Prepared != null)
    150         Prepared(this, EventArgs.Empty);
     171      ExecutionState = ExecutionState.Prepared;
     172      EventHandler handler = Prepared;
     173      if (handler != null) handler(this, EventArgs.Empty);
    151174    }
    152175    public event EventHandler Started;
    153176    protected virtual void OnStarted() {
    154       if (Started != null)
    155         Started(this, EventArgs.Empty);
     177      ExecutionState = ExecutionState.Started;
     178      EventHandler handler = Started;
     179      if (handler != null) handler(this, EventArgs.Empty);
     180    }
     181    public event EventHandler Paused;
     182    protected virtual void OnPaused() {
     183      ExecutionState = ExecutionState.Paused;
     184      EventHandler handler = Paused;
     185      if (handler != null) handler(this, EventArgs.Empty);
    156186    }
    157187    public event EventHandler Stopped;
    158188    protected virtual void OnStopped() {
    159       Canceled = false;
    160       Running = false;
    161       if (Stopped != null)
    162         Stopped(this, EventArgs.Empty);
    163     }
    164     protected virtual void OnCanceledChanged() { }
     189      ExecutionState = ExecutionState.Stopped;
     190      EventHandler handler = Stopped;
     191      if (handler != null) handler(this, EventArgs.Empty);
     192    }
    165193    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
    166194    protected virtual void OnExceptionOccurred(Exception exception) {
    167       if (ExceptionOccurred != null)
    168         ExceptionOccurred(this, new EventArgs<Exception>(exception));
     195      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
     196      if (handler != null) handler(this, new EventArgs<Exception>(exception));
    169197    }
    170198
  • trunk/sources/HeuristicLab.Optimization/3.3/EngineAlgorithm.cs

    r3261 r3262  
    9494    }
    9595
    96     public override TimeSpan ExecutionTime {
    97       get {
    98         if (engine == null) return TimeSpan.Zero;
    99         else return engine.ExecutionTime;
    100       }
    101     }
    102 
    103     public override bool Finished {
    104       get {
    105         if (engine == null) return true;
    106         else return engine.Finished;
    107       }
    108     }
    109 
    11096    protected EngineAlgorithm()
    11197      : base() {
     
    172158    }
    173159
    174     protected override void OnCanceledChanged() {
    175       if (Canceled && (engine != null))
    176         engine.Stop();
    177     }
    178     protected override void OnPrepared() {
     160    public override void Prepare() {
     161      base.Prepare();
    179162      globalScope.Clear();
    180163      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
     
    195178        engine.Prepare(context);
    196179      }
    197       base.OnPrepared();
    198     }
    199     protected override void OnStarted() {
     180    }
     181    public override void Start() {
     182      base.Start();
    200183      if (engine != null) engine.Start();
    201       base.OnStarted();
    202     }
    203 
    204     protected virtual void OnOperatorGraphChanged() { }
    205 
     184    }
     185    public override void Pause() {
     186      base.Pause();
     187      if (engine != null) engine.Pause();
     188    }
     189    public override void Stop() {
     190      base.Stop();
     191      if (engine != null) engine.Stop();
     192    }
     193
     194    #region Events
    206195    public event EventHandler EngineChanged;
    207196    protected virtual void OnEngineChanged() {
     
    209198        EngineChanged(this, EventArgs.Empty);
    210199    }
    211 
    212     private void OperatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
    213       Prepare();
    214     }
     200    protected virtual void OnOperatorGraphChanged() { }
     201
    215202    private void RegisterEngineEvents() {
    216203      Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
    217204      Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);
     205      Engine.Paused += new EventHandler(Engine_Paused);
     206      Engine.Prepared += new EventHandler(Engine_Prepared);
     207      Engine.Started += new EventHandler(Engine_Started);
    218208      Engine.Stopped += new EventHandler(Engine_Stopped);
    219209    }
    220 
    221210    private void DeregisterEngineEvents() {
    222211      Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
    223212      Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);
     213      Engine.Paused -= new EventHandler(Engine_Paused);
     214      Engine.Prepared -= new EventHandler(Engine_Prepared);
     215      Engine.Started -= new EventHandler(Engine_Started);
    224216      Engine.Stopped -= new EventHandler(Engine_Stopped);
    225217    }
    226 
    227218    private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
    228219      OnExceptionOccurred(e.Value);
    229220    }
    230221    private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {
    231       OnExecutionTimeChanged();
     222      ExecutionTime = Engine.ExecutionTime;
     223    }
     224    private void Engine_Paused(object sender, EventArgs e) {
     225      OnPaused();
     226    }
     227    private void Engine_Prepared(object sender, EventArgs e) {
     228      OnPrepared();
     229    }
     230    private void Engine_Started(object sender, EventArgs e) {
     231      OnStarted();
    232232    }
    233233    private void Engine_Stopped(object sender, EventArgs e) {
    234234      OnStopped();
    235235    }
     236
     237    private void OperatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
     238      Prepare();
     239    }
     240    #endregion
    236241  }
    237242}
  • trunk/sources/HeuristicLab.Optimization/3.3/HeuristicLab.Optimization-3.3.csproj

    r3260 r3262  
    8686    <None Include="HeuristicLabOptimizationPlugin.cs.frame" />
    8787    <Compile Include="Algorithm.cs" />
    88     <Compile Include="BatchRun.cs" />
    8988    <Compile Include="RunCollection.cs" />
    9089    <Compile Include="Run.cs" />
  • trunk/sources/HeuristicLab.Optimization/3.3/Interfaces/IAlgorithm.cs

    r3260 r3262  
    2929  /// Interface to represent an algorithm.
    3030  /// </summary>
    31   public interface IAlgorithm : IParameterizedNamedItem {
     31  public interface IAlgorithm : IParameterizedNamedItem, IExecutable {
    3232    Type ProblemType { get; }
    3333    IProblem Problem { get; set; }
    3434    ResultCollection Results { get; }
    35     TimeSpan ExecutionTime { get; }
    36     bool Running { get; }
    37     bool Finished { get; }
    38 
    39     void Prepare();
    40     void Start();
    41     void Stop();
    4235
    4336    void CollectResultValues(IDictionary<string, IItem> values);
    4437
    4538    event EventHandler ProblemChanged;
    46     event EventHandler ExecutionTimeChanged;
    47     event EventHandler RunningChanged;
    48     event EventHandler Prepared;
    49     event EventHandler Started;
    50     event EventHandler Stopped;
    51     event EventHandler<EventArgs<Exception>> ExceptionOccurred;
    5239  }
    5340}
  • trunk/sources/HeuristicLab.Optimization/3.3/UserDefinedAlgorithm.cs

    r3017 r3262  
    5555    public event EventHandler OperatorGraphChanged;
    5656    protected override void OnOperatorGraphChanged() {
    57       if (OperatorGraphChanged != null)
    58         OperatorGraphChanged(this, EventArgs.Empty);
     57      EventHandler handler = OperatorGraphChanged;
     58      if (handler != null) handler(this, EventArgs.Empty);
    5959    }
    6060  }
Note: See TracChangeset for help on using the changeset viewer.