Esempio n. 1
0
        static void Main(string[] args)
        {
            var powerUnitRepository = new PowerUnitRepository();

            var powerUnitGALogic     = new PowerUnitGALogic();
            var populationSize       = 500;
            var crossOverProbabality = 0.9;
            var mutationProbabaity   = 0.001;

            const int MaxNumIterationsSameSolution = 25;     // maximum number of iterations which GA could run on a same error rate


            Display("------------ Load Power Unit Details");
            var powerUnits = powerUnitRepository.PowerUnits;

            DisplayPowerUnitData(powerUnits);

            // 1 create initial population
            Display("------------ Create Initial Population");
            var population = powerUnitGALogic.CreateInitialPopulation(populationSize, powerUnits.Count);

            // 2 create fitness function
            Display("\n------------ Create Fitness function - sum of total diatance of cities within the chromosome - distance low --> better chromosome");
            double maxPossiblePower             = powerUnits.Sum(x => x.UnitCapacity);
            var    numberOfIntervals            = new IntervalFitnessDataRepository(maxPossiblePower).GetNumberOfIntervals();
            var    powerUnitMaintainanceFitness = new PowerUnitMaintainanceFitnessFunction(powerUnitRepository.GetAllPowerUnits(), numberOfIntervals, maxPossiblePower);

            // 3 create GA trainer
            Display("\n------------ Create GA Trainer for iterations");
            TrainEA geneticAlgorithm = powerUnitGALogic.CreateGA(powerUnits.Count, population, powerUnitMaintainanceFitness, crossOverProbabality, mutationProbabaity);

            // 4 iterate and create off spring of new solutions
            Display("\n------------ Run GA for iterations until good solutions found");
            geneticAlgorithm = powerUnitGALogic.RunGA(geneticAlgorithm, MaxNumIterationsSameSolution);

            // 5 display GA results
            Display("\n------------ Display Final solution after iterations");
            powerUnitGALogic.DisplaySolution(geneticAlgorithm, powerUnitRepository.GetAllPowerUnits(), maxPossiblePower);

            Display("\n------------ Done");
            Console.ReadKey();
        }
Esempio n. 2
0
        public FourBitCustomGenome CreateRandomGenome(int geneCountPerChromosome)
        {
            try
            {
                var randomGenome   = new FourBitCustomGenome(geneCountPerChromosome);
                var powerUnits     = new PowerUnitRepository().GetAllPowerUnits();
                var chromosomeData = new FourBitGene[geneCountPerChromosome];
                for (var powerUnitIndex = 0; powerUnitIndex < chromosomeData.Length; powerUnitIndex++)
                {
                    var powerUnitInfo = powerUnits.SingleOrDefault(x => x.UnitNumber == powerUnitIndex + 1);
                    chromosomeData[powerUnitIndex] = GetRandomGeneForPowerUnit(powerUnitInfo);
                }

                DisplayGeneAsString(randomGenome, chromosomeData);

                return(randomGenome);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }