oceanbigone/majorityjudgment

根据多数判断排序候选人

2.1.8 2020-01-26 10:30 UTC

README

简单的PHP多数判断类。更多详情请参考 维基百科

如何安装?

composer require oceanbigone/majorityjudgment

如何使用?

查看使用示例 demo/index.php。您可以在以下页面查看结果: 演示页面

require "../vendor/autoload.php";

//start Ballot
$ballot= new Ballot();

//create Mention
$excellent  = new Mention("Excellent");
$good       = new Mention("Good");
$prettyGood = new Mention("Pretty good");
[...]

//create Candidate
$candidate1 = new Candidate("Mrs ABCDE");
$candidate2 = new Candidate("Mr FGHIJ");

//add Mentions -- from the best to the worst (order is important) !!!!
$ballot->addMention($excellent);
$ballot->addMention($good);
$ballot->addMention($prettyGood);
[...]

//add some Candidats
$ballot->addCandidate($candidate1);
$ballot->addCandidate($candidate2);
[...]


//add votes (keep in mind that each participation need a vote for each candidate !)
$ballot->addVote(new Vote($candidate1,$excellent));
$ballot->addVote(new Vote($candidate2,$prettyGood));

$ballot->addVote(new Vote($candidate1,$good));
$ballot->addVote(new Vote($candidate2,$excellent));
[...]

//get an array of candidate sorted by Majority Jugement, if there is full ex-aequo (even after index added) then they are ordered by name.
$sortedCandidates=$ballot->proceedElection();
var_dump($sortedCandidates);

//details with MeritProfile object
foreach($sortedCandidates as $candidate){
    
    $meritProfil=new MeritProfile();
    
    //get merit profil as Array of Merit object (Merit is an object with two property : mention and percent of this mention) 
    $merits=$meritProfil->getAsMeritArray($candidate,$ballot->getVotes(),$ballot->getMentions());
    
    //display majority mention
    echo $meritProfil->processMajorityMention($candidate,$ballot->getVotes(),$ballot->getMentions()))->getLabel();
    
    //display percent of majority mention
    echo $meritProfil->processPercentOfMajorityMention($candidate,$ballot->getVotes(),$ballot->getMentions()));
        
}

//clear mentions
$ballot->clearMentions();

//clear candidates
$ballot->clearCandidates();

//clear Votes
$ballot->clearVotes();

版本

2.1.8

  • 修复bug:强制将排序键用作字符串

2.1.7

  • 修复完美平局(在排序键末尾添加任意循环索引)时的bug

2.1.6

  • 添加 demo/fixture02-bug.php 以测试bug

2.1.5

  • 修复完美平局时的bug

2.1.4

  • 更新此文件(移除beta警告)

2.1.3

  • 多数判断严格优于50%
  • 添加一个配置文件

2.1.2

  • 移除 var_dump

2.1.1

  • 使用完整键检查平局(而不仅是当前路径)

2.1.0

  • 新算法(不再有平局)

2.0.4

  • 修复:处理低于多数百分比的糟糕百分比时的bug

2.0.3

  • 从结果数组中删除键

2.0.2

  • 修复:格式化键

2.0.1

  • 在README中添加一些帮助信息

2.0.0

  • 返回对象而不是关联数组
    警告:此版本与旧版本不兼容

1.2.8

  • 许可证

1.2.7

  • 最终确定算法并添加说明

1.2.6

  • 修复结果错误

1.2.5

  • 修复生成排序键(在结果数组中)的精度舍入值错误

1.2.4

  • 修复结果错误

1.2.3

  • 平局结果中的符号错误

1.2.2

  • 平局结果中的符号错误

1.2.1

  • 更新README

1.2.0

  • 函数 getMeritProfile 现在是私有的
  • 清理代码中的注释
  • 添加一些文档

1.1.0

  • 函数 getMeritProfile 现在是公共的
  • 函数 getAsMeritArray 现在是 proceedElection

1.0.3

  • 修复 composer.json

1.0.2

  • 在README中添加安装信息

1.0.1

  • 对Packagist进行校正

1.0.0

  • 初始提交