Giter Site home page Giter Site logo

Comments (10)

phplicengine avatar phplicengine commented on June 9, 2024

Now I believe there is bug with getName(), It happens when more than one candidate gets rank 1.

from condorcet.

julien-boudry avatar julien-boudry commented on June 9, 2024

Hi,
See the documentation for getWinner() here : https://github.com/julien-boudry/Condorcet/blob/master/Documentation/Election%20Class/public%20Election--getWinner.md

If you use an advanced method instead of Natural, you can get an array with multiples winners.

Here, Schulze's method certainly produces a tie for first place, which may be normal, depending on the input votes. getWinner therefore returns an array.

from condorcet.

phplicengine avatar phplicengine commented on June 9, 2024

please give a sample code how to use advance method?
@julien-boudry

from condorcet.

phplicengine avatar phplicengine commented on June 9, 2024

Do you mean this?

Election->getWinner('advanced')

?

from condorcet.

julien-boudry avatar julien-boudry commented on June 9, 2024

Election->getWinner() is always an advanced method. Default is 'Schulze Winning' method. Most of the advanced methods can produce a tie on rank, including top rank.

If you want the Natural Condorcet computation, the Marquis de Condorcet Winner. You must use Election->getCondorcetWinner() method. But be careful, the result can be NULL. The advanced methods almost always gives results, not the original Condorcet method.
https://github.com/julien-boudry/Condorcet/blob/master/Documentation/Election%20Class/public%20Election--getCondorcetWinner.md

from condorcet.

phplicengine avatar phplicengine commented on June 9, 2024

Here is my full source. If in form you choose the same number for more than one item, it gets error for getName() on getWinner(). What wrong I did as it is Schulze as you said.

use CondorcetPHP\Condorcet\Condorcet;
use CondorcetPHP\Condorcet\Election;
use CondorcetPHP\Condorcet\Candidate;
use CondorcetPHP\Condorcet\CondorcetUtil;
use CondorcetPHP\Condorcet\Vote;

if (isset($_POST) && $_POST != "") {
    
Condorcet::setDefaultMethod('Schulze');
$election = new Election ();

print("<pre>");
print_r($_POST['item']);

// To get Candidates by Category via Doctrine orm
$category = $em->getRepository('Entities\Category')->findOneBy(['id' => 1]);
$candidates = $category->getCandidates();

$total_items = 0;
foreach ($candidates as $candidate) {
         $candidate = $candidate->getCandidate();
         $election->addCandidate(new Candidate($candidate));
         ++$total_items;
}

$votes = array();
foreach ($_POST['item'] as $item => $ranking) {
    $vote_factor = 1 + $total_items - $ranking;
    if (isset($votes[$vote_factor])) {
       $votes[$vote_factor] = (array) $votes[$vote_factor];
       $votes[$vote_factor][] = $item;
    } else {
       $votes[$vote_factor] = $item;
    }
}    

$result = $election->addVote(new Vote($votes));  

print("<br />");
foreach ($election->getResult('Schulze') as $rank => $candidates) :
    echo 'Rank ' . $rank . ': ';
    echo implode(', ', $candidates);
    echo '<br />';
endforeach;

print_r($result->getSimpleRanking()); // To be saved in db.
echo 'Schulze winner is : ' . $election->getWinner('Schulze')->getName() . '<br />';
}

?>

<html>
    <body>
        <form method="post">
        Wingspan: <select name="item[Wingspan]" id="Wingspan">
                         <option value="1">1</option>
                         <option value="2">2</option>
                         <option value="3">3</option>
                         <option value="4">4</option>
                 </select><br />
        Scythe:  <select name="item[Scythe]" id="Scythe">
                         <option value="1">1</option>
                         <option value="2">2</option>
                         <option value="3">3</option>
                         <option value="4">4</option>
                 </select><br />
        Spirit Island: <select name="item[Spirit Island]" id="Spirit Island">
                         <option value="1">1</option>
                         <option value="2">2</option>
                         <option value="3">3</option>
                         <option value="4">4</option>
                  </select><br />
        Everdell: <select name="item[Everdell]" id="Everdell">
                         <option value="1">1</option>
                         <option value="2">2</option>
                         <option value="3">3</option>
                         <option value="4">4</option>
                  </select><br />
        <input type="submit" name="submit" value="submit">
        </form>
    </body>
</html>

from condorcet.

phplicengine avatar phplicengine commented on June 9, 2024

I think you meant:

foreach ($election->getWinner('Schulze') as $winner) {
    print($winner->getName()."<br />");
}

Is it correct? But it gives nothing if there is only one candidate. How do I know if there are only one or more than one winner(s) to fix this problem?

Please see my source, it seems it is working well, just for sure I want you to look at the source to see if everything is correct or any improvements you can suggest?

It seems

if (isset($_POST) && $_POST != "") {

is not working and for first run when there is nothing posted yet, I get these notices:

Notice:  Undefined index: item on line 19

Notice:  Undefined index: item on line 34

Warning:  Invalid argument supplied for foreach() on line 34.

Why I am getting these errors?
And is there any way to combine both foreach() as one and how to generate form items field from items fetched from db rather than hardcoding them?

I know these are not directly related to Condorcet, but I highly appreciate your help.

from condorcet.

julien-boudry avatar julien-boudry commented on June 9, 2024

Hi @phplicengine,

Two ways.

1.

$winners = $election->getWinner('Schulze');

if (is_array($winners) {
  foreach ($winners as $oneWinner) {
      echo($oneWinner->getName()."<br />");
  }
}
else {
    echo($winners->getName());
}

2.

// On getResult, the ranks are always arrays.
  foreach ($election->getResult('Schulze')[1] as $oneWinner) {
      echo($oneWinner->getName()."<br />");
  }

from condorcet.

julien-boudry avatar julien-boudry commented on June 9, 2024

About your problem with the $_POST in your code. I suppose that $_POST is always set (so your isset is useless) and is never an empty string. So, your condition is always true.

Look with empty() or count() or isset($_POST['item'])

from condorcet.

phplicengine avatar phplicengine commented on June 9, 2024

Here is my code I am using: #42 (comment)
I want to add a blank select option, that my users if they don't want to have some candidates in voting just use the blank select option and don't be forced to select a ranking number from the drop down menu. How to omit blank options completely from listing and voting? @julien-boudry

from condorcet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.