Changeset 15721 for branches/1614_GeneralizedQAP/HeuristicLab.Optimization
- Timestamp:
- 02/04/18 22:33:26 (7 years ago)
- Location:
- branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/Algorithm.cs
r15719 r15721 64 64 } 65 65 } 66 67 public abstract TimeSpan ExecutionTime { get; } 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 } 68 76 69 77 public virtual Type ProblemType { … … 125 133 : base() { 126 134 executionState = ExecutionState.Stopped; 135 executionTime = TimeSpan.Zero; 127 136 storeAlgorithmInEachRun = false; 128 137 runsCounter = 0; … … 132 141 : base(name) { 133 142 executionState = ExecutionState.Stopped; 143 executionTime = TimeSpan.Zero; 134 144 storeAlgorithmInEachRun = false; 135 145 runsCounter = 0; … … 139 149 : base(name, parameters) { 140 150 executionState = ExecutionState.Stopped; 151 executionTime = TimeSpan.Zero; 141 152 storeAlgorithmInEachRun = false; 142 153 runsCounter = 0; … … 146 157 : base(name, description) { 147 158 executionState = ExecutionState.Stopped; 159 executionTime = TimeSpan.Zero; 148 160 storeAlgorithmInEachRun = false; 149 161 runsCounter = 0; … … 153 165 : base(name, description, parameters) { 154 166 executionState = ExecutionState.Stopped; 167 executionTime = TimeSpan.Zero; 155 168 storeAlgorithmInEachRun = false; 156 169 runsCounter = 0; … … 168 181 if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState)); 169 182 executionState = original.executionState; 183 executionTime = original.executionTime; 170 184 problem = cloner.Clone(original.problem); 171 185 storeAlgorithmInEachRun = original.storeAlgorithmInEachRun; … … 246 260 if (handler != null) handler(this, EventArgs.Empty); 247 261 } 262 public event EventHandler ExecutionTimeChanged; 263 protected virtual void OnExecutionTimeChanged() { 264 EventHandler handler = ExecutionTimeChanged; 265 if (handler != null) handler(this, EventArgs.Empty); 266 } 248 267 public event EventHandler ProblemChanged; 249 268 protected virtual void OnProblemChanged() { … … 258 277 public event EventHandler Prepared; 259 278 protected virtual void OnPrepared() { 279 ExecutionTime = TimeSpan.Zero; 260 280 foreach (IStatefulItem statefulObject in this.GetObjectGraphObjects(new HashSet<object>() { Runs }).OfType<IStatefulItem>()) { 261 281 statefulObject.InitializeState(); … … 328 348 } 329 349 #endregion 330 331 [Obsolete("Deprecate, does nothing, needs to be removed from IExecutable")]332 public event EventHandler ExecutionTimeChanged;333 350 } 334 351 } -
branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
r15605 r15721 21 21 22 22 using System; 23 using System.Linq; 23 24 using System.Threading; 24 25 using HeuristicLab.Common; … … 31 32 32 33 private bool pausePending; 33 private bool executionTimeActive;34 private DateTime lastUpdateTime; 34 35 35 36 public string Filename { get; set; } … … 51 52 } 52 53 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 60 54 [StorableConstructor] 61 55 protected BasicAlgorithm(bool deserializing) : base(deserializing) { } … … 64 58 results = cloner.Clone(original.Results); 65 59 initialized = original.initialized; 66 executionTime = original.executionTime;67 60 } 68 61 protected BasicAlgorithm() 69 62 : base() { 70 63 results = new ResultCollection(); 71 executionTime = TimeSpan.Zero;72 64 } 73 65 74 66 public override void Prepare() { 75 executionTime = TimeSpan.Zero;76 67 if (Problem == null) return; 77 68 base.Prepare(); … … 82 73 83 74 public override void Start(CancellationToken cancellationToken) { 84 startDate = DateTime.UtcNow;85 executionTimeActive = true;86 75 base.Start(cancellationToken); 87 76 CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); … … 125 114 private void Run(object state) { 126 115 CancellationToken cancellationToken = (CancellationToken)state; 127 if (!initialized) 128 Initialize(cancellationToken); 129 initialized = true; 130 Run(cancellationToken); 116 lastUpdateTime = DateTime.UtcNow; 117 System.Timers.Timer timer = new System.Timers.Timer(250); 118 timer.AutoReset = true; 119 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); 120 timer.Start(); 121 try { 122 if (!initialized) 123 Initialize(cancellationToken); 124 initialized = true; 125 Run(cancellationToken); 126 } 127 finally { 128 timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed); 129 timer.Stop(); 130 ExecutionTime += DateTime.UtcNow - lastUpdateTime; 131 } 131 132 } 132 133 … … 134 135 protected abstract void Run(CancellationToken cancellationToken); 135 136 136 protected override void OnPaused() { 137 executionTime += DateTime.UtcNow - startDate; 138 executionTimeActive = false; 139 base.OnPaused(); 137 #region Events 138 private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { 139 System.Timers.Timer timer = (System.Timers.Timer)sender; 140 timer.Enabled = false; 141 DateTime now = DateTime.UtcNow; 142 ExecutionTime += now - lastUpdateTime; 143 lastUpdateTime = now; 144 timer.Enabled = true; 140 145 } 146 #endregion 141 147 142 protected override void OnStopped() {143 if (executionTimeActive) { // don't do if going from pause to stop144 executionTime += DateTime.UtcNow - startDate;145 executionTimeActive = false;146 }147 base.OnStopped();148 }149 148 } 150 149 } -
branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/EngineAlgorithm.cs
r15719 r15721 71 71 } 72 72 73 public override TimeSpan ExecutionTime {74 get { return engine.ExecutionTime; }75 }76 77 73 public override ResultCollection Results { 78 74 get { … … 200 196 private void RegisterEngineEvents() { 201 197 Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred); 198 Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged); 202 199 Engine.Paused += new EventHandler(Engine_Paused); 203 200 Engine.Prepared += new EventHandler(Engine_Prepared); … … 207 204 private void DeregisterEngineEvents() { 208 205 Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred); 206 Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged); 209 207 Engine.Paused -= new EventHandler(Engine_Paused); 210 208 Engine.Prepared -= new EventHandler(Engine_Prepared); … … 214 212 private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) { 215 213 OnExceptionOccurred(e.Value); 214 } 215 private void Engine_ExecutionTimeChanged(object sender, EventArgs e) { 216 ExecutionTime = Engine.ExecutionTime; 216 217 } 217 218 private void Engine_Paused(object sender, EventArgs e) { -
branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/HeuristicOptimizationAlgorithm.cs
r15605 r15721 21 21 22 22 using System; 23 using System.Threading;24 23 using HeuristicLab.Common; 25 24 using HeuristicLab.Core; … … 38 37 } 39 38 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 } 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) { } 62 44 63 45 [StorableConstructor] 64 46 protected HeuristicOptimizationAlgorithm(bool deserializing) : base(deserializing) { } 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 } 47 protected HeuristicOptimizationAlgorithm(HeuristicOptimizationAlgorithm original, Cloner cloner) : base(original, cloner) { } 79 48 80 49 #region Events … … 93 62 protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { } 94 63 #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 }109 64 } 110 65 } -
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.