Skip to content

Commit

Permalink
Ranked Pairs: New RP_Variant Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-boudry committed May 29, 2024
1 parent 5d15bf2 commit bc3841f
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 160 deletions.
22 changes: 22 additions & 0 deletions src/Algo/Methods/RankedPairs/RP_VARIANT.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/*
Part of RANKED PAIRS method Module - From the original Condorcet PHP
Condorcet PHP - Election manager and results calculator.
Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems.
By Julien Boudry and contributors - MIT LICENSE (Please read LICENSE.txt)
https://github.com/julien-boudry/Condorcet
*/

declare(strict_types=1);

namespace CondorcetPHP\Condorcet\Algo\Methods\RankedPairs;

enum RP_VARIANT
{
case UNDEFINED;
case WINNING;
case MARGIN;
case MINORITY;
}
2 changes: 1 addition & 1 deletion src/Algo/Methods/RankedPairs/RankedPairsMargin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ class RankedPairsMargin extends RankedPairs_Core
// Method Name
public const array METHOD_NAME = ['Ranked Pairs Margin', 'RankedPairsMargin', 'Tideman Margin', 'RP Margin', 'Ranked Pairs', 'RankedPairs', 'Tideman method'];

protected const string RP_VARIANT_1 = 'margin';
protected const RP_VARIANT VARIANT = RP_VARIANT::MARGIN;
}
2 changes: 1 addition & 1 deletion src/Algo/Methods/RankedPairs/RankedPairsWinning.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ class RankedPairsWinning extends RankedPairs_Core
// Method Name
public const array METHOD_NAME = ['Ranked Pairs Winning', 'RankedPairsWinning', 'Tideman Winning', 'RP Winning'];

protected const string RP_VARIANT_1 = 'win';
protected const RP_VARIANT VARIANT = RP_VARIANT::WINNING;
}
16 changes: 8 additions & 8 deletions src/Algo/Methods/RankedPairs/RankedPairs_Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// Ranked Pairs is a Condorcet Algorithm | http://en.wikipedia.org/wiki/Ranked_Pairs
abstract class RankedPairs_Core extends Method implements MethodInterface
{
protected const string RP_VARIANT_1 = 'abstractVariant';
protected const RP_VARIANT VARIANT = RP_VARIANT::UNDEFINED;

// Limits
public static ?int $MaxCandidates = 60;
Expand Down Expand Up @@ -225,22 +225,22 @@ protected function pairwiseSort(): array
// Defeat
$pairs[$i]['to'] = $challenger_key;

$pairs[$i]['win'] = $challenger_value;
$pairs[$i]['minority'] = $candidate_value['lose'][$challenger_key];
$pairs[$i]['margin'] = $candidate_value['win'][$challenger_key] - $candidate_value['lose'][$challenger_key];
$pairs[$i][RP_VARIANT::WINNING->name] = $challenger_value;
$pairs[$i][RP_VARIANT::MINORITY->name] = $candidate_value['lose'][$challenger_key];
$pairs[$i][RP_VARIANT::MARGIN->name] = $candidate_value['win'][$challenger_key] - $candidate_value['lose'][$challenger_key];

$i++;
}
}
}

