Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/31/19 14:40:15 (5 years ago)
Author:
abeham
Message:

#1614: updated to new persistence and .NET 4.6.1

Location:
branches/1614_GeneralizedQAP/HeuristicLab.Optimization
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/1614_GeneralizedQAP/HeuristicLab.Optimization

  • branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3/MetaOptimizers/BatchRun.cs

    r15721 r16728  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2929using HeuristicLab.Core;
    3030using HeuristicLab.Data;
    31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     31using HEAL.Attic;
    3232
    3333namespace HeuristicLab.Optimization {
     34  [StorableType("3d385be2-5c83-43f2-a693-f984acac44f6")]
    3435  internal enum BatchRunAction { None, Prepare, Start, Pause, Stop };
    3536
     
    3940  [Item("Batch Run", "A run in which an optimizer is executed a given number of times.")]
    4041  [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 110)]
    41   [StorableClass]
     42  [StorableType("E85407E0-18EC-4198-8321-9CF030FDF6D7")]
    4243  public sealed class BatchRun : NamedItem, IOptimizer, IStorableContent {
    4344    public string Filename { get; set; }
     
    199200    }
    200201    [StorableConstructor]
    201     private BatchRun(bool deserializing) : base(deserializing) { }
     202    private BatchRun(StorableConstructorFlag _) : base(_) { }
    202203    [StorableHook(HookType.AfterDeserialization)]
    203204    private void AfterDeserialization() {
  • branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs

    r15721 r16728  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2929using HeuristicLab.Common;
    3030using HeuristicLab.Core;
    31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     31using HEAL.Attic;
    3232
    3333namespace HeuristicLab.Optimization {
     
    3737  [Item("Experiment", "An experiment which contains multiple algorithms, batch runs or other experiments.")]
    3838  [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 100)]
    39   [StorableClass]
     39  [StorableType("A8A4536B-54C1-4A17-AB58-A6006F7F394B")]
    4040  public sealed class Experiment : NamedItem, IOptimizer, IStorableContent {
    4141    public string Filename { get; set; }
     
    161161    }
    162162    [StorableConstructor]
    163     private Experiment(bool deserializing) : base(deserializing) { }
     163    private Experiment(StorableConstructorFlag _) : base(_) { }
    164164    [StorableHook(HookType.AfterDeserialization)]
    165165    private void AfterDeserialization() {
  • branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3/MetaOptimizers/TimeLimitRun.cs

    r15721 r16728  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2727using System.Threading;
    2828using System.Threading.Tasks;
     29using HEAL.Attic;
    2930using HeuristicLab.Collections;
    3031using HeuristicLab.Common;
    3132using HeuristicLab.Common.Resources;
    3233using HeuristicLab.Core;
    33 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3434
    3535namespace HeuristicLab.Optimization {
     
    3939  [Item("Timelimit Run", "A run in which an optimizer is executed a certain maximum time.")]
    4040  [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 115)]
    41   [StorableClass]
     41  [StorableType("85A1AB82-689F-4925-B888-B3886707BE88")]
    4242  public sealed class TimeLimitRun : NamedItem, IOptimizer, IStorableContent, INotifyPropertyChanged {
    4343    public string Filename { get; set; }
     
    6262        if (maximumExecutionTime == value) return;
    6363        maximumExecutionTime = value;
    64         OnPropertyChanged("MaximumExecutionTime");
     64        OnPropertyChanged(nameof(MaximumExecutionTime));
    6565      }
    6666    }
     
    7777        snapshotTimes.Sort();
    7878        FindNextSnapshotTimeIndex(ExecutionTime);
    79         OnPropertyChanged("SnapshotTimes");
     79        OnPropertyChanged(nameof(SnapshotTimes));
    8080      }
    8181    }
     
    8989        if (storeAlgorithmInEachSnapshot == value) return;
    9090        storeAlgorithmInEachSnapshot = value;
    91         OnPropertyChanged("StoreAlgorithmInEachSnapshot");
     91        OnPropertyChanged(nameof(StoreAlgorithmInEachSnapshot));
    9292      }
    9393    }
     
    100100        if (snapshots == value) return;
    101101        snapshots = value;
    102         OnPropertyChanged("Snapshots");
     102        OnPropertyChanged(nameof(Snapshots));
    103103      }
    104104    }
    105105
    106106    #region Inherited Properties
     107    [Storable]
     108    private ExecutionState executionState;
    107109    public ExecutionState ExecutionState {
    108       get { return (Algorithm != null) ? Algorithm.ExecutionState : ExecutionState.Stopped; }
     110      get { return executionState; }
     111      private set {
     112        if (executionState == value) return;
     113        executionState = value;
     114        OnExecutionStateChanged();
     115        OnPropertyChanged(nameof(ExecutionState));
     116      }
    109117    }
    110118
     
    119127      set {
    120128        if (algorithm == value) return;
     129        if (ExecutionState == ExecutionState.Started || ExecutionState == ExecutionState.Paused)
     130          throw new InvalidOperationException("Cannot change algorithm while the TimeLimitRun is running or paused.");
    121131        if (algorithm != null) DeregisterAlgorithmEvents();
    122132        algorithm = value;
     133        if (algorithm != null) RegisterAlgorithmEvents();
     134        OnPropertyChanged(nameof(Algorithm));
     135        OnPropertyChanged(nameof(NestedOptimizers));
    123136        if (algorithm != null) {
    124           RegisterAlgorithmEvents();
    125         }
    126         OnPropertyChanged("Algorithm");
    127         Prepare();
     137          if (algorithm.ExecutionState == ExecutionState.Started || algorithm.ExecutionState == ExecutionState.Paused)
     138            throw new InvalidOperationException("Cannot assign a running or paused algorithm to a TimeLimitRun.");
     139          Prepare();
     140          if (algorithm.ExecutionState == ExecutionState.Stopped) OnStopped();
     141        } else OnStopped();
    128142      }
    129143    }
     
    137151        if (runs == value) return;
    138152        runs = value;
    139         OnPropertyChanged("Runs");
     153        OnPropertyChanged(nameof(Runs));
    140154      }
    141155    }
     
    152166
    153167    [StorableConstructor]
    154     private TimeLimitRun(bool deserializing) : base(deserializing) { }
     168    private TimeLimitRun(StorableConstructorFlag _) : base(_) { }
    155169    private TimeLimitRun(TimeLimitRun original, Cloner cloner)
    156170      : base(original, cloner) {
     
    160174      snapshots = cloner.Clone(original.snapshots);
    161175      storeAlgorithmInEachSnapshot = original.storeAlgorithmInEachSnapshot;
     176      executionState = original.executionState;
    162177      algorithm = cloner.Clone(original.algorithm);
    163178      runs = cloner.Clone(original.runs);
     
    169184      name = ItemName;
    170185      description = ItemDescription;
     186      executionState = ExecutionState.Stopped;
    171187      maximumExecutionTime = TimeSpan.FromMinutes(.5);
    172188      snapshotTimes = new ObservableList<TimeSpan>(new[] {
     
    182198      : base(name) {
    183199      description = ItemDescription;
     200      executionState = ExecutionState.Stopped;
    184201      maximumExecutionTime = TimeSpan.FromMinutes(.5);
    185202      snapshotTimes = new ObservableList<TimeSpan>(new[] {
     
    188205          TimeSpan.FromSeconds(15) });
    189206      snapshotTimesIndex = 0;
     207      snapshots = new RunCollection();
    190208      Runs = new RunCollection { OptimizerName = Name };
    191209      Initialize();
     
    193211    public TimeLimitRun(string name, string description)
    194212      : base(name, description) {
     213      executionState = ExecutionState.Stopped;
    195214      maximumExecutionTime = TimeSpan.FromMinutes(.5);
    196215      snapshotTimes = new ObservableList<TimeSpan>(new[] {
     
    199218          TimeSpan.FromSeconds(15) });
    200219      snapshotTimesIndex = 0;
     220      snapshots = new RunCollection();
    201221      Runs = new RunCollection { OptimizerName = Name };
    202222      Initialize();
     
    211231    private void AfterDeserialization() {
    212232      Initialize();
     233      // BackwardsCompatibility3.3
     234      #region Backwards compatible code, remove with 3.4
     235      if (Algorithm != null && executionState != Algorithm.ExecutionState) {
     236        executionState = Algorithm.ExecutionState;
     237      } else if (Algorithm == null) executionState = ExecutionState.Stopped;
     238      #endregion
    213239    }
    214240
     
    236262    }
    237263    public void Prepare(bool clearRuns) {
     264      if (Algorithm == null) return;
    238265      Algorithm.Prepare(clearRuns);
    239266    }
     
    280307    public event EventHandler Prepared;
    281308    private void OnPrepared() {
     309      ExecutionState = ExecutionState.Prepared;
    282310      var handler = Prepared;
    283311      if (handler != null) handler(this, EventArgs.Empty);
     
    285313    public event EventHandler Started;
    286314    private void OnStarted() {
     315      ExecutionState = ExecutionState.Started;
    287316      var handler = Started;
    288317      if (handler != null) handler(this, EventArgs.Empty);
     
    290319    public event EventHandler Paused;
    291320    private void OnPaused() {
     321      ExecutionState = ExecutionState.Paused;
    292322      var handler = Paused;
    293323      if (handler != null) handler(this, EventArgs.Empty);
     
    295325    public event EventHandler Stopped;
    296326    private void OnStopped() {
     327      ExecutionState = ExecutionState.Stopped;
    297328      var handler = Stopped;
    298329      if (handler != null) handler(this, EventArgs.Empty);
     
    300331    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
    301332    private void OnExceptionOccurred(Exception exception) {
     333      ExecutionState = ExecutionState.Paused;
    302334      var handler = ExceptionOccurred;
    303335      if (handler != null) handler(this, new EventArgs<Exception>(exception));
     
    309341      algorithm.ExceptionOccurred += Algorithm_ExceptionOccurred;
    310342      algorithm.ExecutionTimeChanged += Algorithm_ExecutionTimeChanged;
    311       algorithm.ExecutionStateChanged += Algorithm_ExecutionStateChanged;
    312343      algorithm.Paused += Algorithm_Paused;
    313344      algorithm.Prepared += Algorithm_Prepared;
     
    318349      algorithm.ExceptionOccurred -= Algorithm_ExceptionOccurred;
    319350      algorithm.ExecutionTimeChanged -= Algorithm_ExecutionTimeChanged;
    320       algorithm.ExecutionStateChanged -= Algorithm_ExecutionStateChanged;
    321351      algorithm.Paused -= Algorithm_Paused;
    322352      algorithm.Prepared -= Algorithm_Prepared;
     
    338368      }
    339369      OnExecutionTimeChanged();
    340     }
    341     private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
    342       OnExecutionStateChanged();
    343370    }
    344371    private void Algorithm_Paused(object sender, EventArgs e) {
     
    348375        MakeSnapshot();
    349376        FindNextSnapshotTimeIndex(ExecutionTime);
    350       }
    351       OnPaused();
     377      } else OnPaused();
    352378      if (action == ExecutionState.Started) Algorithm.Start();
    353379      else if (action == ExecutionState.Stopped) Algorithm.Stop();
     
    359385    }
    360386    private void Algorithm_Started(object sender, EventArgs e) {
    361       OnStarted();
     387      if (ExecutionState == ExecutionState.Prepared || ExecutionState == ExecutionState.Paused)
     388        OnStarted();
     389      if (ExecutionState == ExecutionState.Stopped)
     390        throw new InvalidOperationException("Algorithm was started although TimeLimitRun was in state Stopped.");
     391      // otherwise the algorithm was just started after being snapshotted
    362392    }
    363393    private void Algorithm_Stopped(object sender, EventArgs e) {
    364       var cloner = new Cloner();
    365       var algRun = cloner.Clone(Algorithm.Runs.Last());
    366       var clonedSnapshots = cloner.Clone(snapshots);
    367       algRun.Results.Add("TimeLimitRunSnapshots", clonedSnapshots);
    368       Runs.Add(algRun);
    369       Algorithm.Runs.Clear();
    370       OnStopped();
     394      try {
     395        var cloner = new Cloner();
     396        var algRun = cloner.Clone(Algorithm.Runs.Last());
     397        var clonedSnapshots = cloner.Clone(snapshots);
     398        algRun.Results.Add("TimeLimitRunSnapshots", clonedSnapshots);
     399        Runs.Add(algRun);
     400        Algorithm.Runs.Clear();
     401      } finally { OnStopped(); }
    371402    }
    372403    #endregion
     
    377408      while (index < snapshotTimes.Count && snapshotTimes[index] <= reference) {
    378409        index++;
    379       };
     410      }
    380411      snapshotTimesIndex = index;
    381412    }
Note: See TracChangeset for help on using the changeset viewer.