⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cardproblem.cs

📁 Generalization of a Simple Genetic Algorithm (GA)
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using GeneticAlgorithmLib;
using System.ComponentModel;

namespace GeneticAlgorithm
{
    public class CardProblem : BinaryGeneticAlgorithm
    {
        private int m_SumTarget;
        private int m_ProdTarget;
        private int m_NumberOfCards;

        public CardProblem(int NumberOfCards, int SumTarget, int ProductTarget, 
            double ChanceOfInfection, double ChanceOfMutation)
            : base(ChanceOfInfection, ChanceOfMutation)
        {
            this.m_NumberOfCards = NumberOfCards;
            this.m_SumTarget = SumTarget;
            this.m_ProdTarget = ProductTarget;
        }

        #region Overrides
        #region ChooseWinners
        protected override void ChooseWinners(IGenotype First, IGenotype Second, out IGenotype Winner, out IGenotype Loser)
        {
            CardGenes f = First as CardGenes,
                s = Second as CardGenes;
            double fFitness = Evaluate(f),
                sFitness = Evaluate(s);
            if ( fFitness < sFitness)
            {
                Winner = f;
                Loser = s;
            }
            else
            {
                Winner = s;
                Loser = f;
            }
        }
        #endregion

        #region Evaluate
        protected override bool Evaluate(IGenotype Genes)
        {
            CardGenes g = Genes as CardGenes;
            bool returnVal = Evaluate(g) == 0.0;
            return returnVal;
        }
        #endregion

        #region CreateGenotype
        protected override IGenotype CreateGenotype()
        {
            return new CardGenes(this.m_NumberOfCards);
        }
        #endregion
        
        #endregion

        private double Evaluate(CardGenes f)
        {
            double sumErr, prodErr, combinedErr;
            int sum = 0, prod = 1;

            for (int i = 0; i < f.Genes.Count; i++)
            {
                Gene gene = f.Genes[i];
                if (gene.Value == EGene.On)
                {
                    // On means sum
                    sum += (i + 1);
                }
                else
                {
                    // Off means product
                    prod *= (i + 1);
                }
            }
            sumErr = ((double)(sum - m_SumTarget)) / (double)m_SumTarget;
            prodErr = ((double)(prod - m_ProdTarget)) / (double)m_ProdTarget;
            combinedErr = Math.Abs(sumErr) + Math.Abs(prodErr);
            f.Sum = sum;
            f.Product = prod;
            return combinedErr;
        }

        #region Helpers
        /// <summary>
        /// For debugging purposes. Let's us look at the IGenotypes.
        /// </summary>
        /// <param name="CG"></param>
        /// <returns></returns>
        private string GetResultString(CardGenes CG)
        {
            string returnVal = null;
            StringBuilder sbSum = new StringBuilder(),
                sbProd = new StringBuilder();
            for (int i = 0; i < CG.Genes.Count; i++)
            {
                if (CG.Genes[i].Value == EGene.On)
                {
                    sbSum.AppendFormat("{0} ({1}){2}", 
                        CG.Genes[i].NameString, 
                        CG.Genes[i].Value, 
                        Environment.NewLine);
                }
                else
                {
                    sbProd.AppendFormat("{0} ({1}){2}", 
                        CG.Genes[i].NameString, 
                        CG.Genes[i].Value, 
                        Environment.NewLine);
                }
            }
            returnVal = string.Format("The following are in the sum: {0}{2}"
                + "The following are in the product: {1}{2}",
                sbSum.ToString(), sbProd.ToString(), Environment.NewLine);
            return returnVal;
        }
        #endregion


    }

    public class CardGenes : IGenotype
    {
        #region Sum
        private int m_Sum;
        [Browsable(true)]
        public int Sum
        {
            get { return m_Sum; }
            set { m_Sum = value; }
        }
        #endregion

        #region Product
        private int m_Product;
        [Browsable(true)]
        public int Product
        {
            get { return m_Product; }
            set { m_Product = value; }
        }
        #endregion

        private int m_NumberOfCards;
        #region Constructors / Destructors
        public CardGenes(int NumberOfCards)
        {
            this.m_NumberOfCards = NumberOfCards;
        }
        #endregion
        
        #region IGenotype Members
        private List<Gene> m_Genes;
        public List<Gene> Genes
        {
            get
            {
                if (m_Genes == null)
                {
                    m_Genes = new List<Gene>(m_NumberOfCards);
                    Random r = new Random();
                    for (int i = 0; i < m_NumberOfCards; i++)
                    {
                        int j = i + 1;
                        double curr = r.NextDouble();
                        EGene gene = curr < 0.5 ? EGene.Off : EGene.On;
                        m_Genes.Add(new Gene(j.ToString(), gene));
                    }
                }
                return m_Genes;
            }
        }

        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -