Changeset 8206 for branches/GP-MoveOperators/HeuristicLab.Optimization
- Timestamp:
- 07/03/12 16:46:35 (12 years ago)
- Location:
- branches/GP-MoveOperators
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GP-MoveOperators
- Property svn:mergeinfo changed
/trunk/sources merged: 8084,8088-8090,8092-8100,8102-8113,8115,8117-8132,8134-8146,8148-8156,8158-8160,8163-8170,8173-8176,8178-8190,8192-8205
- Property svn:mergeinfo changed
-
branches/GP-MoveOperators/HeuristicLab.Optimization/3.3/Algorithms/Algorithm.cs
r7706 r8206 272 272 public event EventHandler Prepared; 273 273 protected virtual void OnPrepared() { 274 ExecutionState = ExecutionState.Prepared;275 274 ExecutionTime = TimeSpan.Zero; 276 275 foreach (IStatefulItem statefulObject in this.GetObjectGraphObjects(new HashSet<string>() { "runs" }).OfType<IStatefulItem>()) { 277 276 statefulObject.InitializeState(); 278 277 } 278 ExecutionState = ExecutionState.Prepared; 279 279 EventHandler handler = Prepared; 280 280 if (handler != null) handler(this, EventArgs.Empty); … … 294 294 public event EventHandler Stopped; 295 295 protected virtual void OnStopped() { 296 ExecutionState = ExecutionState.Stopped;297 296 foreach (IStatefulItem statefulObject in this.GetObjectGraphObjects(new HashSet<string>() { "runs" }).OfType<IStatefulItem>()) { 298 297 statefulObject.ClearState(); … … 300 299 runsCounter++; 301 300 runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this)); 301 ExecutionState = ExecutionState.Stopped; 302 302 EventHandler handler = Stopped; 303 303 if (handler != null) handler(this, EventArgs.Empty); -
branches/GP-MoveOperators/HeuristicLab.Optimization/3.3/BatchRun.cs
r7576 r8206 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 } -
branches/GP-MoveOperators/HeuristicLab.Optimization/3.3/Experiment.cs
r7797 r8206 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(); … … 326 334 optimizer.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsRemoved); 327 335 } 336 337 private readonly object locker = new object(); 328 338 private void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) { 329 OnExceptionOccurred(e.Value); 339 lock (locker) 340 OnExceptionOccurred(e.Value); 330 341 } 331 342 private void optimizer_ExecutionTimeChanged(object sender, EventArgs e) { 332 ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime); 343 lock (locker) 344 ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime); 333 345 } 334 346 private void optimizer_Paused(object sender, EventArgs e) { 335 if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) OnPaused(); 347 lock (locker) 348 if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) OnPaused(); 336 349 } 337 350 private void optimizer_Prepared(object sender, EventArgs e) { 338 if (Optimizers.All(x => x.ExecutionState == ExecutionState.Prepared)) OnPrepared(); 351 lock (locker) 352 if (Optimizers.All(x => x.ExecutionState == ExecutionState.Prepared)) OnPrepared(); 339 353 } 340 354 private void optimizer_Started(object sender, EventArgs e) { 341 if (ExecutionState != ExecutionState.Started) OnStarted(); 355 lock (locker) 356 if (ExecutionState != ExecutionState.Started) OnStarted(); 342 357 } 343 358 private void optimizer_Stopped(object sender, EventArgs e) { 344 if (experimentStopped) { 345 if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) OnStopped(); 346 } else { 347 if (experimentStarted && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) { 348 Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).Start(); 349 } else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) OnStopped(); 350 else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) OnPaused(); 359 lock (locker) { 360 if (experimentStopped) { 361 if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) OnStopped(); 362 } else { 363 if (experimentStarted && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) { 364 Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).Start(); 365 } else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) OnStopped(); 366 else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) OnPaused(); 367 } 351 368 } 352 369 } 353 370 private void optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) { 354 Runs.RemoveRange(e.OldItems); 355 Runs.AddRange(e.Items); 371 lock (locker) { 372 Runs.RemoveRange(e.OldItems); 373 Runs.AddRange(e.Items); 374 } 356 375 } 357 376 private void optimizer_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) { 358 Runs.AddRange(e.Items); 377 lock (locker) 378 Runs.AddRange(e.Items); 359 379 } 360 380 private void optimizer_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) { 361 Runs.RemoveRange(e.Items); 381 lock (locker) 382 Runs.RemoveRange(e.Items); 362 383 } 363 384
Note: See TracChangeset
for help on using the changeset viewer.