Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/04/18 22:33:26 (7 years ago)
Author:
abeham
Message:

#1614:

  • reverted r15603 by reverse merging
  • added expert system plugins from #2457
Location:
branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3/MetaOptimizers
Files:
3 edited

Legend:

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

    r15605 r15721  
    6868      }
    6969    }
    70    
     70
    7171    [Storable]
    7272    private TimeSpan executionTime;
    7373    public TimeSpan ExecutionTime {
    74       get { return executionTime + (Optimizer?.ExecutionTime ?? TimeSpan.Zero); }
    75     }
     74      get {
     75        if ((Optimizer != null) && (Optimizer.ExecutionState != ExecutionState.Stopped))
     76          return executionTime + Optimizer.ExecutionTime;
     77        else
     78          return executionTime;
     79      }
     80      private set {
     81        executionTime = value;
     82        OnExecutionTimeChanged();
     83      }
     84    }
     85
     86    [Storable]
     87    private TimeSpan runsExecutionTime;
    7688
    7789    [Storable]
     
    162174      executionState = ExecutionState.Stopped;
    163175      executionTime = TimeSpan.Zero;
     176      runsExecutionTime = TimeSpan.Zero;
    164177      repetitions = 10;
    165178      repetitionsCounter = 0;
     
    171184      executionState = ExecutionState.Stopped;
    172185      executionTime = TimeSpan.Zero;
     186      runsExecutionTime = TimeSpan.Zero;
    173187      repetitions = 10;
    174188      repetitionsCounter = 0;
     
    179193      executionState = ExecutionState.Stopped;
    180194      executionTime = TimeSpan.Zero;
     195      runsExecutionTime = TimeSpan.Zero;
    181196      repetitions = 10;
    182197      repetitionsCounter = 0;
     
    194209      executionState = original.executionState;
    195210      executionTime = original.executionTime;
     211      runsExecutionTime = original.runsExecutionTime;
    196212      optimizer = cloner.Clone(original.optimizer);
    197213      repetitions = original.repetitions;
     
    218234        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
    219235      if (Optimizer != null) {
    220         executionTime = TimeSpan.Zero;
     236        ExecutionTime = TimeSpan.Zero;
    221237        RepetitionsCounter = 0;
    222238        if (clearRuns) runs.Clear();
     
    282298      if (handler != null) handler(this, EventArgs.Empty);
    283299    }
    284     [Obsolete("Deprecated needs to be removed")]
    285300    public event EventHandler ExecutionTimeChanged;
     301    private void OnExecutionTimeChanged() {
     302      EventHandler handler = ExecutionTimeChanged;
     303      if (handler != null) handler(this, EventArgs.Empty);
     304    }
    286305    public event EventHandler OptimizerChanged;
    287306    private void OnOptimizerChanged() {
     
    335354    private void RegisterOptimizerEvents() {
    336355      optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Optimizer_ExceptionOccurred);
     356      optimizer.ExecutionTimeChanged += new EventHandler(Optimizer_ExecutionTimeChanged);
    337357      optimizer.Paused += new EventHandler(Optimizer_Paused);
    338358      optimizer.Prepared += new EventHandler(Optimizer_Prepared);
     
    345365    private void DeregisterOptimizerEvents() {
    346366      optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Optimizer_ExceptionOccurred);
     367      optimizer.ExecutionTimeChanged -= new EventHandler(Optimizer_ExecutionTimeChanged);
    347368      optimizer.Paused -= new EventHandler(Optimizer_Paused);
    348369      optimizer.Prepared -= new EventHandler(Optimizer_Prepared);
     
    356377      OnExceptionOccurred(e.Value);
    357378    }
     379    private void Optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
     380      OnExecutionTimeChanged();
     381    }
    358382    private void Optimizer_Paused(object sender, EventArgs e) {
    359383      if (ExecutionState == ExecutionState.Started) {
     
    363387    private void Optimizer_Prepared(object sender, EventArgs e) {
    364388      if (batchRunAction == BatchRunAction.Prepare || ExecutionState == ExecutionState.Stopped) {
     389        ExecutionTime = TimeSpan.Zero;
     390        runsExecutionTime = TimeSpan.Zero;
    365391        RepetitionsCounter = 0;
    366392        OnPrepared();
     
    373399    private void Optimizer_Stopped(object sender, EventArgs e) {
    374400      RepetitionsCounter++;
     401      ExecutionTime += runsExecutionTime;
     402      runsExecutionTime = TimeSpan.Zero;
    375403
    376404      if (batchRunAction == BatchRunAction.Stop) OnStopped();
     
    407435    private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    408436      if (Optimizer != null) Optimizer.Runs.RemoveRange(e.OldItems);
    409       executionTime += SumExecutionTimes(e.OldItems);
    410       executionTime += SumExecutionTimes(e.Items);
     437      foreach (IRun run in e.Items) {
     438        IItem item;
     439        run.Results.TryGetValue("Execution Time", out item);
     440        TimeSpanValue executionTime = item as TimeSpanValue;
     441        if (executionTime != null) ExecutionTime += executionTime.Value;
     442      }
    411443    }
    412444    private void Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    413       executionTime += SumExecutionTimes(e.Items);
     445      foreach (IRun run in e.Items) {
     446        IItem item;
     447        run.Results.TryGetValue("Execution Time", out item);
     448        TimeSpanValue executionTime = item as TimeSpanValue;
     449        if (executionTime != null) {
     450          if (Optimizer.ExecutionState == ExecutionState.Started)
     451            runsExecutionTime += executionTime.Value;
     452          else
     453            ExecutionTime += executionTime.Value;
     454        }
     455      }
    414456    }
    415457    private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    416458      if (Optimizer != null) Optimizer.Runs.RemoveRange(e.Items);
    417       executionTime -= SumExecutionTimes(e.Items);
    418     }
    419     private TimeSpan SumExecutionTimes(IEnumerable<IRun> runs) {
    420       var sum = TimeSpan.Zero;
    421       foreach (IRun run in runs) {
    422         IItem item;
    423         run.Results.TryGetValue("Execution Time", out item);
    424         TimeSpanValue exTime = item as TimeSpanValue;
    425         if (exTime != null) sum += exTime.Value;
    426       }
    427       return sum;
    428459    }
    429460    #endregion
  • branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs

    r15605 r15721  
    7070    private TimeSpan executionTime;
    7171    public TimeSpan ExecutionTime {
    72       get { return executionTime + Optimizers.Aggregate(TimeSpan.Zero, (a, x) => a + x.ExecutionTime); }
     72      get { return executionTime; }
     73      private set {
     74        executionTime = value;
     75        OnExecutionTimeChanged();
     76      }
    7377    }
    7478
     
    278282      if (handler != null) handler(this, EventArgs.Empty);
    279283    }
    280     [Obsolete("deprecated needs to be removed")]
    281284    public event EventHandler ExecutionTimeChanged;
     285    private void OnExecutionTimeChanged() {
     286      EventHandler handler = ExecutionTimeChanged;
     287      if (handler != null) handler(this, EventArgs.Empty);
     288    }
    282289    public event EventHandler Prepared;
    283290    private void OnPrepared() {
     
    362369    private void RegisterOptimizerEvents(IOptimizer optimizer) {
    363370      optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
     371      optimizer.ExecutionTimeChanged += new EventHandler(optimizer_ExecutionTimeChanged);
    364372      optimizer.Paused += new EventHandler(optimizer_Paused);
    365373      optimizer.Prepared += new EventHandler(optimizer_Prepared);
     
    372380    private void DeregisterOptimizerEvents(IOptimizer optimizer) {
    373381      optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
     382      optimizer.ExecutionTimeChanged -= new EventHandler(optimizer_ExecutionTimeChanged);
    374383      optimizer.Paused -= new EventHandler(optimizer_Paused);
    375384      optimizer.Prepared -= new EventHandler(optimizer_Prepared);
     
    386395      lock (locker)
    387396        OnExceptionOccurred(e.Value);
     397    }
     398    private void optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
     399      // only wait for maximally 100ms to acquire lock, otherwise return and don't update the execution time
     400      var success = Monitor.TryEnter(locker, 100);
     401      if (!success) return;
     402      try {
     403        ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime);
     404      } finally {
     405        Monitor.Exit(locker);
     406      }
    388407    }
    389408    private void optimizer_Paused(object sender, EventArgs e) {
  • branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3/MetaOptimizers/TimeLimitRun.cs

    r15605 r15721  
    214214
    215215    private void Initialize() {
    216       timer = new System.Timers.Timer();
    217       timer.Interval = 50;
    218       timer.Elapsed += Timer_Elapsed;
    219216      if (algorithm != null) RegisterAlgorithmEvents();
    220217      snapshotTimes.ItemsAdded += snapshotTimes_Changed;
     
    258255    }
    259256
    260     private System.Timers.Timer timer;
    261 
    262     private void Timer_Elapsed(object sender, EventArgs e) {
    263       if (Algorithm.ExecutionState == ExecutionState.Started) {
    264         if (snapshotTimesIndex < SnapshotTimes.Count && ExecutionTime >= SnapshotTimes[snapshotTimesIndex]
    265           && !pausedForSnapshot) {
    266           pausedForSnapshot = true;
    267           Algorithm.Pause();
    268         }
    269         if (ExecutionTime >= MaximumExecutionTime && !pausedForTermination) {
    270           pausedForTermination = true;
    271           if (!pausedForSnapshot) Algorithm.Pause();
    272         }
    273       }
    274     }
    275 
    276257    #region Events
    277258    protected override void OnNameChanged() {
     
    292273      if (handler != null) handler(this, EventArgs.Empty);
    293274    }
    294     [Obsolete("Deprecated needs to be removed")]
    295275    public event EventHandler ExecutionTimeChanged;
     276    private void OnExecutionTimeChanged() {
     277      var handler = ExecutionTimeChanged;
     278      if (handler != null) handler(this, EventArgs.Empty);
     279    }
    296280    public event EventHandler Prepared;
    297281    private void OnPrepared() {
     
    301285    public event EventHandler Started;
    302286    private void OnStarted() {
    303       timer.Start();
    304287      var handler = Started;
    305288      if (handler != null) handler(this, EventArgs.Empty);
     
    307290    public event EventHandler Paused;
    308291    private void OnPaused() {
    309       timer.Stop();
    310292      var handler = Paused;
    311293      if (handler != null) handler(this, EventArgs.Empty);
     
    313295    public event EventHandler Stopped;
    314296    private void OnStopped() {
    315       timer.Stop();
    316297      var handler = Stopped;
    317298      if (handler != null) handler(this, EventArgs.Empty);
     
    327308    private void RegisterAlgorithmEvents() {
    328309      algorithm.ExceptionOccurred += Algorithm_ExceptionOccurred;
     310      algorithm.ExecutionTimeChanged += Algorithm_ExecutionTimeChanged;
    329311      algorithm.ExecutionStateChanged += Algorithm_ExecutionStateChanged;
    330312      algorithm.Paused += Algorithm_Paused;
     
    335317    private void DeregisterAlgorithmEvents() {
    336318      algorithm.ExceptionOccurred -= Algorithm_ExceptionOccurred;
     319      algorithm.ExecutionTimeChanged -= Algorithm_ExecutionTimeChanged;
    337320      algorithm.ExecutionStateChanged -= Algorithm_ExecutionStateChanged;
    338321      algorithm.Paused -= Algorithm_Paused;
     
    343326    private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
    344327      OnExceptionOccurred(e.Value);
     328    }
     329    private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
     330      if (snapshotTimesIndex < SnapshotTimes.Count && ExecutionTime >= SnapshotTimes[snapshotTimesIndex]
     331        && !pausedForSnapshot) {
     332        pausedForSnapshot = true;
     333        Algorithm.Pause();
     334      }
     335      if (ExecutionTime >= MaximumExecutionTime && !pausedForTermination) {
     336        pausedForTermination = true;
     337        if (!pausedForSnapshot) Algorithm.Pause();
     338      }
     339      OnExecutionTimeChanged();
    345340    }
    346341    private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
Note: See TracChangeset for help on using the changeset viewer.