Esempio n. 1
0
        static void Main(string[] args)
        {
            var knapsackData = new KnapsackData(50);

            knapsackData.AddItem(new Item(10, 60));
            knapsackData.AddItem(new Item(20, 120));
            knapsackData.AddItem(new Item(20, 110));
            knapsackData.AddItem(new Item(80, 150));
            knapsackData.AddItem(new Item(40, 210));
            knapsackData.AddItem(new Item(50, 100));
            knapsackData.AddItem(new Item(10, 100));
            knapsackData.AddItem(new Item(20, 100));
            knapsackData.AddItem(new Item(90, 400));

            Console.WriteLine("Starting processing...");
            var sasolver = new SaSolver(3, 4000.0, 0.00001, 0.9999);
            var solver   = new KnapsackSolver(knapsackData, sasolver);
            var solution = solver.GetSolution();

            Console.WriteLine($"Result: taken time {solution.TakenTime}, picked items");
            foreach (var item in solution.PickedItem)
            {
                Console.WriteLine($"item: weight {item.Weight}, value {item.Value}");
            }
            Console.WriteLine($"Result: gained value {solution.GainedValue}, gained weight {solution.GainedWeight}");
            Console.ReadKey();
        }
Esempio n. 2
0
        public Solution Solve(KnapsackData data)
        {
            _data = data;
            long start   = DateTime.UtcNow.Millisecond;
            var  current = new BinarySolution(data.AvailableItems.Count);
            var  best    = current;

            current.UpdateFitness(data, Alpha);

            while (_temperature > _endingTemperature)
            {
                for (var m = 0; m < _samplingSize; m++)
                {
                    current = GetNextState(current);
                    if (current.Fitness < best.Fitness)
                    {
                        best = current;
                    }
                }

                Cool();
            }

            long end        = DateTime.UtcNow.Millisecond;
            var  pickedItem = GenerateSolution(data, best);

            return(new Solution(pickedItem, end - start));
        }
Esempio n. 3
0
        public List <Item> GenerateSolution(KnapsackData knapsackData, BinarySolution solution)
        {
            var pickedItem = new List <Item>();

            for (var i = 0; i < knapsackData.AvailableItems.Count; i++)
            {
                if (solution.GetBit(i) == 1)
                {
                    pickedItem.Add(knapsackData.AvailableItems[i]);
                }
            }
            return(pickedItem);
        }
        public void UpdateFitness(KnapsackData data, double alpha)
        {
            double sumVal = 0, sumWeight = 0;

            for (var i = 0; i < data.AvailableItems.Count; i++)
            {
                var item = data.AvailableItems[i];
                if (GetBit(i) == 1)
                {
                    sumWeight += item.Weight;
                }
                else
                {
                    sumVal += item.Value;
                }
            }
            var violation = Math.Max(sumWeight / data.MaxWeight - 1, 0);

            Weight  = sumWeight;
            Fitness = sumVal + alpha * violation;
        }
 public KnapsackSolver(KnapsackData data, IKnapsackStrategy strategy)
 {
     _data     = data;
     _strategy = strategy;
 }