Changeset 8331 for branches/ScatterSearch (trunk integration)/HeuristicLab.Optimization/3.3/BatchRun.cs
- Timestamp:
- 07/26/12 09:51:13 (12 years ago)
- Location:
- branches/ScatterSearch (trunk integration)
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ScatterSearch (trunk integration)
- Property svn:ignore
-
old new 21 21 protoc.exe 22 22 _ReSharper.HeuristicLab 3.3 Tests 23 Google.ProtocolBuffers-2.4.1.473.dll
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/ScatterSearch (trunk integration)/HeuristicLab.Optimization/3.3/BatchRun.cs
r7576 r8331 30 30 31 31 namespace HeuristicLab.Optimization { 32 internal enum BatchRunAction { None, Prepare, Start, Pause, Stop }; 33 32 34 /// <summary> 33 35 /// A run in which an optimizer is executed a given number of times. … … 79 81 } 80 82 } 83 84 [Storable] 85 private TimeSpan runsExecutionTime; 81 86 82 87 [Storable] … … 150 155 } 151 156 152 private bool batchRunStarted = false; 153 private bool batchRunPaused = false; 154 private bool batchRunStopped = false; 157 private BatchRunAction batchRunAction = BatchRunAction.None; 155 158 156 159 public BatchRun() … … 160 163 executionState = ExecutionState.Stopped; 161 164 executionTime = TimeSpan.Zero; 165 runsExecutionTime = TimeSpan.Zero; 162 166 repetitions = 10; 163 167 repetitionsCounter = 0; … … 169 173 executionState = ExecutionState.Stopped; 170 174 executionTime = TimeSpan.Zero; 175 runsExecutionTime = TimeSpan.Zero; 171 176 repetitions = 10; 172 177 repetitionsCounter = 0; … … 177 182 executionState = ExecutionState.Stopped; 178 183 executionTime = TimeSpan.Zero; 184 runsExecutionTime = TimeSpan.Zero; 179 185 repetitions = 10; 180 186 repetitionsCounter = 0; … … 192 198 executionState = original.executionState; 193 199 executionTime = original.executionTime; 200 runsExecutionTime = original.runsExecutionTime; 194 201 optimizer = cloner.Clone(original.optimizer); 195 202 repetitions = original.repetitions; 196 203 repetitionsCounter = original.repetitionsCounter; 197 204 runs = cloner.Clone(original.runs); 198 batchRunStarted = original.batchRunStarted; 199 batchRunPaused = original.batchRunPaused; 200 batchRunStopped = original.batchRunStopped; 205 batchRunAction = original.batchRunAction; 201 206 Initialize(); 202 207 } … … 218 223 throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState)); 219 224 if (Optimizer != null) { 225 ExecutionTime = TimeSpan.Zero; 220 226 repetitionsCounter = 0; 221 227 if (clearRuns) runs.Clear(); 222 Optimizer.Prepare(clearRuns); 228 batchRunAction = BatchRunAction.Prepare; 229 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 230 try { Optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { } 223 231 } else { 224 232 ExecutionState = ExecutionState.Stopped; … … 229 237 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 230 238 if (Optimizer == null) return; 231 232 batchRunStarted = true; 233 batchRunPaused = false; 234 batchRunStopped = false; 239 batchRunAction = BatchRunAction.Start; 235 240 if (Optimizer.ExecutionState == ExecutionState.Stopped) Optimizer.Prepare(); 236 Optimizer.Start(); 241 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 242 try { Optimizer.Start(); } catch (InvalidOperationException) { } 237 243 } 238 244 public void Pause() { … … 240 246 throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState)); 241 247 if (Optimizer == null) return; 248 batchRunAction = BatchRunAction.Pause; 242 249 if (Optimizer.ExecutionState != ExecutionState.Started) return; 243 244 batchRunStarted = false; 245 batchRunPaused = true; 246 batchRunStopped = false; 247 Optimizer.Pause(); 250 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 251 try { Optimizer.Pause(); } catch (InvalidOperationException) { } 248 252 } 249 253 public void Stop() { … … 251 255 throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState)); 252 256 if (Optimizer == null) return; 253 if (Optimizer.ExecutionState != ExecutionState.Started && Optimizer.ExecutionState != ExecutionState.Paused) return; 254 batchRunStarted = false; 255 batchRunPaused = false; 256 batchRunStopped = true; 257 Optimizer.Stop(); 257 batchRunAction = BatchRunAction.Stop; 258 if (Optimizer.ExecutionState != ExecutionState.Started && Optimizer.ExecutionState != ExecutionState.Paused) { 259 OnStopped(); 260 return; 261 } 262 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 263 try { Optimizer.Stop(); } catch (InvalidOperationException) { } 258 264 } 259 265 … … 281 287 public event EventHandler Prepared; 282 288 private void OnPrepared() { 289 batchRunAction = BatchRunAction.None; 283 290 ExecutionState = ExecutionState.Prepared; 284 291 EventHandler handler = Prepared; … … 287 294 public event EventHandler Started; 288 295 private void OnStarted() { 296 // no reset of BatchRunAction.Started, because we need to differ which of the two was started by the user 289 297 ExecutionState = ExecutionState.Started; 290 298 EventHandler handler = Started; … … 293 301 public event EventHandler Paused; 294 302 private void OnPaused() { 303 batchRunAction = BatchRunAction.None; 295 304 ExecutionState = ExecutionState.Paused; 296 305 EventHandler handler = Paused; … … 299 308 public event EventHandler Stopped; 300 309 private void OnStopped() { 310 batchRunAction = BatchRunAction.None; 301 311 ExecutionState = ExecutionState.Stopped; 302 312 EventHandler handler = Stopped; … … 343 353 } 344 354 private void Optimizer_Prepared(object sender, EventArgs e) { 345 if (ExecutionState == ExecutionState.Stopped || !batchRunStarted) { 355 if (batchRunAction == BatchRunAction.Prepare || ExecutionState == ExecutionState.Stopped) { 356 ExecutionTime = TimeSpan.Zero; 357 runsExecutionTime = TimeSpan.Zero; 358 repetitionsCounter = 0; 346 359 OnPrepared(); 347 360 } … … 353 366 private void Optimizer_Stopped(object sender, EventArgs e) { 354 367 repetitionsCounter++; 355 356 if (batchRunStopped) OnStopped(); 368 ExecutionTime += runsExecutionTime; 369 runsExecutionTime = TimeSpan.Zero; 370 371 if (batchRunAction == BatchRunAction.Stop) OnStopped(); 357 372 else if (repetitionsCounter >= repetitions) OnStopped(); 358 else if (batchRun Paused) OnPaused();359 else if (batchRun Started) {373 else if (batchRunAction == BatchRunAction.Pause) OnPaused(); 374 else if (batchRunAction == BatchRunAction.Start) { 360 375 Optimizer.Prepare(); 361 376 Optimizer.Start(); 377 } else if (executionState == ExecutionState.Started) { 378 // if the batch run hasn't been started but the inner optimizer was run then pause 379 OnPaused(); 362 380 } else OnStopped(); 363 381 } … … 385 403 } 386 404 private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) { 387 foreach (IRun run in e.OldItems) {388 IItem item;389 run.Results.TryGetValue("Execution Time", out item);390 TimeSpanValue executionTime = item as TimeSpanValue;391 if (executionTime != null) ExecutionTime -= executionTime.Value;392 }393 405 if (Optimizer != null) Optimizer.Runs.RemoveRange(e.OldItems); 394 406 foreach (IRun run in e.Items) { … … 404 416 run.Results.TryGetValue("Execution Time", out item); 405 417 TimeSpanValue executionTime = item as TimeSpanValue; 406 if (executionTime != null) ExecutionTime += executionTime.Value; 418 if (executionTime != null) { 419 if (Optimizer.ExecutionState == ExecutionState.Started) 420 runsExecutionTime += executionTime.Value; 421 else 422 ExecutionTime += executionTime.Value; 423 } 407 424 } 408 425 } 409 426 private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) { 410 foreach (IRun run in e.Items) {411 IItem item;412 run.Results.TryGetValue("Execution Time", out item);413 TimeSpanValue executionTime = item as TimeSpanValue;414 if (executionTime != null) ExecutionTime -= executionTime.Value;415 }416 427 if (Optimizer != null) Optimizer.Runs.RemoveRange(e.Items); 417 428 }
Note: See TracChangeset
for help on using the changeset viewer.