- Timestamp:
- 10/07/10 10:07:12 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Classification/HeuristicLab.Algorithms.DataAnalysis/3.3/CrossValidation.cs
r4563 r4567 43 43 44 44 executionState = ExecutionState.Stopped; 45 executionTime = TimeSpan.Zero;46 45 runs = new RunCollection(); 47 46 runsCounter = 0; … … 73 72 public override IDeepCloneable Clone(Cloner cloner) { 74 73 CrossValidation clone = (CrossValidation)base.Clone(cloner); 75 clone.DeregisterEvents(); //not necessary76 74 clone.executionState = executionState; 77 clone.executionTime = executionTime;78 75 clone.storeAlgorithmInEachRun = storeAlgorithmInEachRun; 79 76 clone.runs = (RunCollection)cloner.Clone(runs); … … 88 85 return clone; 89 86 } 90 91 87 #endregion 92 88 … … 228 224 } 229 225 230 [Storable]231 private TimeSpan executionTime;232 226 public TimeSpan ExecutionTime { 233 227 get { 234 if (ExecutionState != ExecutionState.Stopped && ExecutionState != ExecutionState.Prepared) 235 return executionTime + TimeSpan.FromMilliseconds(clonedAlgorithms.Select(x => x.ExecutionTime.TotalMilliseconds).Sum()); 236 else 237 return executionTime; 238 } 239 private set { 240 executionTime = value; 241 OnExecutionTimeChanged(); 228 if (ExecutionState != ExecutionState.Prepared) 229 return TimeSpan.FromMilliseconds(clonedAlgorithms.Select(x => x.ExecutionTime.TotalMilliseconds).Sum()); 230 return TimeSpan.Zero; 242 231 } 243 232 } … … 247 236 if (ExecutionState == ExecutionState.Started) 248 237 throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState)); 238 results.Clear(); 249 239 clonedAlgorithms.Clear(); 250 240 if (Algorithm != null) { … … 357 347 results.Add(result.Name, result.Value); 358 348 359 results.Add("Execution Time", new TimeSpanValue( TimeSpan.FromMilliseconds(clonedAlgorithms.Select(x => x.ExecutionTime.TotalMilliseconds).Sum())));349 results.Add("Execution Time", new TimeSpanValue(this.ExecutionTime)); 360 350 results.Add("CrossValidation Folds", new RunCollection(runs)); 361 351 } … … 395 385 else throw new NotSupportedException("Could not convert any item type to double"); 396 386 } 397 398 387 #endregion 399 388 … … 404 393 SamplesEnd.ValueChanged += new EventHandler(SamplesEnd_ValueChanged); 405 394 RegisterClonedAlgorithmsEvents(); 406 RegisterRunsEvents();407 }408 private void DeregisterEvents() {409 Folds.ValueChanged -= new EventHandler(Folds_ValueChanged);410 SamplesStart.ValueChanged -= new EventHandler(SamplesStart_ValueChanged);411 SamplesEnd.ValueChanged -= new EventHandler(SamplesEnd_ValueChanged);412 DeregisterClonedAlgorithmsEvents();413 DeregisterRunsEvents();414 415 395 } 416 396 private void Folds_ValueChanged(object sender, EventArgs e) { … … 526 506 private void ClonedAlgorithm_Started(object sender, EventArgs e) { 527 507 lock (locker) { 508 IAlgorithm algorithm = sender as IAlgorithm; 509 if (algorithm != null && !results.ContainsKey(algorithm.Name)) 510 results.Add(new Result(algorithm.Name, "Contains results for the specific fold.", algorithm.Results)); 511 528 512 if (startPending) { 529 513 int startedAlgorithms = clonedAlgorithms.Count(alg => alg.ExecutionState == ExecutionState.Started); … … 563 547 } 564 548 #endregion 565 566 #region run events567 private void RegisterRunsEvents() {568 runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);569 runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsAdded);570 runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);571 }572 private void DeregisterRunsEvents() {573 runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);574 runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsAdded);575 runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);576 }577 private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {578 foreach (IRun run in e.OldItems) {579 IItem item;580 run.Results.TryGetValue("Execution Time", out item);581 TimeSpanValue executionTime = item as TimeSpanValue;582 if (executionTime != null) ExecutionTime = this.executionTime - executionTime.Value;583 }584 foreach (IRun run in e.Items) {585 IItem item;586 run.Results.TryGetValue("Execution Time", out item);587 TimeSpanValue executionTime = item as TimeSpanValue;588 if (executionTime != null) ExecutionTime = this.executionTime + executionTime.Value;589 }590 runsCounter = Runs.Count;591 }592 private void Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {593 foreach (IRun run in e.Items) {594 IItem item;595 run.Results.TryGetValue("Execution Time", out item);596 TimeSpanValue executionTime = item as TimeSpanValue;597 if (executionTime != null) this.executionTime = this.executionTime + executionTime.Value;598 }599 }600 private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {601 foreach (IRun run in e.Items) {602 IItem item;603 run.Results.TryGetValue("Execution Time", out item);604 TimeSpanValue executionTime = item as TimeSpanValue;605 if (executionTime != null) ExecutionTime = this.executionTime - executionTime.Value;606 }607 }608 #endregion609 549 #endregion 610 550 … … 625 565 EventHandler handler = Prepared; 626 566 if (handler != null) handler(this, EventArgs.Empty); 567 OnExecutionTimeChanged(); 627 568 } 628 569 public event EventHandler Started; … … 643 584 private void OnStopped() { 644 585 stopPending = false; 586 Dictionary<string, IItem> collectedResults = new Dictionary<string, IItem>(); 587 CollectResultValues(collectedResults); 588 results.AddRange(collectedResults.Select(x => new Result(x.Key, x.Value)).Cast<IResult>().ToArray()); 645 589 runsCounter++; 646 590 runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this));
Note: See TracChangeset
for help on using the changeset viewer.