Changeset 17115 for stable/HeuristicLab.Optimization/3.3/MetaOptimizers
- Timestamp:
- 07/08/19 15:42:48 (5 years ago)
- Location:
- stable
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
stable
-
stable/HeuristicLab.Optimization
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Optimization merged: 16651
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Optimization/3.3/MetaOptimizers/TimeLimitRun.cs
r17097 r17115 27 27 using System.Threading; 28 28 using System.Threading.Tasks; 29 using HEAL.Attic; 29 30 using HeuristicLab.Collections; 30 31 using HeuristicLab.Common; 31 32 using HeuristicLab.Common.Resources; 32 33 using HeuristicLab.Core; 33 using HEAL.Attic;34 34 35 35 namespace HeuristicLab.Optimization { … … 62 62 if (maximumExecutionTime == value) return; 63 63 maximumExecutionTime = value; 64 OnPropertyChanged( "MaximumExecutionTime");64 OnPropertyChanged(nameof(MaximumExecutionTime)); 65 65 } 66 66 } … … 77 77 snapshotTimes.Sort(); 78 78 FindNextSnapshotTimeIndex(ExecutionTime); 79 OnPropertyChanged( "SnapshotTimes");79 OnPropertyChanged(nameof(SnapshotTimes)); 80 80 } 81 81 } … … 89 89 if (storeAlgorithmInEachSnapshot == value) return; 90 90 storeAlgorithmInEachSnapshot = value; 91 OnPropertyChanged( "StoreAlgorithmInEachSnapshot");91 OnPropertyChanged(nameof(StoreAlgorithmInEachSnapshot)); 92 92 } 93 93 } … … 100 100 if (snapshots == value) return; 101 101 snapshots = value; 102 OnPropertyChanged( "Snapshots");102 OnPropertyChanged(nameof(Snapshots)); 103 103 } 104 104 } 105 105 106 106 #region Inherited Properties 107 [Storable] 108 private ExecutionState executionState; 107 109 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 } 109 117 } 110 118 … … 119 127 set { 120 128 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."); 121 131 if (algorithm != null) DeregisterAlgorithmEvents(); 122 132 algorithm = value; 133 if (algorithm != null) RegisterAlgorithmEvents(); 134 OnPropertyChanged(nameof(Algorithm)); 135 OnPropertyChanged(nameof(NestedOptimizers)); 123 136 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(); 128 142 } 129 143 } … … 137 151 if (runs == value) return; 138 152 runs = value; 139 OnPropertyChanged( "Runs");153 OnPropertyChanged(nameof(Runs)); 140 154 } 141 155 } … … 160 174 snapshots = cloner.Clone(original.snapshots); 161 175 storeAlgorithmInEachSnapshot = original.storeAlgorithmInEachSnapshot; 176 executionState = original.executionState; 162 177 algorithm = cloner.Clone(original.algorithm); 163 178 runs = cloner.Clone(original.runs); … … 169 184 name = ItemName; 170 185 description = ItemDescription; 186 executionState = ExecutionState.Stopped; 171 187 maximumExecutionTime = TimeSpan.FromMinutes(.5); 172 188 snapshotTimes = new ObservableList<TimeSpan>(new[] { … … 182 198 : base(name) { 183 199 description = ItemDescription; 200 executionState = ExecutionState.Stopped; 184 201 maximumExecutionTime = TimeSpan.FromMinutes(.5); 185 202 snapshotTimes = new ObservableList<TimeSpan>(new[] { … … 194 211 public TimeLimitRun(string name, string description) 195 212 : base(name, description) { 213 executionState = ExecutionState.Stopped; 196 214 maximumExecutionTime = TimeSpan.FromMinutes(.5); 197 215 snapshotTimes = new ObservableList<TimeSpan>(new[] { … … 213 231 private void AfterDeserialization() { 214 232 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 215 239 } 216 240 … … 238 262 } 239 263 public void Prepare(bool clearRuns) { 264 if (Algorithm == null) return; 240 265 Algorithm.Prepare(clearRuns); 241 266 } … … 282 307 public event EventHandler Prepared; 283 308 private void OnPrepared() { 309 ExecutionState = ExecutionState.Prepared; 284 310 var handler = Prepared; 285 311 if (handler != null) handler(this, EventArgs.Empty); … … 287 313 public event EventHandler Started; 288 314 private void OnStarted() { 315 ExecutionState = ExecutionState.Started; 289 316 var handler = Started; 290 317 if (handler != null) handler(this, EventArgs.Empty); … … 292 319 public event EventHandler Paused; 293 320 private void OnPaused() { 321 ExecutionState = ExecutionState.Paused; 294 322 var handler = Paused; 295 323 if (handler != null) handler(this, EventArgs.Empty); … … 297 325 public event EventHandler Stopped; 298 326 private void OnStopped() { 327 ExecutionState = ExecutionState.Stopped; 299 328 var handler = Stopped; 300 329 if (handler != null) handler(this, EventArgs.Empty); … … 302 331 public event EventHandler<EventArgs<Exception>> ExceptionOccurred; 303 332 private void OnExceptionOccurred(Exception exception) { 333 ExecutionState = ExecutionState.Paused; 304 334 var handler = ExceptionOccurred; 305 335 if (handler != null) handler(this, new EventArgs<Exception>(exception)); … … 311 341 algorithm.ExceptionOccurred += Algorithm_ExceptionOccurred; 312 342 algorithm.ExecutionTimeChanged += Algorithm_ExecutionTimeChanged; 313 algorithm.ExecutionStateChanged += Algorithm_ExecutionStateChanged;314 343 algorithm.Paused += Algorithm_Paused; 315 344 algorithm.Prepared += Algorithm_Prepared; … … 320 349 algorithm.ExceptionOccurred -= Algorithm_ExceptionOccurred; 321 350 algorithm.ExecutionTimeChanged -= Algorithm_ExecutionTimeChanged; 322 algorithm.ExecutionStateChanged -= Algorithm_ExecutionStateChanged;323 351 algorithm.Paused -= Algorithm_Paused; 324 352 algorithm.Prepared -= Algorithm_Prepared; … … 340 368 } 341 369 OnExecutionTimeChanged(); 342 }343 private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {344 OnExecutionStateChanged();345 370 } 346 371 private void Algorithm_Paused(object sender, EventArgs e) { … … 350 375 MakeSnapshot(); 351 376 FindNextSnapshotTimeIndex(ExecutionTime); 352 } 353 OnPaused(); 377 } else OnPaused(); 354 378 if (action == ExecutionState.Started) Algorithm.Start(); 355 379 else if (action == ExecutionState.Stopped) Algorithm.Stop(); … … 361 385 } 362 386 private void Algorithm_Started(object sender, EventArgs e) { 363 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 364 392 } 365 393 private void Algorithm_Stopped(object sender, EventArgs e) { 366 var cloner = new Cloner(); 367 var algRun = cloner.Clone(Algorithm.Runs.Last()); 368 var clonedSnapshots = cloner.Clone(snapshots); 369 algRun.Results.Add("TimeLimitRunSnapshots", clonedSnapshots); 370 Runs.Add(algRun); 371 Algorithm.Runs.Clear(); 372 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(); } 373 402 } 374 403 #endregion … … 379 408 while (index < snapshotTimes.Count && snapshotTimes[index] <= reference) { 380 409 index++; 381 } ;410 } 382 411 snapshotTimesIndex = index; 383 412 }
Note: See TracChangeset
for help on using the changeset viewer.