usort($pairs, static function (array $a, array $b): int {
if ($a[static::RP_VARIANT_1] < $b[static::RP_VARIANT_1]) {
if ($a[static::VARIANT->name] < $b[static::VARIANT->name]) {
return 1;
} elseif ($a[static::RP_VARIANT_1] > $b[static::RP_VARIANT_1]) {
} elseif ($a[static::VARIANT->name] > $b[static::VARIANT->name]) {
return -1;
} else { // Equal
return $a['minority'] <=> $b['minority'];
return $a[RP_VARIANT::MINORITY->name] <=> $b[RP_VARIANT::MINORITY->name];
}
});

Expand All @@ -251,7 +251,7 @@ protected function pairwiseSort(): array
if ($f === true) {
$newArcs[$i][] = $pairs[$pairsKey];
$f = false;
} elseif ($pairs[$pairsKey][static::RP_VARIANT_1] === $pairs[$pairsKey - 1][static::RP_VARIANT_1] && $pairs[$pairsKey]['minority'] === $pairs[$pairsKey - 1]['minority']) {
} elseif ($pairs[$pairsKey][static::VARIANT->name] === $pairs[$pairsKey - 1][static::VARIANT->name] && $pairs[$pairsKey][RP_VARIANT::MINORITY->name] === $pairs[$pairsKey - 1][RP_VARIANT::MINORITY->name]) {
$newArcs[$i][] = $pairs[$pairsKey];
} else {
$newArcs[++$i][] = $pairs[$pairsKey];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,90 +4,90 @@
{
"from": "B",
"to": "D",
"win": 33,
"minority": 12,
"margin": 21
"WINNING": 33,
"MINORITY": 12,
"MARGIN": 21
}
],
[
{
"from": "E",
"to": "D",
"win": 31,
"minority": 14,
"margin": 17
"WINNING": 31,
"MINORITY": 14,
"MARGIN": 17
}
],
[
{
"from": "A",
"to": "D",
"win": 30,
"minority": 15,
"margin": 15
"WINNING": 30,
"MINORITY": 15,
"MARGIN": 15
}
],
[
{
"from": "C",
"to": "B",
"win": 29,
"minority": 16,
"margin": 13
"WINNING": 29,
"MINORITY": 16,
"MARGIN": 13
}
],
[
{
"from": "D",
"to": "C",
"win": 28,
"minority": 17,
"margin": 11
"WINNING": 28,
"MINORITY": 17,
"MARGIN": 11
}
],
[
{
"from": "E",
"to": "B",
"win": 27,
"minority": 18,
"margin": 9
"WINNING": 27,
"MINORITY": 18,
"MARGIN": 9
}
],
[
{
"from": "A",
"to": "C",
"win": 26,
"minority": 19,
"margin": 7
"WINNING": 26,
"MINORITY": 19,
"MARGIN": 7
}
],
[
{
"from": "B",
"to": "A",
"win": 25,
"minority": 20,
"margin": 5
"WINNING": 25,
"MINORITY": 20,
"MARGIN": 5
}
],
[
{
"from": "C",
"to": "E",
"win": 24,
"minority": 21,
"margin": 3
"WINNING": 24,
"MINORITY": 21,
"MARGIN": 3
}
],
[
{
"from": "E",
"to": "A",
"win": 23,
"minority": 22,
"margin": 1
"WINNING": 23,
"MINORITY": 22,
"MARGIN": 1
}
]
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,90 +4,90 @@
{
"from": "B",
"to": "D",
"win": 33,
"minority": 12,
"margin": 21
"WINNING": 33,
"MINORITY": 12,
"MARGIN": 21
}
],
[
{
"from": "E",
"to": "D",
"win": 31,
"minority": 14,
"margin": 17
"WINNING": 31,
"MINORITY": 14,
"MARGIN": 17
}
],
[
{
"from": "A",
"to": "D",
"win": 30,
"minority": 15,
"margin": 15
"WINNING": 30,
"MINORITY": 15,
"MARGIN": 15
}
],
[
{
"from": "C",
"to": "B",
"win": 29,
"minority": 16,
"margin": 13
"WINNING": 29,
"MINORITY": 16,
"MARGIN": 13
}
],
[
{
"from": "D",
"to": "C",
"win": 28,
"minority": 17,
"margin": 11
"WINNING": 28,
"MINORITY": 17,
"MARGIN": 11
}
],
[
{
"from": "E",
"to": "B",
"win": 27,
"minority": 18,
"margin": 9
"WINNING": 27,
"MINORITY": 18,
"MARGIN": 9
}
],
[
{
"from": "A",
"to": "C",
"win": 26,
"minority": 19,
"margin": 7
"WINNING": 26,
"MINORITY": 19,
"MARGIN": 7
}
],
[
{
"from": "B",
"to": "A",
"win": 25,
"minority": 20,
"margin": 5
"WINNING": 25,
"MINORITY": 20,
"MARGIN": 5
}
],
[
{
"from": "C",
"to": "E",
"win": 24,
"minority": 21,
"margin": 3
"WINNING": 24,
"MINORITY": 21,
"MARGIN": 3
}
],
[
{
"from": "E",
"to": "A",
"win": 23,
"minority": 22,
"margin": 1
"WINNING": 23,
"MINORITY": 22,
"MARGIN": 1
}
]
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,48 @@
{
"from": "Chattanooga",
"to": "Knoxville",
"win": 83,
"minority": 17,
"margin": 66
"WINNING": 83,
"MINORITY": 17,
"MARGIN": 66
}
],
[
{
"from": "Nashville",
"to": "Knoxville",
"win": 68,
"minority": 32,
"margin": 36
"WINNING": 68,
"MINORITY": 32,
"MARGIN": 36
},
{
"from": "Nashville",
"to": "Chattanooga",
"win": 68,
"minority": 32,
"margin": 36
"WINNING": 68,
"MINORITY": 32,
"MARGIN": 36
}
],
[
{
"from": "Nashville",
"to": "Memphis",
"win": 58,
"minority": 42,
"margin": 16
"WINNING": 58,
"MINORITY": 42,
"MARGIN": 16
},
{
"from": "Knoxville",
"to": "Memphis",
"win": 58,
"minority": 42,
"margin": 16
"WINNING": 58,
"MINORITY": 42,
"MARGIN": 16
},
{
"from": "Chattanooga",
"to": "Memphis",
"win": 58,
"minority": 42,
"margin": 16
"WINNING": 58,
"MINORITY": 42,
"MARGIN": 16
}
]
],
Expand Down
Loading

0 comments on commit bc3841f

Please sign in to comment.