简体   繁体   English

轮循算法在php中等距离分配

[英]round-robin algorithm equal home away distribution in php

I have been looking for this solution and there are few docs but couldn't get them correctly. 我一直在寻找这种解决方案,并且文档很少,但是无法正确获取它们。 Please have a look. 请看一看。 I tried to implement the round robin algorithm to create a match schedule but stack on home away distribution. 我试图实现循环算法来创建比赛时间表,但在主场分发中进行堆叠。

$allTeam=[3,10,8,7];
$leng=sizeof($allTeam);
$lastHalf=$leng-1;
for ($t=0; $t <$leng-1 ; $t++) { 
        for ($i=0; $i < $leng/2; $i++) { 
            if($t % 2== 0){
                \Log::info('Home Team => '.$allTeam[$i].' vs '.  $allTeam[$lastHalf-$i].'<=Away Team');
            }else{
                \Log::info('Away Team =>'.$allTeam[$i].' vs '.  $allTeam[$lastHalf-$i].'<= Home Team');
            }

        }

       /*now rotate the array. For this first insert the last item into postion 1*/
        array_splice( $allTeam, 1, 0, $allTeam[$leng-1]);
        /*now pop up the last element*/
        array_pop($allTeam);
 }

this is the result 这是结果

Home Team => 3 vs 7<=Away Team  
Home Team => 10 vs 8<=Away Team  

Away Team =>3 vs 8<= Home Team  
Away Team =>7 vs 10<= Home Team  

Home Team => 3 vs 10<=Away Team  
Home Team => 8 vs 7<=Away Team  

You can see it's not equally distributed. 您会看到它分布不均。 How can I make sure that if one team plays as home team in first round then this team should play away in next round? 我如何确定如果一支球队在第一轮作为主队出战,那么该球队应该在下一轮出战? Similarly if away in first round then home in next round? 同样,如果在第一轮出局,然后在下一轮出局?

Thank you. 谢谢。

It's easier then what you're doing, if you just create every permutation, then each team will play home and away an equal amount of times between teams, so if there are 4 teams you would have 3 home 3 away, generating would be done like: 这比您正在做的事情容易,如果您只创建每个排列,那么每个团队将在主场比赛并且在各队之间的距离相等,因此,如果有4个团队,您将有3个主场3场比赛,那么生成喜欢:

<?php
$allTeam = [3, 10, 8, 7];

$result = [];
foreach ($allTeam as $home) {
    foreach ($allTeam as $away) {
        if ($home === $away) {
           continue;
        }
        $result[] = 'Home Team => '.$home.' vs '.$away.' <= Away Team';
    } 
}

print_r($result);

https://3v4l.org/EAl8G https://3v4l.org/EAl8G

Result: 结果:

Array
(
    [0] => Home Team => 3 vs 10 <= Away Team
    [1] => Home Team => 3 vs 8 <= Away Team
    [2] => Home Team => 3 vs 7 <= Away Team
    [3] => Home Team => 10 vs 3 <= Away Team
    [4] => Home Team => 10 vs 8 <= Away Team
    [5] => Home Team => 10 vs 7 <= Away Team
    [6] => Home Team => 8 vs 3 <= Away Team
    [7] => Home Team => 8 vs 10 <= Away Team
    [8] => Home Team => 8 vs 7 <= Away Team
    [9] => Home Team => 7 vs 3 <= Away Team
    [10] => Home Team => 7 vs 10 <= Away Team
    [11] => Home Team => 7 vs 8 <= Away Team
)

Then you would have another loop which sorts them in a way which makes it look like a home -> away -> home -> away scenario. 然后,您将有另一个循环,以某种方式对它们进行排序,使其看起来像一个主场->客场->主场->客场。

I solved it with some modification. 我通过一些修改解决了它。 Here is my final code. 这是我的最终代码。 It may help others 可能对别人有帮助

$allTeam=[1,2,3,4];
    /*now make the draw and insert it in the table*/
    $leng=sizeof($allTeam);
    $firstHalf=0;
    $lastHalf=$leng-1;

    $isHome=true;
    for ($t=0; $t <$leng-1 ; $t++) { 
        for ($i=0; $i < $leng/2; $i++) {
            if($i==0){
                if($isHome){
                    \Log::info('Home team=> '.$allTeam[$i].' vs Away team=> '.$allTeam[$lastHalf-$i]);
                    $isHome=false;
                }else{
                    \Log::info('Home team=> '.$allTeam[$lastHalf-$i].' vs Away team=> '.$allTeam[$i]);
                    $isHome=true;
                }
            }else{
                if($i%2==0){
                /*make first half home team*/
                \Log::info('Home team=> '.$allTeam[$i].' vs Away team=> '.$allTeam[$lastHalf-$i]);

                }else{
                    \Log::info('Home team=> '.$allTeam[$lastHalf-$i].' vs Away team=> '.$allTeam[$i]);  
                }
            }


        }
        /*now rotate the array. For this first insert the last item into postion 1*/
        array_splice( $allTeam, 1, 0, $allTeam[$leng-1]);
        /*now pop up the last element*/
        array_pop($allTeam);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM