public ParticleSwarmOptimization(int dimensions, int numberOfParticles, double[] upperBounds, double[] lowerBounds, Func <double[], double> CalculateFitness, Func <double, bool> TargetReached, FitnessMethod FitnessMethod, double velocityFactor = 0.5, double omega = 0.3, double fp = 1, double fg = 0.3)
        {
            if (dimensions < 1)
            {
                throw new ArgumentException("Dimensions should be > 1");
            }
            if (numberOfParticles < 1)
            {
                throw new ArgumentException("Number of particles should be > 1");
            }
            for (int i = 0; i < numberOfParticles; i++)
            {
                Particles.Add(new Particle(dimensions));
            }

            this.velocityFactor   = velocityFactor;
            this.omega            = omega;
            this.fp               = fp;
            this.fg               = fg;
            this.CalculateFitness = CalculateFitness;
            this.FitnessMethod    = FitnessMethod;
            this.TargetReached    = TargetReached;
            this.lowerBounds      = lowerBounds;
            this.upperBounds      = upperBounds;
            Initialize(upperBounds, lowerBounds);
        }
Example #2
0
 public bool ShouldUpdateParticleBestFitness(FitnessMethod fitnessMethod)
 {
     if (fitnessMethod == FitnessMethod.Maximize)
     {
         return(Fitness > BestFitness);
     }
     else
     {
         return(Fitness < BestFitness);
     }
 }
Example #3
0
        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name='problem'>Anzuwendendes Problem</param>
        public Evolution(Problem problem)
        {
            countGene = problem.countGene;
            maxGenerations = problem.maxGenerations;
            countIndividuals = problem.countIndividuals;
            countChilds = problem.countChilds;
            recombinationProbability = problem.recombinationProbability;
            InvertOnMutate = problem.InvertOnMutate;
            minAllelValue = problem.minAllelValue;
            maxAllelValue = problem.maxAllelValue;
            SelPropType = problem.SelPropType;
            SelType = problem.SelType;
            Encryption = problem.Encryption;
            TournamentMemberCount = problem.TournamentMemberCount;

            sb = problem.Output;

            bestFitness = double.MaxValue;
            averageFitness = double.MaxValue;
            bestFitnessGeneration = 0;
            bestSolutions = new List<double>();
            bestSolutionsGeneration = new List<int>();
            bestList = new List<Genome>(maxGenerations-1);

            CalcFitness = problem.CalcFitnessDefault;

            switch (Encryption)
            {
                case Helper.Enums.Encryption.None :
                {
                    Recombine = problem.RecombineDefault;
                    Mutate = problem.MutateDefault;
                    break;
                }
                case Helper.Enums.Encryption.Binary :
                {
                    Recombine = problem.RecombineBinary;
                    Mutate = problem.MutateBinary;
                    break;
                }
                case Helper.Enums.Encryption.Real :
                {
                    Recombine = problem.RecombineReal;
                    Mutate = problem.MutateReal;
                    break;
                }
            }
        }
Example #4
0
 // ======================================== CONSTRUCTOR ==============================================
 /// <summary>
 /// Instanciate a <see cref="T:NEAT.NEATManager"/>. It sets the configs for it to run correctly.
 /// </summary>
 /// <param name="inputs">The number of inputs neurons of a neural network.</param>
 /// <param name="outputs">The number of outputs of a neural network.</param>
 /// <param name="fitnessMethod">The method that computes the fitness of a dead player.</param>
 /// <param name="size">The size of the population.</param>
 /// <param name="mode">The mode that will be used to run NEAT.</param>
 public NEATManager(int inputs, int outputs, FitnessMethod fitnessMethod, int size = 200, NEATMode mode = NEATMode.Separately)
 {
     Mode           = mode;
     PopulationSize = size;
     if (Mode == NEATMode.Separately)
     {
         CurrentPlayerIndex = 0;
     }
     history              = new List <NeuralNetwork.ConnectionHistory>();
     Players              = new List <Player>();
     SpeciesList          = new List <Species>();
     nextInnovationNumber = 0;
     for (int i = 0; i < size; i++)
     {
         Players.Add(new Player(inputs, outputs, ref fitnessMethod));
         Players[i].Brain.Mutate(history, ref nextInnovationNumber);
     }
     Generation = 1;
 }
Example #5
0
 public Player(int inputs, int outputs, ref FitnessMethod fitnessMethod)
 {
     Brain   = new Genome(inputs, outputs);
     IsAlive = true;
     fitness = fitnessMethod;
 }