Changeset 8194
- Timestamp:
- 07/03/12 13:58:47 (12 years ago)
- Location:
- trunk/sources/HeuristicLab.Optimization/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimization/3.3/BatchRun.cs
r8190 r8194 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. … … 153 155 } 154 156 155 private bool batchRunPrepared = false; 156 private bool batchRunStarted = false; 157 private bool batchRunPaused = false; 158 private bool batchRunStopped = false; 157 private BatchRunAction batchRunAction = BatchRunAction.None; 159 158 160 159 public BatchRun() … … 204 203 repetitionsCounter = original.repetitionsCounter; 205 204 runs = cloner.Clone(original.runs); 206 batchRunStarted = original.batchRunStarted; 207 batchRunPaused = original.batchRunPaused; 208 batchRunStopped = original.batchRunStopped; 209 batchRunPrepared = original.batchRunPrepared; 205 batchRunAction = original.batchRunAction; 210 206 Initialize(); 211 207 } … … 230 226 repetitionsCounter = 0; 231 227 if (clearRuns) runs.Clear(); 232 batchRunPrepared = true; 233 batchRunStarted = false; 234 batchRunPaused = false; 235 batchRunStopped = false; 236 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) { } 237 231 } else { 238 232 ExecutionState = ExecutionState.Stopped; … … 243 237 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 244 238 if (Optimizer == null) return; 245 246 batchRunPrepared = false; 247 batchRunStarted = true; 248 batchRunPaused = false; 249 batchRunStopped = false; 239 batchRunAction = BatchRunAction.Start; 250 240 if (Optimizer.ExecutionState == ExecutionState.Stopped) Optimizer.Prepare(); 251 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) { } 252 243 } 253 244 public void Pause() { … … 255 246 throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState)); 256 247 if (Optimizer == null) return; 248 batchRunAction = BatchRunAction.Pause; 257 249 if (Optimizer.ExecutionState != ExecutionState.Started) return; 258 259 batchRunPrepared = false; 260 batchRunStarted = false; 261 batchRunPaused = true; 262 batchRunStopped = false; 263 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) { } 264 252 } 265 253 public void Stop() { … … 267 255 throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState)); 268 256 if (Optimizer == null) return; 257 batchRunAction = BatchRunAction.Stop; 269 258 if (Optimizer.ExecutionState != ExecutionState.Started && Optimizer.ExecutionState != ExecutionState.Paused) { 270 259 OnStopped(); 271 260 return; 272 261 } 273 274 batchRunPrepared = false; 275 batchRunStarted = false; 276 batchRunPaused = false; 277 batchRunStopped = true; 278 Optimizer.Stop(); 262 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 263 try { Optimizer.Stop(); } catch (InvalidOperationException) { } 279 264 } 280 265 … … 302 287 public event EventHandler Prepared; 303 288 private void OnPrepared() { 304 batchRun Prepared = false;289 batchRunAction = BatchRunAction.None; 305 290 ExecutionState = ExecutionState.Prepared; 306 291 EventHandler handler = Prepared; … … 309 294 public event EventHandler Started; 310 295 private void OnStarted() { 311 // TODO add coment296 // no reset of BatchRunAction.Started, because we need to differ which of the two was started by the user 312 297 ExecutionState = ExecutionState.Started; 313 298 EventHandler handler = Started; … … 316 301 public event EventHandler Paused; 317 302 private void OnPaused() { 318 batchRun Paused = false;303 batchRunAction = BatchRunAction.None; 319 304 ExecutionState = ExecutionState.Paused; 320 305 EventHandler handler = Paused; … … 323 308 public event EventHandler Stopped; 324 309 private void OnStopped() { 325 //reset flags because it cannot be done if the optimizer gets prepared 326 batchRunStopped = false; 310 batchRunAction = BatchRunAction.None; 327 311 ExecutionState = ExecutionState.Stopped; 328 312 EventHandler handler = Stopped; … … 369 353 } 370 354 private void Optimizer_Prepared(object sender, EventArgs e) { 371 if (batchRun Prepared || (ExecutionState == ExecutionState.Stopped)) {355 if (batchRunAction == BatchRunAction.Prepare || ExecutionState == ExecutionState.Stopped) { 372 356 ExecutionTime = TimeSpan.Zero; 373 357 runsExecutionTime = TimeSpan.Zero; … … 385 369 runsExecutionTime = TimeSpan.Zero; 386 370 387 if (batchRun Stopped) OnStopped();371 if (batchRunAction == BatchRunAction.Stop) OnStopped(); 388 372 else if (repetitionsCounter >= repetitions) OnStopped(); 389 else if (batchRun Paused) OnPaused();390 else if (batchRun Started) {373 else if (batchRunAction == BatchRunAction.Pause) OnPaused(); 374 else if (batchRunAction == BatchRunAction.Start) { 391 375 Optimizer.Prepare(); 392 376 Optimizer.Start(); -
trunk/sources/HeuristicLab.Optimization/3.3/Experiment.cs
r8170 r8194 178 178 experimentStarted = false; 179 179 experimentStopped = false; 180 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started)) 181 //if (clearRuns || optimizer.ExecutionState != ExecutionState.Prepared) 182 optimizer.Prepare(clearRuns); 180 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started)) { 181 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 182 try { optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { } 183 } 183 184 } 184 185 public void Start() { … … 190 191 experimentStopped = false; 191 192 IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)); 192 if (optimizer != null) optimizer.Start(); 193 if (optimizer != null) { 194 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 195 try { optimizer.Start(); } catch (InvalidOperationException) { } 196 } 193 197 } 194 198 public void Pause() { … … 199 203 experimentStarted = false; 200 204 experimentStopped = false; 201 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) 202 optimizer.Pause(); 205 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) { 206 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 207 try { optimizer.Pause(); } catch (InvalidOperationException) { } 208 } 203 209 } 204 210 public void Stop() { … … 210 216 experimentStopped = true; 211 217 if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) { 212 foreach (IOptimizer optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) 213 optimizer.Stop(); 218 foreach (var optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) { 219 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 220 try { optimizer.Stop(); } catch (InvalidOperationException) { } 221 } 214 222 } else { 215 223 OnStopped();
Note: See TracChangeset
for help on using the changeset viewer.