Search code examples
scilabvoting-system

Implementing Schulze voting method in SciLab


I have to implement the Schulze method in SciLab. Unfortunately, I'm completely new to this tool and neither am I good at such kind of job. Could anyone advise something as to where to look for some examples and tools to do it as fast and easy as possible? The program shouldn't be extremely flexible or qualitative, it would be fine if it just worked with some hard-coded input. As I understand, the Schulze method can be implemented using graph and I've found a toolbox for SciLab. Should I use it?

Update:

Here's what I managed to come up with. The code is a total mess and I admit it since I'm really bad at working with such kind of languages. I've tested it with the example from Wikipedia, seems to work.

The code is too long so here's a pastebin


Solution

  • You can find a pseudo implementation on Wikipedia:

    # Input: d[i,j], the number of voters who prefer candidate i to candidate j.
    # Output: p[i,j], the strength of the strongest path from candidate i to candidate j.
    
    for i from 1 to C
       for j from 1 to C
          if (i ≠ j) then
             if (d[i,j] > d[j,i]) then
                p[i,j] := d[i,j]
             else
                p[i,j] := 0
    
    for i from 1 to C
       for j from 1 to C
          if (i ≠ j) then
             for k from 1 to C
                if (i ≠ k and j ≠ k) then
                   p[j,k] := max ( p[j,k], min ( p[j,i], p[i,k] ) )
    

    Translating this to SciLab would require using functions, for-loops, if-else constructs, max, min.

    Below I plainly translated the pseudo code into Scilab code. I haven't tested it and you'll have to find out the arguments to call it with.

    function p = schulzeMethod(d, C)
    
        // Initialize a zero matrix p of the same size as d
        p = zeros(size(d))
    
        for i = 1:C
            for j = 1:C
                if i ~= j then
                    if d(i,j) > d(j,i) then
                        p(i,j) = d(i,j)
                    else
                        p(i,j) = 0
                    end
                end
            end
        end
    
        for i = 1:C
            for j = 1:C
                if i ~= j then
                    for k = 1:C
                        if (i ~= k) & ( j ~= k) then
                            p(j,k) = max(p(j,k), min(p(j,i), p(i,k)))
                        end
                    end
                 end
             end
         end
    
    endfunction
    
    // Create some random values
    C = 10
    d = rand(C, C)
    
    // Call the function above
    p = schulzeMethod(d, C)
    
    disp(p)    
    

    Good luck, hope it helps! Please give some feedback if it worked to help others.