- Timestamp:
- 02/04/18 22:33:26 (7 years ago)
- 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 68 68 } 69 69 } 70 70 71 71 [Storable] 72 72 private TimeSpan executionTime; 73 73 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; 76 88 77 89 [Storable] … … 162 174 executionState = ExecutionState.Stopped; 163 175 executionTime = TimeSpan.Zero; 176 runsExecutionTime = TimeSpan.Zero; 164 177 repetitions = 10; 165 178 repetitionsCounter = 0; … … 171 184 executionState = ExecutionState.Stopped; 172 185 executionTime = TimeSpan.Zero; 186 runsExecutionTime = TimeSpan.Zero; 173 187 repetitions = 10; 174 188 repetitionsCounter = 0; … … 179 193 executionState = ExecutionState.Stopped; 180 194 executionTime = TimeSpan.Zero; 195 runsExecutionTime = TimeSpan.Zero; 181 196 repetitions = 10; 182 197 repetitionsCounter = 0; … … 194 209 executionState = original.executionState; 195 210 executionTime = original.executionTime; 211 runsExecutionTime = original.runsExecutionTime; 196 212 optimizer = cloner.Clone(original.optimizer); 197 213 repetitions = original.repetitions; … … 218 234 throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState)); 219 235 if (Optimizer != null) { 220 executionTime = TimeSpan.Zero;236 ExecutionTime = TimeSpan.Zero; 221 237 RepetitionsCounter = 0; 222 238 if (clearRuns) runs.Clear(); … … 282 298 if (handler != null) handler(this, EventArgs.Empty); 283 299 } 284 [Obsolete("Deprecated needs to be removed")]285 300 public event EventHandler ExecutionTimeChanged; 301 private void OnExecutionTimeChanged() { 302 EventHandler handler = ExecutionTimeChanged; 303 if (handler != null) handler(this, EventArgs.Empty); 304 } 286 305 public event EventHandler OptimizerChanged; 287 306 private void OnOptimizerChanged() { … … 335 354 private void RegisterOptimizerEvents() { 336 355 optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Optimizer_ExceptionOccurred); 356 optimizer.ExecutionTimeChanged += new EventHandler(Optimizer_ExecutionTimeChanged); 337 357 optimizer.Paused += new EventHandler(Optimizer_Paused); 338 358 optimizer.Prepared += new EventHandler(Optimizer_Prepared); … … 345 365 private void DeregisterOptimizerEvents() { 346 366 optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Optimizer_ExceptionOccurred); 367 optimizer.ExecutionTimeChanged -= new EventHandler(Optimizer_ExecutionTimeChanged); 347 368 optimizer.Paused -= new EventHandler(Optimizer_Paused); 348 369 optimizer.Prepared -= new EventHandler(Optimizer_Prepared); … … 356 377 OnExceptionOccurred(e.Value); 357 378 } 379 private void Optimizer_ExecutionTimeChanged(object sender, EventArgs e) { 380 OnExecutionTimeChanged(); 381 } 358 382 private void Optimizer_Paused(object sender, EventArgs e) { 359 383 if (ExecutionState == ExecutionState.Started) { … … 363 387 private void Optimizer_Prepared(object sender, EventArgs e) { 364 388 if (batchRunAction == BatchRunAction.Prepare || ExecutionState == ExecutionState.Stopped) { 389 ExecutionTime = TimeSpan.Zero; 390 runsExecutionTime = TimeSpan.Zero; 365 391 RepetitionsCounter = 0; 366 392 OnPrepared(); … … 373 399 private void Optimizer_Stopped(object sender, EventArgs e) { 374 400 RepetitionsCounter++; 401 ExecutionTime += runsExecutionTime; 402 runsExecutionTime = TimeSpan.Zero; 375 403 376 404 if (batchRunAction == BatchRunAction.Stop) OnStopped(); … … 407 435 private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) { 408 436 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 } 411 443 } 412 444 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 } 414 456 } 415 457 private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) { 416 458 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;428 459 } 429 460 #endregion -
branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs
r15605 r15721 70 70 private TimeSpan executionTime; 71 71 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 } 73 77 } 74 78 … … 278 282 if (handler != null) handler(this, EventArgs.Empty); 279 283 } 280 [Obsolete("deprecated needs to be removed")]281 284 public event EventHandler ExecutionTimeChanged; 285 private void OnExecutionTimeChanged() { 286 EventHandler handler = ExecutionTimeChanged; 287 if (handler != null) handler(this, EventArgs.Empty); 288 } 282 289 public event EventHandler Prepared; 283 290 private void OnPrepared() { … … 362 369 private void RegisterOptimizerEvents(IOptimizer optimizer) { 363 370 optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred); 371 optimizer.ExecutionTimeChanged += new EventHandler(optimizer_ExecutionTimeChanged); 364 372 optimizer.Paused += new EventHandler(optimizer_Paused); 365 373 optimizer.Prepared += new EventHandler(optimizer_Prepared); … … 372 380 private void DeregisterOptimizerEvents(IOptimizer optimizer) { 373 381 optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred); 382 optimizer.ExecutionTimeChanged -= new EventHandler(optimizer_ExecutionTimeChanged); 374 383 optimizer.Paused -= new EventHandler(optimizer_Paused); 375 384 optimizer.Prepared -= new EventHandler(optimizer_Prepared); … … 386 395 lock (locker) 387 396 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 } 388 407 } 389 408 private void optimizer_Paused(object sender, EventArgs e) { -
branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3/MetaOptimizers/TimeLimitRun.cs
r15605 r15721 214 214 215 215 private void Initialize() { 216 timer = new System.Timers.Timer();217 timer.Interval = 50;218 timer.Elapsed += Timer_Elapsed;219 216 if (algorithm != null) RegisterAlgorithmEvents(); 220 217 snapshotTimes.ItemsAdded += snapshotTimes_Changed; … … 258 255 } 259 256 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 276 257 #region Events 277 258 protected override void OnNameChanged() { … … 292 273 if (handler != null) handler(this, EventArgs.Empty); 293 274 } 294 [Obsolete("Deprecated needs to be removed")]295 275 public event EventHandler ExecutionTimeChanged; 276 private void OnExecutionTimeChanged() { 277 var handler = ExecutionTimeChanged; 278 if (handler != null) handler(this, EventArgs.Empty); 279 } 296 280 public event EventHandler Prepared; 297 281 private void OnPrepared() { … … 301 285 public event EventHandler Started; 302 286 private void OnStarted() { 303 timer.Start();304 287 var handler = Started; 305 288 if (handler != null) handler(this, EventArgs.Empty); … … 307 290 public event EventHandler Paused; 308 291 private void OnPaused() { 309 timer.Stop();310 292 var handler = Paused; 311 293 if (handler != null) handler(this, EventArgs.Empty); … … 313 295 public event EventHandler Stopped; 314 296 private void OnStopped() { 315 timer.Stop();316 297 var handler = Stopped; 317 298 if (handler != null) handler(this, EventArgs.Empty); … … 327 308 private void RegisterAlgorithmEvents() { 328 309 algorithm.ExceptionOccurred += Algorithm_ExceptionOccurred; 310 algorithm.ExecutionTimeChanged += Algorithm_ExecutionTimeChanged; 329 311 algorithm.ExecutionStateChanged += Algorithm_ExecutionStateChanged; 330 312 algorithm.Paused += Algorithm_Paused; … … 335 317 private void DeregisterAlgorithmEvents() { 336 318 algorithm.ExceptionOccurred -= Algorithm_ExceptionOccurred; 319 algorithm.ExecutionTimeChanged -= Algorithm_ExecutionTimeChanged; 337 320 algorithm.ExecutionStateChanged -= Algorithm_ExecutionStateChanged; 338 321 algorithm.Paused -= Algorithm_Paused; … … 343 326 private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) { 344 327 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(); 345 340 } 346 341 private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
Note: See TracChangeset
for help on using the changeset viewer.