public void Start_AnyTaskWithException_Exception() { var pipeline = ""; var target = new ParallelTaskExecutor(); target.Add(() => { throw new Exception("1"); }); target.Add(() => { Thread.Sleep(5); pipeline += "2"; }); target.Add(() => { Thread.Sleep(5); pipeline += "3"; }); Assert.Catch <Exception>(() => { target.Start(); }, "1"); }
public void Start_ParallelManySlowFitness_Timeout() { var taskExecutor = new ParallelTaskExecutor(); taskExecutor.MinThreads = 100; taskExecutor.MaxThreads = 100; taskExecutor.Timeout = TimeSpan.FromMilliseconds(1000); var selection = new RouletteWheelSelection(); var crossover = new OnePointCrossover(1); var mutation = new UniformMutation(); var chromosome = new ChromosomeStub(); var target = new GeneticAlgorithm(new Population(100, 150, chromosome), new FitnessStub() { SupportsParallel = true, ParallelSleep = 1500 }, selection, crossover, mutation); target.TaskExecutor = taskExecutor; Assert.Catch <TimeoutException>(() => { target.Start(); }, "The fitness evaluation reached the 00:00:01 timeout."); Assert.IsFalse(target.IsRunning); Assert.AreEqual(GeneticAlgorithmState.Stopped, target.State); }
public void Start_ManyTasksWithGreaterNumberOfThreads_ParallelExecuted() { var pipeline = ""; var target = new ParallelTaskExecutor { MinThreads = int.MaxValue, MaxThreads = int.MaxValue }; target.Add(() => { pipeline += "1"; }); target.Add(() => { Thread.Sleep(100); pipeline += "2"; }); target.Add(() => { Thread.Sleep(10); pipeline += "3"; }); var actual = target.Start(); Assert.IsTrue(actual); Assert.AreEqual("132", pipeline); }
public void Stop_Tasks_ShutdownCalled() { var pipeline = ""; var target = new ParallelTaskExecutor { Timeout = TimeSpan.FromMilliseconds(1000) }; target.Add(() => { Thread.Sleep(500); pipeline += "1"; }); target.Add(() => { Thread.Sleep(500); pipeline += "2"; }); target.Add(() => { Thread.Sleep(500); pipeline += "3"; }); Parallel.Invoke( () => target.Start(), () => { Thread.Sleep(100); target.Stop(); }); Assert.IsFalse(target.IsRunning); }
public void Stop_ManyTasks_StopAll() { var pipeline = ""; var target = new ParallelTaskExecutor(); target.Timeout = TimeSpan.FromMilliseconds(1000); target.Add(() => { Thread.Sleep(5); pipeline += "1"; }); target.Add(() => { Thread.Sleep(5); pipeline += "2"; }); target.Add(() => { Thread.Sleep(5); pipeline += "3"; }); Parallel.Invoke( () => Assert.IsTrue(target.Start()), () => { Thread.Sleep(100); target.Stop(); }); }
public void Start_ManyTasks_ParallelExecuted() { var pipeline = ""; var target = new ParallelTaskExecutor(); target.Add(() => { pipeline += "1"; }); target.Add(() => { Thread.Sleep(100); pipeline += "2"; }); target.Add(() => { Thread.Sleep(10); pipeline += "3"; }); var actual = target.Start(); Assert.IsTrue(actual); Assert.AreEqual("132", pipeline); }
public void Initialize(IOptimizerConfiguration config, OptimizerFitness fitness) { _config = config; _fitness = fitness; _executor = new ParallelTaskExecutor() { MinThreads = 1 }; _executor.MaxThreads = _config.MaxThreads > 0 ? _config.MaxThreads : 8; }
public void Start_ParallelManyGenerations_Optimization() { var taskExecutor = new ParallelTaskExecutor(); taskExecutor.MinThreads = 100; taskExecutor.MaxThreads = 100; var selection = new EliteSelection(); var crossover = new OnePointCrossover(1); var mutation = new UniformMutation(); var chromosome = new ChromosomeStub(); FlowAssert.IsAtLeastOneAttemptOk(20, () => { var target = new GeneticAlgorithm(new Population(100, 150, chromosome), new FitnessStub() { SupportsParallel = true }, selection, crossover, mutation); target.TaskExecutor = taskExecutor; Assert.AreEqual(GeneticAlgorithmState.NotStarted, target.State); Assert.IsFalse(target.IsRunning); target.Start(); Assert.AreEqual(GeneticAlgorithmState.TerminationReached, target.State); Assert.IsFalse(target.IsRunning); Assert.IsTrue(target.Population.CurrentGeneration.Chromosomes.Count >= 100); Assert.IsTrue(target.Population.CurrentGeneration.Chromosomes.Count <= 150); Assert.IsNotNull(target.Population.BestChromosome); Assert.IsTrue(target.Population.BestChromosome.Fitness >= 0.9); Assert.IsTrue(target.Population.Generations.Count > 0); }); FlowAssert.IsAtLeastOneAttemptOk(20, () => { var target = new GeneticAlgorithm(new Population(100, 150, chromosome), new FitnessStub() { SupportsParallel = true }, selection, crossover, mutation); target.TaskExecutor = taskExecutor; target.Start(); Assert.IsTrue(target.Population.CurrentGeneration.Chromosomes.Count >= 100); Assert.IsTrue(target.Population.CurrentGeneration.Chromosomes.Count <= 150); Assert.IsNotNull(target.Population.BestChromosome); Assert.IsTrue(target.Population.BestChromosome.Fitness >= 0.9); Assert.IsTrue(target.Population.Generations.Count > 0); }); }
public void Start_Timeout_False() { var pipeline = "1"; var target = new ParallelTaskExecutor(); target.Timeout = TimeSpan.FromMilliseconds(2); target.Add(() => { Thread.Sleep(10); pipeline += "2"; }); var actual = target.Start(); Assert.IsFalse(actual); }
public void Start_MaxThreads1_DoNotBlockOtherThreads() { var target = new ParallelTaskExecutor { MinThreads = 1, MaxThreads = 1 }; target.Add(() => { }); target.Add(() => { Thread.Sleep(200); }); target.Add(() => { Thread.Sleep(10); }); int otherThreadCount = 0; var otherThread = new System.Timers.Timer(50) { AutoReset = true }; otherThread.Elapsed += (sender, arg) => { otherThreadCount++; }; otherThread.Start(); Task.Run(() => { target.Start(); }).Wait(); otherThread.Stop(); Assert.GreaterOrEqual(otherThreadCount, 2); }
public static FloatingPointChromosome EvolveGeneticAlgorithm(FloatingPointChromosome chromosome, IOthelloAgent agent, string chromosomeLabel = "") { IPopulation population = new TplPopulation(30, 60, chromosome); IFitness fitness = new EvaluationFitness(agent); ISelection selection = new RouletteWheelSelection(); //Guess ICrossover crossover = new UniformCrossover(); //Guess IMutation mutation = new UniformMutation(); //Guess ITermination stagnation = new FitnessStagnationTermination(500); ITermination threshold = new FitnessThresholdTermination(.9); ITaskExecutor taskExecutor = new ParallelTaskExecutor() { MaxThreads = Environment.ProcessorCount, MinThreads = Environment.ProcessorCount / 2 }; GeneticAlgorithm algorithm = new GeneticAlgorithm(population, fitness, selection, crossover, mutation) { TaskExecutor = new TplTaskExecutor(), MutationProbability = .2f }; algorithm.TaskExecutor = taskExecutor; algorithm.Termination = stagnation; algorithm.Start(); SaveChromosome((FloatingPointChromosome)algorithm.BestChromosome, chromosomeLabel); Debug.WriteLine("finished Training, with {0} time spent on evolving", algorithm.TimeEvolving); Debug.WriteLine("fitness of this generation vs the last : {0}", algorithm.Fitness); return((FloatingPointChromosome)algorithm.BestChromosome); }
private void EvolveGeneticStrategyButton_Click(object sender, RoutedEventArgs e) { OutputTextBlock.Text = "Evolving..."; Task.Run(() => { var chromosome = new BlackjackChromosome(); var fitness = new BlackjackFitness(); var population = new Population(Settings.GeneticSettings.MinPopulationSize, Settings.GeneticSettings.MaxPopulationSize, chromosome); ISelection selection; switch (Settings.GeneticSettings.SelectionType) { case SelectionType.Elite: selection = new EliteSelection(); break; case SelectionType.RouletteWheel: selection = new RouletteWheelSelection(); break; case SelectionType.StochasticUniversalSampling: selection = new StochasticUniversalSamplingSelection(); break; case SelectionType.Tournament: selection = new TournamentSelection(Settings.GeneticSettings.TournamentSize); break; default: throw new InvalidOperationException(); } ICrossover crossover; switch (Settings.GeneticSettings.CrossoverType) { case CrossoverType.AlternatingPosition: crossover = new AlternatingPositionCrossover(); break; case CrossoverType.CutAndSplice: crossover = new CutAndSpliceCrossover(); break; case CrossoverType.Cycle: crossover = new CycleCrossover(); break; case CrossoverType.OnePoint: crossover = new OnePointCrossover(); break; case CrossoverType.TwoPoint: crossover = new TwoPointCrossover(); break; case CrossoverType.OrderBased: crossover = new OrderBasedCrossover(); break; case CrossoverType.Ordered: crossover = new OrderedCrossover(); break; case CrossoverType.PartiallyMapped: crossover = new PartiallyMappedCrossover(); break; case CrossoverType.PositionBased: crossover = new PositionBasedCrossover(); break; case CrossoverType.ThreeParent: crossover = new ThreeParentCrossover(); break; case CrossoverType.Uniform: crossover = new UniformCrossover(Settings.Current.GeneticSettings.MixProbability); break; case CrossoverType.VotingRecombination: crossover = new VotingRecombinationCrossover(); break; default: throw new InvalidOperationException(); } var mutation = new UniformMutation(); var termination = new FitnessStagnationTermination(Settings.Current.GeneticSettings.NumStagnantGenerations); var taskExecutor = new ParallelTaskExecutor(); var ga = new GeneticAlgorithm( population, fitness, selection, crossover, mutation); ga.Termination = termination; ga.TaskExecutor = taskExecutor; ga.MutationProbability = Settings.GeneticSettings.MutationProbability; ga.CrossoverProbability = Settings.GeneticSettings.CrossoverProbability; var latestFitness = double.MinValue; ga.GenerationRan += (s, o) => { geneticStrategy = (IStrategy)ga.BestChromosome; var generationNumber = ga.GenerationsNumber; var bestFitness = ga.BestChromosome.Fitness.Value; var avgFitness = ga.Population.CurrentGeneration.Chromosomes.Average(c => c.Fitness.Value); Dispatcher.Invoke(() => { if (generationNumber == 1) { OutputTextBlock.Text = string.Empty; } OutputTextBlock.Text = $"Gen: {generationNumber}\tFit: {bestFitness}\tAvg: {avgFitness.ToString("0")}\n" + OutputTextBlock.Text; if (bestFitness != latestFitness) { latestFitness = bestFitness; var savedImageName = Settings.Current.GeneticSettings.SaveImagePerGeneration ? "gen" + generationNumber : null; StrategyViewer.Draw(GeneticStrategyCanvas, geneticStrategy, $"Best from generation {generationNumber}", savedImageName); } }, DispatcherPriority.Background); }; ga.TerminationReached += (s, o) => { Dispatcher.Invoke(() => { OutputTextBlock.Text = "Termination reached.\n" + OutputTextBlock.Text; TestGeneticStrategyButton.IsEnabled = true; }, DispatcherPriority.Background); }; ga.Start(); }); }
public void Optimize(bool parallel = false) { Console.WriteLine("Running optimization"); // Rank StopGain StopLoss MinEpsGrowth var chromosome = new SimChromosome(); var population = new Population(25, 50, chromosome); var fitness = new SimFitness(List, Dbfile); var selection = new EliteSelection(); var crossover = new UniformCrossover(0.5f); var mutation = new UniformMutation(); var termination = new FitnessStagnationTermination(100); var ga = new GeneticAlgorithm( population, fitness, selection, crossover, mutation); if (parallel) { var taskExecutor = new ParallelTaskExecutor(); taskExecutor.MinThreads = 4; taskExecutor.MaxThreads = 12; ga.TaskExecutor = taskExecutor; } ga.Termination = termination; Console.WriteLine("Generation: (minRank, stopGain, stopLoss) = value"); var latestFitness = 0.0; ga.GenerationRan += (sender, e) => { var bestChromosome = ga.BestChromosome as SimChromosome; var bestFitness = bestChromosome.Fitness.Value; if (bestFitness != latestFitness) { latestFitness = bestFitness; var Rank = (double)bestChromosome.GetGene(0).Value; var StopGain = (double)bestChromosome.GetGene(1).Value; var StopLoss = (double)bestChromosome.GetGene(2).Value; var MinGrowth = (double)bestChromosome.GetGene(3).Value; Console.WriteLine( "Generation {0,2}: ({1},{2},{3}) = {4}", ga.GenerationsNumber, Rank, StopGain, StopLoss, bestFitness ); } }; ga.Start(); }
/// <summary> /// Initializes a new instance of the <see cref="AlgorithmOptimumFinder"/> class /// </summary> /// <param name="start">Algorithm start date</param> /// <param name="end">Algorithm end date</param> /// <param name="fitScore">Argument of <see cref="FitnessScore"/> type. Fintess function to rank the backtest results</param> /// <param name="filterEnabled">Indicates whether to apply fitness filter to backtest results</param> public AlgorithmOptimumFinder(DateTime start, DateTime end, FitnessScore fitScore, bool filterEnabled) { // Assign Dates and Criteria to sort the results StartDate = start; EndDate = end; FitnessScore = fitScore; // Common properties var selection = new RouletteWheelSelection(); // Properties specific to optimization modes IFitness fitness; PopulationBase population; ITaskExecutor executor; ITermination termination; // Task execution mode switch (Shared.Config.TaskExecutionMode) { // Enable fitness filtering while searching for optimum parameters case TaskExecutionMode.Linear: executor = new LinearTaskExecutor(); fitness = new OptimizerFitness(StartDate, EndDate, fitScore, filterEnabled); break; case TaskExecutionMode.Parallel: executor = new ParallelTaskExecutor(); fitness = new OptimizerFitness(StartDate, EndDate, fitScore, filterEnabled); break; case TaskExecutionMode.Azure: executor = new ParallelTaskExecutor(); fitness = new AzureFitness(StartDate, EndDate, fitScore, filterEnabled); break; default: throw new Exception("Executor initialization failed"); } // Optimization mode switch (Shared.Config.OptimizationMode) { case OptimizationMode.BruteForce: { // Create cartesian population population = new PopulationCartesian(Shared.Config.GeneConfigArray); termination = new GenerationNumberTermination(1); break; } case OptimizationMode.Genetic: { // Create random population population = new PopulationRandom(Shared.Config.GeneConfigArray, Shared.Config.PopulationInitialSize) { GenerationMaxSize = Shared.Config.GenerationMaxSize }; // Logical terminaton var localTerm = new LogicalOrTermination(); localTerm.AddTermination(new FruitlessGenerationsTermination(3)); if (Shared.Config.Generations.HasValue) { localTerm.AddTermination(new GenerationNumberTermination(Shared.Config.Generations.Value)); } if (Shared.Config.StagnationGenerations.HasValue) { localTerm.AddTermination(new FitnessStagnationTermination(Shared.Config.StagnationGenerations.Value)); } termination = localTerm; break; } default: throw new Exception("Optimization mode specific objects were not initialized"); } // Create GA itself GenAlgorithm = new GeneticAlgorithm(population, fitness, executor) { // Reference types Selection = selection, Termination = termination, // Values types CrossoverParentsNumber = Shared.Config.CrossoverParentsNumber, CrossoverMixProbability = Shared.Config.CrossoverMixProbability, MutationProbability = Shared.Config.MutationProbability }; }
public GeneticOptimizer(GeneticOptimizerConfiguration configuration, Func <double[], double> objectiveFunction, Action <string> generationRanCallback = null) { //store configuration _configuration = configuration; _generationRanCallback = generationRanCallback; //set min/max/precision of input variables var minValues = new double[_configuration.Variables.Count]; var maxValues = new double[_configuration.Variables.Count]; var fractionDigits = new int[_configuration.Variables.Count]; for (int index = 0; index < _configuration.Variables.Count; index++) { minValues[index] = _configuration.Variables[index].MinimumValue; maxValues[index] = _configuration.Variables[index].MaximumValue; fractionDigits[index] = _configuration.Variables[index].NumberDigitsPrecision; } //total bits var totalBits = new int[] { 64 }; //chromosome var chromosome = new FloatingPointChromosome(minValues, maxValues, totalBits, fractionDigits); //population var population = new Population(MinimumNumberPopulation, MaximumNumberPopulation, chromosome); //set fitness function var fitnessFunction = new FuncFitness(c => { var fc = c as FloatingPointChromosome; var inputs = fc.ToFloatingPoints(); var result = objectiveFunction(inputs); //add to results if (!Double.IsNaN(result)) { var list = inputs.ToList(); list.Add(result); _result.IterationArray.Add(string.Join(",", list)); } return(result); }); //other variables var selection = new EliteSelection(); var crossover = new UniformCrossover(0.5f); var mutation = new FlipBitMutation(); var termination = new FitnessThresholdTermination(); _algorithm = new GeneticAlgorithm(population, fitnessFunction, selection, crossover, mutation) { Termination = termination, }; //task parallelism var taskExecutor = new ParallelTaskExecutor(); taskExecutor.MinThreads = 1; taskExecutor.MaxThreads = _configuration.NumberThreadsToUse; _algorithm.TaskExecutor = taskExecutor; //if (_configuration.NumberThreadsToUse > 1) //{ // var taskExecutor = new ParallelTaskExecutor(); // taskExecutor.MinThreads = 1; // taskExecutor.MaxThreads = _configuration.NumberThreadsToUse; // _algorithm.TaskExecutor = taskExecutor; //} //register generation ran callback _algorithm.GenerationRan += AlgorithmOnGenerationRan; }