Changeset 8956
- Timestamp:
- 11/28/12 01:59:10 (12 years ago)
- Location:
- branches/RuntimeOptimizer
- Files:
-
- 1 deleted
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/RuntimeOptimizer/HeuristicLab.Optimization.Views/3.3/HeuristicLab.Optimization.Views-3.3.csproj
r8955 r8956 398 398 </ProjectReference> 399 399 </ItemGroup> 400 <ItemGroup>401 <EmbeddedResource Include="TimeLimitRunView.resx">402 <DependentUpon>TimeLimitRunView.cs</DependentUpon>403 </EmbeddedResource>404 </ItemGroup>405 400 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 406 401 <!-- To modify your build process, add your task inside one of the targets below and uncomment it. -
branches/RuntimeOptimizer/HeuristicLab.Optimization.Views/3.3/TimeLimitRunView.Designer.cs
r8955 r8956 50 50 this.runsView = new HeuristicLab.Optimization.Views.RunCollectionView(); 51 51 this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); 52 this.snapshotButton = new System.Windows.Forms.Button(); 52 53 ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).BeginInit(); 53 54 this.tabControl.SuspendLayout(); … … 109 110 this.timeLimitTextBox.Size = new System.Drawing.Size(338, 20); 110 111 this.timeLimitTextBox.TabIndex = 17; 111 this.timeLimitTextBox.Leave += new System.EventHandler(this.timeLimitTextBox_Leave);112 112 this.timeLimitTextBox.Validating += new System.ComponentModel.CancelEventHandler(this.timeLimitTextBox_Validating); 113 113 // … … 127 127 this.snapShotsTextBox.Location = new System.Drawing.Point(69, 52); 128 128 this.snapShotsTextBox.Name = "snapShotsTextBox"; 129 this.snapShotsTextBox.Size = new System.Drawing.Size(14 8, 20);129 this.snapShotsTextBox.Size = new System.Drawing.Size(142, 20); 130 130 this.snapShotsTextBox.TabIndex = 17; 131 131 // … … 266 266 this.openFileDialog.Title = "Open Optimizer"; 267 267 // 268 // snapshotButton 269 // 270 this.snapshotButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 271 this.snapshotButton.Location = new System.Drawing.Point(120, 440); 272 this.snapshotButton.Name = "snapshotButton"; 273 this.snapshotButton.Size = new System.Drawing.Size(24, 24); 274 this.snapshotButton.TabIndex = 20; 275 this.snapshotButton.Text = "Snapshot"; 276 this.snapshotButton.UseVisualStyleBackColor = true; 277 this.snapshotButton.Click += new System.EventHandler(this.snapshotButton_Click); 278 // 268 279 // TimeLimitRunView 269 280 // 270 this.AutoScale Dimensions = new System.Drawing.SizeF(6F, 13F);271 this. AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;281 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit; 282 this.Controls.Add(this.snapshotButton); 272 283 this.Controls.Add(this.tabControl); 273 284 this.Controls.Add(this.timeLimitTextBox); … … 283 294 this.Controls.SetChildIndex(this.timeLimitLabel, 0); 284 295 this.Controls.SetChildIndex(this.timeLimitTextBox, 0); 296 this.Controls.SetChildIndex(this.tabControl, 0); 285 297 this.Controls.SetChildIndex(this.nameLabel, 0); 286 298 this.Controls.SetChildIndex(this.nameTextBox, 0); … … 292 304 this.Controls.SetChildIndex(this.executionTimeTextBox, 0); 293 305 this.Controls.SetChildIndex(this.startButton, 0); 294 this.Controls.SetChildIndex(this. tabControl, 0);306 this.Controls.SetChildIndex(this.snapshotButton, 0); 295 307 ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).EndInit(); 296 308 this.tabControl.ResumeLayout(false); … … 320 332 private RunCollectionView runsView; 321 333 private System.Windows.Forms.OpenFileDialog openFileDialog; 334 private System.Windows.Forms.Button snapshotButton; 322 335 } 323 336 } -
branches/RuntimeOptimizer/HeuristicLab.Optimization.Views/3.3/TimeLimitRunView.cs
r8955 r8956 25 25 using System.Windows.Forms; 26 26 using HeuristicLab.Common; 27 using HeuristicLab.Common.Resources; 27 28 using HeuristicLab.Core; 28 29 using HeuristicLab.Core.Views; … … 45 46 public TimeLimitRunView() { 46 47 InitializeComponent(); 48 snapshotButton.Text = String.Empty; 49 snapshotButton.Image = VSImageLibrary.Breakpoint; 47 50 } 48 51 … … 69 72 try { 70 73 if (Content == null) { 71 timeLimitTextBox.Tag = TimeSpan.FromSeconds(60); 72 timeLimitTextBox.Text = FormatTimeSpan((TimeSpan)timeLimitTextBox.Tag); 74 timeLimitTextBox.Text = FormatTimeSpan(TimeSpan.FromSeconds(60)); 73 75 snapShotsTextBox.Text = String.Empty; 74 76 storeAlgorithmInEachSnapshotCheckBox.Checked = false; … … 77 79 runsView.Content = null; 78 80 } else { 79 timeLimitTextBox.Tag = Content.MaximumExecutionTime;80 81 timeLimitTextBox.Text = FormatTimeSpan(Content.MaximumExecutionTime); 81 82 snapShotsTextBox.Text = String.Join(" ; ", Content.SnapshotTimes); … … 87 88 } finally { SuppressEvents = false; } 88 89 } 90 89 91 protected override void SetEnabledStateOfControls() { 90 92 base.SetEnabledStateOfControls(); … … 99 101 } 100 102 103 protected override void SetEnabledStateOfExecutableButtons() { 104 base.SetEnabledStateOfExecutableButtons(); 105 snapshotButton.Enabled = Content != null && (Content.ExecutionState == ExecutionState.Started || Content.ExecutionState == ExecutionState.Paused); 106 } 107 101 108 protected override void OnClosed(FormClosedEventArgs e) { 102 109 if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) { … … 151 158 152 159 #region Control events 153 private void timeLimitTextBox_Leave(object sender, EventArgs e) {154 if (SuppressEvents) return;155 SuppressEvents = true;156 try {157 timeLimitTextBox.Text = FormatTimeSpan((TimeSpan)timeLimitTextBox.Tag);158 } finally { SuppressEvents = false; }159 }160 161 160 private void timeLimitTextBox_Validating(object sender, CancelEventArgs e) { 162 161 if (SuppressEvents) return; … … 179 178 ts = GetTimeSpanFromFormat(value, text.Substring(length - 1, text.Length - (length - 1)).TrimStart()); 180 179 } else ts = TimeSpan.FromSeconds(value); 181 timeLimitTextBox.Tag= ts;180 Content.MaximumExecutionTime = ts; 182 181 e.Cancel = false; 183 182 errorProvider.SetError(timeLimitTextBox, String.Empty); … … 252 251 } 253 252 } 253 254 private void snapshotButton_Click(object sender, EventArgs e) { 255 Content.Snapshot(); 256 } 254 257 #endregion 255 258 #endregion -
branches/RuntimeOptimizer/HeuristicLab.Optimization/3.3/MetaOptimizers/TimeLimitRun.cs
r8955 r8956 26 26 using System.Linq; 27 27 using System.Threading; 28 using System.Threading.Tasks; 28 29 using System.Timers; 29 30 using HeuristicLab.Collections; … … 55 56 #endregion 56 57 57 private readonly Timer snapTimer;58 private readonly Timer runTimer;58 private Timer snapTimer, runTimer; 59 private readonly ManualResetEvent algorithmStarted = new ManualResetEvent(false); 59 60 private readonly ManualResetEvent algorithmPaused = new ManualResetEvent(false); 60 61 private readonly ManualResetEvent algorithmStopped = new ManualResetEvent(false); 62 [Storable] 63 private bool snapshotTimesDirty; 61 64 62 65 [Storable] … … 81 84 if (snapshotTimes == value) return; 82 85 snapshotTimes = value; 83 snapshotTimes Index = 0;86 snapshotTimesDirty = true; 84 87 OnPropertyChanged("SnapshotTimes"); 85 88 } … … 124 127 set { 125 128 if (algorithm == value) return; 126 if (algorithm != null) { 127 DeregisterAlgorithmEvents(); 128 var runs = algorithm.Runs; 129 algorithm = null; //necessary to avoid removing the runs from the old algorithm 130 Runs.RemoveRange(runs); 131 } 129 if (algorithm != null) DeregisterAlgorithmEvents(); 132 130 algorithm = value; 133 131 if (algorithm != null) { 134 132 RegisterAlgorithmEvents(); 135 Runs.AddRange(algorithm.Runs); 133 if (algorithm.ExecutionState == ExecutionState.Started) algorithmStarted.Set(); 134 else algorithmStarted.Reset(); 136 135 if (algorithm.ExecutionState == ExecutionState.Paused) algorithmPaused.Set(); 137 136 else algorithmPaused.Reset(); … … 167 166 168 167 [StorableConstructor] 169 private TimeLimitRun(bool deserializing) 170 : base(deserializing) { 171 // the timer will not be serialized 172 snapTimer = new Timer() { 173 AutoReset = false, 174 Enabled = false, 175 Interval = double.MaxValue 176 }; 177 runTimer = new Timer() { 178 AutoReset = false, 179 Enabled = false, 180 Interval = double.MaxValue 181 }; 182 } 168 private TimeLimitRun(bool deserializing) : base(deserializing) { } 183 169 private TimeLimitRun(TimeLimitRun original, Cloner cloner) 184 170 : base(original, cloner) { … … 186 172 snapshotTimes = new ObservableList<double>(original.snapshotTimes); 187 173 snapshotTimesIndex = original.snapshotTimesIndex; 188 snapTimer = new Timer() {189 AutoReset = original.snapTimer.AutoReset,190 Enabled = original.snapTimer.Enabled,191 Interval = original.snapTimer.Interval192 };193 runTimer = new Timer() {194 AutoReset = original.runTimer.AutoReset,195 Enabled = original.runTimer.Enabled,196 Interval = original.runTimer.Interval197 };198 174 snapshots = cloner.Clone(original.snapshots); 175 snapshotTimesDirty = original.snapshotTimesDirty; 199 176 storeAlgorithmInEachSnapshot = original.storeAlgorithmInEachSnapshot; 200 177 algorithm = cloner.Clone(original.algorithm); 201 178 runs = cloner.Clone(original.runs); 179 180 snapTimer = new Timer() { 181 AutoReset = original.snapTimer.AutoReset, 182 Interval = original.snapTimer.Interval 183 }; 184 runTimer = new Timer() { 185 AutoReset = original.runTimer.AutoReset, 186 Interval = original.runTimer.Interval 187 }; 188 if (original.algorithm != null && original.algorithm.ExecutionState == ExecutionState.Started) 189 algorithmStarted.Set(); 190 if (original.algorithm != null && original.algorithm.ExecutionState == ExecutionState.Paused) 191 algorithmPaused.Set(); 192 if (original.algorithm != null && original.algorithm.ExecutionState == ExecutionState.Stopped) 193 algorithmStopped.Set(); 194 202 195 Initialize(); 203 196 } … … 215 208 AutoReset = false, 216 209 Enabled = false, 217 Interval = snapshotTimes[snapshotTimesIndex]210 Interval = 100 218 211 }; 219 212 runTimer = new Timer() { … … 224 217 snapshots = new RunCollection(); 225 218 Runs = new RunCollection { AlgorithmName = Name }; 219 Initialize(); 226 220 } 227 221 public TimeLimitRun(string name) … … 237 231 AutoReset = false, 238 232 Enabled = false, 239 Interval = snapshotTimes[snapshotTimesIndex]233 Interval = 100 240 234 }; 241 235 snapshots = new RunCollection(); … … 246 240 }; 247 241 Runs = new RunCollection { AlgorithmName = Name }; 242 Initialize(); 248 243 } 249 244 public TimeLimitRun(string name, string description) … … 258 253 AutoReset = false, 259 254 Enabled = false, 260 Interval = snapshotTimes[snapshotTimesIndex]255 Interval = 100 261 256 }; 262 257 snapshots = new RunCollection(); … … 267 262 }; 268 263 Runs = new RunCollection { AlgorithmName = Name }; 264 Initialize(); 269 265 } 270 266 … … 276 272 [StorableHook(HookType.AfterDeserialization)] 277 273 private void AfterDeserialization() { 274 // the timers will not be serialized 275 snapTimer = new Timer() { 276 AutoReset = false, 277 Enabled = false, 278 Interval = 100 279 }; 280 runTimer = new Timer() { 281 AutoReset = false, 282 Enabled = false, 283 Interval = double.MaxValue 284 }; 285 if (Algorithm != null && Algorithm.ExecutionState == ExecutionState.Started) 286 algorithmStarted.Set(); 287 else algorithmStarted.Reset(); 288 if (Algorithm != null && Algorithm.ExecutionState == ExecutionState.Paused) 289 algorithmPaused.Set(); 290 else algorithmPaused.Reset(); 291 if (Algorithm != null && Algorithm.ExecutionState == ExecutionState.Stopped) 292 algorithmStopped.Set(); 293 else algorithmStopped.Reset(); 278 294 Initialize(); 279 295 } … … 281 297 private void Initialize() { 282 298 if (algorithm != null) RegisterAlgorithmEvents(); 299 snapshotTimes.ItemsAdded += snapshotTimes_Changed; 300 snapshotTimes.ItemsMoved += snapshotTimes_Changed; 301 snapshotTimes.ItemsRemoved += snapshotTimes_Changed; 302 snapshotTimes.ItemsReplaced += snapshotTimes_Changed; 303 snapshotTimes.CollectionReset += snapshotTimes_Changed; 283 304 snapTimer.Elapsed += snapshotTimer_Elapsed; 284 305 runTimer.Elapsed += runTimer_Elapsed; 285 306 } 286 307 287 private readonly object timerLock = new object(); 308 private void snapshotTimes_Changed(object sender, EventArgs e) { 309 snapshotTimesDirty = true; 310 } 311 312 private readonly object snapshotLock = new object(); 288 313 private void snapshotTimer_Elapsed(object sender, ElapsedEventArgs e) { 289 lock (timerLock) { 290 runTimer.Stop(); 291 if (Algorithm == null) return; 292 if (ExecutionState != ExecutionState.Started) return; 314 snapTimer.Stop(); 315 lock (snapshotLock) { 316 try { 317 if (Algorithm == null) return; 318 if (ExecutionState != ExecutionState.Started) return; 319 320 var execTime = ExecutionTime; 321 if (snapshotTimesIndex < SnapshotTimes.Count && 322 SnapshotTimes[snapshotTimesIndex] <= execTime.TotalMilliseconds) { 323 DoSnapshot(); 324 snapshotTimesIndex++; 325 try { 326 Algorithm.Start(); 327 algorithmStarted.WaitOne(); 328 } catch (InvalidOperationException) { } 329 } 330 } catch { } 331 snapTimer.Start(); 332 } 333 } 334 335 private void runTimer_Elapsed(object sender, ElapsedEventArgs e) { 336 snapTimer.Stop(); 337 lock (snapshotLock) { 338 try { 339 Algorithm.Stop(); 340 algorithmStopped.WaitOne(); 341 } catch (InvalidOperationException) { } 342 } 343 } 344 345 private void FindNextSnapshotTimeIndex(TimeSpan reference) { 346 var index = 0; 347 while (index < snapshotTimes.Count && (snapshotTimes[index] - reference.TotalMilliseconds) <= 0) { 348 index++; 349 }; 350 snapshotTimesIndex = index; 351 } 352 353 private void DoSnapshot() { 354 lock (snapshotLock) { 355 var wasPaused = Algorithm.ExecutionState == ExecutionState.Paused; 293 356 var error = false; 294 try { Algorithm.Pause(); } catch (InvalidOperationException) { error = true; } 357 try { 358 Algorithm.Pause(); 359 algorithmPaused.WaitOne(); 360 } catch (InvalidOperationException) { 361 error = true; 362 } 295 363 if (error && ExecutionState != ExecutionState.Paused) return; // Algorithm is either stopped or prepared 296 364 297 algorithmPaused.WaitOne();298 365 MakeSnapshot(); 299 try { Algorithm.Start(); } catch (InvalidOperationException) { } 300 } 301 } 302 303 private void runTimer_Elapsed(object sender, ElapsedEventArgs e) { 304 lock (timerLock) { 305 runTimer.Stop(); 306 snapTimer.Stop(); 307 try { Algorithm.Stop(); } catch (InvalidOperationException) { } 308 } 309 } 310 311 private double? GetNextInterval(TimeSpan reference) { 312 double interval; 313 while ((interval = snapshotTimes[snapshotTimesIndex] - reference.TotalMilliseconds) <= 0) { 314 snapshotTimesIndex++; 315 if (snapshotTimesIndex >= snapshotTimes.Count) return null; 316 }; 317 return interval; 366 if (!wasPaused) { 367 try { 368 Algorithm.Start(); 369 algorithmStarted.WaitOne(); 370 } catch (InvalidOperationException) { } 371 } 372 } 318 373 } 319 374 320 375 private void MakeSnapshot() { 321 string runName = ExecutionTime.ToString() + " " + algorithm.Name + " Snapshot"; 376 string time = Math.Round(ExecutionTime.TotalSeconds, 1).ToString("0.0"); 377 string runName = "Snapshot " + time + "s " + algorithm.Name; 322 378 Run run; 323 379 if (StoreAlgorithmInEachSnapshot) { 324 run = new Run(runName, algorithm); 380 var temporarilySetStoreFlag = !Algorithm.StoreAlgorithmInEachRun; 381 if (temporarilySetStoreFlag) Algorithm.StoreAlgorithmInEachRun = true; 382 run = new Run(runName, Algorithm); 383 if (temporarilySetStoreFlag) Algorithm.StoreAlgorithmInEachRun = false; 325 384 } else { 326 385 // duplicate code of Run.Initialize - necessary, because Algorithm property doesn't have a setter … … 339 398 } 340 399 400 public void Snapshot() { 401 Task.Factory.StartNew(DoSnapshot); 402 } 403 341 404 public void Prepare() { 342 405 Prepare(false); … … 434 497 } 435 498 private void Algorithm_Paused(object sender, EventArgs e) { 499 algorithmStarted.Reset(); 436 500 algorithmPaused.Set(); 437 501 algorithmStopped.Reset(); … … 439 503 } 440 504 private void Algorithm_Prepared(object sender, EventArgs e) { 505 algorithmStarted.Reset(); 441 506 algorithmPaused.Reset(); 442 507 algorithmStopped.Reset(); … … 448 513 } 449 514 private void Algorithm_Started(object sender, EventArgs e) { 515 algorithmStarted.Set(); 450 516 algorithmPaused.Reset(); 451 517 algorithmStopped.Reset(); 452 518 var execTime = ExecutionTime; 453 var interval = GetNextInterval(execTime);454 if (interval.HasValue) {455 snap Timer.Interval = interval.Value;456 snapTimer.Start();519 if (snapshotTimesDirty) { 520 SnapshotTimes.Sort(); 521 snapshotTimesDirty = false; // sort will mark it dirty again 522 FindNextSnapshotTimeIndex(execTime); 457 523 } 458 524 runTimer.Interval = (MaximumExecutionTime - execTime).TotalMilliseconds; 525 snapTimer.Start(); 459 526 runTimer.Start(); 460 527 OnStarted(); 461 528 } 462 529 private void Algorithm_Stopped(object sender, EventArgs e) { 530 algorithmStarted.Reset(); 463 531 algorithmPaused.Reset(); 464 532 algorithmStopped.Set(); 465 snapTimer.Stop(); 466 runTimer.Stop(); 467 MakeSnapshot(); 468 var snapshotsRun = new Run() { Name = algorithm.Runs.Last().Name }; 469 foreach (var s in snapshots) 470 snapshotsRun.Parameters.Add(s.Name, s); 471 Runs.Add(snapshotsRun); 533 lock (snapshotLock) { 534 snapTimer.Stop(); 535 runTimer.Stop(); 536 MakeSnapshot(); 537 } 538 var cloner = new Cloner(); 539 var algRun = cloner.Clone(Algorithm.Runs.Last()); 540 var clonedSnapshots = cloner.Clone(snapshots); 541 try { 542 algRun.Results.Add("Snapshots", clonedSnapshots); 543 } catch (ArgumentException ex) { 544 // TODO strategy? 545 } 546 Runs.Add(algRun); 547 Algorithm.Runs.Clear(); 472 548 OnStopped(); 473 549 }
Note: See TracChangeset
for help on using the changeset viewer.