Ejemplo n.º 1
0
        // 计算某一个个体的适应度,最重要的地方
        public static int GetFitness(GiftTicketIndividual individual)
        {
            int fitness = 0;

            // 个体和候选解比较,约接近候选集,适应度越高

            for (int i = 0; i < individual.Size(); i++)
            {//所有优惠总和
                fitness += individual.GetGene(i).DiscountMoney();
                //截止日期越前越优先使用
                if (individual.GetGene(i).Ticket != null)
                {
                    TimeSpan ts = individual.GetGene(i).Ticket.ExpiredDate - DateTime.Now;
                    //这个影响比较大,暂时不计算过期时间
                    // fitness -= ts.Days;
                }
            }

            return(fitness);
        }
Ejemplo n.º 2
0
        // 变异
        private static void Mutate(GiftTicketIndividual indiv)
        {
            // Console.WriteLine("变异");
            // Loop through genes
            for (int i = 0; i < indiv.Size(); i++)
            {
                if (new Random().NextDouble() <= mutationRate)
                {
                    //从剩余的优惠券中,随机一个优惠券
                    GiftTicketMatch gene   = indiv.GetGene(i);
                    GiftTicket      ticket = null;
                    if (indiv.tickets.Count > 0)
                    {
                        Random random     = new Random(GetRandomSeed());
                        int    pickTicket = random.Next(0, indiv.tickets.Count);

                        //    int pickTicket = (int)(new Random().NextDouble() * indiv.tickets.Count);
                        ticket = indiv.tickets[pickTicket];
                        indiv.SetMatch(i, gene, ticket);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        // 交叉
        private static GiftTicketIndividual Crossover(GiftTicketIndividual indiv1, GiftTicketIndividual indiv2)
        {
            //  Console.WriteLine("交叉");
            GiftTicketIndividual newSol = new GiftTicketIndividual();

            newSol.RetailsOrg = GiftTicketAlgorithm.RetailDetails;
            newSol.TicketsOrg = GiftTicketAlgorithm.Tickets;
            // Loop through genes
            for (int i = 0; i < indiv1.Size(); i++)
            {
                // 以一定概率交叉,如果优惠券被占用了???那么交叉完了之后这个优惠券就变成空?
                if (new Random().NextDouble() <= uniformRate)
                {
                    //检查优惠券是否被占用?如果被占用是否还有可以替换的优惠券使用?有就替换掉?

                    newSol.SetGene(i, indiv1.GetGene(i));
                }
                else
                {
                    newSol.SetGene(i, indiv2.GetGene(i));
                }
            }
            return(newSol);
        }