Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. import static java.lang.Math.min;
  2.  
  3. public abstract class KnapsackStrategy implements Strategy{
  4.     private int fromOneRod;
  5.     private PartitionSet [] bestCut;
  6.     private PartitionSet [] oneRod;
  7.     private Offer offer;
  8.     private Requirements requirements;
  9.     private int writes;
  10.  
  11.     public PartitionSet solve(Offer offer, Requirements requirements) {
  12.         Mask mask = new Mask(requirements);
  13.         this.requirements = requirements;
  14.         this.offer = offer;
  15.         int sizeOfDP = 1<<requirements.getRods().length;
  16.  
  17.  
  18.         fromOneRod = 0;
  19.         writes = 0;
  20.  
  21.  
  22.         bestCut = new PartitionSet[sizeOfDP];
  23.         oneRod = new PartitionSet[sizeOfDP];
  24.         bestCut[0] = new PartitionSet(0, 0, new RodPartition[0]);
  25.         PartitionSet result = getBestCut((Mask) mask.clone());
  26.         System.out.println(fromOneRod);
  27.         System.out.println(writes);
  28.         return result;
  29.     }
  30.  
  31.     public PartitionSet getBestCut(Mask mask){
  32.         int positionFromMask = mask.getPosition();
  33.         if(bestCut[positionFromMask] != null){
  34.             return bestCut[positionFromMask];
  35.         }
  36.         writes +=1;
  37.         Mask ourMask = (Mask) mask.clone();
  38.         while(ourMask.nextSubset(mask)){
  39.  
  40.             PartitionSet set1 = getFromOneRod(ourMask.getComplement(mask));
  41.  
  42.             if(! set1.isReachable()){
  43.                 continue;
  44.             }
  45.             PartitionSet set2 = getBestCut(ourMask);
  46.  
  47.             bestCut[positionFromMask]=chooseBestSet(bestCut[positionFromMask], new PartitionSet(set1, set2));
  48.         }
  49.         return bestCut[positionFromMask];
  50.     }
  51.     private PartitionSet getFromOneRod(Mask mask){
  52.         //TODO tablicowac
  53.         if(oneRod[mask.getPosition()] == null){
  54.             Rod[] fulfilled = requirements.getRods(mask);
  55.             int fulfilledLength = 0;
  56.             for(Rod rod : fulfilled){
  57.                 fulfilledLength += rod.getLength();
  58.             }
  59.             Rod source = chooseBestRod(offer, fulfilledLength);
  60.             if(source == null){
  61.                 return PartitionSet.getDummy();
  62.             }
  63.             Integer[] lengths = new Integer[fulfilled.length];
  64.             for(int i = 0;  i< fulfilled.length; i++) {
  65.                 lengths[i] = fulfilled[i].getLength();
  66.             }
  67.             int wasteSum = source.getLength()-fulfilledLength;
  68.             RodPartition[] partitions = new RodPartition[1];
  69.             partitions[0] = new RodPartition(source.getLength(), lengths);
  70.             fromOneRod +=1;
  71.             oneRod[mask.getPosition()] = new PartitionSet(source.getPrice(), wasteSum, partitions);
  72.         }
  73.         return oneRod[mask.getPosition()];
  74.     }
  75.  
  76.     abstract protected Rod chooseBestRod(Offer offer, int length);
  77.  
  78.     abstract protected PartitionSet chooseBestSet(PartitionSet set1, PartitionSet set2);
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement