Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/03/12 16:46:35 (12 years ago)
Author:
gkronber
Message:

#1847: merged r8084:8205 from trunk into GP move operators branch

Location:
branches/GP-MoveOperators
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/GP-MoveOperators

  • branches/GP-MoveOperators/HeuristicLab.Optimization/3.3/Algorithms/Algorithm.cs

    r7706 r8206  
    272272    public event EventHandler Prepared;
    273273    protected virtual void OnPrepared() {
    274       ExecutionState = ExecutionState.Prepared;
    275274      ExecutionTime = TimeSpan.Zero;
    276275      foreach (IStatefulItem statefulObject in this.GetObjectGraphObjects(new HashSet<string>() { "runs" }).OfType<IStatefulItem>()) {
    277276        statefulObject.InitializeState();
    278277      }
     278      ExecutionState = ExecutionState.Prepared;
    279279      EventHandler handler = Prepared;
    280280      if (handler != null) handler(this, EventArgs.Empty);
     
    294294    public event EventHandler Stopped;
    295295    protected virtual void OnStopped() {
    296       ExecutionState = ExecutionState.Stopped;
    297296      foreach (IStatefulItem statefulObject in this.GetObjectGraphObjects(new HashSet<string>() { "runs" }).OfType<IStatefulItem>()) {
    298297        statefulObject.ClearState();
     
    300299      runsCounter++;
    301300      runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this));
     301      ExecutionState = ExecutionState.Stopped;
    302302      EventHandler handler = Stopped;
    303303      if (handler != null) handler(this, EventArgs.Empty);
  • branches/GP-MoveOperators/HeuristicLab.Optimization/3.3/BatchRun.cs

    r7576 r8206  
    3030
    3131namespace HeuristicLab.Optimization {
     32  internal enum BatchRunAction { None, Prepare, Start, Pause, Stop };
     33
    3234  /// <summary>
    3335  /// A run in which an optimizer is executed a given number of times.
     
    7981      }
    8082    }
     83
     84    [Storable]
     85    private TimeSpan runsExecutionTime;
    8186
    8287    [Storable]
     
    150155    }
    151156
    152     private bool batchRunStarted = false;
    153     private bool batchRunPaused = false;
    154     private bool batchRunStopped = false;
     157    private BatchRunAction batchRunAction = BatchRunAction.None;
    155158
    156159    public BatchRun()
     
    160163      executionState = ExecutionState.Stopped;
    161164      executionTime = TimeSpan.Zero;
     165      runsExecutionTime = TimeSpan.Zero;
    162166      repetitions = 10;
    163167      repetitionsCounter = 0;
     
    169173      executionState = ExecutionState.Stopped;
    170174      executionTime = TimeSpan.Zero;
     175      runsExecutionTime = TimeSpan.Zero;
    171176      repetitions = 10;
    172177      repetitionsCounter = 0;
     
    177182      executionState = ExecutionState.Stopped;
    178183      executionTime = TimeSpan.Zero;
     184      runsExecutionTime = TimeSpan.Zero;
    179185      repetitions = 10;
    180186      repetitionsCounter = 0;
     
    192198      executionState = original.executionState;
    193199      executionTime = original.executionTime;
     200      runsExecutionTime = original.runsExecutionTime;
    194201      optimizer = cloner.Clone(original.optimizer);
    195202      repetitions = original.repetitions;
    196203      repetitionsCounter = original.repetitionsCounter;
    197204      runs = cloner.Clone(original.runs);
    198       batchRunStarted = original.batchRunStarted;
    199       batchRunPaused = original.batchRunPaused;
    200       batchRunStopped = original.batchRunStopped;
     205      batchRunAction = original.batchRunAction;
    201206      Initialize();
    202207    }
     
    218223        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
    219224      if (Optimizer != null) {
     225        ExecutionTime = TimeSpan.Zero;
    220226        repetitionsCounter = 0;
    221227        if (clearRuns) runs.Clear();
    222         Optimizer.Prepare(clearRuns);
     228        batchRunAction = BatchRunAction.Prepare;
     229        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     230        try { Optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { }
    223231      } else {
    224232        ExecutionState = ExecutionState.Stopped;
     
    229237        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
    230238      if (Optimizer == null) return;
    231 
    232       batchRunStarted = true;
    233       batchRunPaused = false;
    234       batchRunStopped = false;
     239      batchRunAction = BatchRunAction.Start;
    235240      if (Optimizer.ExecutionState == ExecutionState.Stopped) Optimizer.Prepare();
    236       Optimizer.Start();
     241      // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     242      try { Optimizer.Start(); } catch (InvalidOperationException) { }
    237243    }
    238244    public void Pause() {
     
    240246        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
    241247      if (Optimizer == null) return;
     248      batchRunAction = BatchRunAction.Pause;
    242249      if (Optimizer.ExecutionState != ExecutionState.Started) return;
    243 
    244       batchRunStarted = false;
    245       batchRunPaused = true;
    246       batchRunStopped = false;
    247       Optimizer.Pause();
     250      // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     251      try { Optimizer.Pause(); } catch (InvalidOperationException) { }
    248252    }
    249253    public void Stop() {
     
    251255        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
    252256      if (Optimizer == null) return;
    253       if (Optimizer.ExecutionState != ExecutionState.Started && Optimizer.ExecutionState != ExecutionState.Paused) return;
    254       batchRunStarted = false;
    255       batchRunPaused = false;
    256       batchRunStopped = true;
    257       Optimizer.Stop();
     257      batchRunAction = BatchRunAction.Stop;
     258      if (Optimizer.ExecutionState != ExecutionState.Started && Optimizer.ExecutionState != ExecutionState.Paused) {
     259        OnStopped();
     260        return;
     261      }
     262      // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     263      try { Optimizer.Stop(); } catch (InvalidOperationException) { }
    258264    }
    259265
     
    281287    public event EventHandler Prepared;
    282288    private void OnPrepared() {
     289      batchRunAction = BatchRunAction.None;
    283290      ExecutionState = ExecutionState.Prepared;
    284291      EventHandler handler = Prepared;
     
    287294    public event EventHandler Started;
    288295    private void OnStarted() {
     296      // no reset of BatchRunAction.Started, because we need to differ which of the two was started by the user
    289297      ExecutionState = ExecutionState.Started;
    290298      EventHandler handler = Started;
     
    293301    public event EventHandler Paused;
    294302    private void OnPaused() {
     303      batchRunAction = BatchRunAction.None;
    295304      ExecutionState = ExecutionState.Paused;
    296305      EventHandler handler = Paused;
     
    299308    public event EventHandler Stopped;
    300309    private void OnStopped() {
     310      batchRunAction = BatchRunAction.None;
    301311      ExecutionState = ExecutionState.Stopped;
    302312      EventHandler handler = Stopped;
     
    343353    }
    344354    private void Optimizer_Prepared(object sender, EventArgs e) {
    345       if (ExecutionState == ExecutionState.Stopped || !batchRunStarted) {
     355      if (batchRunAction == BatchRunAction.Prepare || ExecutionState == ExecutionState.Stopped) {
     356        ExecutionTime = TimeSpan.Zero;
     357        runsExecutionTime = TimeSpan.Zero;
     358        repetitionsCounter = 0;
    346359        OnPrepared();
    347360      }
     
    353366    private void Optimizer_Stopped(object sender, EventArgs e) {
    354367      repetitionsCounter++;
    355 
    356       if (batchRunStopped) OnStopped();
     368      ExecutionTime += runsExecutionTime;
     369      runsExecutionTime = TimeSpan.Zero;
     370
     371      if (batchRunAction == BatchRunAction.Stop) OnStopped();
    357372      else if (repetitionsCounter >= repetitions) OnStopped();
    358       else if (batchRunPaused) OnPaused();
    359       else if (batchRunStarted) {
     373      else if (batchRunAction == BatchRunAction.Pause) OnPaused();
     374      else if (batchRunAction == BatchRunAction.Start) {
    360375        Optimizer.Prepare();
    361376        Optimizer.Start();
     377      } else if (executionState == ExecutionState.Started) {
     378        // if the batch run hasn't been started but the inner optimizer was run then pause
     379        OnPaused();
    362380      } else OnStopped();
    363381    }
     
    385403    }
    386404    private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    387       foreach (IRun run in e.OldItems) {
    388         IItem item;
    389         run.Results.TryGetValue("Execution Time", out item);
    390         TimeSpanValue executionTime = item as TimeSpanValue;
    391         if (executionTime != null) ExecutionTime -= executionTime.Value;
    392       }
    393405      if (Optimizer != null) Optimizer.Runs.RemoveRange(e.OldItems);
    394406      foreach (IRun run in e.Items) {
     
    404416        run.Results.TryGetValue("Execution Time", out item);
    405417        TimeSpanValue executionTime = item as TimeSpanValue;
    406         if (executionTime != null) ExecutionTime += executionTime.Value;
     418        if (executionTime != null) {
     419          if (Optimizer.ExecutionState == ExecutionState.Started)
     420            runsExecutionTime += executionTime.Value;
     421          else
     422            ExecutionTime += executionTime.Value;
     423        }
    407424      }
    408425    }
    409426    private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    410       foreach (IRun run in e.Items) {
    411         IItem item;
    412         run.Results.TryGetValue("Execution Time", out item);
    413         TimeSpanValue executionTime = item as TimeSpanValue;
    414         if (executionTime != null) ExecutionTime -= executionTime.Value;
    415       }
    416427      if (Optimizer != null) Optimizer.Runs.RemoveRange(e.Items);
    417428    }
  • branches/GP-MoveOperators/HeuristicLab.Optimization/3.3/Experiment.cs

    r7797 r8206  
    178178      experimentStarted = false;
    179179      experimentStopped = false;
    180       foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started))
    181         //if (clearRuns || optimizer.ExecutionState != ExecutionState.Prepared)
    182           optimizer.Prepare(clearRuns);
     180      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started)) {
     181        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     182        try { optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { }
     183      }
    183184    }
    184185    public void Start() {
     
    190191      experimentStopped = false;
    191192      IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
    192       if (optimizer != null) optimizer.Start();
     193      if (optimizer != null) {
     194        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     195        try { optimizer.Start(); } catch (InvalidOperationException) { }
     196      }
    193197    }
    194198    public void Pause() {
     
    199203      experimentStarted = false;
    200204      experimentStopped = false;
    201       foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started))
    202         optimizer.Pause();
     205      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) {
     206        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     207        try { optimizer.Pause(); } catch (InvalidOperationException) { }
     208      }
    203209    }
    204210    public void Stop() {
     
    210216      experimentStopped = true;
    211217      if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) {
    212         foreach (IOptimizer optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused)))
    213           optimizer.Stop();
     218        foreach (var optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) {
     219          // a race-condition may occur when the optimizer has changed the state by itself in the meantime
     220          try { optimizer.Stop(); } catch (InvalidOperationException) { }
     221        }
    214222      } else {
    215223        OnStopped();
     
    326334      optimizer.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsRemoved);
    327335    }
     336
     337    private readonly object locker = new object();
    328338    private void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
    329       OnExceptionOccurred(e.Value);
     339      lock (locker)
     340        OnExceptionOccurred(e.Value);
    330341    }
    331342    private void optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
    332       ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime);
     343      lock (locker)
     344        ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime);
    333345    }
    334346    private void optimizer_Paused(object sender, EventArgs e) {
    335       if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) OnPaused();
     347      lock (locker)
     348        if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) OnPaused();
    336349    }
    337350    private void optimizer_Prepared(object sender, EventArgs e) {
    338       if (Optimizers.All(x => x.ExecutionState == ExecutionState.Prepared)) OnPrepared();
     351      lock (locker)
     352        if (Optimizers.All(x => x.ExecutionState == ExecutionState.Prepared)) OnPrepared();
    339353    }
    340354    private void optimizer_Started(object sender, EventArgs e) {
    341       if (ExecutionState != ExecutionState.Started) OnStarted();
     355      lock (locker)
     356        if (ExecutionState != ExecutionState.Started) OnStarted();
    342357    }
    343358    private void optimizer_Stopped(object sender, EventArgs e) {
    344       if (experimentStopped) {
    345         if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) OnStopped();
    346       } else {
    347         if (experimentStarted && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) {
    348           Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).Start();
    349         } else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) OnStopped();
    350         else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) OnPaused();
     359      lock (locker) {
     360        if (experimentStopped) {
     361          if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) OnStopped();
     362        } else {
     363          if (experimentStarted && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) {
     364            Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).Start();
     365          } else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) OnStopped();
     366          else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) OnPaused();
     367        }
    351368      }
    352369    }
    353370    private void optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    354       Runs.RemoveRange(e.OldItems);
    355       Runs.AddRange(e.Items);
     371      lock (locker) {
     372        Runs.RemoveRange(e.OldItems);
     373        Runs.AddRange(e.Items);
     374      }
    356375    }
    357376    private void optimizer_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    358       Runs.AddRange(e.Items);
     377      lock (locker)
     378        Runs.AddRange(e.Items);
    359379    }
    360380    private void optimizer_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    361       Runs.RemoveRange(e.Items);
     381      lock (locker)
     382        Runs.RemoveRange(e.Items);
    362383    }
    363384
Note: See TracChangeset for help on using the changeset viewer.