public Knapsack ReadKnapsack() { if (_streamReader.EndOfStream || _solutionStreamReader != null && _solutionStreamReader.EndOfStream) { _streamReader.Close(); _solutionStreamReader?.Close(); return null; } var line = _streamReader.ReadLine(); var solutionLine = _solutionStreamReader?.ReadLine(); if (solutionLine != null && string.IsNullOrEmpty(solutionLine) || string.IsNullOrEmpty(line)) { _streamReader.Close(); _solutionStreamReader?.Close(); return null; } var splitted = line.Split(' '); var solutionSplitted = solutionLine?.Split(' '); if (splitted.Length < 4) throw new FormatException("Invalid format of input data on line " + _lineNumber); var n = Parse(splitted[1]); if (n < 0) throw new FormatException("Invalid format of input data on line " + _lineNumber); var capacity = Parse(splitted[2]); var values = new int[n * 2]; _lineNumber++; for (var i = 0; i < n; i++) { values[i * 2] = Parse(splitted[3 + i * 2]); values[i * 2 + 1] = Parse(splitted[3 + i * 2 + 1]); } Knapsack knapsack = new Knapsack(values, capacity, 0); int solution; if (false && solutionSplitted != null) { solution = Parse(solutionSplitted[2]); } else { Algorithm algo = new CostDecomposition(); Console.WriteLine($"Calculating correct solution using algo {algo.ToString()}"); algo.Knapsack = knapsack; solution = algo.Solve(); } knapsack.Solution = solution; return knapsack; }
protected override Answer Calculate() { Knapsack bag = new Knapsack(MaxWeight); MaxValueItems = bag.Items.ToArray(); Explore(bag, Items.ToList()); return(new Answer { Items = MaxValueItems, ExecCount = CallCount }); }
static void Main(string[] args) { List <Item> list = new List <Item>(); //item value - weight list.Add(new Item(10, 1)); list.Add(new Item(5, 2)); list.Add(new Item(40, 8)); list.Add(new Item(45, 10)); Knapsack sack = new Knapsack(list, 10); Console.WriteLine($"Greedy: {sack.Greedy()}"); Console.WriteLine($"Brute force: {sack.Iterative()}"); Console.WriteLine($"Divide & conquer: {sack.Recursive(sack.MaxWeight, 0)}"); Console.WriteLine($"Dynamic: {sack.Dynamic()}"); }
static void Main(string[] args) { int W = 11; int[] V = new int[4] { 30, 14, 16, 9 }; int[] Weights = new int[4] { 6, 3, 4, 2 }; Random objR = new Random(); Knapsack objK = new Knapsack(); //for (int i = 0; i < V.Length; i++) //{ // V[i] = objR.Next(1, 10); // Weights[i] = objR.Next(1, 10); //} Console.WriteLine(objK.ComputeDPKnapsack(W, V, Weights)); Console.ReadLine(); }
private static void Main(string[] args) { Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(); Action <object> write = Console.Write; var stopwatch = new Stopwatch(); stopwatch.Start(); write("Running ..\n\n"); var rand = new Random(); // -------------- Insert data here const int N = 10; const int maxWeight = 20; var items = new List <Item>(); for (var i = 0; i < N; i++) { items.Add(new Item { w = rand.Next(1, 10), v = rand.Next(1, 100) }); } Knapsack.Init(items, maxWeight); Knapsack.Run(); stopwatch.Stop(); write("Done\n\n"); Knapsack.PrintPicksMatrix(write); Knapsack.Print(write, true); write(string.Format("\n\nDuration: {0}\nPress a key to exit\n", stopwatch.Elapsed.ToString())); Console.ReadKey(); }
public void Explore(Knapsack knapsack, List <Item> options) { //篩選小於背包剩餘可負重的物品一一嘗試 //這裡直接用Where過濾,若要提高效能需再優化 foreach (var item in options.Where(o => o.Weight <= knapsack.AllowedWeight)) { CallCount++; knapsack.AddItem(item); //放入物品 if (knapsack.CurrentValue > MaxValue) //若超越最高記錄就記錄 { MaxValueItems = knapsack.Items.ToArray(); MaxValue = knapsack.CurrentValue; } if (!Duplicatable) //若不允許物品重複,已放入背包的項目需移出選項 { Explore(knapsack, options.Except(new Item[] { item }).ToList()); } else { Explore(knapsack, options.ToList()); } knapsack.RemoveItem(item); } }
public Knapsack ReadKnapsack() { if (_streamReader.EndOfStream || _solutionStreamReader != null && _solutionStreamReader.EndOfStream) { _streamReader.Close(); _solutionStreamReader?.Close(); return(null); } var line = _streamReader.ReadLine(); var solutionLine = _solutionStreamReader?.ReadLine(); if (solutionLine != null && string.IsNullOrEmpty(solutionLine) || string.IsNullOrEmpty(line)) { _streamReader.Close(); _solutionStreamReader?.Close(); return(null); } var splitted = line.Split(' '); var solutionSplitted = solutionLine?.Split(' '); if (splitted.Length < 4) { throw new FormatException("Invalid format of input data on line " + _lineNumber); } var n = Parse(splitted[1]); if (n < 0) { throw new FormatException("Invalid format of input data on line " + _lineNumber); } var capacity = Parse(splitted[2]); var values = new int[n * 2]; _lineNumber++; for (var i = 0; i < n; i++) { values[i * 2] = Parse(splitted[3 + i * 2]); values[i * 2 + 1] = Parse(splitted[3 + i * 2 + 1]); } Knapsack knapsack = new Knapsack(values, capacity, 0); int solution; if (false && solutionSplitted != null) { solution = Parse(solutionSplitted[2]); } else { Algorithm algo = new CostDecomposition(); Console.WriteLine($"Calculating correct solution using algo {algo.ToString()}"); algo.Knapsack = knapsack; solution = algo.Solve(); } knapsack.Solution = solution; return(knapsack); }