Advertisement
Guest User

Schulze method

a guest
May 24th, 2015
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 2.72 KB | None | 0 0
  1. clear
  2. function p = schulzeMethod(d, C)
  3.     p = zeros(size(d))
  4.     for i = 1:C
  5.         for j = 1:C
  6.             if i ~= j then
  7.                 if d(i,j) > d(j,i) then
  8.                     p(i,j) = d(i,j)
  9.                 else
  10.                     p(i,j) = 0
  11.                 end
  12.             end
  13.         end
  14.     end
  15.  
  16.     for i = 1:C
  17.         for j = 1:C
  18.             if i ~= j then
  19.                 for k = 1:C
  20.                     if (i ~= k) & ( j ~= k) then
  21.                         p(j,k) = max(p(j,k), min(p(j,i), p(i,k)))
  22.                     end
  23.                 end
  24.              end
  25.          end
  26.      end
  27. endfunction
  28.  
  29. function res = isWinner(paths, candidate, numberOfCandidates)
  30.     res = 1
  31.     for j = 1:numberOfCandidates
  32.         if (j ~= candidate) then
  33.             if (paths(candidate, j) < paths(j, candidate)) then
  34.                 res = 0
  35.             end
  36.         end            
  37.     end
  38.     return res
  39. endfunction
  40.  
  41. function preference = countPreference(table, first, second)
  42.     preference = 0
  43.     for j = 1:8
  44.        
  45.         firstIndex = find( table(j).options == first )
  46.         secondIndex = find( table(j).options == second )
  47.         if firstIndex < secondIndex then
  48.             preference = preference + table(j).voters
  49.         end
  50.        
  51.     end
  52.     return preference
  53. endfunction
  54.  
  55. SizeOfOptions = 5
  56. preferencesTableColumn1 = struct('options', ['a', 'c', 'b', 'e', 'd'], 'voters', 5)
  57. preferencesTableColumn2 = struct('options', ['a', 'd', 'e', 'c', 'b'], 'voters', 5)
  58. preferencesTableColumn3 = struct('options', ['b', 'e', 'd', 'a', 'c'], 'voters', 8)
  59. preferencesTableColumn4 = struct('options', ['c', 'a', 'b', 'e', 'd'], 'voters', 3)
  60. preferencesTableColumn5 = struct('options', ['c', 'a', 'e', 'b', 'd'], 'voters', 7)
  61. preferencesTableColumn6 = struct('options', ['c', 'b', 'a', 'd', 'e'], 'voters', 2)
  62. preferencesTableColumn7 = struct('options', ['d', 'c', 'e', 'b', 'a'], 'voters', 7)
  63. preferencesTableColumn8 = struct('options', ['e', 'b', 'a', 'd', 'c'], 'voters', 8)
  64. preferencesTable = [preferencesTableColumn1, preferencesTableColumn2, preferencesTableColumn3, preferencesTableColumn4, preferencesTableColumn5, preferencesTableColumn6, preferencesTableColumn7, preferencesTableColumn8]
  65.  
  66. numberOfCandidates = SizeOfOptions
  67. candidates = ['a', 'b', 'c', 'd', 'e']
  68.  
  69. for i = 1:numberOfCandidates
  70.     for j = 1:numberOfCandidates
  71.             prefTable(i, j) = countPreference(preferencesTable, candidates(i), candidates(j))
  72.     end
  73. end
  74.  
  75. printf('PAIRS BUILT FROM PREFERENCES TABLE: ')
  76. disp(prefTable)
  77.  
  78. paths = schulzeMethod(prefTable, numberOfCandidates)
  79.  
  80. for i = 1:numberOfCandidates
  81.     c(i) = isWinner(paths, i, numberOfCandidates)
  82. end
  83. printf('WINNER IS: ')
  84. disp(candidates(find(c == 1)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement