private void ConfigureProblem() { SamplesStart.Value = 0; if (Problem != null) { SamplesEnd.Value = Problem.ProblemData.Dataset.Rows; DataAnalysisProblemData problemData = Problem.ProblemData as DataAnalysisProblemData; if (problemData != null) { problemData.TrainingPartitionParameter.Hidden = true; problemData.TestPartitionParameter.Hidden = true; } ISymbolicDataAnalysisProblem symbolicProblem = Problem as ISymbolicDataAnalysisProblem; if (symbolicProblem != null) { symbolicProblem.FitnessCalculationPartitionParameter.Hidden = true; symbolicProblem.FitnessCalculationPartition.Start = SamplesStart.Value; symbolicProblem.FitnessCalculationPartition.End = SamplesEnd.Value; symbolicProblem.ValidationPartitionParameter.Hidden = true; symbolicProblem.ValidationPartition.Start = 0; symbolicProblem.ValidationPartition.End = 0; } } else { SamplesEnd.Value = 0; } }
public void Start(CancellationToken cancellationToken) { lock (locker) { if (startPending) { return; } startPending = true; } try { if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) { throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); } seed = RandomSeedGenerator.GetSeed(); if (Algorithm == null) { return; } //create cloned algorithms if (clonedAlgorithms.Count == 0) { int testSamplesCount = (SamplesEnd.Value - SamplesStart.Value) / Folds.Value; IDataset shuffledDataset = null; for (int i = 0; i < Folds.Value; i++) { var cloner = new Cloner(); if (ShuffleSamples.Value) { var random = new FastRandom(seed); var dataAnalysisProblem = (IDataAnalysisProblem)algorithm.Problem; var dataset = (Dataset)dataAnalysisProblem.ProblemData.Dataset; shuffledDataset = shuffledDataset ?? dataset.Shuffle(random); cloner.RegisterClonedObject(dataset, shuffledDataset); } IAlgorithm clonedAlgorithm = cloner.Clone(Algorithm); clonedAlgorithm.Name = algorithm.Name + " Fold " + i; IDataAnalysisProblem problem = clonedAlgorithm.Problem as IDataAnalysisProblem; ISymbolicDataAnalysisProblem symbolicProblem = problem as ISymbolicDataAnalysisProblem; int testStart = (i * testSamplesCount) + SamplesStart.Value; int testEnd = (i + 1) == Folds.Value ? SamplesEnd.Value : (i + 1) * testSamplesCount + SamplesStart.Value; problem.ProblemData.TrainingPartition.Start = SamplesStart.Value; problem.ProblemData.TrainingPartition.End = SamplesEnd.Value; problem.ProblemData.TestPartition.Start = testStart; problem.ProblemData.TestPartition.End = testEnd; DataAnalysisProblemData problemData = problem.ProblemData as DataAnalysisProblemData; if (problemData != null) { problemData.TrainingPartitionParameter.Hidden = false; problemData.TestPartitionParameter.Hidden = false; } if (symbolicProblem != null) { symbolicProblem.FitnessCalculationPartition.Start = SamplesStart.Value; symbolicProblem.FitnessCalculationPartition.End = SamplesEnd.Value; } clonedAlgorithm.Prepare(); clonedAlgorithms.Add(clonedAlgorithm); } } OnStarted(); } finally { if (startPending) { startPending = false; } } availableWorkers = new SemaphoreSlim(NumberOfWorkers.Value, NumberOfWorkers.Value); allAlgorithmsFinished = new ManualResetEventSlim(false); var startedTasks = new List <Task>(clonedAlgorithms.Count); //start prepared or paused cloned algorithms foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) { if (pausePending || stopPending || ExecutionState != ExecutionState.Started) { break; } if (clonedAlgorithm.ExecutionState == ExecutionState.Prepared || clonedAlgorithm.ExecutionState == ExecutionState.Paused) { availableWorkers.Wait(); lock (locker) { if (pausePending || stopPending || ExecutionState != ExecutionState.Started) { break; } var task = clonedAlgorithm.StartAsync(cancellationToken); startedTasks.Add(task); } } } allAlgorithmsFinished.Wait(); Task.WaitAll(startedTasks.ToArray()); // to get exceptions not handled within the tasks }
public void Start() { if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) { throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); } if (Algorithm != null) { //create cloned algorithms if (clonedAlgorithms.Count == 0) { int testSamplesCount = (SamplesEnd.Value - SamplesStart.Value) / Folds.Value; for (int i = 0; i < Folds.Value; i++) { IAlgorithm clonedAlgorithm = (IAlgorithm)algorithm.Clone(); clonedAlgorithm.Name = algorithm.Name + " Fold " + i; IDataAnalysisProblem problem = clonedAlgorithm.Problem as IDataAnalysisProblem; ISymbolicDataAnalysisProblem symbolicProblem = problem as ISymbolicDataAnalysisProblem; int testStart = (i * testSamplesCount) + SamplesStart.Value; int testEnd = (i + 1) == Folds.Value ? SamplesEnd.Value : (i + 1) * testSamplesCount + SamplesStart.Value; problem.ProblemData.TrainingPartition.Start = SamplesStart.Value; problem.ProblemData.TrainingPartition.End = SamplesEnd.Value; problem.ProblemData.TestPartition.Start = testStart; problem.ProblemData.TestPartition.End = testEnd; DataAnalysisProblemData problemData = problem.ProblemData as DataAnalysisProblemData; if (problemData != null) { problemData.TrainingPartitionParameter.Hidden = false; problemData.TestPartitionParameter.Hidden = false; } if (symbolicProblem != null) { symbolicProblem.FitnessCalculationPartition.Start = SamplesStart.Value; symbolicProblem.FitnessCalculationPartition.End = SamplesEnd.Value; } clonedAlgorithm.Prepare(); clonedAlgorithms.Add(clonedAlgorithm); } } //start prepared or paused cloned algorithms int startedAlgorithms = 0; foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) { if (startedAlgorithms < NumberOfWorkers.Value) { if (clonedAlgorithm.ExecutionState == ExecutionState.Prepared || clonedAlgorithm.ExecutionState == ExecutionState.Paused) { // start and wait until the alg is started using (var signal = new ManualResetEvent(false)) { EventHandler signalSetter = (sender, args) => { signal.Set(); }; clonedAlgorithm.Started += signalSetter; clonedAlgorithm.Start(); signal.WaitOne(); clonedAlgorithm.Started -= signalSetter; startedAlgorithms++; } } } } OnStarted(); } }