Changeset 15603
- Timestamp:
- 01/12/18 13:33:23 (6 years ago)
- Location:
- branches/GeneralizedQAP
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Optimization.Views/3.3/IOptimizerView.Designer.cs
r14185 r15603 45 45 /// </summary> 46 46 private void InitializeComponent() { 47 this.components = new System.ComponentModel.Container(); 47 48 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(IOptimizerView)); 48 49 this.startButton = new System.Windows.Forms.Button(); … … 52 53 this.stopButton = new System.Windows.Forms.Button(); 53 54 this.resetButton = new System.Windows.Forms.Button(); 55 this.executionTimer = new System.Windows.Forms.Timer(this.components); 54 56 ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).BeginInit(); 55 57 this.SuspendLayout(); … … 132 134 this.resetButton.Click += new System.EventHandler(this.resetButton_Click); 133 135 // 136 // executionTimer 137 // 138 this.executionTimer.Interval = 200; 139 this.executionTimer.Tick += new System.EventHandler(this.executionTimer_Tick); 140 // 134 141 // IOptimizerView 135 142 // … … 166 173 protected System.Windows.Forms.Button pauseButton; 167 174 protected System.Windows.Forms.Button stopButton; 175 protected System.Windows.Forms.Timer executionTimer; 168 176 protected System.Windows.Forms.Button resetButton; 169 177 -
branches/GeneralizedQAP/HeuristicLab.Optimization.Views/3.3/IOptimizerView.cs
r15367 r15603 44 44 Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred); 45 45 Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged); 46 Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);47 46 Content.Prepared -= new EventHandler(Content_Prepared); 48 47 Content.Started -= new EventHandler(Content_Started); … … 55 54 Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred); 56 55 Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged); 57 Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);58 56 Content.Prepared += new EventHandler(Content_Prepared); 59 57 Content.Started += new EventHandler(Content_Started); … … 64 62 protected override void OnContentChanged() { 65 63 base.OnContentChanged(); 66 if (Content == null) { 67 executionTimeTextBox.Text = "-"; 68 } else { 64 if (Content != null) { 69 65 Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started; 70 executionTimeTextBox.Text = Content.ExecutionTime.ToString();71 66 } 67 UpdateExecutionTime(); 72 68 } 73 69 … … 90 86 else { 91 87 nameTextBox.Enabled = infoLabel.Enabled = true; 88 UpdateExecutionTime(); 92 89 ReadOnly = Locked = false; 93 90 SetEnabledStateOfExecutableButtons(); … … 98 95 Invoke(new EventHandler(Content_Started), sender, e); 99 96 else { 97 executionTimer.Start(); 100 98 nameTextBox.Enabled = infoLabel.Enabled = false; 101 99 ReadOnly = Locked = true; … … 107 105 Invoke(new EventHandler(Content_Paused), sender, e); 108 106 else { 107 executionTimer.Stop(); 108 UpdateExecutionTime(); 109 109 nameTextBox.Enabled = infoLabel.Enabled = true; 110 110 ReadOnly = Locked = false; … … 116 116 Invoke(new EventHandler(Content_Stopped), sender, e); 117 117 else { 118 executionTimer.Stop(); 119 UpdateExecutionTime(); 118 120 nameTextBox.Enabled = infoLabel.Enabled = true; 119 121 ReadOnly = Locked = false; … … 121 123 } 122 124 } 123 protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {124 if (InvokeRequired)125 Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);126 else127 executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();128 }129 125 protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) { 130 126 if (InvokeRequired) 131 127 Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e); 132 else 128 else { 129 executionTimer.Stop(); 130 UpdateExecutionTime(); 133 131 ErrorHandling.ShowErrorDialog(this, e.Value); 132 } 134 133 } 135 134 #endregion … … 147 146 protected virtual void resetButton_Click(object sender, EventArgs e) { 148 147 Content.Prepare(false); 148 } 149 private void executionTimer_Tick(object sender, EventArgs e) { 150 UpdateExecutionTime(); 151 } 152 153 private void UpdateExecutionTime() { 154 executionTimeTextBox.Text = (Content == null ? "-" : Content.ExecutionTime.ToString()); 149 155 } 150 156 #endregion -
branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/Algorithm.cs
r15287 r15603 64 64 } 65 65 } 66 67 [Storable] 68 private TimeSpan executionTime; 69 public TimeSpan ExecutionTime { 70 get { return executionTime; } 71 protected set { 72 executionTime = value; 73 OnExecutionTimeChanged(); 74 } 75 } 66 67 public abstract TimeSpan ExecutionTime { get; } 76 68 77 69 public virtual Type ProblemType { … … 133 125 : base() { 134 126 executionState = ExecutionState.Stopped; 135 executionTime = TimeSpan.Zero;136 127 storeAlgorithmInEachRun = false; 137 128 runsCounter = 0; … … 141 132 : base(name) { 142 133 executionState = ExecutionState.Stopped; 143 executionTime = TimeSpan.Zero;144 134 storeAlgorithmInEachRun = false; 145 135 runsCounter = 0; … … 149 139 : base(name, parameters) { 150 140 executionState = ExecutionState.Stopped; 151 executionTime = TimeSpan.Zero;152 141 storeAlgorithmInEachRun = false; 153 142 runsCounter = 0; … … 157 146 : base(name, description) { 158 147 executionState = ExecutionState.Stopped; 159 executionTime = TimeSpan.Zero;160 148 storeAlgorithmInEachRun = false; 161 149 runsCounter = 0; … … 165 153 : base(name, description, parameters) { 166 154 executionState = ExecutionState.Stopped; 167 executionTime = TimeSpan.Zero;168 155 storeAlgorithmInEachRun = false; 169 156 runsCounter = 0; … … 181 168 if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState)); 182 169 executionState = original.executionState; 183 executionTime = original.executionTime;184 170 problem = cloner.Clone(original.problem); 185 171 storeAlgorithmInEachRun = original.storeAlgorithmInEachRun; … … 259 245 if (handler != null) handler(this, EventArgs.Empty); 260 246 } 261 public event EventHandler ExecutionTimeChanged;262 protected virtual void OnExecutionTimeChanged() {263 EventHandler handler = ExecutionTimeChanged;264 if (handler != null) handler(this, EventArgs.Empty);265 }266 247 public event EventHandler ProblemChanged; 267 248 protected virtual void OnProblemChanged() { … … 276 257 public event EventHandler Prepared; 277 258 protected virtual void OnPrepared() { 278 ExecutionTime = TimeSpan.Zero;279 259 foreach (IStatefulItem statefulObject in this.GetObjectGraphObjects(new HashSet<object>() { Runs }).OfType<IStatefulItem>()) { 280 260 statefulObject.InitializeState(); … … 347 327 } 348 328 #endregion 329 330 [Obsolete("Deprecate, does nothing, needs to be removed from IExecutable")] 331 public event EventHandler ExecutionTimeChanged; 349 332 } 350 333 } -
branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
r15367 r15603 21 21 22 22 using System; 23 using System.Linq;24 23 using System.Threading; 25 24 using HeuristicLab.Common; … … 32 31 33 32 private bool pausePending; 34 private DateTime lastUpdateTime;33 private bool executionTimeActive; 35 34 36 35 public string Filename { get; set; } … … 52 51 } 53 52 53 [Storable] 54 private TimeSpan executionTime; 55 private DateTime startDate; 56 public override TimeSpan ExecutionTime { 57 get { return executionTime + (executionTimeActive ? DateTime.UtcNow - startDate : TimeSpan.Zero); } 58 } 59 54 60 [StorableConstructor] 55 61 protected BasicAlgorithm(bool deserializing) : base(deserializing) { } … … 58 64 results = cloner.Clone(original.Results); 59 65 initialized = original.initialized; 66 executionTime = original.executionTime; 60 67 } 61 68 protected BasicAlgorithm() 62 69 : base() { 63 70 results = new ResultCollection(); 71 executionTime = TimeSpan.Zero; 64 72 } 65 73 66 74 public override void Prepare() { 75 executionTime = TimeSpan.Zero; 67 76 if (Problem == null) return; 68 77 base.Prepare(); … … 73 82 74 83 public override void Start(CancellationToken cancellationToken) { 84 startDate = DateTime.UtcNow; 85 executionTimeActive = true; 75 86 base.Start(cancellationToken); 76 87 CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); … … 117 128 private void Run(object state) { 118 129 CancellationToken cancellationToken = (CancellationToken)state; 119 lastUpdateTime = DateTime.UtcNow; 120 System.Timers.Timer timer = new System.Timers.Timer(250); 121 timer.AutoReset = true; 122 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); 123 timer.Start(); 124 try { 125 if (!initialized) 126 Initialize(cancellationToken); 127 initialized = true; 128 Run(cancellationToken); 129 } 130 finally { 131 timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed); 132 timer.Stop(); 133 ExecutionTime += DateTime.UtcNow - lastUpdateTime; 134 } 130 if (!initialized) 131 Initialize(cancellationToken); 132 initialized = true; 133 Run(cancellationToken); 135 134 } 136 135 … … 138 137 protected abstract void Run(CancellationToken cancellationToken); 139 138 140 #region Events 141 private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { 142 System.Timers.Timer timer = (System.Timers.Timer)sender; 143 timer.Enabled = false; 144 DateTime now = DateTime.UtcNow; 145 ExecutionTime += now - lastUpdateTime; 146 lastUpdateTime = now; 147 timer.Enabled = true; 139 protected override void OnPaused() { 140 executionTime += DateTime.UtcNow - startDate; 141 executionTimeActive = false; 142 base.OnPaused(); 148 143 } 149 #endregion150 144 145 protected override void OnStopped() { 146 if (executionTimeActive) { // don't do if going from pause to stop 147 executionTime += DateTime.UtcNow - startDate; 148 executionTimeActive = false; 149 } 150 base.OnStopped(); 151 } 151 152 } 152 153 } -
branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/EngineAlgorithm.cs
r15287 r15603 71 71 } 72 72 73 public override TimeSpan ExecutionTime { 74 get { return engine.ExecutionTime; } 75 } 76 73 77 public override ResultCollection Results { 74 78 get { … … 195 199 private void RegisterEngineEvents() { 196 200 Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred); 197 Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);198 201 Engine.Paused += new EventHandler(Engine_Paused); 199 202 Engine.Prepared += new EventHandler(Engine_Prepared); … … 203 206 private void DeregisterEngineEvents() { 204 207 Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred); 205 Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);206 208 Engine.Paused -= new EventHandler(Engine_Paused); 207 209 Engine.Prepared -= new EventHandler(Engine_Prepared); … … 211 213 private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) { 212 214 OnExceptionOccurred(e.Value); 213 }214 private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {215 ExecutionTime = Engine.ExecutionTime;216 215 } 217 216 private void Engine_Paused(object sender, EventArgs e) { -
branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/HeuristicOptimizationAlgorithm.cs
r14185 r15603 21 21 22 22 using System; 23 using System.Threading; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 37 38 } 38 39 39 protected HeuristicOptimizationAlgorithm() : base() { } 40 protected HeuristicOptimizationAlgorithm(string name) : base(name) { } 41 protected HeuristicOptimizationAlgorithm(string name, ParameterCollection parameters) : base(name, parameters) { } 42 protected HeuristicOptimizationAlgorithm(string name, string description) : base(name, description) { } 43 protected HeuristicOptimizationAlgorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { } 40 [Storable] 41 private TimeSpan executionTime; 42 private DateTime startDate; 43 public override TimeSpan ExecutionTime { 44 get { return executionTime + (ExecutionState == ExecutionState.Started ? DateTime.UtcNow - startDate : TimeSpan.Zero); } 45 } 46 47 protected HeuristicOptimizationAlgorithm() : base() { 48 executionTime = TimeSpan.Zero; 49 } 50 protected HeuristicOptimizationAlgorithm(string name) : base(name) { 51 executionTime = TimeSpan.Zero; 52 } 53 protected HeuristicOptimizationAlgorithm(string name, ParameterCollection parameters) : base(name, parameters) { 54 executionTime = TimeSpan.Zero; 55 } 56 protected HeuristicOptimizationAlgorithm(string name, string description) : base(name, description) { 57 executionTime = TimeSpan.Zero; 58 } 59 protected HeuristicOptimizationAlgorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { 60 executionTime = TimeSpan.Zero; 61 } 44 62 45 63 [StorableConstructor] 46 64 protected HeuristicOptimizationAlgorithm(bool deserializing) : base(deserializing) { } 47 protected HeuristicOptimizationAlgorithm(HeuristicOptimizationAlgorithm original, Cloner cloner) : base(original, cloner) { } 65 protected HeuristicOptimizationAlgorithm(HeuristicOptimizationAlgorithm original, Cloner cloner) 66 : base(original, cloner) { 67 executionTime = original.executionTime; 68 } 69 70 public override void Prepare() { 71 executionTime = TimeSpan.Zero; 72 base.Prepare(); 73 } 74 75 public override void Start(CancellationToken cancellationToken) { 76 startDate = DateTime.UtcNow; 77 base.Start(cancellationToken); 78 } 48 79 49 80 #region Events … … 62 93 protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { } 63 94 #endregion 95 96 protected override void OnPaused() { 97 var elapsed = DateTime.UtcNow - startDate; 98 executionTime += elapsed; 99 base.OnPaused(); 100 } 101 102 protected override void OnStopped() { 103 if (ExecutionState == ExecutionState.Started) { 104 var elapsed = DateTime.UtcNow - startDate; 105 executionTime += elapsed; 106 } 107 base.OnStopped(); 108 } 64 109 } 65 110 } -
branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/MetaOptimizers/BatchRun.cs
r15287 r15603 68 68 } 69 69 } 70 70 71 71 [Storable] 72 72 private TimeSpan executionTime; 73 73 public TimeSpan ExecutionTime { 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; 74 get { return executionTime + (Optimizer?.ExecutionTime ?? TimeSpan.Zero); } 75 } 88 76 89 77 [Storable] … … 174 162 executionState = ExecutionState.Stopped; 175 163 executionTime = TimeSpan.Zero; 176 runsExecutionTime = TimeSpan.Zero;177 164 repetitions = 10; 178 165 repetitionsCounter = 0; … … 184 171 executionState = ExecutionState.Stopped; 185 172 executionTime = TimeSpan.Zero; 186 runsExecutionTime = TimeSpan.Zero;187 173 repetitions = 10; 188 174 repetitionsCounter = 0; … … 193 179 executionState = ExecutionState.Stopped; 194 180 executionTime = TimeSpan.Zero; 195 runsExecutionTime = TimeSpan.Zero;196 181 repetitions = 10; 197 182 repetitionsCounter = 0; … … 209 194 executionState = original.executionState; 210 195 executionTime = original.executionTime; 211 runsExecutionTime = original.runsExecutionTime;212 196 optimizer = cloner.Clone(original.optimizer); 213 197 repetitions = original.repetitions; … … 234 218 throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState)); 235 219 if (Optimizer != null) { 236 ExecutionTime = TimeSpan.Zero;220 executionTime = TimeSpan.Zero; 237 221 RepetitionsCounter = 0; 238 222 if (clearRuns) runs.Clear(); … … 297 281 if (handler != null) handler(this, EventArgs.Empty); 298 282 } 283 [Obsolete("Deprecated needs to be removed")] 299 284 public event EventHandler ExecutionTimeChanged; 300 private void OnExecutionTimeChanged() {301 EventHandler handler = ExecutionTimeChanged;302 if (handler != null) handler(this, EventArgs.Empty);303 }304 285 public event EventHandler OptimizerChanged; 305 286 private void OnOptimizerChanged() { … … 353 334 private void RegisterOptimizerEvents() { 354 335 optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Optimizer_ExceptionOccurred); 355 optimizer.ExecutionTimeChanged += new EventHandler(Optimizer_ExecutionTimeChanged);356 336 optimizer.Paused += new EventHandler(Optimizer_Paused); 357 337 optimizer.Prepared += new EventHandler(Optimizer_Prepared); … … 364 344 private void DeregisterOptimizerEvents() { 365 345 optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Optimizer_ExceptionOccurred); 366 optimizer.ExecutionTimeChanged -= new EventHandler(Optimizer_ExecutionTimeChanged);367 346 optimizer.Paused -= new EventHandler(Optimizer_Paused); 368 347 optimizer.Prepared -= new EventHandler(Optimizer_Prepared); … … 376 355 OnExceptionOccurred(e.Value); 377 356 } 378 private void Optimizer_ExecutionTimeChanged(object sender, EventArgs e) {379 OnExecutionTimeChanged();380 }381 357 private void Optimizer_Paused(object sender, EventArgs e) { 382 358 if (ExecutionState == ExecutionState.Started) { … … 386 362 private void Optimizer_Prepared(object sender, EventArgs e) { 387 363 if (batchRunAction == BatchRunAction.Prepare || ExecutionState == ExecutionState.Stopped) { 388 ExecutionTime = TimeSpan.Zero;389 runsExecutionTime = TimeSpan.Zero;390 364 RepetitionsCounter = 0; 391 365 OnPrepared(); … … 398 372 private void Optimizer_Stopped(object sender, EventArgs e) { 399 373 RepetitionsCounter++; 400 ExecutionTime += runsExecutionTime;401 runsExecutionTime = TimeSpan.Zero;402 374 403 375 if (batchRunAction == BatchRunAction.Stop) OnStopped(); … … 434 406 private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) { 435 407 if (Optimizer != null) Optimizer.Runs.RemoveRange(e.OldItems); 436 foreach (IRun run in e.Items) { 408 executionTime += SumExecutionTimes(e.OldItems); 409 executionTime += SumExecutionTimes(e.Items); 410 } 411 private void Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) { 412 executionTime += SumExecutionTimes(e.Items); 413 } 414 private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) { 415 if (Optimizer != null) Optimizer.Runs.RemoveRange(e.Items); 416 executionTime -= SumExecutionTimes(e.Items); 417 } 418 private TimeSpan SumExecutionTimes(IEnumerable<IRun> runs) { 419 var sum = TimeSpan.Zero; 420 foreach (IRun run in runs) { 437 421 IItem item; 438 422 run.Results.TryGetValue("Execution Time", out item); 439 TimeSpanValue executionTime = item as TimeSpanValue; 440 if (executionTime != null) ExecutionTime += executionTime.Value; 441 } 442 } 443 private void Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) { 444 foreach (IRun run in e.Items) { 445 IItem item; 446 run.Results.TryGetValue("Execution Time", out item); 447 TimeSpanValue executionTime = item as TimeSpanValue; 448 if (executionTime != null) { 449 if (Optimizer.ExecutionState == ExecutionState.Started) 450 runsExecutionTime += executionTime.Value; 451 else 452 ExecutionTime += executionTime.Value; 453 } 454 } 455 } 456 private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) { 457 if (Optimizer != null) Optimizer.Runs.RemoveRange(e.Items); 423 TimeSpanValue exTime = item as TimeSpanValue; 424 if (exTime != null) sum += exTime.Value; 425 } 426 return sum; 458 427 } 459 428 #endregion -
branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs
r15339 r15603 70 70 private TimeSpan executionTime; 71 71 public TimeSpan ExecutionTime { 72 get { return executionTime; } 73 private set { 74 executionTime = value; 75 OnExecutionTimeChanged(); 76 } 72 get { return executionTime + Optimizers.Aggregate(TimeSpan.Zero, (a, x) => a + x.ExecutionTime); } 77 73 } 78 74 … … 249 245 if (handler != null) handler(this, EventArgs.Empty); 250 246 } 247 [Obsolete("deprecated needs to be removed")] 251 248 public event EventHandler ExecutionTimeChanged; 252 private void OnExecutionTimeChanged() {253 EventHandler handler = ExecutionTimeChanged;254 if (handler != null) handler(this, EventArgs.Empty);255 }256 249 public event EventHandler Prepared; 257 250 private void OnPrepared() { … … 334 327 private void RegisterOptimizerEvents(IOptimizer optimizer) { 335 328 optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred); 336 optimizer.ExecutionTimeChanged += new EventHandler(optimizer_ExecutionTimeChanged);337 329 optimizer.Paused += new EventHandler(optimizer_Paused); 338 330 optimizer.Prepared += new EventHandler(optimizer_Prepared); … … 345 337 private void DeregisterOptimizerEvents(IOptimizer optimizer) { 346 338 optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred); 347 optimizer.ExecutionTimeChanged -= new EventHandler(optimizer_ExecutionTimeChanged);348 339 optimizer.Paused -= new EventHandler(optimizer_Paused); 349 340 optimizer.Prepared -= new EventHandler(optimizer_Prepared); … … 360 351 lock (locker) 361 352 OnExceptionOccurred(e.Value); 362 }363 private void optimizer_ExecutionTimeChanged(object sender, EventArgs e) {364 // only wait for maximally 100ms to acquire lock, otherwise return and don't update the execution time365 var success = Monitor.TryEnter(locker, 100);366 if (!success) return;367 try {368 ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime);369 } finally {370 Monitor.Exit(locker);371 }372 353 } 373 354 private void optimizer_Paused(object sender, EventArgs e) { -
branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/MetaOptimizers/TimeLimitRun.cs
r15287 r15603 214 214 215 215 private void Initialize() { 216 timer = new System.Timers.Timer(); 217 timer.Interval = 50; 218 timer.Elapsed += Timer_Elapsed; 216 219 if (algorithm != null) RegisterAlgorithmEvents(); 217 220 snapshotTimes.ItemsAdded += snapshotTimes_Changed; … … 255 258 } 256 259 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 257 276 #region Events 258 277 protected override void OnNameChanged() { … … 273 292 if (handler != null) handler(this, EventArgs.Empty); 274 293 } 294 [Obsolete("Deprecated needs to be removed")] 275 295 public event EventHandler ExecutionTimeChanged; 276 private void OnExecutionTimeChanged() {277 var handler = ExecutionTimeChanged;278 if (handler != null) handler(this, EventArgs.Empty);279 }280 296 public event EventHandler Prepared; 281 297 private void OnPrepared() { … … 285 301 public event EventHandler Started; 286 302 private void OnStarted() { 303 timer.Start(); 287 304 var handler = Started; 288 305 if (handler != null) handler(this, EventArgs.Empty); … … 290 307 public event EventHandler Paused; 291 308 private void OnPaused() { 309 timer.Stop(); 292 310 var handler = Paused; 293 311 if (handler != null) handler(this, EventArgs.Empty); … … 295 313 public event EventHandler Stopped; 296 314 private void OnStopped() { 315 timer.Stop(); 297 316 var handler = Stopped; 298 317 if (handler != null) handler(this, EventArgs.Empty); … … 308 327 private void RegisterAlgorithmEvents() { 309 328 algorithm.ExceptionOccurred += Algorithm_ExceptionOccurred; 310 algorithm.ExecutionTimeChanged += Algorithm_ExecutionTimeChanged;311 329 algorithm.ExecutionStateChanged += Algorithm_ExecutionStateChanged; 312 330 algorithm.Paused += Algorithm_Paused; … … 317 335 private void DeregisterAlgorithmEvents() { 318 336 algorithm.ExceptionOccurred -= Algorithm_ExceptionOccurred; 319 algorithm.ExecutionTimeChanged -= Algorithm_ExecutionTimeChanged;320 337 algorithm.ExecutionStateChanged -= Algorithm_ExecutionStateChanged; 321 338 algorithm.Paused -= Algorithm_Paused; … … 326 343 private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) { 327 344 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();340 345 } 341 346 private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
Note: See TracChangeset
for help on using the changeset viewer.