Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/06/17 11:39:20 (7 years ago)
Author:
gkronber
Message:

#2760: merged r14864, r14865, r14904, r15002, r15026, r15077, r15111 from trunk to stable

File:
1 edited

Legend:

Unmodified
Added
Removed
  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/CrossValidation.cs

    r15149 r15150  
    3333using HeuristicLab.Problems.DataAnalysis;
    3434using HeuristicLab.Problems.DataAnalysis.Symbolic;
     35using HeuristicLab.Random;
    3536
    3637namespace HeuristicLab.Algorithms.DataAnalysis {
     
    3940  [StorableClass]
    4041  public sealed class CrossValidation : ParameterizedNamedItem, IAlgorithm, IStorableContent {
     42    [Storable]
     43    private int seed;
     44
    4145    public CrossValidation()
    4246      : base() {
     
    5660      samplesStart = new IntValue(0);
    5761      samplesEnd = new IntValue(0);
     62      shuffleSamples = new BoolValue(false);
    5863      storeAlgorithmInEachRun = false;
    5964
     
    7176    [StorableHook(HookType.AfterDeserialization)]
    7277    private void AfterDeserialization() {
     78      // BackwardsCompatibility3.3
     79      #region Backwards compatible code, remove with 3.4
     80      if (shuffleSamples == null) shuffleSamples = new BoolValue(false);
     81      #endregion
     82
    7383      RegisterEvents();
    7484      if (Algorithm != null) RegisterAlgorithmEvents();
     
    8999      samplesStart = cloner.Clone(original.samplesStart);
    90100      samplesEnd = cloner.Clone(original.samplesEnd);
     101      shuffleSamples = cloner.Clone(original.shuffleSamples);
     102      seed = original.seed;
     103
    91104      RegisterEvents();
    92105      if (Algorithm != null) RegisterAlgorithmEvents();
     
    170183      get { return results; }
    171184    }
    172 
     185    [Storable]
     186    private BoolValue shuffleSamples;
     187    public BoolValue ShuffleSamples {
     188      get { return shuffleSamples; }
     189    }
    173190    [Storable]
    174191    private IntValue folds;
     
    270287        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
    271288
     289      seed = new FastRandom().NextInt();
     290
    272291      if (Algorithm != null) {
    273292        //create cloned algorithms
    274293        if (clonedAlgorithms.Count == 0) {
    275294          int testSamplesCount = (SamplesEnd.Value - SamplesStart.Value) / Folds.Value;
    276 
     295          IDataset shuffledDataset = null;
    277296          for (int i = 0; i < Folds.Value; i++) {
    278             IAlgorithm clonedAlgorithm = (IAlgorithm)algorithm.Clone();
     297            var cloner = new Cloner();
     298            if (ShuffleSamples.Value) {
     299              var random = new FastRandom(seed);
     300              var dataAnalysisProblem = (IDataAnalysisProblem)algorithm.Problem;
     301              var dataset = (Dataset)dataAnalysisProblem.ProblemData.Dataset;
     302              shuffledDataset = shuffledDataset ?? dataset.Shuffle(random);
     303              cloner.RegisterClonedObject(dataset, shuffledDataset);
     304            }
     305            IAlgorithm clonedAlgorithm = cloner.Clone(Algorithm);
    279306            clonedAlgorithm.Name = algorithm.Name + " Fold " + i;
    280307            IDataAnalysisProblem problem = clonedAlgorithm.Problem as IDataAnalysisProblem;
     
    422449        // clone manually to correctly clone references between cloned root objects
    423450        Cloner cloner = new Cloner();
     451        if (ShuffleSamples.Value) {
     452          var dataset = (Dataset)Problem.ProblemData.Dataset;
     453          var random = new FastRandom(seed);
     454          var shuffledDataset = dataset.Shuffle(random);
     455          cloner.RegisterClonedObject(dataset, shuffledDataset);
     456        }
    424457        var problemDataClone = (IRegressionProblemData)cloner.Clone(Problem.ProblemData);
    425458        // set partitions of problem data clone correctly
     
    453486        // at least one algorithm (GBT with logistic regression loss) produces a classification solution even though the original problem is a regression problem.
    454487        var targetVariable = solutions.Value.First().ProblemData.TargetVariable;
    455         var problemDataClone = new ClassificationProblemData(Problem.ProblemData.Dataset,
    456           Problem.ProblemData.AllowedInputVariables, targetVariable);
     488        var dataset = (Dataset)Problem.ProblemData.Dataset;
     489        if (ShuffleSamples.Value) {
     490          var random = new FastRandom(seed);
     491          dataset = dataset.Shuffle(random);
     492        }
     493        var problemDataClone = new ClassificationProblemData(dataset, Problem.ProblemData.AllowedInputVariables, targetVariable);
    457494        // set partitions of problem data clone correctly
    458495        problemDataClone.TrainingPartition.Start = SamplesStart.Value; problemDataClone.TrainingPartition.End = SamplesEnd.Value;
     
    537574      algorithm.ProblemChanged += new EventHandler(Algorithm_ProblemChanged);
    538575      algorithm.ExecutionStateChanged += new EventHandler(Algorithm_ExecutionStateChanged);
    539       if (Problem != null) Problem.Reset += new EventHandler(Problem_Reset);
     576      if (Problem != null) {
     577        Problem.Reset += new EventHandler(Problem_Reset);
     578      }
    540579    }
    541580    private void DeregisterAlgorithmEvents() {
    542581      algorithm.ProblemChanged -= new EventHandler(Algorithm_ProblemChanged);
    543582      algorithm.ExecutionStateChanged -= new EventHandler(Algorithm_ExecutionStateChanged);
    544       if (Problem != null) Problem.Reset -= new EventHandler(Problem_Reset);
     583      if (Problem != null) {
     584        Problem.Reset -= new EventHandler(Problem_Reset);
     585      }
    545586    }
    546587    private void Algorithm_ProblemChanged(object sender, EventArgs e) {
     
    560601      ConfigureProblem();
    561602    }
    562 
    563603    private void Problem_Reset(object sender, EventArgs e) {
    564604      ConfigureProblem();
    565605    }
    566 
    567606    private void ConfigureProblem() {
    568607      SamplesStart.Value = 0;
     
    590629    private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
    591630      switch (Algorithm.ExecutionState) {
    592         case ExecutionState.Prepared: OnPrepared();
     631        case ExecutionState.Prepared:
     632          OnPrepared();
    593633          break;
    594634        case ExecutionState.Started: throw new InvalidOperationException("Algorithm template can not be started.");
    595635        case ExecutionState.Paused: throw new InvalidOperationException("Algorithm template can not be paused.");
    596         case ExecutionState.Stopped: OnStopped();
     636        case ExecutionState.Stopped:
     637          OnStopped();
    597638          break;
    598639      }
     
    724765      AggregateResultValues(collectedResults);
    725766      results.AddRange(collectedResults.Select(x => new Result(x.Key, x.Value)).Cast<IResult>().ToArray());
     767      clonedAlgorithms.Clear();
    726768      runsCounter++;
    727769      runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this));
Note: See TracChangeset for help on using the changeset viewer.