예제 #1
0
파일: Cargo.cs 프로젝트: yongzpub/Alogrithm
 public Cargo Clone() {
     Cargo result = new Cargo(this.MaxVolumn, this.MaxWeight);
     result.VolumnAvailable = this.VolumnAvailable;
     result.WeightAvailable = this.WeightAvailable;
     result.Value = this.Value;
     result.Items.AddRange(this.Items);
     return result;
 }
예제 #2
0
 public Tuple<int, int, Cargo> GetMaxValue(Cargo cargo, List<Item> items)
 {
     Cargo c = cargo.Clone();
     int count = (int)(items.Count * Math.Log(items.Count));
     foreach (Item i in items.OrderByDescending(i => i.Value / (decimal)(Math.Sqrt(i.Volumn * i.Volumn + i.Weight * i.Weight)))) {
         try {
             c.AddItem(i);
             count++;
         } catch { }
     }
     return new Tuple<int, int, Cargo>(count, 1, c);
 }
예제 #3
0
 static void Main(string[] args)
 {
     Cargo cargo = new Cargo(50, 50);
     List<Item> items = new List<Item> {new Item("1", 10, 1, 60), new Item("2", 20, 1, 100), new Item("3", 30, 1, 120) };
     Print(cargo, items);
     items = new List<Item> {new Item("1", 12, 1, 60), new Item("2", 40, 1, 160), new Item("3", 12, 1, 60), new Item("4", 12, 1, 60) };
     Print(cargo, items);
     items = new List<Item> { new Item("1", 1, 1, 5), new Item("2", 3, 1, 2), new Item("3", 5, 0, 7), new Item("4", 7, 1, 3), new Item("5", 11, 1, 6) };
     cargo = new Cargo(20, 50);
     Print(cargo, items);
     Console.ReadKey();
 }
예제 #4
0
 static void Print(Cargo cargo, List<Item> items)
 {
     CargoDynamicsProgramming c = new CargoDynamicsProgramming();
     CargoGreedy g = new CargoGreedy();
     Console.Write($"{cargo}; ");
     items.ForEach((i) => { Console.Write($"{i} "); });
     Console.WriteLine();
     Tuple<int, int, Cargo> t = c.GetMaxValue(cargo, items);
     Console.Write($"{t.Item1}: {t.Item2}: {t.Item3.Value}; ");
     t.Item3.Items.ForEach((i) => { Console.Write($"{i} "); });
     Console.WriteLine();
     t = g.GetMaxValue(cargo, items);
     Console.Write($"{t.Item1}: {t.Item2}: {t.Item3.Value}; ");
     t.Item3.Items.ForEach((i) => { Console.Write($"{i} "); });
     Console.WriteLine();
 }
 // Greedy Alorithm
 //  MaxValue(i, w, v) = MAX( MaxValue(i-1, w, v),
 //                      I(i).Valve + MAX( MaxValue(i-1, w - I(i).w, v - I(i).v)
 protected int mProcess(Cargo cargo, List<Item> items, Dictionary<string, Cargo> mem, int i, double weight, double volumn) {
     int count = 0;
     string cur = this.mGetLabel(i, weight, volumn);
     if (i <= 0 || weight <= 0 || volumn <= 0 || mem.ContainsKey(cur))
         return count;
     count++;
     Item item = items[i - 1];
     bool couldLoad = weight >= item.Weight && volumn >= item.Volumn;
     if (couldLoad)
         count += this.mProcess(cargo, items, mem, i - 1, weight - item.Weight, volumn - item.Volumn);
     count += this.mProcess(cargo, items, mem, i - 1, weight, volumn);
     decimal value = (couldLoad)? item.Value + this.mGetMem(cargo, mem, i - 1, weight - item.Weight, volumn - item.Volumn).Value: 0;
     if ((!couldLoad) || value <= this.mGetMem(cargo, mem, i - 1, weight, volumn).Value) {
         mem[cur] = this.mGetMem(cargo, mem, i - 1, weight, volumn);
         return count;
     }
     mem[cur] = this.mGetMem(cargo, mem, i - 1, weight - item.Weight, volumn - item.Volumn).Clone();
     mem[cur].AddItem(item);
     return count;
 }
 // Dynamics Programming
 //  MaxValue = 
 public Tuple<int, int, Cargo> GetMaxValue(Cargo cargo, List<Item> items) {
     Dictionary<string, Cargo> mem = new Dictionary<string, Cargo>();
     int count = this.mProcess(cargo, items, mem, items.Count, cargo.MaxWeight, cargo.MaxVolumn);
     return new Tuple<int, int, Cargo>(count, mem.Count, this.mGetMem(cargo, mem, items.Count, cargo.MaxWeight, cargo.MaxVolumn));
 }
 protected Cargo mGetMem(Cargo cargo, Dictionary<string, Cargo> mem, int i, double weight, double volumn) =>
     (i <= 0 || weight <= 0 || volumn <= 0) ? cargo : mem[this.mGetLabel(i, weight, volumn)];