Ejemplo n.º 1
0
        public static double EvaluateFitness(Chromosome chromosome)
        {
            double fitnessValue = 0;

            //decode chromosome
            Bag bag = new Bag();

            for (int i = 0; i < chromosome.Count; i++)
            {
                if (chromosome.Genes[i].BinaryValue == 1)
                {
                    bag.AddItem(knapsackItems[i]);
                }
            }



            if (bag.TotalWeight <= 400)
            {
                //put fitnessValue between 0 and 1
                fitnessValue = (bag.TotalValue / Convert.ToDouble(10000));
                //Console.WriteLine(" ----------------------- Total Weight : {0}   Total Value : {1} Fitness : {2}  ", bag.TotalWeight, bag.TotalWeight, fitnessValue.ToString());
            }

            return(fitnessValue);
        }
Ejemplo n.º 2
0
        private static void ga_OnGenerationComplete(object sender, GaEventArgs e)
        {
            // lay giai phap tot nhat
            var chromosome = e.Population.GetTop(1)[0];

            //decode chromosome
            Bag bag = new Bag();

            for (int i = 0; i < chromosome.Count; i++)
            {
                if (chromosome.Genes[i].BinaryValue == 1)
                {
                    bag.AddItem(knapsackItems[i]);
                }
            }
            Console.WriteLine(" Generation info: Total Weight : {0}   Total Value : {1} Fitness : {2}", bag.TotalWeight, bag.TotalValue, e.Population.MaximumFitness);
        }
Ejemplo n.º 3
0
        public static double EvaluateFitness(Chromosome chromosome)
        {
            double fitnessValue = 0;

            //decode chromosome
            Bag bag = new Bag();

            for (int i = 0; i < chromosome.Count; i++)
            {
                if (chromosome.Genes[i].BinaryValue == 1)
                {
                    bag.AddItem(knapsackItems[i]);
                }
            }
            if (bag.TotalWeight <= 400)
            {
                fitnessValue = (bag.TotalValue / Convert.ToDouble(10000));
            }

            return(fitnessValue);
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            knapsackItems = new List <Bag.Item>();
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Map", Weight = 9, Value = 150
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Water", Weight = 153, Value = 200
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Compass", Weight = 13, Value = 35
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Sandwitch", Weight = 50, Value = 160
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Glucose", Weight = 15, Value = 60
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Tin", Weight = 68, Value = 45
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Banana", Weight = 27, Value = 60
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Apple", Weight = 39, Value = 40
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Cheese", Weight = 23, Value = 30
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Beer", Weight = 52, Value = 10
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Suntan Cream", Weight = 11, Value = 70
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Camera", Weight = 32, Value = 30
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "T-shirt", Weight = 24, Value = 15
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Trousers", Weight = 48, Value = 10
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Umbrella", Weight = 73, Value = 40
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "WaterProof Trousers", Weight = 42, Value = 70
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Note-Case", Weight = 22, Value = 80
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Sunglasses", Weight = 7, Value = 20
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Towel", Weight = 18, Value = 12
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Socks", Weight = 4, Value = 50
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "Book", Weight = 30, Value = 10
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "waterproof overclothes ", Weight = 43, Value = 75
            });

            const double crossoverProbability = 0.65;
            const double mutationProbability  = 0.08;
            const int    elitismPercentage    = 5;

            //create the population
            var population = new Population(100, 22, false, false);

            //create the genetic operators
            var elite = new Elite(elitismPercentage);

            var crossover = new Crossover(crossoverProbability, true)
            {
                CrossoverType = CrossoverType.SinglePoint
            };

            var mutation = new BinaryMutate(mutationProbability, true);

            //create the GA itself
            var ga = new GeneticAlgorithm(population, EvaluateFitness);

            //subscribe to the GAs Generation Complete event
            ga.OnGenerationComplete += ga_OnGenerationComplete;

            //add the operators to the ga process pipeline
            ga.Operators.Add(elite);
            ga.Operators.Add(crossover);
            ga.Operators.Add(mutation);

            //run the GA
            ga.Run(TerminateAlgorithm);


            //get the best solution and print Items
            var Bestchromosome = ga.Population.GetTop(1)[0];

            //decode chromosome
            Console.WriteLine("Maximum value of knapsack contains these Items : ");
            Bag BestBag = new Bag();

            for (int i = 0; i < Bestchromosome.Count; i++)
            {
                if (Bestchromosome.Genes[i].BinaryValue == 1)
                {
                    Console.WriteLine(knapsackItems[i].ToString());
                    BestBag.AddItem(knapsackItems[i]);
                }
            }
            Console.WriteLine(" Best knapsack information:\n Total Weight : {0}   Total Value : {1} Fitness : {2}", BestBag.TotalWeight, BestBag.TotalValue, Bestchromosome.Fitness);

            Console.ReadKey();
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            knapsackItems = new List <Bag.Item>();
            knapsackItems.Add(new Bag.Item()
            {
                Name = "a", Weight = 9, Value = 150
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "b", Weight = 153, Value = 200
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "c", Weight = 13, Value = 35
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "d", Weight = 50, Value = 160
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "e", Weight = 15, Value = 60
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "f", Weight = 68, Value = 45
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "g", Weight = 27, Value = 60
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "h", Weight = 39, Value = 40
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "i", Weight = 23, Value = 30
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "j", Weight = 52, Value = 10
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "k", Weight = 11, Value = 70
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "l", Weight = 32, Value = 30
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "m", Weight = 24, Value = 15
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "n", Weight = 48, Value = 10
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "o", Weight = 73, Value = 40
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "p", Weight = 42, Value = 70
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "q", Weight = 22, Value = 80
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "r", Weight = 7, Value = 20
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "s", Weight = 18, Value = 12
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "t", Weight = 4, Value = 50
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "u", Weight = 30, Value = 10
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "v", Weight = 43, Value = 75
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "v", Weight = 43, Value = 75
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "v", Weight = 43, Value = 75
            });
            knapsackItems.Add(new Bag.Item()
            {
                Name = "v", Weight = 43, Value = 75
            });

            const double crossoverProbability = 0.65;
            const double mutationProbability  = 0.08;
            const int    elitismPercentage    = 5;

            // tao quan the
            var population = new Population(100, 22, false, false);

            // tao toan tu di truyen
            var elite = new Elite(elitismPercentage);

            var crossover = new Crossover(crossoverProbability, true)
            {
                CrossoverType = CrossoverType.SinglePoint
            };
            var mutation = new BinaryMutate(mutationProbability, true);

            // tao GA
            var ga = new GeneticAlgorithm(population, EvaluateFitness);

            ga.OnGenerationComplete += ga_OnGenerationComplete;

            // them tung toan tu vao GA
            ga.Operators.Add(elite);
            ga.Operators.Add(crossover);
            ga.Operators.Add(mutation);

            // chay GA
            ga.Run(TerminateAlgorithm);

            // lay do vat tot nhat
            var Bestchromosome = ga.Population.GetTop(1)[0];

            // decode chromosome
            Console.WriteLine("Maximum value of knapsack contains these Items : ");
            Bag BestBag = new Bag();

            for (int i = 0; i < Bestchromosome.Count; i++)
            {
                if (Bestchromosome.Genes[i].BinaryValue == 1)
                {
                    Console.WriteLine(knapsackItems[i].ToString());
                    BestBag.AddItem(knapsackItems[i]);
                }
            }
            Console.WriteLine(" Best knapsack information:\n Total Weight : {0}   Total Value : {1} Fitness : {2}", BestBag.TotalWeight, BestBag.TotalValue, Bestchromosome.Fitness);
            Console.ReadKey();
        }