Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14383


Ignore:
Timestamp:
11/10/16 15:26:17 (7 years ago)
Author:
bburlacu
Message:

#2679: Sync outer and inner optimizer states (fix pause/resume and stop issues).

Location:
branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/GoalSeekingOptimizer.cs

    r14380 r14383  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Drawing;
    2425using System.Linq;
    25 using System.Threading;
    2626using HeuristicLab.Common;
    2727using HeuristicLab.Core;
     
    3131
    3232namespace HeuristicLab.GoalSeeking {
     33  internal enum GoalSeekingOptimizerAction { None, Prepare, Start, Stop, Pause }
     34
    3335  [StorableClass]
    3436  [Item("GoalSeeking Optimizer", "A wrapper for the GoalSeekingProblem class to facilitate adding models and problem data.")]
     
    9092    }
    9193
     94    private GoalSeekingOptimizerAction gsoAction;
     95
    9296    public GoalSeekingOptimizer() {
    9397      Name = "Goal Seeking Optimizer";
     
    136140
    137141    private int calculatedRows;
    138     private readonly ManualResetEvent mutex = new ManualResetEvent(false);
    139142    public void Start() {
    140143      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
    141144        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
    142145      if (optimizer == null) return;
    143 
    144       Prepare(clearRuns: true);
    145 
    146       calculatedRows = 0;
    147 
    148       var row = problemData.TrainingIndices.Skip(calculatedRows).First();
    149       problem.Configure(problemData, row);
    150       optimizer.Prepare();
     146      gsoAction = GoalSeekingOptimizerAction.Start;
     147      if (Optimizer.ExecutionState == ExecutionState.Stopped) {
     148        Optimizer.Prepare(clearRuns: true);
     149        calculatedRows = 0;
     150
     151        var row = problemData.TrainingIndices.Skip(calculatedRows).First();
     152        problem.Configure(problemData, row);
     153        optimizer.Prepare();
     154      }
    151155      optimizer.Start();
    152156    }
     
    156160        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
    157161      if (optimizer == null) return;
     162      gsoAction = GoalSeekingOptimizerAction.Pause;
    158163      if (optimizer.ExecutionState != ExecutionState.Started) return;
    159164      // a race-condition may occur when the algorithm has changed the state by itself in the meantime
     
    166171        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
    167172      if (optimizer == null) return;
     173      gsoAction = GoalSeekingOptimizerAction.Stop;
    168174      if (optimizer.ExecutionState != ExecutionState.Started && optimizer.ExecutionState != ExecutionState.Paused) {
    169175        OnStopped();
     
    185191        ExecutionTime = TimeSpan.Zero;
    186192        if (clearRuns) optimizer.Runs.Clear();
     193        gsoAction = GoalSeekingOptimizerAction.Prepare;
    187194        // a race-condition may occur when the algorithm has changed the state by itself in the meantime
    188195        try { optimizer.Prepare(clearRuns); }
     
    220227    public string Filename { get; set; }
    221228
     229    public new static Image StaticItemImage {
     230      get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
     231    }
     232    public override Image ItemImage {
     233      get {
     234        if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VSImageLibrary.BatchRunPrepared;
     235        else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VSImageLibrary.BatchRunStarted;
     236        else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VSImageLibrary.BatchRunPaused;
     237        else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VSImageLibrary.BatchRunStopped;
     238        else return base.ItemImage;
     239      }
     240    }
     241
    222242    #region Events
    223243    protected override void OnNameChanged() {
     
    241261    public event EventHandler Prepared;
    242262    private void OnPrepared() {
     263      gsoAction = GoalSeekingOptimizerAction.None;
    243264      ExecutionState = ExecutionState.Prepared;
    244265      EventHandler handler = Prepared;
     
    255276    public event EventHandler Paused;
    256277    private void OnPaused() {
     278      gsoAction = GoalSeekingOptimizerAction.None;
    257279      ExecutionState = ExecutionState.Paused;
    258280      EventHandler handler = Paused;
     
    262284    public event EventHandler Stopped;
    263285    private void OnStopped() {
     286      gsoAction = GoalSeekingOptimizerAction.None;
    264287      ExecutionState = ExecutionState.Stopped;
    265288      EventHandler handler = Stopped;
     
    326349
    327350    private void Optimizer_Prepared(object sender, EventArgs e) {
    328       if (ExecutionState == ExecutionState.Stopped) {
     351      if (gsoAction == GoalSeekingOptimizerAction.Prepare || ExecutionState == ExecutionState.Stopped) {
     352        calculatedRows = 0;
    329353        ExecutionTime = TimeSpan.Zero;
    330354        OnPrepared();
     
    341365      calculatedRows++;
    342366      ExecutionTime += optimizer.ExecutionTime;
    343 
    344367      var remainingRows = problemData.TrainingIndices.Skip(calculatedRows);
    345       if (remainingRows.Any()) {
     368
     369      if (gsoAction == GoalSeekingOptimizerAction.Stop) OnStopped();
     370      else if (!remainingRows.Any()) OnStopped();
     371      else if (gsoAction == GoalSeekingOptimizerAction.Pause) OnPaused();
     372      else if (gsoAction == GoalSeekingOptimizerAction.Start) {
    346373        var row = remainingRows.First();
    347374        problem.Configure(problemData, row);
    348375        optimizer.Prepare();
    349376        optimizer.Start();
    350       } else {
    351         OnStopped();
    352       }
     377      } else if (ExecutionState == ExecutionState.Started) {
     378        OnPaused();
     379      } else OnStopped();
    353380    }
    354381    #endregion
  • branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/GoalSeekingProblem.csproj

    r14380 r14383  
    124124      <SpecificVersion>False</SpecificVersion>
    125125      <HintPath>..\HeuristicLab trunk sources\binHeuristicLab.Common-3.3.dll</HintPath>
     126      <Private>False</Private>
     127    </Reference>
     128    <Reference Include="HeuristicLab.Common.Resources-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     129      <SpecificVersion>False</SpecificVersion>
     130      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Common.Resources-3.3.dll</HintPath>
    126131      <Private>False</Private>
    127132    </Reference>
  • branches/HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/Properties/AssemblyInfo.cs

    r14380 r14383  
    5454// [assembly: AssemblyVersion("1.0.*")]
    5555[assembly: AssemblyVersion("3.3.0.0")]
    56 [assembly: AssemblyFileVersion("3.3.11.14379")]
     56[assembly: AssemblyFileVersion("3.3.11.14380")]
Note: See TracChangeset for help on using the changeset viewer.