Advertisement
medmedin2014

Untitled

Mar 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Comparator;
  4. import java.util.List;
  5.  
  6. public class KnapsackProb {
  7.  
  8.     public static void main(String[] args) {
  9.         double[][] items = new double[][] { { 5, 3, 6, 12, 7 }, { 2, 3, 4, 7, 1 } };
  10.         double[][] items2 = new double[][] { { 18, 15, 10 }, { 25, 24, 15 } };
  11.         double[][] items3 = new double[][] { { 5, 4, 10, 7, 10, 4 }, { 9, 10, 12, 20, 18, 9 } };
  12.         // knapsack(0,items2,21);
  13.         knapsack(items, 21);
  14.         // knapsack(items3,25);
  15.     }
  16.  
  17.     public static void knapsack(double[][] items, double maxCapacity) {
  18.         List<Double> sackOrdered = new ArrayList<Double>();
  19.         List<Double> sack = new ArrayList<Double>();
  20.         List<Integer> order = new ArrayList<Integer>();
  21.         ArrayList<Integer> index = new ArrayList<Integer>();
  22.        
  23.         List<Data> listOfData = new ArrayList<>();
  24.  
  25.         int row = items.length;
  26.         int col = items[0].length;
  27.  
  28.         // items[0][i] = weights //items[1][i]= values
  29.         double[] weights = new double[col];
  30.         double[] values = new double[col];
  31.         double totalWeight = 0;
  32.         double totalValue = 0;
  33.         // System.out.println(row);
  34.         // System.out.println(col);
  35.  
  36.         double ratio = 0.0;
  37.         for (int j = 0; j < col; j++) {
  38.             weights[j] = items[0][j];
  39.             values[j] = items[1][j];
  40.             ratio = values[j] / weights[j];
  41.             listOfData.add(new Data(j,ratio));
  42.             sackOrdered.add(ratio);
  43.             index.add(j);
  44.             // System.out.println(values[j]+" * "+ weights[j]);
  45.         }
  46.  
  47.         for (Data data : listOfData) {
  48.             System.out.println(data);
  49.         }
  50.        
  51.         listOfData.sort(Comparator.comparingDouble(Data::getValue).reversed());
  52.        
  53.         System.out.println("\n-----After sorting");
  54.         for (Data data : listOfData) {
  55.             System.out.println(data);
  56.         }
  57.  
  58.         int[] indexes = listOfData.stream().mapToInt(Data::getInitialIndex).toArray();
  59.         System.out.println(Arrays.toString(indexes));
  60.  
  61.         // System.out.println(Arrays.toString(indexes));
  62.  
  63.         // System.out.println("after ordering "+sackOrdered);
  64.  
  65.         for (int j = 0; j < col; j++) {
  66.             // System.out.println(items[i][j]);
  67.             // weights[j] = items[0][j];
  68.             // values[j] = items[1][j];
  69.             // System.out.println(weights[j] + " //// "+values[j]);
  70.  
  71.             if (maxCapacity > 0) {
  72.  
  73.                 sack.add(weights[indexes[j]]);
  74.                 order.add(indexes[j]);
  75.                 totalWeight = totalWeight + weights[indexes[j]];
  76.                 totalValue = totalValue + values[indexes[j]];
  77.  
  78.                 // System.out.println("sack contents before checking the weight"+sack);
  79.                 /*
  80.                  * System.out.println("order  "+order);
  81.                  * System.out.println("totalWeight = "+totalWeight);
  82.                  * System.out.println("totalValues = "+totalValue);
  83.                  */
  84.  
  85.                 if (totalWeight > maxCapacity) {
  86.                     // sack.add(weights[j]);
  87.                     // order.add(j);
  88.                     sack.remove(sack.size() - 1);
  89.                     order.remove(order.size() - 1);
  90.                     totalWeight = totalWeight - weights[indexes[j]];
  91.                     totalValue = totalValue - values[indexes[j]];
  92.  
  93.                     // System.out.println("sack contents after checking the weight"+sack);
  94.                     // System.out.println("order "+order);
  95.                     // System.out.println("totalWeight = "+totalWeight);
  96.                     // System.out.println("totalValues = "+totalValue);
  97.  
  98.                 } else {
  99.  
  100.                 }
  101.  
  102.             }
  103.  
  104.         }
  105.         System.out.println("order  " + order);
  106.         System.out.println("totalWeight = " + totalWeight);
  107.         System.out.println("totalValues = " + totalValue);
  108.     }
  109.  
  110. }
  111.  
  112. class Data {
  113.     private int initialIndex;
  114.     private double value;
  115.  
  116.     public Data(int initialIndex, double value) {
  117.         super();
  118.         this.initialIndex = initialIndex;
  119.         this.value = value;
  120.     }
  121.  
  122.     public double getValue() {
  123.         return value;
  124.     }
  125.  
  126.     public int getInitialIndex() {
  127.         return initialIndex;
  128.     }
  129.    
  130.     @Override
  131.     public String toString() {
  132.         return "Data(initialIndex= " + initialIndex + ", value= " + value + ")";
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement