Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.40 KB | None | 0 0
  1. function mainBin
  2.     bin = {};
  3.     items = [10 12 12 8  1 13 15 7 18 22 2 15 2 7 10 22];
  4.     heuristics = [2, 1,4];
  5.     hyperHeuristics = [12 2 0.8 ; 5 10 0.4 ; 17 4 0.3];
  6.     featuresV = [1,2,3];
  7.     maxCapacity = 25;
  8.  
  9.     while (~isempty(items))
  10.         idHeuristic = getHeuristicByEuclideanDistanceBin(items, hyperHeuristics, featuresV, maxCapacity);
  11.         [bin, items] = placeItemByHeuristic(heuristics(idHeuristic), bin, maxCapacity, items);
  12.     end
  13.     for i = 1: size(bin,2)
  14.         %disp(bin{i});
  15.     end
  16.     disp(getQBin(bin,maxCapacity));
  17.     %disp(size(bin,2));
  18.    
  19. end
  20.  
  21. function Q = getQBin(bin, maxCapacity)
  22.     Q = 0;
  23.     disp(size(bin,2));
  24.     for i = 1: size(bin,2)
  25.         Q = Q + (maxCapacity -sum(bin{i}));
  26.     end
  27.     Q = Q/(maxCapacity*size(bin,2));
  28. end
  29.  
  30. function value = getFeaturesBin(idFeature, items, maxCapacity)
  31.     switch idFeature
  32.         % Average of items
  33.         case  1
  34.             value = mean(items);
  35.         %standard dev
  36.         case 2
  37.             value = std(items);
  38.         %relation items greater than capacity divided by two
  39.         case 3
  40.             value = length(items(items > maxCapacity / 2))/length(items);
  41.     end
  42. end
  43.  
  44. function id = getHeuristicByEuclideanDistanceBin(items, hyperHeuristics, featuresV, maxCapacity)
  45.     [rows, ~] = size(hyperHeuristics);
  46.     aux = [];
  47.     for i = 1:size(featuresV,2)
  48.         aux = [aux getFeaturesBin(featuresV(i),items, maxCapacity)];
  49.     end
  50.     min = intmax;
  51.     id = 1;
  52.     for row = 1: rows
  53.         possibleMin = sqrt(sum((aux-hyperHeuristics(row,:)).^2));
  54.         if(possibleMin < min)
  55.             min = possibleMin;
  56.             id = row;
  57.         end
  58.     end
  59. end
  60. function [bin, items] = placeItemByHeuristic(id, bin, maxCapacity, items)
  61.     itFits = false;
  62.     switch id
  63.         %First fit
  64.         case 1
  65.             for i  = 1: size(bin,2)
  66.                 x = sum(bin{i});
  67.                 if maxCapacity - x >= items(1)
  68.                     itFits = true;
  69.                     bin{i} = [bin{i} items(1)];
  70.                     break;
  71.                 end
  72.             end
  73.         %Best Fit
  74.         case 2
  75.             index = -1;
  76.             best = intmax;
  77.             for i  = 1: size(bin,2)
  78.                 x = sum(bin{i});
  79.                 if maxCapacity - x >= items(1)
  80.                     if maxCapacity - x - items(1) < best
  81.                         best = maxCapacity - x - items(1);
  82.                         index = i;
  83.                     end
  84.                  end
  85.             end
  86.             if index ~= -1
  87.                 bin{index} = [bin{index} items(1)];
  88.                 itFits = true;
  89.             end
  90.            
  91.         %Worst Fit
  92.         case 3
  93.             index = -1;
  94.             worst = -1;
  95.             for i  = 1: size(bin,2)
  96.                 x = sum(bin{i});
  97.                 if maxCapacity - x >= items(1)
  98.                     if maxCapacity - x - items(1) > worst
  99.                         worst = maxCapacity - x - items(1);
  100.                         index = i;
  101.                     end
  102.                  end
  103.             end
  104.             if index ~= -1
  105.                 bin{index} = [bin{index} items(1)];
  106.                 itFits = true;
  107.             end
  108.         %Almost Worst Fit
  109.         case 4
  110.            index = -1;
  111.            worst = -1;
  112.            indexNTB = -1;
  113.            notThatBad = -1;
  114.            for i  = 1: size(bin,2)
  115.                x = sum(bin{i});
  116.                if maxCapacity - x >= items(1)
  117.                    if maxCapacity - x - items(1) > worst
  118.                        worst = maxCapacity - x - items(1);
  119.                        index = i;
  120.                    end
  121.                 end
  122.            end
  123.            for i  = 1: size(bin,2)
  124.                x = sum(bin{i});
  125.                if maxCapacity - x >= items(1)
  126.                    if maxCapacity - x - items(1) > notThatBad && i  ~= index
  127.                        notThatBad = maxCapacity - x - items(1);
  128.                        indexNTB = i;
  129.                    end
  130.                 end
  131.            end
  132.            if index ~= -1 && indexNTB ~= -1
  133.                bin{indexNTB} = [bin{indexNTB} items(1)];
  134.                itFits = true;
  135.            end  
  136.            if index ~= -1 && indexNTB == -1
  137.                bin{index} = [bin{index} items(1)];
  138.                itFits = true;
  139.            end  
  140.          
  141.     end
  142.     if ~itFits
  143.         bin{size(bin,2)+1} = [items(1)];
  144.     end
  145.     items(1) = [];
  146. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement