Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/07/10 10:07:12 (14 years ago)
Author:
mkommend
Message:

Filled ResultCollection in CrossValidation (ticket #1199).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Classification/HeuristicLab.Algorithms.DataAnalysis/3.3/CrossValidation.cs

    r4563 r4567  
    4343
    4444      executionState = ExecutionState.Stopped;
    45       executionTime = TimeSpan.Zero;
    4645      runs = new RunCollection();
    4746      runsCounter = 0;
     
    7372    public override IDeepCloneable Clone(Cloner cloner) {
    7473      CrossValidation clone = (CrossValidation)base.Clone(cloner);
    75       clone.DeregisterEvents(); //not necessary
    7674      clone.executionState = executionState;
    77       clone.executionTime = executionTime;
    7875      clone.storeAlgorithmInEachRun = storeAlgorithmInEachRun;
    7976      clone.runs = (RunCollection)cloner.Clone(runs);
     
    8885      return clone;
    8986    }
    90 
    9187    #endregion
    9288
     
    228224    }
    229225
    230     [Storable]
    231     private TimeSpan executionTime;
    232226    public TimeSpan ExecutionTime {
    233227      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;
    242231      }
    243232    }
     
    247236      if (ExecutionState == ExecutionState.Started)
    248237        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
     238      results.Clear();
    249239      clonedAlgorithms.Clear();
    250240      if (Algorithm != null) {
     
    357347        results.Add(result.Name, result.Value);
    358348
    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));
    360350      results.Add("CrossValidation Folds", new RunCollection(runs));
    361351    }
     
    395385      else throw new NotSupportedException("Could not convert any item type to double");
    396386    }
    397 
    398387    #endregion
    399388
     
    404393      SamplesEnd.ValueChanged += new EventHandler(SamplesEnd_ValueChanged);
    405394      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 
    415395    }
    416396    private void Folds_ValueChanged(object sender, EventArgs e) {
     
    526506    private void ClonedAlgorithm_Started(object sender, EventArgs e) {
    527507      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
    528512        if (startPending) {
    529513          int startedAlgorithms = clonedAlgorithms.Count(alg => alg.ExecutionState == ExecutionState.Started);
     
    563547    }
    564548    #endregion
    565 
    566     #region run events
    567     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     #endregion
    609549    #endregion
    610550
     
    625565      EventHandler handler = Prepared;
    626566      if (handler != null) handler(this, EventArgs.Empty);
     567      OnExecutionTimeChanged();
    627568    }
    628569    public event EventHandler Started;
     
    643584    private void OnStopped() {
    644585      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());
    645589      runsCounter++;
    646590      runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this));
Note: See TracChangeset for help on using the changeset viewer.