Comments (10)
Now I believe there is bug with getName()
, It happens when more than one candidate gets rank 1.
from condorcet.
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.
please give a sample code how to use advance method?
@julien-boudry
from condorcet.
Do you mean this?
Election->getWinner('advanced')
?
from condorcet.
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.
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.
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.
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.
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.
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)
- Improve performances of countValidVoteWithConstraint/sumValidVoteWithConstraint
- Console: Check false or null php.ini memory_limit on large election HOT 1
- PHP Fatal error HOT 7
- Largest remainder methods: Half votes rounded down, party receiving less seats HOT 5
- Unserialize crash with new && empty election object
- Implement VoteFaire Ranking (Popularity)?
- [Public API Reform] Pairwise key 'null' to 'tie'
- [Public API Reform] Split $election->getResult methods, for filtering by tags
- [Public API Reform] Is it useful to edit candidate name with history. Should be immutable?
- [Public API Reform] Do a really independant Pairwise object HOT 5
- [Command Line] Error importing CondorcetFormat: Argument #1 ($callBack) must be of type Closure HOT 6
- $election->getResult() results in error when there are 0 votes HOT 3
- Investigate about removeVote feature and Pairwise integrity
- Allow either commas or semicolons in Condorcet Election Format HOT 11
- Different results when running different numbers of methods HOT 4
- Problem in Schulze_Proportional_Prefilter in Experimental/Schulze-STV branch HOT 5
- [Public API Reform] Remove static setters setMaxParseIteration / setMaxVoteNumber HOT 1
- Tests: parallel mod (paratest, pest --parallel) fail erratically and inconsistently (and worse with JIT) HOT 3
- [Experimental Schulze-STV Branch] Wrong test results from various methods with Explicit Ranking HOT 14
- New Method: Smith Set HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from condorcet.