コード例 #1
0
 public TrainEA CreateGA(int powerUnitCount, BasicPopulation population, PowerUnitMaintainanceFitnessFunction maintainanceFitness, double crossOverProbabality, double mutationProbabality)
 {
     try
     {
         var geneticAlgorithm = new TrainEA(population, maintainanceFitness);
         geneticAlgorithm.AddOperation(crossOverProbabality, new CustomCrossOver());
         geneticAlgorithm.AddOperation(mutationProbabality, new CustomMutation());
         return(geneticAlgorithm);
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
         throw;
     }
 }
コード例 #2
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();
        }