Esempio n. 1
0
        public GeneString[] getBest(int num)
        {
            GeneString[] toReturn = new GeneString[num];

            Array.Copy(result, toReturn, num);

            return toReturn;
        }
Esempio n. 2
0
        public GeneticAlgorithm newInstance(GeneString[] initial, GeneticInstanceParameters parameters)
        {
            if (!(parameters is FuzzyGeneticsInstanceParameters))
                throw new WrongParameterTypeException("Fuzzy Genetics only accepts Fuzzy Genetics Instance Parameters");

            FuzzyGeneticsInstanceParameters fuzzyParameters = (FuzzyGeneticsInstanceParameters)parameters;

            return new FuzzyGenetics(initial, fuzzyParameters);
        }
        public GeneticAlgorithm newInstance(GeneString[] initial, GeneticInstanceParameters parameters)
        {
            if (!(parameters is RandomRestartInstanceParameters))
                throw new WrongParameterTypeException("A Random Restart Genetics instance can only be constructed using Random Restart Genetics Instance Parameters");

            RandomRestartInstanceParameters rrParameters = (RandomRestartInstanceParameters)parameters;

            return new RandomRestartGenetics(initial, rrParameters);
        }
 public GeneratorParameters(GeneString[] initial, int numGenes, int populationSize, bool randomize, Object parameters)
 {
     if (initial == null) initial = new GeneString[0];
     this.initial = initial;
     this.numGenes = numGenes;
     this.populationSize = populationSize;
     this.randomize = randomize;
     this.parameters = parameters;
 }
Esempio n. 5
0
        public GeneString[] getAll()
        {
            GeneString[] toReturn = new GeneString[population.Count];

            for (int n = 0; n < toReturn.Length; n++)
            {
                toReturn[n] = population[n].genestring;
            }

            return toReturn;
        }
Esempio n. 6
0
        public GeneString[] getBest(int num)
        {
            GeneString[] bestGeneStrings = new GeneString[num];

            for (int n = 0; n < num; n++)
            {
                bestGeneStrings[n] = population[n].genestring;
            }

            return bestGeneStrings;
        }
Esempio n. 7
0
        // creates a basic genetics object with an initial population of genestrings and mutations of those strings.
        public BasicGenetics(GeneString[] initial, BasicGeneticInstanceParameters parameters)
        {
            fitnessFunction = parameters.fitnessFunction;

            if (initial != null) parameters.generatorParameters.initial = initial;

            GeneString[] initialPopulation = parameters.generator.generatePopulation(parameters.generatorParameters);

            population = evaluateAll(initialPopulation);

            population.Sort();
        }
        public int evaluate(GeneString genestring)
        {
            int total = 0;
            for (int n = 0; n < 100; n++)
            {
                World world = new World(new MissileProgramSemantics().parse(genestring));

                total += evaluate(world);
            }

            int lengthWeight = (int)Math.Pow(1.006d, genestring.getGenes().Length);
            lengthWeight /= 10;

            return total - lengthWeight;
        }
        public int evaluate(GeneString geneString)
        {
            if (geneString == null) return 0;

            String[] testStrings = getTestStrings();

            List<Swap> swaps = semantics.parse(geneString);

            String[] reversedTests = getReversedStrings(testStrings, swaps);

            int result = 0;
            for (int n = 0; n < testStrings.Length; n++)
            {
                result += evaluate(testStrings[n], reversedTests[n]);
            }

            return result;
        }
Esempio n. 10
0
        protected Gene[] getCrossoverGenes(GeneString otherSource, bool keepLength)
        {
            Gene[] otherGenes = otherSource.getGenes();

            int maxCut = Math.Min(genes.Length, otherGenes.Length);

            int cutPoint1 = SharedRandom.Next(maxCut);
            int cutPoint2 = (keepLength) ? cutPoint1 : SharedRandom.Next(maxCut);

            Gene[] newGenes = new Gene[cutPoint1 + otherGenes.Length - cutPoint2];

            for (int n = 0; n < cutPoint1; n++)
            {
                newGenes[n] = genes[n].copy();
            }
            for (int n = 0; n < otherGenes.Length - cutPoint2; n++)
            {
                newGenes[n + cutPoint1] = otherGenes[n + cutPoint2].copy();
            }

            return newGenes;
        }
Esempio n. 11
0
 public FuzzyGenetics(GeneString[] initial, FuzzyGeneticsInstanceParameters parameters)
 {
     this.initial = initial;
 }
Esempio n. 12
0
        public GeneticAlgorithm newInstance(GeneString[] initial, GeneticInstanceParameters parameters)
        {
            if (!(parameters is BasicGeneticInstanceParameters))
                throw new WrongParameterTypeException("A Basic Genetics instance can only be constructed using Basic Genetics Instance Parameters");

            BasicGeneticInstanceParameters bParameters = (BasicGeneticInstanceParameters)parameters;

            return new BasicGenetics(initial, bParameters);
        }
Esempio n. 13
0
 public GeneStringIndexPair(GeneString genestring, int index)
 {
     this.genestring = genestring;
     this.index = index;
 }
Esempio n. 14
0
        // evaluates all members of an array of genestrings and puts them in a list of string/fitness pairs. (Null strings get a value of 0)
        private List<GeneStringFitnessPair> evaluateAll(GeneString[] geneStrings)
        {
            fitnessEvaluated = new GeneStringFitnessPair[geneStrings.Length];

            tasksQueued = geneStrings.Length;
            for (int n = 0; n < geneStrings.Length; n++)
            {
                ThreadPool.QueueUserWorkItem(ThreadPoolCallBack, new GeneStringIndexPair(geneStrings[n], n));
            }

            tasksDone.WaitOne();

            tasksDone.Reset();

            return fitnessEvaluated.ToList();
        }
Esempio n. 15
0
        // remainder of 1 - mutation - crossover = copy chance
        // returns max fitness this round
        public int run(GeneticRunParameters parameters)
        {
            if (!(parameters is BasicGeneticRunParameters)) throw new WrongParameterTypeException("Basic Genetics only take Basic Genetics Run Parameters");

            BasicGeneticRunParameters bParameters = (BasicGeneticRunParameters)parameters;

            if (bParameters.mutationChance + bParameters.crossoverChance > 1.0d)
                throw new OutOfBoundsException("mutation chance + crossover chance is more than 100%");

            double crossoverChance = bParameters.crossoverChance + bParameters.mutationChance; // < mutation = mutation, else < crossover = crossover, else copy

            GeneString[] newGeneration = new GeneString[population.Count];

            for (int n = 0; n < bParameters.keepPerRound; n++)
            {
                newGeneration[n] = population[n].genestring.copy();
            }

            for (int n = bParameters.keepPerRound; n < population.Count; n++)
            {
                Double rnd = SharedRandom.NextDouble();

                if (rnd < bParameters.mutationChance) newGeneration[n] = mutateFrom(bParameters.keepPerRound, bParameters.mutationSeverity);
                else if (rnd < crossoverChance) newGeneration[n] = crossoverFrom(bParameters.keepPerRound, bParameters.keepLength);
                else newGeneration[n] = copyFrom(bParameters.keepPerRound);
            }

            population = evaluateAll(newGeneration);

            population.Sort();

            return population[0].fitness;
        }
 public GeneStringFitnessPair(GeneString genestring, int fitness)
 {
     this.genestring = genestring;
     this.fitness = fitness;
 }
        public RandomRestartGenetics(GeneString[] initial, RandomRestartInstanceParameters parameters)
        {
            this.initial = initial;

            this.goalValue = parameters.startingGoal;
        }
        private int runChampionRound(RandomRestartRunParameters rrParameters, List<GeneStringFitnessPair> bestResults)
        {
            if (rrParameters.verbose) Console.WriteLine("running champion round");

            GeneString[] geneStrings = new GeneString[bestResults.Count];

            for (int n = 0; n < bestResults.Count; n++)
            {
                geneStrings[n] = bestResults[n].genestring;
            }

            subGenetics = rrParameters.algorithm.newInstance(geneStrings, rrParameters.algorithmInstanceParameters);

            return runUntilRepeating(rrParameters.algorithmRunParameters, rrParameters.numRepeatsBeforeRestart, rrParameters.verbose);
        }
Esempio n. 19
0
 public abstract GeneString crossover(GeneString otherSource, bool keepLength);