示例#1
0
 static void FirstFit(List <Item> items, Knapsack sack)
 {
     for (int i = 0; !sack.IsFull; i++)
     {
         sack.AddItem(items[i]);
     }
 }
        static void Main()
        {
            Product beer = new Product("beer", 3, 2);
            Product vodka = new Product("vodka", 8, 12);
            Product cheese = new Product("cheese", 4, 5);
            Product nuts = new Product("nuts", 1, 4);
            Product ham = new Product("ham", 2, 3);
            Product whiskey = new Product("whiskey", 8, 13);

            List<Product> products = new List<Product>();
            products.Add(beer);
            products.Add(vodka);
            products.Add(cheese);
            products.Add(nuts);
            products.Add(ham);
            products.Add(whiskey);

            int bagCapacity = 10;

            Knapsack sack = new Knapsack();

            List<Product> productsInSack = sack.FindBestItems(products, bagCapacity);
            
            Console.WriteLine(ListToString(productsInSack));
        }
示例#3
0
        public static void Main()
        {
            Console.WriteLine("Enter M and N");
            string[] knapsackParts = Console.ReadLine().Split(' ');
            int      M             = int.Parse(knapsackParts[0]);
            int      N             = int.Parse(knapsackParts[1]);

            Knapsack knapsack = new Knapsack(M, N);

            Console.WriteLine("Enter {0} items with cost and weight", N);
            for (int i = 0; i < N; i++)
            {
                string[] itemParts = Console.ReadLine().Split(' ');
                int      cost      = int.Parse(itemParts[0]);
                int      weight    = int.Parse(itemParts[1]);
                Item     item      = new Item(cost, weight);
                knapsack.AddItem(item);
            }

            Console.WriteLine();

            KnapsackProblemSolver solver = new KnapsackProblemSolver(knapsack);

            solver.Solve();
        }
示例#4
0
        static void LargestToSmallest(List <Item> items, Knapsack sack)
        {
            items = items.OrderBy(x => x.Weight).Reverse().ToList();

            for (int i = 0; !sack.IsFull; i++)
            {
                sack.AddItem(items[i]);
            }
        }
        static void Main()
        {
            Knapsack sack = new Knapsack(10);

            List<Product> items = new List<Product>()
            {
                new Product("beer", 3, 2),
                new Product("vodka", 8, 12),
                new Product("cheese", 4, 5),
                new Product("nuts", 1, 4),
                new Product("ham", 2, 3),
                new Product("whiskey", 8, 13)
            };

            ProcessItems(items.Count,10,items);
        }
示例#6
0
        static void Main()
        {
            Knapsack sack = new Knapsack(10);

            List <Product> items = new List <Product>()
            {
                new Product("beer", 3, 2),
                new Product("vodka", 8, 12),
                new Product("cheese", 4, 5),
                new Product("nuts", 1, 4),
                new Product("ham", 2, 3),
                new Product("whiskey", 8, 13)
            };

            ProcessItems(items.Count, 10, items);
        }
示例#7
0
文件: Program.cs 项目: eProw/Console
        static void Main(string[] args)
        {
            var capacity = 14;
            var items    = new Item[]
            {
                new Item(weight: 1, value: 13),
                new Item(weight: 6, value: 5),
                new Item(weight: 6, value: 4),
                new Item(weight: 5, value: 2)
            };

            var result = new Knapsack().Fill(items, capacity);

            result.ShowBackTrack(items, capacity, items.Length);
            Console.WriteLine($"Maximum value is: {result.GetMaxValue()}");

            Console.WriteLine("Press any key...");
            Console.ReadKey();
        }
示例#8
0
        static void Main(string[] args)
        {
            var sack1  = new Knapsack("JustABag", 15);
            var items1 = new List <Item>()
            {
                new Item("GreenBox", 12, 4),
                new Item("GrayBox", 1, 2),
                new Item("YellowBox", 4, 10),
                new Item("PinkBox,", 1, 1),
                new Item("BlueBox", 2, 2)
            };

            var sack2  = new Knapsack("JustABag", 15);
            var items2 = new List <Item>()
            {
                new Item("GreenBox", 12, 4),
                new Item("GrayBox", 1, 2),
                new Item("YellowBox", 4, 10),
                new Item("PinkBox,", 1, 1),
                new Item("BlueBox", 2, 2)
            };

            var sack3  = new Knapsack("JustABag", 15);
            var items3 = new List <Item>()
            {
                new Item("GreenBox", 12, 4),
                new Item("GrayBox", 1, 2),
                new Item("YellowBox", 4, 10),
                new Item("PinkBox,", 1, 1),
                new Item("BlueBox", 2, 2)
            };

            FirstFit(items1, sack1);
            sack1.Items.ForEach(x => Console.WriteLine(x));
            SmallestToLargest(items2, sack2);
            Console.WriteLine();
            sack2.Items.ForEach(x => Console.WriteLine(x));
            LargestToSmallest(items3, sack3);
            Console.WriteLine();
            sack3.Items.ForEach(x => Console.WriteLine(x));
        }
示例#9
0
 public KnapsackTest()
 {
     knapsack = new Knapsack(SIZE);
 }
示例#10
0
 public KnapsackProblemSolver(Knapsack knapsack)
 {
     this.knapsack   = knapsack;
     this.population = new List <string>();
     this.random     = new Random();
 }