Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.56 KB | None | 0 0
  1. package evolution.operators;
  2.  
  3. import evolution.Population;
  4. import evolution.RandomNumberGenerator;
  5. import evolution.individuals.Individual;
  6. import evolution.individuals.IntegerIndividual;
  7.  
  8. import java.util.Arrays;
  9. import java.util.Vector;
  10.  
  11. public class BinPackingOperator implements Operator {
  12.  
  13.     private Vector<Double> weights; //weights of items
  14.     private double operationProbability;
  15.     private int K; // bin count
  16.     private RandomNumberGenerator randomNumberGenerator;
  17.  
  18.  
  19.     public BinPackingOperator(Vector<Double> weights, double operationProbability, int K) {
  20.         this.weights = weights;
  21.         this.operationProbability = operationProbability;
  22.         this.K = K;
  23.         this.randomNumberGenerator = RandomNumberGenerator.getInstance();
  24.     }
  25.  
  26.  
  27.     @Override
  28.     public void operate(Population parents, Population offspring) {
  29.  
  30.         int populationSize = parents.getPopulationSize();
  31.  
  32.         for (int i = 0; i < populationSize; i++) {
  33.  
  34.             IntegerIndividual p1 = (IntegerIndividual) parents.get(i);
  35.             IntegerIndividual o1 = (IntegerIndividual) p1.clone();
  36.  
  37.             if ( randomNumberGenerator.nextDouble() < operationProbability)
  38.             {
  39.                 //find bins with biggest difference
  40.                 var binWeights =  getBinWeights(o1);
  41.  
  42.                 double minWeight = Integer.MAX_VALUE;
  43.                 double maxWeight = Integer.MIN_VALUE;
  44.                 int aIndex = 0;
  45.                 int bIndex = 0;
  46.                 for (int j = 0; j < binWeights.length; j++) {
  47.                     if (binWeights[j] > maxWeight) {
  48.                         maxWeight = binWeights[j];
  49.                         bIndex = j;
  50.                     }
  51.                     if(binWeights[j] < minWeight) {
  52.  
  53.                         minWeight = binWeights[j];
  54.                         aIndex = j;
  55.                     }
  56.                 }
  57.                 var difference = maxWeight - minWeight;
  58.                 //swap items between packages if it lowers the difference
  59.                 var genes = o1.toIntArray();
  60.  
  61.                 for (int j = 0; j < genes.length; j++) {
  62.                     if (genes[j] == aIndex) {
  63.                         var newDifference = Math.abs((binWeights[bIndex] + weights.get(j)) - (binWeights[aIndex] - weights.get(j)));
  64.                         if (newDifference < difference)
  65.                         {
  66.                             o1.set(j, bIndex);
  67.                             difference = newDifference;
  68.                         }
  69.                     }
  70.                     if (genes[j] == bIndex) {
  71.                         var newDifference = Math.abs((binWeights[aIndex] + weights.get(j)) - (binWeights[bIndex] - weights.get(j)));
  72.                         if (newDifference < difference)
  73.                         {
  74.                             o1.set(j, aIndex);
  75.                             difference = newDifference;
  76.                         }
  77.                     }
  78.                 }
  79.             }
  80.  
  81.             offspring.add(o1);
  82.         }
  83.     }
  84.     public int[] getBinWeights(Individual ind) {
  85.  
  86.         int[] binWeights = new int[K];
  87.  
  88.         int[] bins = ((IntegerIndividual) ind).toIntArray();
  89.  
  90.         for (int i = 0; i < bins.length; i++) {
  91.  
  92.             binWeights[bins[i]] += weights.get(i);
  93.         }
  94.  
  95.         return binWeights;
  96.  
  97.     }
  98. }
  99. //package evolution.operators;
  100. //
  101. //import evolution.Population;
  102. //import evolution.RandomNumberGenerator;
  103. //import evolution.individuals.Individual;
  104. //import evolution.individuals.IntegerIndividual;
  105. //
  106. //import java.util.Vector;
  107. //
  108. //public class BinPackingOperator implements Operator {
  109. //
  110. //    double mutationProbability;
  111. //    double geneChangeProbability;
  112. //    Vector<Double> weights;
  113. //    int K;
  114. //
  115. //    RandomNumberGenerator rng = RandomNumberGenerator.getInstance();
  116. //
  117. //
  118. //    public int[] getBinWeights(Individual ind) {
  119. //
  120. //
  121. //        int[] bins = ((IntegerIndividual) ind).toIntArray();
  122. //
  123. //        int[] binWeights = new int[K];
  124. //
  125. //        for (int i = 0; i < bins.length; i++) {
  126. //
  127. //            binWeights[bins[i]] += weights.get(i);
  128. //        }
  129. //
  130. //        return binWeights;
  131. //
  132. //    }
  133. //
  134. //    int find (IntegerIndividual ind, int value){
  135. //        int[] arrayInd = ind.toIntArray();
  136. //        for(int i = 0; i < ind.length(); i++){
  137. //            if(arrayInd[i] == value){
  138. //                return i;
  139. //            }
  140. //        }
  141. //        return -1;
  142. //    }
  143. //
  144. //
  145. //    public BinPackingOperator(Vector<Double> weights, double mutationProbability,   int K) {
  146. //        this.mutationProbability = mutationProbability;
  147. //        this.geneChangeProbability = geneChangeProbability;
  148. //        this.weights = weights;
  149. //        this.K = K;
  150. //    }
  151. //
  152. //    @Override
  153. //    public void operate(Population parents, Population offspring) {
  154. //
  155. //        int size = parents.getPopulationSize();
  156. //
  157. //        for (int i = 0; i < size; i++) {
  158. //
  159. //            IntegerIndividual p1 = (IntegerIndividual) parents.get(i);
  160. //            IntegerIndividual o1 = (IntegerIndividual) p1.clone();
  161. //
  162. //            if (rng.nextDouble() < mutationProbability) {
  163. //                int[] binWeights = getBinWeights(o1);
  164. //
  165. //                double min = Integer.MAX_VALUE;
  166. //                double max = Integer.MIN_VALUE;
  167. //                int minPos = 0;
  168. //                int maxPos = 0;
  169. //                for (int j = 0; j < K; j++) {
  170. //                    if(binWeights[j] < binWeights[minPos]){
  171. //                        minPos = j;
  172. //                    }
  173. //                    if (binWeights[j] > binWeights[maxPos]) {
  174. //                        maxPos = j;
  175. //                    }
  176. //                }
  177. //                int difference = binWeights[maxPos] - binWeights[minPos];
  178. //                int[] arrayInd = o1.toIntArray();
  179. //                for(int j = 0; j < arrayInd.length; j++){
  180. //                    if(arrayInd[j] == maxPos){
  181. //                        if(Math.abs(difference - 2*weights.get(j)) < Math.abs(difference)){
  182. //                            o1.set(j,minPos);
  183. //                            difference -= 2*weights.get(j);
  184. //                        }
  185. //                    }
  186. //                    if(arrayInd[j] == minPos){
  187. //                        if(Math.abs(difference + 2*weights.get(j)) < Math.abs(difference)){
  188. //                            o1.set(j,maxPos);
  189. //                            difference += 2*weights.get(j);
  190. //                        }
  191. //                    }
  192. //                }
  193. //
  194. //            }
  195. //
  196. //            offspring.add(o1);
  197. //        }
  198. //    }
  199. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement