Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package evolution.operators;
- import evolution.Population;
- import evolution.RandomNumberGenerator;
- import evolution.individuals.Individual;
- import evolution.individuals.IntegerIndividual;
- import java.util.Arrays;
- import java.util.Vector;
- public class BinPackingOperator implements Operator {
- private Vector<Double> weights; //weights of items
- private double operationProbability;
- private int K; // bin count
- private RandomNumberGenerator randomNumberGenerator;
- public BinPackingOperator(Vector<Double> weights, double operationProbability, int K) {
- this.weights = weights;
- this.operationProbability = operationProbability;
- this.K = K;
- this.randomNumberGenerator = RandomNumberGenerator.getInstance();
- }
- @Override
- public void operate(Population parents, Population offspring) {
- int populationSize = parents.getPopulationSize();
- for (int i = 0; i < populationSize; i++) {
- IntegerIndividual p1 = (IntegerIndividual) parents.get(i);
- IntegerIndividual o1 = (IntegerIndividual) p1.clone();
- if ( randomNumberGenerator.nextDouble() < operationProbability)
- {
- //find bins with biggest difference
- var binWeights = getBinWeights(o1);
- double minWeight = Integer.MAX_VALUE;
- double maxWeight = Integer.MIN_VALUE;
- int aIndex = 0;
- int bIndex = 0;
- for (int j = 0; j < binWeights.length; j++) {
- if (binWeights[j] > maxWeight) {
- maxWeight = binWeights[j];
- bIndex = j;
- }
- if(binWeights[j] < minWeight) {
- minWeight = binWeights[j];
- aIndex = j;
- }
- }
- var difference = maxWeight - minWeight;
- //swap items between packages if it lowers the difference
- var genes = o1.toIntArray();
- for (int j = 0; j < genes.length; j++) {
- if (genes[j] == aIndex) {
- var newDifference = Math.abs((binWeights[bIndex] + weights.get(j)) - (binWeights[aIndex] - weights.get(j)));
- if (newDifference < difference)
- {
- o1.set(j, bIndex);
- difference = newDifference;
- }
- }
- if (genes[j] == bIndex) {
- var newDifference = Math.abs((binWeights[aIndex] + weights.get(j)) - (binWeights[bIndex] - weights.get(j)));
- if (newDifference < difference)
- {
- o1.set(j, aIndex);
- difference = newDifference;
- }
- }
- }
- }
- offspring.add(o1);
- }
- }
- public int[] getBinWeights(Individual ind) {
- int[] binWeights = new int[K];
- int[] bins = ((IntegerIndividual) ind).toIntArray();
- for (int i = 0; i < bins.length; i++) {
- binWeights[bins[i]] += weights.get(i);
- }
- return binWeights;
- }
- }
- //package evolution.operators;
- //
- //import evolution.Population;
- //import evolution.RandomNumberGenerator;
- //import evolution.individuals.Individual;
- //import evolution.individuals.IntegerIndividual;
- //
- //import java.util.Vector;
- //
- //public class BinPackingOperator implements Operator {
- //
- // double mutationProbability;
- // double geneChangeProbability;
- // Vector<Double> weights;
- // int K;
- //
- // RandomNumberGenerator rng = RandomNumberGenerator.getInstance();
- //
- //
- // public int[] getBinWeights(Individual ind) {
- //
- //
- // int[] bins = ((IntegerIndividual) ind).toIntArray();
- //
- // int[] binWeights = new int[K];
- //
- // for (int i = 0; i < bins.length; i++) {
- //
- // binWeights[bins[i]] += weights.get(i);
- // }
- //
- // return binWeights;
- //
- // }
- //
- // int find (IntegerIndividual ind, int value){
- // int[] arrayInd = ind.toIntArray();
- // for(int i = 0; i < ind.length(); i++){
- // if(arrayInd[i] == value){
- // return i;
- // }
- // }
- // return -1;
- // }
- //
- //
- // public BinPackingOperator(Vector<Double> weights, double mutationProbability, int K) {
- // this.mutationProbability = mutationProbability;
- // this.geneChangeProbability = geneChangeProbability;
- // this.weights = weights;
- // this.K = K;
- // }
- //
- // @Override
- // public void operate(Population parents, Population offspring) {
- //
- // int size = parents.getPopulationSize();
- //
- // for (int i = 0; i < size; i++) {
- //
- // IntegerIndividual p1 = (IntegerIndividual) parents.get(i);
- // IntegerIndividual o1 = (IntegerIndividual) p1.clone();
- //
- // if (rng.nextDouble() < mutationProbability) {
- // int[] binWeights = getBinWeights(o1);
- //
- // double min = Integer.MAX_VALUE;
- // double max = Integer.MIN_VALUE;
- // int minPos = 0;
- // int maxPos = 0;
- // for (int j = 0; j < K; j++) {
- // if(binWeights[j] < binWeights[minPos]){
- // minPos = j;
- // }
- // if (binWeights[j] > binWeights[maxPos]) {
- // maxPos = j;
- // }
- // }
- // int difference = binWeights[maxPos] - binWeights[minPos];
- // int[] arrayInd = o1.toIntArray();
- // for(int j = 0; j < arrayInd.length; j++){
- // if(arrayInd[j] == maxPos){
- // if(Math.abs(difference - 2*weights.get(j)) < Math.abs(difference)){
- // o1.set(j,minPos);
- // difference -= 2*weights.get(j);
- // }
- // }
- // if(arrayInd[j] == minPos){
- // if(Math.abs(difference + 2*weights.get(j)) < Math.abs(difference)){
- // o1.set(j,maxPos);
- // difference += 2*weights.get(j);
- // }
- // }
- // }
- //
- // }
- //
- // offspring.add(o1);
- // }
- // }
- //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement