[5617] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5617] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
[8836] | 26 | using System.Threading;
|
---|
[5617] | 27 | using HeuristicLab.Collections;
|
---|
| 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Data;
|
---|
| 31 | using HeuristicLab.Optimization;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[5886] | 34 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
[14864] | 35 | using HeuristicLab.Random;
|
---|
[5617] | 36 |
|
---|
| 37 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[13238] | 38 | [Item("Cross Validation (CV)", "Cross-validation wrapper for data analysis algorithms.")]
|
---|
[12504] | 39 | [Creatable(CreatableAttribute.Categories.DataAnalysis, Priority = 100)]
|
---|
[5617] | 40 | [StorableClass]
|
---|
| 41 | public sealed class CrossValidation : ParameterizedNamedItem, IAlgorithm, IStorableContent {
|
---|
[15002] | 42 | [Storable]
|
---|
| 43 | private int seed;
|
---|
[14904] | 44 |
|
---|
[5617] | 45 | public CrossValidation()
|
---|
| 46 | : base() {
|
---|
| 47 | name = ItemName;
|
---|
| 48 | description = ItemDescription;
|
---|
| 49 |
|
---|
| 50 | executionState = ExecutionState.Stopped;
|
---|
[8962] | 51 | runs = new RunCollection { OptimizerName = name };
|
---|
[5617] | 52 | runsCounter = 0;
|
---|
| 53 |
|
---|
| 54 | algorithm = null;
|
---|
| 55 | clonedAlgorithms = new ItemCollection<IAlgorithm>();
|
---|
| 56 | results = new ResultCollection();
|
---|
| 57 |
|
---|
| 58 | folds = new IntValue(2);
|
---|
| 59 | numberOfWorkers = new IntValue(1);
|
---|
| 60 | samplesStart = new IntValue(0);
|
---|
| 61 | samplesEnd = new IntValue(0);
|
---|
[14864] | 62 | shuffleSamples = new BoolValue(false);
|
---|
[5617] | 63 | storeAlgorithmInEachRun = false;
|
---|
| 64 |
|
---|
| 65 | RegisterEvents();
|
---|
| 66 | if (Algorithm != null) RegisterAlgorithmEvents();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public string Filename { get; set; }
|
---|
| 70 |
|
---|
| 71 | #region persistence and cloning
|
---|
| 72 | [StorableConstructor]
|
---|
| 73 | private CrossValidation(bool deserializing)
|
---|
| 74 | : base(deserializing) {
|
---|
| 75 | }
|
---|
| 76 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 77 | private void AfterDeserialization() {
|
---|
| 78 | RegisterEvents();
|
---|
| 79 | if (Algorithm != null) RegisterAlgorithmEvents();
|
---|
[15026] | 80 |
|
---|
| 81 | if (shuffleSamples == null)
|
---|
| 82 | shuffleSamples = new BoolValue(false);
|
---|
[5617] | 83 | }
|
---|
| 84 |
|
---|
| 85 | private CrossValidation(CrossValidation original, Cloner cloner)
|
---|
| 86 | : base(original, cloner) {
|
---|
| 87 | executionState = original.executionState;
|
---|
| 88 | storeAlgorithmInEachRun = original.storeAlgorithmInEachRun;
|
---|
| 89 | runs = cloner.Clone(original.runs);
|
---|
| 90 | runsCounter = original.runsCounter;
|
---|
| 91 | algorithm = cloner.Clone(original.algorithm);
|
---|
| 92 | clonedAlgorithms = cloner.Clone(original.clonedAlgorithms);
|
---|
| 93 | results = cloner.Clone(original.results);
|
---|
| 94 |
|
---|
| 95 | folds = cloner.Clone(original.folds);
|
---|
| 96 | numberOfWorkers = cloner.Clone(original.numberOfWorkers);
|
---|
| 97 | samplesStart = cloner.Clone(original.samplesStart);
|
---|
| 98 | samplesEnd = cloner.Clone(original.samplesEnd);
|
---|
[14864] | 99 | shuffleSamples = cloner.Clone(original.shuffleSamples);
|
---|
[15002] | 100 | seed = original.seed;
|
---|
| 101 |
|
---|
[5617] | 102 | RegisterEvents();
|
---|
| 103 | if (Algorithm != null) RegisterAlgorithmEvents();
|
---|
| 104 | }
|
---|
| 105 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 106 | return new CrossValidation(this, cloner);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | #endregion
|
---|
| 110 |
|
---|
| 111 | #region properties
|
---|
| 112 | [Storable]
|
---|
| 113 | private IAlgorithm algorithm;
|
---|
| 114 | public IAlgorithm Algorithm {
|
---|
| 115 | get { return algorithm; }
|
---|
| 116 | set {
|
---|
| 117 | if (ExecutionState != ExecutionState.Prepared && ExecutionState != ExecutionState.Stopped)
|
---|
| 118 | throw new InvalidOperationException("Changing the algorithm is only allowed if the CrossValidation is stopped or prepared.");
|
---|
| 119 | if (algorithm != value) {
|
---|
[5659] | 120 | if (value != null && value.Problem != null && !(value.Problem is IDataAnalysisProblem))
|
---|
[5617] | 121 | throw new ArgumentException("Only algorithms with a DataAnalysisProblem could be used for the cross validation.");
|
---|
| 122 | if (algorithm != null) DeregisterAlgorithmEvents();
|
---|
| 123 | algorithm = value;
|
---|
| 124 | Parameters.Clear();
|
---|
| 125 |
|
---|
| 126 | if (algorithm != null) {
|
---|
| 127 | algorithm.StoreAlgorithmInEachRun = false;
|
---|
| 128 | RegisterAlgorithmEvents();
|
---|
| 129 | algorithm.Prepare(true);
|
---|
| 130 | Parameters.AddRange(algorithm.Parameters);
|
---|
| 131 | }
|
---|
| 132 | OnAlgorithmChanged();
|
---|
| 133 | Prepare();
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 |
|
---|
| 139 | [Storable]
|
---|
[5659] | 140 | private IDataAnalysisProblem problem;
|
---|
| 141 | public IDataAnalysisProblem Problem {
|
---|
[5617] | 142 | get {
|
---|
| 143 | if (algorithm == null)
|
---|
| 144 | return null;
|
---|
[5659] | 145 | return (IDataAnalysisProblem)algorithm.Problem;
|
---|
[5617] | 146 | }
|
---|
| 147 | set {
|
---|
| 148 | if (ExecutionState != ExecutionState.Prepared && ExecutionState != ExecutionState.Stopped)
|
---|
| 149 | throw new InvalidOperationException("Changing the problem is only allowed if the CrossValidation is stopped or prepared.");
|
---|
| 150 | if (algorithm == null) throw new ArgumentNullException("Could not set a problem before an algorithm was set.");
|
---|
| 151 | algorithm.Problem = value;
|
---|
| 152 | problem = value;
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | IProblem IAlgorithm.Problem {
|
---|
| 157 | get { return Problem; }
|
---|
| 158 | set {
|
---|
| 159 | if (value != null && !ProblemType.IsInstanceOfType(value))
|
---|
| 160 | throw new ArgumentException("Only DataAnalysisProblems could be used for the cross validation.");
|
---|
[5659] | 161 | Problem = (IDataAnalysisProblem)value;
|
---|
[5617] | 162 | }
|
---|
| 163 | }
|
---|
| 164 | public Type ProblemType {
|
---|
[5659] | 165 | get { return typeof(IDataAnalysisProblem); }
|
---|
[5617] | 166 | }
|
---|
| 167 |
|
---|
| 168 | [Storable]
|
---|
| 169 | private ItemCollection<IAlgorithm> clonedAlgorithms;
|
---|
| 170 |
|
---|
| 171 | public IEnumerable<IOptimizer> NestedOptimizers {
|
---|
| 172 | get {
|
---|
| 173 | if (Algorithm == null) yield break;
|
---|
| 174 | yield return Algorithm;
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | [Storable]
|
---|
| 179 | private ResultCollection results;
|
---|
| 180 | public ResultCollection Results {
|
---|
| 181 | get { return results; }
|
---|
| 182 | }
|
---|
| 183 | [Storable]
|
---|
[14864] | 184 | private BoolValue shuffleSamples;
|
---|
| 185 | public BoolValue ShuffleSamples {
|
---|
| 186 | get { return shuffleSamples; }
|
---|
| 187 | }
|
---|
| 188 | [Storable]
|
---|
[5617] | 189 | private IntValue folds;
|
---|
| 190 | public IntValue Folds {
|
---|
| 191 | get { return folds; }
|
---|
| 192 | }
|
---|
| 193 | [Storable]
|
---|
| 194 | private IntValue samplesStart;
|
---|
| 195 | public IntValue SamplesStart {
|
---|
| 196 | get { return samplesStart; }
|
---|
| 197 | }
|
---|
| 198 | [Storable]
|
---|
| 199 | private IntValue samplesEnd;
|
---|
| 200 | public IntValue SamplesEnd {
|
---|
| 201 | get { return samplesEnd; }
|
---|
| 202 | }
|
---|
| 203 | [Storable]
|
---|
| 204 | private IntValue numberOfWorkers;
|
---|
| 205 | public IntValue NumberOfWorkers {
|
---|
| 206 | get { return numberOfWorkers; }
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | [Storable]
|
---|
| 210 | private bool storeAlgorithmInEachRun;
|
---|
| 211 | public bool StoreAlgorithmInEachRun {
|
---|
| 212 | get { return storeAlgorithmInEachRun; }
|
---|
| 213 | set {
|
---|
| 214 | if (storeAlgorithmInEachRun != value) {
|
---|
| 215 | storeAlgorithmInEachRun = value;
|
---|
| 216 | OnStoreAlgorithmInEachRunChanged();
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | [Storable]
|
---|
| 222 | private int runsCounter;
|
---|
| 223 | [Storable]
|
---|
| 224 | private RunCollection runs;
|
---|
| 225 | public RunCollection Runs {
|
---|
| 226 | get { return runs; }
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | [Storable]
|
---|
| 230 | private ExecutionState executionState;
|
---|
| 231 | public ExecutionState ExecutionState {
|
---|
| 232 | get { return executionState; }
|
---|
| 233 | private set {
|
---|
| 234 | if (executionState != value) {
|
---|
| 235 | executionState = value;
|
---|
| 236 | OnExecutionStateChanged();
|
---|
| 237 | OnItemImageChanged();
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
[7201] | 241 | public static new Image StaticItemImage {
|
---|
| 242 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
|
---|
| 243 | }
|
---|
[5617] | 244 | public override Image ItemImage {
|
---|
| 245 | get {
|
---|
| 246 | if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePrepared;
|
---|
| 247 | else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStarted;
|
---|
| 248 | else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePaused;
|
---|
| 249 | else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStopped;
|
---|
[7201] | 250 | else return base.ItemImage;
|
---|
[5617] | 251 | }
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | public TimeSpan ExecutionTime {
|
---|
| 255 | get {
|
---|
| 256 | if (ExecutionState != ExecutionState.Prepared)
|
---|
| 257 | return TimeSpan.FromMilliseconds(clonedAlgorithms.Select(x => x.ExecutionTime.TotalMilliseconds).Sum());
|
---|
| 258 | return TimeSpan.Zero;
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 | #endregion
|
---|
| 262 |
|
---|
[8738] | 263 | protected override void OnNameChanged() {
|
---|
| 264 | base.OnNameChanged();
|
---|
[8962] | 265 | Runs.OptimizerName = Name;
|
---|
[8738] | 266 | }
|
---|
| 267 |
|
---|
[5617] | 268 | public void Prepare() {
|
---|
| 269 | if (ExecutionState == ExecutionState.Started)
|
---|
| 270 | throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
|
---|
| 271 | results.Clear();
|
---|
| 272 | clonedAlgorithms.Clear();
|
---|
| 273 | if (Algorithm != null) {
|
---|
| 274 | Algorithm.Prepare();
|
---|
| 275 | if (Algorithm.ExecutionState == ExecutionState.Prepared) OnPrepared();
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 | public void Prepare(bool clearRuns) {
|
---|
| 279 | if (clearRuns) runs.Clear();
|
---|
| 280 | Prepare();
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | public void Start() {
|
---|
| 284 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
|
---|
| 285 | throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
|
---|
| 286 |
|
---|
[15002] | 287 | seed = new FastRandom().NextInt();
|
---|
| 288 |
|
---|
[9493] | 289 | if (Algorithm != null) {
|
---|
[5617] | 290 | //create cloned algorithms
|
---|
| 291 | if (clonedAlgorithms.Count == 0) {
|
---|
| 292 | int testSamplesCount = (SamplesEnd.Value - SamplesStart.Value) / Folds.Value;
|
---|
[14864] | 293 | IDataset shuffledDataset = null;
|
---|
[5617] | 294 | for (int i = 0; i < Folds.Value; i++) {
|
---|
[14864] | 295 | var cloner = new Cloner();
|
---|
| 296 | if (ShuffleSamples.Value) {
|
---|
[15002] | 297 | var random = new FastRandom(seed);
|
---|
[14864] | 298 | var dataAnalysisProblem = (IDataAnalysisProblem)algorithm.Problem;
|
---|
| 299 | var dataset = (Dataset)dataAnalysisProblem.ProblemData.Dataset;
|
---|
[15002] | 300 | shuffledDataset = shuffledDataset ?? dataset.Shuffle(random);
|
---|
[14864] | 301 | cloner.RegisterClonedObject(dataset, shuffledDataset);
|
---|
| 302 | }
|
---|
| 303 | IAlgorithm clonedAlgorithm = cloner.Clone(Algorithm);
|
---|
[5617] | 304 | clonedAlgorithm.Name = algorithm.Name + " Fold " + i;
|
---|
[5659] | 305 | IDataAnalysisProblem problem = clonedAlgorithm.Problem as IDataAnalysisProblem;
|
---|
[5886] | 306 | ISymbolicDataAnalysisProblem symbolicProblem = problem as ISymbolicDataAnalysisProblem;
|
---|
| 307 |
|
---|
| 308 | int testStart = (i * testSamplesCount) + SamplesStart.Value;
|
---|
| 309 | int testEnd = (i + 1) == Folds.Value ? SamplesEnd.Value : (i + 1) * testSamplesCount + SamplesStart.Value;
|
---|
| 310 |
|
---|
[7107] | 311 | problem.ProblemData.TrainingPartition.Start = SamplesStart.Value;
|
---|
| 312 | problem.ProblemData.TrainingPartition.End = SamplesEnd.Value;
|
---|
[5886] | 313 | problem.ProblemData.TestPartition.Start = testStart;
|
---|
| 314 | problem.ProblemData.TestPartition.End = testEnd;
|
---|
| 315 | DataAnalysisProblemData problemData = problem.ProblemData as DataAnalysisProblemData;
|
---|
| 316 | if (problemData != null) {
|
---|
| 317 | problemData.TrainingPartitionParameter.Hidden = false;
|
---|
| 318 | problemData.TestPartitionParameter.Hidden = false;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | if (symbolicProblem != null) {
|
---|
| 322 | symbolicProblem.FitnessCalculationPartition.Start = SamplesStart.Value;
|
---|
| 323 | symbolicProblem.FitnessCalculationPartition.End = SamplesEnd.Value;
|
---|
| 324 | }
|
---|
[7738] | 325 | clonedAlgorithm.Prepare();
|
---|
[5617] | 326 | clonedAlgorithms.Add(clonedAlgorithm);
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | //start prepared or paused cloned algorithms
|
---|
| 331 | int startedAlgorithms = 0;
|
---|
| 332 | foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) {
|
---|
| 333 | if (startedAlgorithms < NumberOfWorkers.Value) {
|
---|
| 334 | if (clonedAlgorithm.ExecutionState == ExecutionState.Prepared ||
|
---|
| 335 | clonedAlgorithm.ExecutionState == ExecutionState.Paused) {
|
---|
[8836] | 336 |
|
---|
| 337 | // start and wait until the alg is started
|
---|
| 338 | using (var signal = new ManualResetEvent(false)) {
|
---|
| 339 | EventHandler signalSetter = (sender, args) => { signal.Set(); };
|
---|
| 340 | clonedAlgorithm.Started += signalSetter;
|
---|
| 341 | clonedAlgorithm.Start();
|
---|
| 342 | signal.WaitOne();
|
---|
| 343 | clonedAlgorithm.Started -= signalSetter;
|
---|
| 344 |
|
---|
| 345 | startedAlgorithms++;
|
---|
| 346 | }
|
---|
[5617] | 347 | }
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
| 350 | OnStarted();
|
---|
| 351 | }
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | private bool pausePending;
|
---|
| 355 | public void Pause() {
|
---|
| 356 | if (ExecutionState != ExecutionState.Started)
|
---|
| 357 | throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
|
---|
| 358 | if (!pausePending) {
|
---|
| 359 | pausePending = true;
|
---|
[9493] | 360 | PauseAllClonedAlgorithms();
|
---|
[5617] | 361 | }
|
---|
| 362 | }
|
---|
| 363 | private void PauseAllClonedAlgorithms() {
|
---|
| 364 | foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) {
|
---|
| 365 | if (clonedAlgorithm.ExecutionState == ExecutionState.Started)
|
---|
| 366 | clonedAlgorithm.Pause();
|
---|
| 367 | }
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | private bool stopPending;
|
---|
| 371 | public void Stop() {
|
---|
| 372 | if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
|
---|
| 373 | throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".",
|
---|
| 374 | ExecutionState));
|
---|
| 375 | if (!stopPending) {
|
---|
| 376 | stopPending = true;
|
---|
[9493] | 377 | StopAllClonedAlgorithms();
|
---|
[5617] | 378 | }
|
---|
| 379 | }
|
---|
| 380 | private void StopAllClonedAlgorithms() {
|
---|
| 381 | foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) {
|
---|
| 382 | if (clonedAlgorithm.ExecutionState == ExecutionState.Started ||
|
---|
| 383 | clonedAlgorithm.ExecutionState == ExecutionState.Paused)
|
---|
| 384 | clonedAlgorithm.Stop();
|
---|
| 385 | }
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | #region collect parameters and results
|
---|
| 389 | public override void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
| 390 | values.Add("Algorithm Name", new StringValue(Name));
|
---|
| 391 | values.Add("Algorithm Type", new StringValue(GetType().GetPrettyName()));
|
---|
| 392 | values.Add("Folds", new IntValue(Folds.Value));
|
---|
| 393 |
|
---|
| 394 | if (algorithm != null) {
|
---|
| 395 | values.Add("CrossValidation Algorithm Name", new StringValue(Algorithm.Name));
|
---|
| 396 | values.Add("CrossValidation Algorithm Type", new StringValue(Algorithm.GetType().GetPrettyName()));
|
---|
| 397 | base.CollectParameterValues(values);
|
---|
| 398 | }
|
---|
| 399 | if (Problem != null) {
|
---|
| 400 | values.Add("Problem Name", new StringValue(Problem.Name));
|
---|
| 401 | values.Add("Problem Type", new StringValue(Problem.GetType().GetPrettyName()));
|
---|
| 402 | Problem.CollectParameterValues(values);
|
---|
| 403 | }
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | public void CollectResultValues(IDictionary<string, IItem> results) {
|
---|
[6566] | 407 | var clonedResults = (ResultCollection)this.results.Clone();
|
---|
| 408 | foreach (var result in clonedResults) {
|
---|
| 409 | results.Add(result.Name, result.Value);
|
---|
| 410 | }
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | private void AggregateResultValues(IDictionary<string, IItem> results) {
|
---|
[5617] | 414 | IEnumerable<IRun> runs = clonedAlgorithms.Select(alg => alg.Runs.FirstOrDefault()).Where(run => run != null);
|
---|
| 415 | IEnumerable<KeyValuePair<string, IItem>> resultCollections = runs.Where(x => x != null).SelectMany(x => x.Results).ToList();
|
---|
| 416 |
|
---|
| 417 | foreach (IResult result in ExtractAndAggregateResults<IntValue>(resultCollections))
|
---|
| 418 | results.Add(result.Name, result.Value);
|
---|
| 419 | foreach (IResult result in ExtractAndAggregateResults<DoubleValue>(resultCollections))
|
---|
| 420 | results.Add(result.Name, result.Value);
|
---|
| 421 | foreach (IResult result in ExtractAndAggregateResults<PercentValue>(resultCollections))
|
---|
| 422 | results.Add(result.Name, result.Value);
|
---|
[6184] | 423 | foreach (IResult result in ExtractAndAggregateRegressionSolutions(resultCollections)) {
|
---|
| 424 | results.Add(result.Name, result.Value);
|
---|
| 425 | }
|
---|
[6239] | 426 | foreach (IResult result in ExtractAndAggregateClassificationSolutions(resultCollections)) {
|
---|
| 427 | results.Add(result.Name, result.Value);
|
---|
| 428 | }
|
---|
[5617] | 429 | results.Add("Execution Time", new TimeSpanValue(this.ExecutionTime));
|
---|
| 430 | results.Add("CrossValidation Folds", new RunCollection(runs));
|
---|
| 431 | }
|
---|
| 432 |
|
---|
[6184] | 433 | private IEnumerable<IResult> ExtractAndAggregateRegressionSolutions(IEnumerable<KeyValuePair<string, IItem>> resultCollections) {
|
---|
| 434 | Dictionary<string, List<IRegressionSolution>> resultSolutions = new Dictionary<string, List<IRegressionSolution>>();
|
---|
| 435 | foreach (var result in resultCollections) {
|
---|
| 436 | var regressionSolution = result.Value as IRegressionSolution;
|
---|
| 437 | if (regressionSolution != null) {
|
---|
| 438 | if (resultSolutions.ContainsKey(result.Key)) {
|
---|
| 439 | resultSolutions[result.Key].Add(regressionSolution);
|
---|
| 440 | } else {
|
---|
| 441 | resultSolutions.Add(result.Key, new List<IRegressionSolution>() { regressionSolution });
|
---|
| 442 | }
|
---|
| 443 | }
|
---|
| 444 | }
|
---|
| 445 | List<IResult> aggregatedResults = new List<IResult>();
|
---|
| 446 | foreach (KeyValuePair<string, List<IRegressionSolution>> solutions in resultSolutions) {
|
---|
[6566] | 447 | // clone manually to correctly clone references between cloned root objects
|
---|
| 448 | Cloner cloner = new Cloner();
|
---|
[15002] | 449 | if (ShuffleSamples.Value) {
|
---|
| 450 | var dataset = (Dataset)Problem.ProblemData.Dataset;
|
---|
| 451 | var random = new FastRandom(seed);
|
---|
| 452 | var shuffledDataset = dataset.Shuffle(random);
|
---|
| 453 | cloner.RegisterClonedObject(dataset, shuffledDataset);
|
---|
| 454 | }
|
---|
| 455 | var problemDataClone = (IRegressionProblemData)cloner.Clone(Problem.ProblemData);
|
---|
[6566] | 456 | // set partitions of problem data clone correctly
|
---|
[6184] | 457 | problemDataClone.TrainingPartition.Start = SamplesStart.Value; problemDataClone.TrainingPartition.End = SamplesEnd.Value;
|
---|
| 458 | problemDataClone.TestPartition.Start = SamplesStart.Value; problemDataClone.TestPartition.End = SamplesEnd.Value;
|
---|
[6566] | 459 | // clone models
|
---|
[7738] | 460 | var ensembleSolution = new RegressionEnsembleSolution(problemDataClone);
|
---|
| 461 | ensembleSolution.AddRegressionSolutions(solutions.Value);
|
---|
[6184] | 462 |
|
---|
[6250] | 463 | aggregatedResults.Add(new Result(solutions.Key + " (ensemble)", ensembleSolution));
|
---|
[6184] | 464 | }
|
---|
[6250] | 465 | List<IResult> flattenedResults = new List<IResult>();
|
---|
| 466 | CollectResultsRecursively("", aggregatedResults, flattenedResults);
|
---|
| 467 | return flattenedResults;
|
---|
[6184] | 468 | }
|
---|
| 469 |
|
---|
[6239] | 470 | private IEnumerable<IResult> ExtractAndAggregateClassificationSolutions(IEnumerable<KeyValuePair<string, IItem>> resultCollections) {
|
---|
| 471 | Dictionary<string, List<IClassificationSolution>> resultSolutions = new Dictionary<string, List<IClassificationSolution>>();
|
---|
| 472 | foreach (var result in resultCollections) {
|
---|
| 473 | var classificationSolution = result.Value as IClassificationSolution;
|
---|
| 474 | if (classificationSolution != null) {
|
---|
| 475 | if (resultSolutions.ContainsKey(result.Key)) {
|
---|
| 476 | resultSolutions[result.Key].Add(classificationSolution);
|
---|
| 477 | } else {
|
---|
| 478 | resultSolutions.Add(result.Key, new List<IClassificationSolution>() { classificationSolution });
|
---|
| 479 | }
|
---|
| 480 | }
|
---|
| 481 | }
|
---|
[6250] | 482 | var aggregatedResults = new List<IResult>();
|
---|
[6239] | 483 | foreach (KeyValuePair<string, List<IClassificationSolution>> solutions in resultSolutions) {
|
---|
[14781] | 484 | // at least one algorithm (GBT with logistic regression loss) produces a classification solution even though the original problem is a regression problem.
|
---|
| 485 | var targetVariable = solutions.Value.First().ProblemData.TargetVariable;
|
---|
[15002] | 486 | var dataset = (Dataset)Problem.ProblemData.Dataset;
|
---|
| 487 | if (ShuffleSamples.Value) {
|
---|
| 488 | var random = new FastRandom(seed);
|
---|
| 489 | dataset = dataset.Shuffle(random);
|
---|
| 490 | }
|
---|
| 491 | var problemDataClone = new ClassificationProblemData(dataset, Problem.ProblemData.AllowedInputVariables, targetVariable);
|
---|
[6566] | 492 | // set partitions of problem data clone correctly
|
---|
[6239] | 493 | problemDataClone.TrainingPartition.Start = SamplesStart.Value; problemDataClone.TrainingPartition.End = SamplesEnd.Value;
|
---|
| 494 | problemDataClone.TestPartition.Start = SamplesStart.Value; problemDataClone.TestPartition.End = SamplesEnd.Value;
|
---|
[6566] | 495 | // clone models
|
---|
[8528] | 496 | var ensembleSolution = new ClassificationEnsembleSolution(problemDataClone);
|
---|
| 497 | ensembleSolution.AddClassificationSolutions(solutions.Value);
|
---|
[6239] | 498 |
|
---|
[6250] | 499 | aggregatedResults.Add(new Result(solutions.Key + " (ensemble)", ensembleSolution));
|
---|
[6239] | 500 | }
|
---|
[6250] | 501 | List<IResult> flattenedResults = new List<IResult>();
|
---|
| 502 | CollectResultsRecursively("", aggregatedResults, flattenedResults);
|
---|
| 503 | return flattenedResults;
|
---|
[6239] | 504 | }
|
---|
| 505 |
|
---|
[6250] | 506 | private void CollectResultsRecursively(string path, IEnumerable<IResult> results, IList<IResult> flattenedResults) {
|
---|
| 507 | foreach (IResult result in results) {
|
---|
| 508 | flattenedResults.Add(new Result(path + result.Name, result.Value));
|
---|
| 509 | ResultCollection childCollection = result.Value as ResultCollection;
|
---|
| 510 | if (childCollection != null) {
|
---|
| 511 | CollectResultsRecursively(path + result.Name + ".", childCollection, flattenedResults);
|
---|
| 512 | }
|
---|
| 513 | }
|
---|
| 514 | }
|
---|
| 515 |
|
---|
[5617] | 516 | private static IEnumerable<IResult> ExtractAndAggregateResults<T>(IEnumerable<KeyValuePair<string, IItem>> results)
|
---|
| 517 | where T : class, IItem, new() {
|
---|
| 518 | Dictionary<string, List<double>> resultValues = new Dictionary<string, List<double>>();
|
---|
| 519 | foreach (var resultValue in results.Where(r => r.Value.GetType() == typeof(T))) {
|
---|
| 520 | if (!resultValues.ContainsKey(resultValue.Key))
|
---|
| 521 | resultValues[resultValue.Key] = new List<double>();
|
---|
| 522 | resultValues[resultValue.Key].Add(ConvertToDouble(resultValue.Value));
|
---|
| 523 | }
|
---|
| 524 |
|
---|
| 525 | DoubleValue doubleValue;
|
---|
| 526 | if (typeof(T) == typeof(PercentValue))
|
---|
| 527 | doubleValue = new PercentValue();
|
---|
| 528 | else if (typeof(T) == typeof(DoubleValue))
|
---|
| 529 | doubleValue = new DoubleValue();
|
---|
| 530 | else if (typeof(T) == typeof(IntValue))
|
---|
| 531 | doubleValue = new DoubleValue();
|
---|
| 532 | else
|
---|
| 533 | throw new NotSupportedException();
|
---|
| 534 |
|
---|
| 535 | List<IResult> aggregatedResults = new List<IResult>();
|
---|
| 536 | foreach (KeyValuePair<string, List<double>> resultValue in resultValues) {
|
---|
| 537 | doubleValue.Value = resultValue.Value.Average();
|
---|
[6250] | 538 | aggregatedResults.Add(new Result(resultValue.Key + " (average)", (IItem)doubleValue.Clone()));
|
---|
[5617] | 539 | doubleValue.Value = resultValue.Value.StandardDeviation();
|
---|
[6250] | 540 | aggregatedResults.Add(new Result(resultValue.Key + " (std.dev.)", (IItem)doubleValue.Clone()));
|
---|
[5617] | 541 | }
|
---|
| 542 | return aggregatedResults;
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 | private static double ConvertToDouble(IItem item) {
|
---|
| 546 | if (item is DoubleValue) return ((DoubleValue)item).Value;
|
---|
| 547 | else if (item is IntValue) return ((IntValue)item).Value;
|
---|
| 548 | else throw new NotSupportedException("Could not convert any item type to double");
|
---|
| 549 | }
|
---|
| 550 | #endregion
|
---|
| 551 |
|
---|
| 552 | #region events
|
---|
| 553 | private void RegisterEvents() {
|
---|
| 554 | Folds.ValueChanged += new EventHandler(Folds_ValueChanged);
|
---|
| 555 | RegisterClonedAlgorithmsEvents();
|
---|
| 556 | }
|
---|
| 557 | private void Folds_ValueChanged(object sender, EventArgs e) {
|
---|
| 558 | if (ExecutionState != ExecutionState.Prepared)
|
---|
| 559 | throw new InvalidOperationException("Can not change number of folds if the execution state is not prepared.");
|
---|
| 560 | }
|
---|
[7107] | 561 |
|
---|
[5617] | 562 |
|
---|
| 563 | #region template algorithms events
|
---|
| 564 | public event EventHandler AlgorithmChanged;
|
---|
| 565 | private void OnAlgorithmChanged() {
|
---|
| 566 | EventHandler handler = AlgorithmChanged;
|
---|
| 567 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 568 | OnProblemChanged();
|
---|
| 569 | if (Problem == null) ExecutionState = ExecutionState.Stopped;
|
---|
| 570 | }
|
---|
| 571 | private void RegisterAlgorithmEvents() {
|
---|
| 572 | algorithm.ProblemChanged += new EventHandler(Algorithm_ProblemChanged);
|
---|
| 573 | algorithm.ExecutionStateChanged += new EventHandler(Algorithm_ExecutionStateChanged);
|
---|
[14904] | 574 | if (Problem != null) {
|
---|
| 575 | Problem.Reset += new EventHandler(Problem_Reset);
|
---|
| 576 | }
|
---|
[5617] | 577 | }
|
---|
| 578 | private void DeregisterAlgorithmEvents() {
|
---|
| 579 | algorithm.ProblemChanged -= new EventHandler(Algorithm_ProblemChanged);
|
---|
| 580 | algorithm.ExecutionStateChanged -= new EventHandler(Algorithm_ExecutionStateChanged);
|
---|
[14904] | 581 | if (Problem != null) {
|
---|
| 582 | Problem.Reset -= new EventHandler(Problem_Reset);
|
---|
| 583 | }
|
---|
[5617] | 584 | }
|
---|
| 585 | private void Algorithm_ProblemChanged(object sender, EventArgs e) {
|
---|
[5659] | 586 | if (algorithm.Problem != null && !(algorithm.Problem is IDataAnalysisProblem)) {
|
---|
[5617] | 587 | algorithm.Problem = problem;
|
---|
| 588 | throw new ArgumentException("A cross validation algorithm can only contain DataAnalysisProblems.");
|
---|
| 589 | }
|
---|
[8970] | 590 | if (problem != null) problem.Reset -= new EventHandler(Problem_Reset);
|
---|
[5659] | 591 | problem = (IDataAnalysisProblem)algorithm.Problem;
|
---|
[9517] | 592 | if (problem != null) problem.Reset += new EventHandler(Problem_Reset);
|
---|
[5617] | 593 | OnProblemChanged();
|
---|
| 594 | }
|
---|
| 595 | public event EventHandler ProblemChanged;
|
---|
| 596 | private void OnProblemChanged() {
|
---|
| 597 | EventHandler handler = ProblemChanged;
|
---|
| 598 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[8969] | 599 | ConfigureProblem();
|
---|
| 600 | }
|
---|
| 601 | private void Problem_Reset(object sender, EventArgs e) {
|
---|
| 602 | ConfigureProblem();
|
---|
| 603 | }
|
---|
[8970] | 604 | private void ConfigureProblem() {
|
---|
| 605 | SamplesStart.Value = 0;
|
---|
[5806] | 606 | if (Problem != null) {
|
---|
[5617] | 607 | SamplesEnd.Value = Problem.ProblemData.Dataset.Rows;
|
---|
[5886] | 608 |
|
---|
| 609 | DataAnalysisProblemData problemData = Problem.ProblemData as DataAnalysisProblemData;
|
---|
| 610 | if (problemData != null) {
|
---|
| 611 | problemData.TrainingPartitionParameter.Hidden = true;
|
---|
| 612 | problemData.TestPartitionParameter.Hidden = true;
|
---|
| 613 | }
|
---|
| 614 | ISymbolicDataAnalysisProblem symbolicProblem = Problem as ISymbolicDataAnalysisProblem;
|
---|
| 615 | if (symbolicProblem != null) {
|
---|
| 616 | symbolicProblem.FitnessCalculationPartitionParameter.Hidden = true;
|
---|
| 617 | symbolicProblem.FitnessCalculationPartition.Start = SamplesStart.Value;
|
---|
| 618 | symbolicProblem.FitnessCalculationPartition.End = SamplesEnd.Value;
|
---|
| 619 | symbolicProblem.ValidationPartitionParameter.Hidden = true;
|
---|
| 620 | symbolicProblem.ValidationPartition.Start = 0;
|
---|
| 621 | symbolicProblem.ValidationPartition.End = 0;
|
---|
| 622 | }
|
---|
[5806] | 623 | } else
|
---|
[5617] | 624 | SamplesEnd.Value = 0;
|
---|
| 625 | }
|
---|
| 626 |
|
---|
| 627 | private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
| 628 | switch (Algorithm.ExecutionState) {
|
---|
[14864] | 629 | case ExecutionState.Prepared:
|
---|
| 630 | OnPrepared();
|
---|
[5617] | 631 | break;
|
---|
| 632 | case ExecutionState.Started: throw new InvalidOperationException("Algorithm template can not be started.");
|
---|
| 633 | case ExecutionState.Paused: throw new InvalidOperationException("Algorithm template can not be paused.");
|
---|
[14864] | 634 | case ExecutionState.Stopped:
|
---|
| 635 | OnStopped();
|
---|
[5617] | 636 | break;
|
---|
| 637 | }
|
---|
| 638 | }
|
---|
| 639 | #endregion
|
---|
| 640 |
|
---|
| 641 | #region clonedAlgorithms events
|
---|
| 642 | private void RegisterClonedAlgorithmsEvents() {
|
---|
| 643 | clonedAlgorithms.ItemsAdded += new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_ItemsAdded);
|
---|
| 644 | clonedAlgorithms.ItemsRemoved += new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_ItemsRemoved);
|
---|
| 645 | clonedAlgorithms.CollectionReset += new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_CollectionReset);
|
---|
| 646 | foreach (IAlgorithm algorithm in clonedAlgorithms)
|
---|
| 647 | RegisterClonedAlgorithmEvents(algorithm);
|
---|
| 648 | }
|
---|
| 649 | private void DeregisterClonedAlgorithmsEvents() {
|
---|
| 650 | clonedAlgorithms.ItemsAdded -= new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_ItemsAdded);
|
---|
| 651 | clonedAlgorithms.ItemsRemoved -= new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_ItemsRemoved);
|
---|
| 652 | clonedAlgorithms.CollectionReset -= new CollectionItemsChangedEventHandler<IAlgorithm>(ClonedAlgorithms_CollectionReset);
|
---|
| 653 | foreach (IAlgorithm algorithm in clonedAlgorithms)
|
---|
| 654 | DeregisterClonedAlgorithmEvents(algorithm);
|
---|
| 655 | }
|
---|
| 656 | private void ClonedAlgorithms_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IAlgorithm> e) {
|
---|
| 657 | foreach (IAlgorithm algorithm in e.Items)
|
---|
| 658 | RegisterClonedAlgorithmEvents(algorithm);
|
---|
| 659 | }
|
---|
| 660 | private void ClonedAlgorithms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IAlgorithm> e) {
|
---|
| 661 | foreach (IAlgorithm algorithm in e.Items)
|
---|
| 662 | DeregisterClonedAlgorithmEvents(algorithm);
|
---|
| 663 | }
|
---|
| 664 | private void ClonedAlgorithms_CollectionReset(object sender, CollectionItemsChangedEventArgs<IAlgorithm> e) {
|
---|
| 665 | foreach (IAlgorithm algorithm in e.OldItems)
|
---|
| 666 | DeregisterClonedAlgorithmEvents(algorithm);
|
---|
| 667 | foreach (IAlgorithm algorithm in e.Items)
|
---|
| 668 | RegisterClonedAlgorithmEvents(algorithm);
|
---|
| 669 | }
|
---|
| 670 | private void RegisterClonedAlgorithmEvents(IAlgorithm algorithm) {
|
---|
| 671 | algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(ClonedAlgorithm_ExceptionOccurred);
|
---|
| 672 | algorithm.ExecutionTimeChanged += new EventHandler(ClonedAlgorithm_ExecutionTimeChanged);
|
---|
| 673 | algorithm.Started += new EventHandler(ClonedAlgorithm_Started);
|
---|
| 674 | algorithm.Paused += new EventHandler(ClonedAlgorithm_Paused);
|
---|
| 675 | algorithm.Stopped += new EventHandler(ClonedAlgorithm_Stopped);
|
---|
| 676 | }
|
---|
| 677 | private void DeregisterClonedAlgorithmEvents(IAlgorithm algorithm) {
|
---|
| 678 | algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(ClonedAlgorithm_ExceptionOccurred);
|
---|
| 679 | algorithm.ExecutionTimeChanged -= new EventHandler(ClonedAlgorithm_ExecutionTimeChanged);
|
---|
| 680 | algorithm.Started -= new EventHandler(ClonedAlgorithm_Started);
|
---|
| 681 | algorithm.Paused -= new EventHandler(ClonedAlgorithm_Paused);
|
---|
| 682 | algorithm.Stopped -= new EventHandler(ClonedAlgorithm_Stopped);
|
---|
| 683 | }
|
---|
| 684 | private void ClonedAlgorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
| 685 | OnExceptionOccurred(e.Value);
|
---|
| 686 | }
|
---|
| 687 | private void ClonedAlgorithm_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
| 688 | OnExecutionTimeChanged();
|
---|
| 689 | }
|
---|
| 690 |
|
---|
| 691 | private readonly object locker = new object();
|
---|
[9504] | 692 | private readonly object resultLocker = new object();
|
---|
[5617] | 693 | private void ClonedAlgorithm_Started(object sender, EventArgs e) {
|
---|
[9493] | 694 | IAlgorithm algorithm = sender as IAlgorithm;
|
---|
[9504] | 695 | lock (resultLocker) {
|
---|
| 696 | if (algorithm != null && !results.ContainsKey(algorithm.Name))
|
---|
| 697 | results.Add(new Result(algorithm.Name, "Contains results for the specific fold.", algorithm.Results));
|
---|
| 698 | }
|
---|
[5617] | 699 | }
|
---|
| 700 |
|
---|
| 701 | private void ClonedAlgorithm_Paused(object sender, EventArgs e) {
|
---|
| 702 | lock (locker) {
|
---|
| 703 | if (pausePending && clonedAlgorithms.All(alg => alg.ExecutionState != ExecutionState.Started))
|
---|
| 704 | OnPaused();
|
---|
| 705 | }
|
---|
| 706 | }
|
---|
| 707 |
|
---|
| 708 | private void ClonedAlgorithm_Stopped(object sender, EventArgs e) {
|
---|
| 709 | lock (locker) {
|
---|
| 710 | if (!stopPending && ExecutionState == ExecutionState.Started) {
|
---|
[8965] | 711 | IAlgorithm preparedAlgorithm = clonedAlgorithms.FirstOrDefault(alg => alg.ExecutionState == ExecutionState.Prepared ||
|
---|
| 712 | alg.ExecutionState == ExecutionState.Paused);
|
---|
[5617] | 713 | if (preparedAlgorithm != null) preparedAlgorithm.Start();
|
---|
| 714 | }
|
---|
| 715 | if (ExecutionState != ExecutionState.Stopped) {
|
---|
| 716 | if (clonedAlgorithms.All(alg => alg.ExecutionState == ExecutionState.Stopped))
|
---|
| 717 | OnStopped();
|
---|
| 718 | else if (stopPending &&
|
---|
| 719 | clonedAlgorithms.All(
|
---|
| 720 | alg => alg.ExecutionState == ExecutionState.Prepared || alg.ExecutionState == ExecutionState.Stopped))
|
---|
| 721 | OnStopped();
|
---|
| 722 | }
|
---|
| 723 | }
|
---|
| 724 | }
|
---|
| 725 | #endregion
|
---|
| 726 | #endregion
|
---|
| 727 |
|
---|
| 728 | #region event firing
|
---|
| 729 | public event EventHandler ExecutionStateChanged;
|
---|
| 730 | private void OnExecutionStateChanged() {
|
---|
| 731 | EventHandler handler = ExecutionStateChanged;
|
---|
| 732 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 733 | }
|
---|
| 734 | public event EventHandler ExecutionTimeChanged;
|
---|
| 735 | private void OnExecutionTimeChanged() {
|
---|
| 736 | EventHandler handler = ExecutionTimeChanged;
|
---|
| 737 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 738 | }
|
---|
| 739 | public event EventHandler Prepared;
|
---|
| 740 | private void OnPrepared() {
|
---|
| 741 | ExecutionState = ExecutionState.Prepared;
|
---|
| 742 | EventHandler handler = Prepared;
|
---|
| 743 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 744 | OnExecutionTimeChanged();
|
---|
| 745 | }
|
---|
| 746 | public event EventHandler Started;
|
---|
| 747 | private void OnStarted() {
|
---|
| 748 | ExecutionState = ExecutionState.Started;
|
---|
| 749 | EventHandler handler = Started;
|
---|
| 750 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 751 | }
|
---|
| 752 | public event EventHandler Paused;
|
---|
| 753 | private void OnPaused() {
|
---|
| 754 | pausePending = false;
|
---|
| 755 | ExecutionState = ExecutionState.Paused;
|
---|
| 756 | EventHandler handler = Paused;
|
---|
| 757 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 758 | }
|
---|
| 759 | public event EventHandler Stopped;
|
---|
| 760 | private void OnStopped() {
|
---|
| 761 | stopPending = false;
|
---|
| 762 | Dictionary<string, IItem> collectedResults = new Dictionary<string, IItem>();
|
---|
[6566] | 763 | AggregateResultValues(collectedResults);
|
---|
[5617] | 764 | results.AddRange(collectedResults.Select(x => new Result(x.Key, x.Value)).Cast<IResult>().ToArray());
|
---|
[14864] | 765 | clonedAlgorithms.Clear();
|
---|
[5617] | 766 | runsCounter++;
|
---|
| 767 | runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this));
|
---|
| 768 | ExecutionState = ExecutionState.Stopped;
|
---|
| 769 | EventHandler handler = Stopped;
|
---|
| 770 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 771 | }
|
---|
| 772 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
| 773 | private void OnExceptionOccurred(Exception exception) {
|
---|
| 774 | EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
|
---|
| 775 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
| 776 | }
|
---|
| 777 | public event EventHandler StoreAlgorithmInEachRunChanged;
|
---|
| 778 | private void OnStoreAlgorithmInEachRunChanged() {
|
---|
| 779 | EventHandler handler = StoreAlgorithmInEachRunChanged;
|
---|
| 780 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 781 | }
|
---|
| 782 | #endregion
|
---|
| 783 | }
|
---|
| 784 | }
|
---|