Ejemplo n.º 1
0
        /// <summary>
        /// Returns total sum of distance between cities represnted in the chromosome
        /// </summary>
        /// <param name="phenotype"></param>
        /// <returns></returns>
        public double CalculateScore(IMLMethod phenotype)
        {
            try
            {
                FourBitCustomGenome genome     = (FourBitCustomGenome)phenotype;
                FourBitGene []      genomeData = ((FourBitCustomGenome)genome).Data;
                //double maxPossiblePower = PowerUnits.Sum(x => x.UnitCapacity);

                new PowerUnitGALogic().DisplayGeneAsString(genome, genomeData);

                var intervalFitnessDataRepository = new IntervalFitnessDataRepository(MaxPossiblePower);
                var intervalRawData = intervalFitnessDataRepository.IntervalRawData;

                for (int i = 0; i < NumberOfIntervals; i++)
                {
                    IntervalsFitnessData interval = intervalRawData[i];
                    //interval.MaxReserve = maxPossiblePower;
                    for (int j = 0; j < genomeData.Length; j++)
                    {
                        PowerUnit   powerUnit             = PowerUnits[j];
                        FourBitGene fourBitGene           = genomeData[j];
                        int         geneBitIndex          = i;
                        var         isPowerUnitMaintained = fourBitGene.Gene[geneBitIndex] == 1;
                        if (isPowerUnitMaintained)
                        {
                            interval.ReducedAmountOnMaintainance = interval.ReducedAmountOnMaintainance + (1 * powerUnit.UnitCapacity);
                        }
                        else
                        {
                            interval.ReducedAmountOnMaintainance = interval.ReducedAmountOnMaintainance + (0 * powerUnit.UnitCapacity);
                        }
                    }

                    var totalPowerReductionOnMaintanceAndUsage =
                        interval.PowerRequirement + interval.ReducedAmountOnMaintainance;
                    interval.ReserveAfterMaintainance = interval.MaxReserve - totalPowerReductionOnMaintanceAndUsage;
                    //if (interval.ReserveAfterMaintainance < 0.0)
                    //{
                    //    // the chromosome is not suitable for out requirement
                    //    chromosomeFitness = 0.0;
                    //}
                }

                var reserveAfterMaintainanceMin = intervalRawData.Min(x => x.ReserveAfterMaintainance);
                // minimal rerserve after maintainance and usage provides chormosomes fitness
                //var chromosomeFitness = reserveAfterMaintainanceMin > 0.0 ? reserveAfterMaintainanceMin : 0.0;
                var chromosomeFitness = reserveAfterMaintainanceMin;
                Console.WriteLine("\tFitness = {0} - of {1}, {2}, {3}, {4}", chromosomeFitness,
                                  intervalRawData[0].ReserveAfterMaintainance, intervalRawData[1].ReserveAfterMaintainance,
                                  intervalRawData[2].ReserveAfterMaintainance, intervalRawData[3].ReserveAfterMaintainance);
                return(chromosomeFitness);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Ejemplo n.º 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();
        }
Ejemplo n.º 3
0
        public void DisplaySolution(TrainEA geneticAlgorithm, PowerUnit[] powerUnits, double maxPossiblePower)
        {
            try
            {
                var            bestChromosome     = (FourBitCustomGenome)geneticAlgorithm.Population.BestGenome;
                FourBitGene [] bestChomosomeGenes = bestChromosome.Data;

                // display best chromosome genes
                for (var i = 0; i < bestChomosomeGenes.Length; i++)
                {
                    // display chromosome to user
                    var geneBitsString = GetGeneBitString(bestChomosomeGenes[i]);
                    if (i == 0)
                    {
                        Console.Write("Best Chromosome Genes = [ {0}", geneBitsString);
                    }
                    else if (i < bestChomosomeGenes.Length - 1)
                    {
                        Console.Write(", {0}", geneBitsString);
                    }
                    else
                    {
                        Console.Write(", {0}]\n\n", geneBitsString);
                    }
                }

                var intervalFitnessDataRepository = new IntervalFitnessDataRepository(maxPossiblePower);
                var intervalRawData = intervalFitnessDataRepository.IntervalRawData;

                for (int i = 0; i < intervalRawData.Count; i++)
                {
                    IntervalsFitnessData interval = intervalRawData[i];
                    for (int j = 0; j < bestChomosomeGenes.Length; j++)
                    {
                        PowerUnit   powerUnit             = powerUnits[j];
                        FourBitGene fourBitGene           = bestChomosomeGenes[j];
                        int         geneBitIndex          = i;
                        var         isPowerUnitMaintained = fourBitGene.Gene[geneBitIndex] == 1;
                        if (isPowerUnitMaintained)
                        {
                            interval.ReducedAmountOnMaintainance = interval.ReducedAmountOnMaintainance + (1 * powerUnit.UnitCapacity);
                        }
                        else
                        {
                            interval.ReducedAmountOnMaintainance = interval.ReducedAmountOnMaintainance + (0 * powerUnit.UnitCapacity);
                        }
                    }

                    var totalPowerReductionOnMaintanceAndUsage =
                        interval.PowerRequirement + interval.ReducedAmountOnMaintainance;
                    interval.ReserveAfterMaintainance = interval.MaxReserve - totalPowerReductionOnMaintanceAndUsage;
                    if (interval.ReserveAfterMaintainance < 0.0)
                    {
                        // the chromosome is not suitable for out requirement
                        Console.WriteLine("Error - On Interval {0} has net reserve of {1} ", interval.IntervalId, interval.ReserveAfterMaintainance);
                    }
                }

                foreach (var interval in intervalRawData)
                {
                    Console.WriteLine(
                        "Interval Id = {0} , Max Reserve = {1}, Power Requirement = {2} , Reduced on maintainance = {3} , Reserve after Maintainance = {4}",
                        interval.IntervalId, interval.MaxReserve, interval.PowerRequirement,
                        interval.ReducedAmountOnMaintainance, interval.ReserveAfterMaintainance);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }