Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function mainBin
- bin = {};
- items = [10 12 12 8 1 13 15 7 18 22 2 15 2 7 10 22];
- heuristics = [2, 1,4];
- hyperHeuristics = [12 2 0.8 ; 5 10 0.4 ; 17 4 0.3];
- featuresV = [1,2,3];
- maxCapacity = 25;
- while (~isempty(items))
- idHeuristic = getHeuristicByEuclideanDistanceBin(items, hyperHeuristics, featuresV, maxCapacity);
- [bin, items] = placeItemByHeuristic(heuristics(idHeuristic), bin, maxCapacity, items);
- end
- for i = 1: size(bin,2)
- %disp(bin{i});
- end
- disp(getQBin(bin,maxCapacity));
- %disp(size(bin,2));
- end
- function Q = getQBin(bin, maxCapacity)
- Q = 0;
- disp(size(bin,2));
- for i = 1: size(bin,2)
- Q = Q + (maxCapacity -sum(bin{i}));
- end
- Q = Q/(maxCapacity*size(bin,2));
- end
- function value = getFeaturesBin(idFeature, items, maxCapacity)
- switch idFeature
- % Average of items
- case 1
- value = mean(items);
- %standard dev
- case 2
- value = std(items);
- %relation items greater than capacity divided by two
- case 3
- value = length(items(items > maxCapacity / 2))/length(items);
- end
- end
- function id = getHeuristicByEuclideanDistanceBin(items, hyperHeuristics, featuresV, maxCapacity)
- [rows, ~] = size(hyperHeuristics);
- aux = [];
- for i = 1:size(featuresV,2)
- aux = [aux getFeaturesBin(featuresV(i),items, maxCapacity)];
- end
- min = intmax;
- id = 1;
- for row = 1: rows
- possibleMin = sqrt(sum((aux-hyperHeuristics(row,:)).^2));
- if(possibleMin < min)
- min = possibleMin;
- id = row;
- end
- end
- end
- function [bin, items] = placeItemByHeuristic(id, bin, maxCapacity, items)
- itFits = false;
- switch id
- %First fit
- case 1
- for i = 1: size(bin,2)
- x = sum(bin{i});
- if maxCapacity - x >= items(1)
- itFits = true;
- bin{i} = [bin{i} items(1)];
- break;
- end
- end
- %Best Fit
- case 2
- index = -1;
- best = intmax;
- for i = 1: size(bin,2)
- x = sum(bin{i});
- if maxCapacity - x >= items(1)
- if maxCapacity - x - items(1) < best
- best = maxCapacity - x - items(1);
- index = i;
- end
- end
- end
- if index ~= -1
- bin{index} = [bin{index} items(1)];
- itFits = true;
- end
- %Worst Fit
- case 3
- index = -1;
- worst = -1;
- for i = 1: size(bin,2)
- x = sum(bin{i});
- if maxCapacity - x >= items(1)
- if maxCapacity - x - items(1) > worst
- worst = maxCapacity - x - items(1);
- index = i;
- end
- end
- end
- if index ~= -1
- bin{index} = [bin{index} items(1)];
- itFits = true;
- end
- %Almost Worst Fit
- case 4
- index = -1;
- worst = -1;
- indexNTB = -1;
- notThatBad = -1;
- for i = 1: size(bin,2)
- x = sum(bin{i});
- if maxCapacity - x >= items(1)
- if maxCapacity - x - items(1) > worst
- worst = maxCapacity - x - items(1);
- index = i;
- end
- end
- end
- for i = 1: size(bin,2)
- x = sum(bin{i});
- if maxCapacity - x >= items(1)
- if maxCapacity - x - items(1) > notThatBad && i ~= index
- notThatBad = maxCapacity - x - items(1);
- indexNTB = i;
- end
- end
- end
- if index ~= -1 && indexNTB ~= -1
- bin{indexNTB} = [bin{indexNTB} items(1)];
- itFits = true;
- end
- if index ~= -1 && indexNTB == -1
- bin{index} = [bin{index} items(1)];
- itFits = true;
- end
- end
- if ~itFits
- bin{size(bin,2)+1} = [items(1)];
- end
- items(1) = [];
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement