Example #1
0
        public GA(IDataSet <T>[] ds, IBreeder <T> _breeder, double _mutationRate)
        {
            data         = ds;
            breeder      = _breeder;
            mutationRate = _mutationRate;

            myFactory.Factory.init();

            r = new Random(DateTime.Now.Millisecond);
        }
        static void Main(string[] args)
        {
            NeuralNetworkConfigurationSettings networkConfig = new NeuralNetworkConfigurationSettings
            {
                NumInputNeurons    = 1,
                NumOutputNeurons   = 1,
                NumHiddenLayers    = 2,
                NumHiddenNeurons   = 3,
                SummationFunction  = new SimpleSummation(),
                ActivationFunction = new TanhActivationFunction()
            };
            GenerationConfigurationSettings generationSettings = new GenerationConfigurationSettings
            {
                UseMultithreading    = true,
                GenerationPopulation = 500
            };
            EvolutionConfigurationSettings evolutionSettings = new EvolutionConfigurationSettings
            {
                NormalMutationRate  = 0.05,
                HighMutationRate    = 0.5,
                GenerationsPerEpoch = 10,
                NumEpochs           = 1000,
                NumTopEvalsToReport = 10
            };
            MutationConfigurationSettings mutationSettings = new MutationConfigurationSettings
            {
                MutateAxonActivationFunction       = true,
                MutateNumberOfHiddenLayers         = true,
                MutateNumberOfHiddenNeuronsInLayer = true,
                MutateSomaBiasFunction             = true,
                MutateSomaSummationFunction        = true,
                MutateSynapseWeights = true
            };
            var random = new RandomWeightInitializer(new Random());
            INeuralNetworkFactory factory          = NeuralNetworkFactory.GetInstance(SomaFactory.GetInstance(networkConfig.SummationFunction), AxonFactory.GetInstance(networkConfig.ActivationFunction), SynapseFactory.GetInstance(new RandomWeightInitializer(new Random()), AxonFactory.GetInstance(networkConfig.ActivationFunction)), SynapseFactory.GetInstance(new ConstantWeightInitializer(1.0), AxonFactory.GetInstance(new IdentityActivationFunction())), random);
            IBreeder            breeder            = BreederFactory.GetInstance(factory, random).Create();
            IMutator            mutator            = MutatorFactory.GetInstance(factory, random).Create(mutationSettings);
            IEvalWorkingSet     history            = EvalWorkingSetFactory.GetInstance().Create(50);
            IEvaluatableFactory evaluatableFactory = new GameEvaluationFactory();

            IStorageProxy proxy  = new NodeJSProxy(1, "http://localhost:3000", "123456789");
            IEpochAction  action = new BestPerformerUpdater(proxy);

            var GAFactory             = GeneticAlgorithmFactory.GetInstance(evaluatableFactory);
            IGeneticAlgorithm evolver = GAFactory.Create(networkConfig, generationSettings, evolutionSettings, factory, breeder, mutator, history, evaluatableFactory, action);

            evolver.RunSimulation();
        }
Example #3
0
        public AccidentGA(IDataSet <string[]>[] ds, IBreeder <string[]> _breeder, double mutationRate, string _filePath, IParser _parser, bool prefilleddata = false)
            : base(ds, _breeder, mutationRate)
        {
            filePath = _filePath;
            breeder  = _breeder;
            parser   = _parser;

            percMatches = new double[data.Length];

            if (ds == null)
            {
                throw new Exception("Must initialise a dataset");
            }

            if (!prefilleddata)
            {
                GetData();
            }
        }
        private GeneticAlgorithm(NeuralNetworkConfigurationSettings networkConfig, GenerationConfigurationSettings generationConfig, EvolutionConfigurationSettings evolutionConfig, INeuralNetworkFactory networkFactory, IBreeder breeder, IMutator mutator, IEvalWorkingSet workingSet, IEvaluatableFactory evaluatableFactory, IEpochAction epochAction)
        {
            _networkConfig    = networkConfig;
            _generationConfig = generationConfig;
            _evolutionConfig  = evolutionConfig;
            _epochAction      = epochAction;
            var sessions = new List <ITrainingSession>();

            _networkFactory     = networkFactory;
            _breeder            = breeder;
            _mutator            = mutator;
            _history            = workingSet;
            _evaluatableFactory = evaluatableFactory;
            for (int i = 0; i < _generationConfig.GenerationPopulation; i++)
            {
                var network = _networkFactory.Create(_networkConfig.NumInputNeurons, _networkConfig.NumOutputNeurons, _networkConfig.NumHiddenLayers, _networkConfig.NumHiddenNeurons);
                sessions.Add(new TrainingSession(network, _evaluatableFactory.Create(network), i));
            }
            _generation = new Generation(sessions, _generationConfig);
        }
Example #5
0
        public Generator(int population, int cityCount, int generations, double mutationRate, int seed)
        {
            this.logger = new GeneratorLogger();

            this.population   = population;
            this.cityCount    = cityCount;
            this.generations  = generations;
            this.mutationRate = mutationRate;
            this.seed         = seed;

            this.abstractFactory = new AbstractFactory();
            this.cityFactory     = abstractFactory.CreateCityFactory(this.seed);
            this.breedingFactory = abstractFactory.CreateBreedingFactory();
            ISolutionFactory solutionFactory = abstractFactory.CreateSolutionFactory();

            IMutater mutater = breedingFactory.CreateMutater(mutationRate);

            this.solutionFactory = solutionFactory;
            this.bestFit         = new BestFit();

            this.breeder = breedingFactory.CreateBreeder(mutater, solutionFactory);
        }
 public IGeneticAlgorithm Create(NeuralNetworkConfigurationSettings networkConfig, GenerationConfigurationSettings generationConfig, EvolutionConfigurationSettings evolutionConfig, INeuralNetworkFactory networkFactory, IBreeder breeder, IMutator mutator, IEvalWorkingSet workingSet, IEvaluatableFactory evaluatableFactory, IEpochAction epochAction)
 {
     return(GeneticAlgorithm.GetInstance(networkConfig, generationConfig, evolutionConfig, _networkFactory, breeder, mutator, workingSet, _evaluatableFactory, epochAction));
 }
 public IGeneticAlgorithm Create(NeuralNetworkConfigurationSettings networkConfig, GenerationConfigurationSettings generationConfig, EvolutionConfigurationSettings evolutionConfig, INeuralNetworkFactory networkFactory, IBreeder breeder, IMutator mutator, IEvalWorkingSet workingSet, IEvaluatableFactory evaluatableFactory, IEpochAction epochAction)
 {
     return GeneticAlgorithm.GetInstance(networkConfig, generationConfig, evolutionConfig, _networkFactory, breeder, mutator, workingSet, _evaluatableFactory, epochAction);
 }
Example #8
0
        public void Solve(BionicModel model, BionicSolverSettings settings)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            OnSolverStarting(new EventArgs());

            IInitialPopulationGenerator initializer = InitialPopulationGeneratorFactory.CreateInitialPopulationGenerator();
            ISelector selector = SelectorFactory.CreateSelector();
            IBreeder  breeder  = BreederFactory.CreateBreeder();

            // 1. Zeroing generation number
            model.CurrentGeneration = 0;

            OnSolverStarted(new EventArgs());

            // 2. Creating initial population
            CopyIndividuals(initializer.GenerateInitialPopulation(settings.InitialPopulationSize, model.Attributes), model.CurrentPopulation);
            CalculateFitness(model, model.CurrentPopulation, settings);
            model.ApplyFunctionalConstraints();

            OnPopulationUpdated(new EventArgs());

            while (model.CurrentGeneration < settings.MaxGenerations)
            {
                // 3. Selecting most fit individuals
                IEnumerable <Individual> fitIndividuals = selector.Select(model.CurrentPopulation, model.FitnessCriterion, settings.SelectionCap);
                if (fitIndividuals.Count() == 0)
                {
                    OnSolverFinished(new SolverFinishedEventArgs("All individuals are inactive"));
                    return;
                }

                // Selected subpopulation will contain only selected active individuals
                Population selectedSubPopulation = new Population(fitIndividuals.Count());
                foreach (Individual fitIndividual in fitIndividuals)
                {
                    selectedSubPopulation.Add(fitIndividual);
                }

                // 4. Breeding the selected ones
                IEnumerable <Individual> descendants = breeder.Breed(selectedSubPopulation, model.Attributes,
                                                                     settings.DescendantsRangePercent, settings.DescendantsNum, model.CurrentGeneration + 1);
                if (descendants.Count() == 0)
                {
                    OnSolverFinished(new SolverFinishedEventArgs("No descendants was created"));
                    return;
                }

                // Descendants subpopulation will contain only new individuals
                Population descendantsSubPopulation = new Population(descendants.Count());
                foreach (Individual descendant in descendants)
                {
                    descendantsSubPopulation.Add(descendant);
                }

                // 5. Calculating fitness of the descendants
                CalculateFitness(model, descendantsSubPopulation, settings);
                // At this point, all descendants are still active, although
                // there may be individuals that don't fit f. c.

                // 6. Forming new population
                if (settings.ApplyElitism)
                {
                    // First unite descendants with their parents (elitism)
                    // We can do this without fear of ID collision because this
                    // was taken into account on the breeding step
                    CopyIndividuals(descendantsSubPopulation, selectedSubPopulation);

                    // Now this united population becomes current, we just
                    // throw away old individuals
                    model.CurrentPopulation.Clear();
                    CopyIndividuals(selectedSubPopulation, model.CurrentPopulation);
                }
                else
                {
                    // We throw away parents and old individuals,
                    // descendants are taking over the place
                    model.CurrentPopulation.Clear();
                    CopyIndividuals(descendantsSubPopulation, model.CurrentPopulation);
                }

                model.ApplyFunctionalConstraints();

                // 7. Updating current generation number
                model.CurrentGeneration = model.CurrentPopulation.Max(ind => ind.Value.GenerationNumber);

                OnPopulationUpdated(new EventArgs());
            }

            OnSolverFinished(new SolverFinishedEventArgs());
        }
Example #9
0
 public FloatGA(IDataSet <float>[] ds, IBreeder <float> _breeder, double mutationRate)
     : base(ds, _breeder, mutationRate)
 {
     breeder = _breeder;
 }