Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6767


Ignore:
Timestamp:
09/15/11 14:15:43 (13 years ago)
Author:
mkommend
Message:

#1573, #1574: Decoupled execution states of Experiment and BatchRun with the nested optimizers.

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

Legend:

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

    r6534 r6767  
    147147    }
    148148
    149     private bool stopPending;
     149    private bool batchRunStarted = false;
     150    private bool batchRunPaused = false;
     151    private bool batchRunStopped = false;
    150152
    151153    public BatchRun()
     
    158160      repetitionsCounter = 0;
    159161      Runs = new RunCollection();
    160       stopPending = false;
    161162    }
    162163    public BatchRun(string name)
     
    168169      repetitionsCounter = 0;
    169170      Runs = new RunCollection();
    170       stopPending = false;
    171171    }
    172172    public BatchRun(string name, string description)
     
    177177      repetitionsCounter = 0;
    178178      Runs = new RunCollection();
    179       stopPending = false;
    180179    }
    181180    [StorableConstructor]
    182     private BatchRun(bool deserializing)
    183       : base(deserializing) {
    184       stopPending = false;
    185     }
     181    private BatchRun(bool deserializing) : base(deserializing) { }
    186182    [StorableHook(HookType.AfterDeserialization)]
    187183    private void AfterDeserialization() {
     
    197193      repetitionsCounter = original.repetitionsCounter;
    198194      runs = cloner.Clone(original.runs);
    199       stopPending = original.stopPending;
     195      batchRunStarted = original.batchRunStarted;
     196      batchRunPaused = original.batchRunPaused;
     197      batchRunStopped = original.batchRunStopped;
    200198      Initialize();
    201199    }
     
    227225      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
    228226        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
    229       if (Optimizer != null) Optimizer.Start();
     227      if (Optimizer == null) return;
     228
     229      batchRunStarted = true;
     230      batchRunPaused = false;
     231      batchRunStopped = false;
     232      if (Optimizer.ExecutionState != ExecutionState.Prepared) Optimizer.Prepare();
     233      Optimizer.Start();
    230234    }
    231235    public void Pause() {
    232236      if (ExecutionState != ExecutionState.Started)
    233237        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
    234       if ((Optimizer != null) && (Optimizer.ExecutionState == ExecutionState.Started))
    235         Optimizer.Pause();
     238      if (Optimizer == null) return;
     239      if (Optimizer.ExecutionState != ExecutionState.Started) return;
     240
     241      batchRunStarted = false;
     242      batchRunPaused = true;
     243      batchRunStopped = false;
     244      Optimizer.Pause();
    236245    }
    237246    public void Stop() {
    238247      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
    239248        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
    240       stopPending = true;
    241       if ((Optimizer != null) &&
    242           ((Optimizer.ExecutionState == ExecutionState.Started) || (Optimizer.ExecutionState == ExecutionState.Paused)))
    243         Optimizer.Stop();
     249      if (Optimizer == null) return;
     250      if (Optimizer.ExecutionState != ExecutionState.Started && Optimizer.ExecutionState != ExecutionState.Paused) return;
     251      batchRunStarted = false;
     252      batchRunPaused = false;
     253      batchRunStopped = true;
     254      Optimizer.Stop();
    244255    }
    245256
     
    324335    }
    325336    private void Optimizer_Paused(object sender, EventArgs e) {
    326       OnPaused();
     337      if (ExecutionState == ExecutionState.Started) {
     338        batchRunStarted = false;
     339        batchRunPaused = true;
     340        batchRunStopped = false;
     341        OnPaused();
     342      }
    327343    }
    328344    private void Optimizer_Prepared(object sender, EventArgs e) {
    329       if ((ExecutionState == ExecutionState.Paused) || (ExecutionState == ExecutionState.Stopped))
     345      if (ExecutionState == ExecutionState.Stopped || !batchRunStarted) {
    330346        OnPrepared();
     347      }
    331348    }
    332349    private void Optimizer_Started(object sender, EventArgs e) {
    333       stopPending = false;
    334350      if (ExecutionState != ExecutionState.Started)
    335351        OnStarted();
     
    338354      repetitionsCounter++;
    339355
    340       if (!stopPending && (repetitionsCounter < repetitions)) {
     356      if (batchRunStopped) OnStopped();
     357      else if (repetitionsCounter >= repetitions) OnStopped();
     358      else if (batchRunPaused) OnPaused();
     359      else if (batchRunStarted) {
    341360        Optimizer.Prepare();
    342361        Optimizer.Start();
    343       } else {
    344         OnStopped();
    345       }
     362      } else OnStopped();
    346363    }
    347364    private void Optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
  • trunk/sources/HeuristicLab.Optimization/3.3/Experiment.cs

    r6534 r6767  
    104104    }
    105105
    106     private bool stopPending;
     106    private bool experimentStarted = false;
     107    private bool experimentStopped = false;
    107108
    108109    public Experiment()
     
    114115      optimizers = new OptimizerList();
    115116      Runs = new RunCollection();
    116       stopPending = false;
    117117      Initialize();
    118118    }
     
    124124      optimizers = new OptimizerList();
    125125      Runs = new RunCollection();
    126       stopPending = false;
    127126      Initialize();
    128127    }
     
    133132      optimizers = new OptimizerList();
    134133      Runs = new RunCollection();
    135       stopPending = false;
    136134      Initialize();
    137135    }
    138136    [StorableConstructor]
    139     private Experiment(bool deserializing)
    140       : base(deserializing) {
    141       stopPending = false;
    142     }
     137    private Experiment(bool deserializing) : base(deserializing) { }
    143138    [StorableHook(HookType.AfterDeserialization)]
    144139    private void AfterDeserialization() {
     
    151146      optimizers = cloner.Clone(original.optimizers);
    152147      runs = cloner.Clone(original.runs);
    153       stopPending = original.stopPending;
     148
     149      experimentStarted = original.experimentStarted;
     150      experimentStopped = original.experimentStopped;
    154151      Initialize();
    155152    }
     
    172169      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
    173170        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
    174       if (Optimizers.Count > 0) {
    175         if (clearRuns) runs.Clear();
    176         foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started))
     171      if (Optimizers.Count == 0) return;
     172
     173      if (clearRuns) runs.Clear();
     174      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started))
     175        if (clearRuns || optimizer.ExecutionState != ExecutionState.Prepared)
    177176          optimizer.Prepare(clearRuns);
    178       }
    179177    }
    180178    public void Start() {
    181179      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
    182180        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
    183       stopPending = false;
    184       if (Optimizers.Count > 0) {
    185         IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
    186         if (optimizer != null) optimizer.Start();
    187       }
     181      if (Optimizers.Count == 0) return;
     182
     183      experimentStarted = true;
     184      experimentStopped = false;
     185      IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
     186      if (optimizer != null) optimizer.Start();
    188187    }
    189188    public void Pause() {
    190189      if (ExecutionState != ExecutionState.Started)
    191190        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
    192       if (Optimizers.Count > 0) {
    193         foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started))
    194           optimizer.Pause();
    195       }
     191      if (Optimizers.Count == 0) return;
     192
     193      experimentStarted = false;
     194      experimentStopped = false;
     195      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started))
     196        optimizer.Pause();
    196197    }
    197198    public void Stop() {
    198199      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
    199200        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
    200       stopPending = true;
    201       if (Optimizers.Count > 0) {
    202         foreach (IOptimizer optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused)))
    203           optimizer.Stop();
    204       }
     201      if (Optimizers.Count == 0) return;
     202
     203      experimentStarted = false;
     204      experimentStopped = true;
     205      foreach (IOptimizer optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused)))
     206        optimizer.Stop();
    205207    }
    206208
     
    333335    }
    334336    private void optimizer_Stopped(object sender, EventArgs e) {
    335       if (!stopPending && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) {
     337      if (experimentStopped && Optimizers.All(o => o.ExecutionState == ExecutionState.Stopped || o.ExecutionState == ExecutionState.Prepared)) OnStopped();
     338      else if (Optimizers.All(o => o.ExecutionState == ExecutionState.Stopped)) OnStopped();
     339      else if (Optimizers.Any(o => o.ExecutionState == ExecutionState.Paused) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) OnPaused();
     340      else if (experimentStarted && !experimentStopped && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) {
    336341        Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).Start();
    337       } else {
    338         if (Optimizers.All(x => (x.ExecutionState != ExecutionState.Started) && (x.ExecutionState != ExecutionState.Paused))) {
    339           stopPending = false;
    340           OnStopped();
    341         }
    342342      }
    343343    }
Note: See TracChangeset for help on using the changeset viewer.