/// <summary>
        /// Initializes a new instance of the <see cref="GeneticSharp.Domain.GeneticAlgorithm"/> class.
        /// </summary>
        /// <param name="population">The chromosomes population.</param>
        /// <param name="fitness">The fitness evaluation function.</param>
        /// <param name="selection">The selection operator.</param>
        /// <param name="crossover">The crossover operator.</param>
        /// <param name="mutation">The mutation operator.</param>
        public GeneticAlgorithm(
            IPopulation population,
            IFitness fitness,
            ISelection selection,
            ICrossover crossover,
            IMutation mutation)
        {
            ExceptionHelper.ThrowIfNull("population", population);
            ExceptionHelper.ThrowIfNull("fitness", fitness);
            ExceptionHelper.ThrowIfNull("selection", selection);
            ExceptionHelper.ThrowIfNull("crossover", crossover);
            ExceptionHelper.ThrowIfNull("mutation", mutation);

            Population  = population;
            Fitness     = fitness;
            Selection   = selection;
            Crossover   = crossover;
            Mutation    = mutation;
            Reinsertion = new ElitistReinsertion();
            Termination = new GenerationNumberTermination(1);

            CrossoverProbability = DefaultCrossoverProbability;
            MutationProbability  = DefaultMutationProbability;
            TimeEvolving         = TimeSpan.Zero;
            State        = GeneticAlgorithmState.NotStarted;
            TaskExecutor = new LinearTaskExecutor();
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AutoConfigFitness"/> class.
 /// </summary>
 /// <param name="targetFitness">The target fitness.</param>
 /// <param name="targetChromosome">The target chromosome.</param>
 public AutoConfigFitness(IFitness targetFitness, IChromosome targetChromosome)
 {
     m_targetFitness    = targetFitness;
     m_targetChromosome = targetChromosome;
     PopulationMinSize  = 100;
     PopulationMaxSize  = 100;
     Termination        = new TimeEvolvingTermination(TimeSpan.FromSeconds(30));
     TaskExecutor       = new LinearTaskExecutor();
 }
Exemplo n.º 3
0
        public void Start_Task_TaskRan()
        {
            var pipeline = "";
            var target   = new LinearTaskExecutor();

            target.Add(() => pipeline += "1");
            target.Add(() => pipeline += "2");
            target.Add(() => pipeline += "3");

            Assert.IsTrue(target.Start());
            Assert.AreEqual("123", pipeline);
        }
        public void Start_TakeMoreThanTimeout_False()
        {
            var pipeline = "";
            var target   = new LinearTaskExecutor();

            target.Add(() => pipeline += "1");
            target.Add(() => {
                pipeline += "2";
                Thread.Sleep(100);
            });
            target.Add(() => pipeline += "3");

            target.Timeout = TimeSpan.FromMilliseconds(50);
            Assert.IsFalse(target.Start());
            Assert.AreEqual("12", pipeline);
        }
Exemplo n.º 5
0
        public void Stop_ManyTasks_True()
        {
            var pipeline = "";
            var target   = new LinearTaskExecutor();

            target.Add(() => pipeline += "1");
            target.Add(() =>
            {
                pipeline += "2";
                Thread.Sleep(1000);
            });
            target.Add(() => pipeline += "3");

            Parallel.Invoke(
                () => Assert.IsTrue(target.Start()),
                () =>
            {
                Thread.Sleep(5);
                target.Stop();
            });
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GeneticSharp.Domain.GeneticAlgorithmCCE"/> class.
        /// </summary>
        /// <param name="species">The species for this CCE</param>
        public GeneticAlgorithmCCE(List <CCESpecies> species)
        {
            Species      = species;
            TimeEvolving = TimeSpan.Zero;
            State        = GeneticAlgorithmState.NotStarted;
//			TaskExecutorFitMaster = new SmartThreadPoolTaskExecutor () {
//				MinThreads = 4,
//				MaxThreads = 8
//			};
            TaskExecutorFitMaster = new LinearTaskExecutor();
            TaskExecutorFit       = new LinearTaskExecutor();
//			TaskExecutorGen = new LinearTaskExecutor ();
//			TaskExecutorFit = new SmartThreadPoolTaskExecutor()
//			{
//				MinThreads = 5,
//				MaxThreads = 60
//			};
            TaskExecutorGen = new SmartThreadPoolTaskExecutor()
            {
                MinThreads = 4,
                MaxThreads = 8
            };
        }
Exemplo n.º 7
0
        /// <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
            };
        }