After getting negative feedback from asking this question in a new question... here is my revised question. Yes it is the same project I am working on, but I was unclear that I needed to basically have a Round Robin type of scheduler.
在一个新问题中提出这个问题后得到负面反馈......这是我修改过的问题。是的,这是我正在进行的同一个项目,但我不清楚我需要基本上有一个Round Robin类型的调度程序。
I'm working on a Round Robin Style Hockey League Scheduler, and need some help.
我正在研究Round Robin Style Hockey League Scheduler,需要一些帮助。
The overall goal is for the end admin user to be able to punch in 3 variables and have it perform a round Robin style schedule until the WEEKS counter has been hit. Below is an example of the amount of teams and the amount of weeks games are played.
总体目标是让最终管理员用户能够打入3个变量并让它执行循环罗宾风格的计划直到WEEKS计数器被击中。以下是球队数量和比赛周数的示例。
$Teams = array('team1','team2','team3','team4','team5','team6','team7','team8');
$Weeks = 16;
The goal is to have it loop 16 times, making 4 games a week, having each team playing 1 time a week. The round robin algorithm should have teams playing different teams each week until all possibles combinations have been made, but not exceeding 16 weeks. In the event that we only have 4 teams or less teams than possible combinations, we would need to have the round robin start over again until the weeks number was hit.
目标是让它循环16次,每周制作4个游戏,让每个团队每周玩1次。循环算法应该让团队每周都有不同的团队,直到所有可能的组合完成,但不超过16周。如果我们只有4支队伍或更少的队伍而不是可能的组合,我们需要重新开始循环,直到周数被击中。
EDIT:
I am about 90% into what I needed this script to do... but I am stuck on one thing. I need help with merging a multi-dimensional array.
我大约90%是我需要这个脚本做的...但我坚持一件事。我需要帮助合并多维数组。
First are the Tiers. Next are the Weeks (all are week 1). Then are the Games for the team match up.
首先是Tiers。接下来是周(都是第1周)。然后是团队的比赛匹配。
Array
(
[1] => Array
(
[1] => Array
(
[1] => Array
(
[home] => Whalers
[visitor] => Lumberjacks
)
[2] => Array
(
[home] => Team America
[visitor] => Wolfpack
)
)
)
[2] => Array
(
[1] => Array
(
[1] => Array
(
[home] => Warriors
[visitor] => Litchfield Builders
)
[2] => Array
(
[home] => Icemen
[visitor] => Nighthawks
)
)
)
[3] => Array
(
[1] => Array
(
[1] => Array
(
[home] => The Freeze
[visitor] => Devils Rejects
)
[2] => Array
(
[home] => Cobras
[visitor] => New Haven Raiders
)
[3] => Array
(
[home] => Crusaders
[visitor] => Whalers
)
[4] => Array
(
[home] => Blizzard
[visitor] => CT Redlines
)
)
)
)
I want the end result to drop the tier and merge all same weeks games together to look like the following:
我希望最终结果放弃层并将所有相同周的游戏合并在一起,如下所示:
Array
(
[1] => Array
(
[1] => Array
(
[home] => Whalers
[visitor] => Lumberjacks
)
[2] => Array
(
[home] => Team America
[visitor] => Wolfpack
)
[3] => Array
(
[home] => Warriors
[visitor] => Litchfield Builders
)
[4] => Array
(
[home] => Icemen
[visitor] => Nighthawks
)
[5] => Array
(
[home] => The Freeze
[visitor] => Devils Rejects
)
[6] => Array
(
[home] => Cobras
[visitor] => New Haven Raiders
)
[6] => Array
(
[home] => Crusaders
[visitor] => Whalers
)
[8] => Array
(
[home] => Blizzard
[visitor] => CT Redlines
)
)
)
4 个解决方案
#1
1
Maybe something like this?
也许是这样的?
<?php
$teams = array(
'Team 1',
'Team 2',
'Team 3',
'Team 4',
'Team 5',
'Team 6',
'Team 7',
'Team 8'
);
function getMatches($teams) {
shuffle($teams);
return call_user_func_array('array_combine', array_chunk($teams, sizeof($teams) / 2));
}
for ($i = 0; $i < 14; $i += 1) {
print_r(getMatches($teams));
}
I didn't really get how you define the schedule, so if you can explain this a bit, I'll try to help.
我没有真正了解你如何定义时间表,所以如果你能解释一下,我会尽力帮助。
#2
1
Pop one off, randomize, pop another. There's your game. If one is left over, some random team has to be a workhorse and play two games this week:
弹出一个,随机,弹出另一个。这是你的游戏。如果剩下一个,一些随机团队必须成为一个主力,并在本周玩两场比赛:
for ($week=1; $i<=$totalWeeksPlayed; $i++)
{
$games = 0;
$temp = $teams;
while (count($temp) > 1)
{
$team = array_shift($temp);
shuffle($temp);
$opponent = array_shift($temp);
$game[$week][$games] = $team . ' vs' . $opponent;
$games++;
}
if (count($temp) == 1)
{
$workhorses = $teams;
unset($workhorses[array_search($temp[0], $teams));
shuffle($workhorses);
$team = $temp[0];
$opponent = array_shift($workhorses);
$game[$week][$games] = $team . ' vs' . $opponent;
$games++;
}
}
#3
1
Question below copied from above.
从上面复制下面的问题。
Correct me if I get this wrong, but if all teams have to play on the same regular basis, is it even possible to have all teams play the same amount of matches, if there is an odd number of teams? – Yoshi May 3 '11 at 15:05
如果我弄错了,请纠正我,但是如果所有球队都必须定期进行比赛,那么如果队员数量奇数,是否可以让所有球队都进行相同数量的比赛? - Yoshi 2011年5月3日15:05
Michelle,
The number of teams you are trying to pair-up (in this case 8 teams for 16 weeks) can be a daunting task, and is just the beginning of the "scheduling process". Once the correct, balanced team pairings have been determined, it's just the beginning of putting a schedule together for distribution. Next, a list of the 4 weekly time slots includes the; day of the week, start time and location name for each time slot for the whole 16 week season. Comment: What would be most helpful for you is to get an 8 team scheduling matrix that has balanced opponent, and home & away status. It makes a big difference in the quality of a schedule. It's important to evenly distribute early and late time slots, equal home & away status, and equal team distribution with opponents. Most of the balance is accomplished by using a balanced team pair matrix.
您尝试配对的团队数量(在这种情况下为8个团队,为期16周)可能是一项艰巨的任务,而且只是“调度过程”的开始。一旦确定了正确,平衡的团队配对,这只是将时间表放在一起进行分发的开始。接下来,每周4个时段的列表包括:星期几,整个16周季节的每个时段的开始时间和地点名称。评论:对你最有帮助的是获得一个8队的调度矩阵,它具有平衡的对手,以及主客场状态。它对时间表的质量产生很大影响。重要的是均匀分配早期和晚期时段,平等的主场和客场状态,以及与对手平等的球队分配。大部分余额是通过使用平衡的团队对矩阵来完成的。
After 35 years of teaching, coaching and scheduling sports in the Boston area, I can offer the following information. Creating league or game schedules for sports organizations seems to be a never ending task shared by many and is repeated over and over as the ages of the participants grow and those running the leagues change, the learning curve associated with creating schedules is significant for those who take over.
在波士顿地区经过35年的教学,辅导和日程安排后,我可以提供以下信息。为体育组织创建联赛或比赛时间表似乎是许多人共享的永无止境的任务,随着参与者年龄的增长和联赛的变化而反复重复,与创建时间表相关的学习曲线对于那些接管。
With that said, I am amazed at how many highly educated mathematical wizards are involved with trying to come up with the perfect solution (algorithm) that will solve one's scheduling problem. Myself and a friend (who is a mathematical/programmer genius) over a period of 3 years created software that perfectly balances all the important components when creating schedules for 4 to 22 teams. What we learned is that there is no algorithm that can handle all the possible variables that are added to the normal variables to create balanced schedules. What I am saying is there are just as many "what ifs" as there are mathematical permutations and combinations that deal with just creating a team matrix listing opponents and home & visitor status of games.
话虽如此,我很惊讶有多少受过高等教育的数学向导试图提出解决一个人调度问题的完美解决方案(算法)。我和我的朋友(他是一名数学/程序员天才)在3年的时间里创建了软件,可以在为4到22个团队创建时间表时完美地平衡所有重要组件。我们学到的是,没有算法可以处理添加到常规变量的所有可能变量,以创建平衡的计划。我所说的是有多少“假设”,因为有数学排列和组合只涉及创建一个团队矩阵列出对手和游戏的家庭和访客状态。
For example: Let's create a perfectly balanced schedule for an 9 team division playing 4 games a week. After nine weeks all teams have played played 8 games, all have had 1 bye, all have played two times in each of the 4 time slots, and all have been scheduled as the home team 4 times and the visitor team 4 times.
例如:让我们为每周玩4场比赛的9支球队创建一个完美平衡的赛程。九个星期后,所有队伍都打了8场比赛,所有队员都有1场比赛,所有队员都在4个时段中都打过两次,所有队伍都被安排了4次主队和4次访客队。
What more could anybody want? Well now comes the fun. Because the 4 time slots you chose has 2 games each Saturday, and 2 games each Sunday, the first problem pops up (after the schedules are created, published and passed out, when 2 coaches from 2 different teams call and say they work Saturdays, can you move our games to Sundays? Sure, I can do that. I'll just make the changes, re-publish and re-distribute the schedules.
还有什么人想要的吗?好了,现在很有趣。因为你选择的4个时段每个星期六有2个游戏,每个星期天有2个游戏,所以弹出第一个问题(在制定,发布和传递时间表之后,当来自2个不同团队的2个教练打电话并说他们星期六工作时,你可以将我们的游戏转移到星期日吗?当然,我可以做到这一点。我只会进行更改,重新发布和重新分发时间表。
After the new schedules are distributed, several days later, a different coach calls up and says, "Hey, you moved some of my games to Saturday, I work Saturdays.., move them back". The phone rings again. This time a it's a coach from another team and says they are in a tournament the 5th week of the schedule and can't play that week. Finally the last call comes in from yet another coach. He says the parents of one of his players teaches CCD classes Sunday afternoons, and half of his team is in the CCD class and wants to move all our Sunday games to Saturday. Enough!
在新的时间表发布后,几天后,一位不同的教练打来电话说:“嘿,你把我的一些游戏搬到星期六,我星期六工作......,把它们搬回来”。电话再次响起。这次是来自另一支球队的教练,并表示他们在比赛的第5周参加比赛,并且不能在那一周比赛。最后,最后一个电话来自另一位教练。他说,他的一名球员的父母在周日下午教授CCD课程,他的一半球队参加了CCD课程,并希望将我们所有的周日比赛都移到周六。足够!
My point is no matter what you do or how perfect a schedule is, the best solution is to find out as many of the player/coach team limitations or restrictions before you assign the playing days and time slots to any schedule. These unpredictable variables makes a mess out of a perfect schedule. People do get angry and complain when undesirable schedules are distributed. When a schedule is bad enough, some parents won't sign their youngster to play the following year. This happens when you have a young family with two or three small children, and Dad's work limits his ability to be there. When there is an early morning game, I think you can see the difficulty for Mom's when it all falls on her shoulders.
我的观点是,无论你做什么或时间表有多完美,最好的解决方案是在将比赛时间和时间段分配给任何赛程之前找出尽可能多的球员/教练团队限制或限制。这些不可预测的变量使得完美的时间表变得混乱。当分发不受欢迎的时间表时,人们会生气并抱怨。如果时间表足够糟糕,一些家长将不会在第二年签下他们的小伙伴。当你有一个有两三个小孩的年轻家庭时就会发生这种情况,爸爸的工作限制了他在那里的能力。当有一个清晨游戏时,我认为你可以看到妈妈的困难,当它全部落在她的肩膀上。
For those that are new to scheduling, stay with it. It gets better over time after you gain a little experience dealing with the problems. If you purchase a scheduling software program to calculate team pairs, be careful. Insist on seeing a full single round robin of the schedule they create. Check for the things described above (about balance and distribution).
对于那些不熟悉日程安排的人,请坚持下去。在获得处理问题的一点经验之后,它会随着时间的推移变得更好。如果您购买计划软件程序来计算团队对,请小心。坚持看到他们创建的计划的完整单循环。检查上述内容(关于平衡和分配)。
Bob R
#4
0
Given a table of teams, say team ( teamname );
给出一个团队表,比如团队(teamname);
And a table of fixtures
还有一个固定装置表
fixture ( date; );
夹具(日期;);
With the relationship decomposed by 'playing'
通过'播放'分解关系
playing ( fixture_date. teamname );
玩(fixture_date。队名);
Then it's would simply be a matter of iterating through each date, then team and selecting a team at random who does not already have a fixture for that date, and who have not played the selected team (or not played the selected team recently).
然后,只需要迭代每个日期,然后团队并随机选择一个团队,该团队尚未拥有该日期,并且没有参与所选团队(或者最近未参加选定的团队)。
Although a simpler solution would be to have team[n] (where n is 0....number of teams -1) play team[(n+(number of teams)) % X] for varying values of X.
虽然更简单的解决方案是让团队[n](其中n为0 ......团队数量-1)为不同的X值发挥团队[(n +(团队数量))%X]。
#1
1
Maybe something like this?
也许是这样的?
<?php
$teams = array(
'Team 1',
'Team 2',
'Team 3',
'Team 4',
'Team 5',
'Team 6',
'Team 7',
'Team 8'
);
function getMatches($teams) {
shuffle($teams);
return call_user_func_array('array_combine', array_chunk($teams, sizeof($teams) / 2));
}
for ($i = 0; $i < 14; $i += 1) {
print_r(getMatches($teams));
}
I didn't really get how you define the schedule, so if you can explain this a bit, I'll try to help.
我没有真正了解你如何定义时间表,所以如果你能解释一下,我会尽力帮助。
#2
1
Pop one off, randomize, pop another. There's your game. If one is left over, some random team has to be a workhorse and play two games this week:
弹出一个,随机,弹出另一个。这是你的游戏。如果剩下一个,一些随机团队必须成为一个主力,并在本周玩两场比赛:
for ($week=1; $i<=$totalWeeksPlayed; $i++)
{
$games = 0;
$temp = $teams;
while (count($temp) > 1)
{
$team = array_shift($temp);
shuffle($temp);
$opponent = array_shift($temp);
$game[$week][$games] = $team . ' vs' . $opponent;
$games++;
}
if (count($temp) == 1)
{
$workhorses = $teams;
unset($workhorses[array_search($temp[0], $teams));
shuffle($workhorses);
$team = $temp[0];
$opponent = array_shift($workhorses);
$game[$week][$games] = $team . ' vs' . $opponent;
$games++;
}
}
#3
1
Question below copied from above.
从上面复制下面的问题。
Correct me if I get this wrong, but if all teams have to play on the same regular basis, is it even possible to have all teams play the same amount of matches, if there is an odd number of teams? – Yoshi May 3 '11 at 15:05
如果我弄错了,请纠正我,但是如果所有球队都必须定期进行比赛,那么如果队员数量奇数,是否可以让所有球队都进行相同数量的比赛? - Yoshi 2011年5月3日15:05
Michelle,
The number of teams you are trying to pair-up (in this case 8 teams for 16 weeks) can be a daunting task, and is just the beginning of the "scheduling process". Once the correct, balanced team pairings have been determined, it's just the beginning of putting a schedule together for distribution. Next, a list of the 4 weekly time slots includes the; day of the week, start time and location name for each time slot for the whole 16 week season. Comment: What would be most helpful for you is to get an 8 team scheduling matrix that has balanced opponent, and home & away status. It makes a big difference in the quality of a schedule. It's important to evenly distribute early and late time slots, equal home & away status, and equal team distribution with opponents. Most of the balance is accomplished by using a balanced team pair matrix.
您尝试配对的团队数量(在这种情况下为8个团队,为期16周)可能是一项艰巨的任务,而且只是“调度过程”的开始。一旦确定了正确,平衡的团队配对,这只是将时间表放在一起进行分发的开始。接下来,每周4个时段的列表包括:星期几,整个16周季节的每个时段的开始时间和地点名称。评论:对你最有帮助的是获得一个8队的调度矩阵,它具有平衡的对手,以及主客场状态。它对时间表的质量产生很大影响。重要的是均匀分配早期和晚期时段,平等的主场和客场状态,以及与对手平等的球队分配。大部分余额是通过使用平衡的团队对矩阵来完成的。
After 35 years of teaching, coaching and scheduling sports in the Boston area, I can offer the following information. Creating league or game schedules for sports organizations seems to be a never ending task shared by many and is repeated over and over as the ages of the participants grow and those running the leagues change, the learning curve associated with creating schedules is significant for those who take over.
在波士顿地区经过35年的教学,辅导和日程安排后,我可以提供以下信息。为体育组织创建联赛或比赛时间表似乎是许多人共享的永无止境的任务,随着参与者年龄的增长和联赛的变化而反复重复,与创建时间表相关的学习曲线对于那些接管。
With that said, I am amazed at how many highly educated mathematical wizards are involved with trying to come up with the perfect solution (algorithm) that will solve one's scheduling problem. Myself and a friend (who is a mathematical/programmer genius) over a period of 3 years created software that perfectly balances all the important components when creating schedules for 4 to 22 teams. What we learned is that there is no algorithm that can handle all the possible variables that are added to the normal variables to create balanced schedules. What I am saying is there are just as many "what ifs" as there are mathematical permutations and combinations that deal with just creating a team matrix listing opponents and home & visitor status of games.
话虽如此,我很惊讶有多少受过高等教育的数学向导试图提出解决一个人调度问题的完美解决方案(算法)。我和我的朋友(他是一名数学/程序员天才)在3年的时间里创建了软件,可以在为4到22个团队创建时间表时完美地平衡所有重要组件。我们学到的是,没有算法可以处理添加到常规变量的所有可能变量,以创建平衡的计划。我所说的是有多少“假设”,因为有数学排列和组合只涉及创建一个团队矩阵列出对手和游戏的家庭和访客状态。
For example: Let's create a perfectly balanced schedule for an 9 team division playing 4 games a week. After nine weeks all teams have played played 8 games, all have had 1 bye, all have played two times in each of the 4 time slots, and all have been scheduled as the home team 4 times and the visitor team 4 times.
例如:让我们为每周玩4场比赛的9支球队创建一个完美平衡的赛程。九个星期后,所有队伍都打了8场比赛,所有队员都有1场比赛,所有队员都在4个时段中都打过两次,所有队伍都被安排了4次主队和4次访客队。
What more could anybody want? Well now comes the fun. Because the 4 time slots you chose has 2 games each Saturday, and 2 games each Sunday, the first problem pops up (after the schedules are created, published and passed out, when 2 coaches from 2 different teams call and say they work Saturdays, can you move our games to Sundays? Sure, I can do that. I'll just make the changes, re-publish and re-distribute the schedules.
还有什么人想要的吗?好了,现在很有趣。因为你选择的4个时段每个星期六有2个游戏,每个星期天有2个游戏,所以弹出第一个问题(在制定,发布和传递时间表之后,当来自2个不同团队的2个教练打电话并说他们星期六工作时,你可以将我们的游戏转移到星期日吗?当然,我可以做到这一点。我只会进行更改,重新发布和重新分发时间表。
After the new schedules are distributed, several days later, a different coach calls up and says, "Hey, you moved some of my games to Saturday, I work Saturdays.., move them back". The phone rings again. This time a it's a coach from another team and says they are in a tournament the 5th week of the schedule and can't play that week. Finally the last call comes in from yet another coach. He says the parents of one of his players teaches CCD classes Sunday afternoons, and half of his team is in the CCD class and wants to move all our Sunday games to Saturday. Enough!
在新的时间表发布后,几天后,一位不同的教练打来电话说:“嘿,你把我的一些游戏搬到星期六,我星期六工作......,把它们搬回来”。电话再次响起。这次是来自另一支球队的教练,并表示他们在比赛的第5周参加比赛,并且不能在那一周比赛。最后,最后一个电话来自另一位教练。他说,他的一名球员的父母在周日下午教授CCD课程,他的一半球队参加了CCD课程,并希望将我们所有的周日比赛都移到周六。足够!
My point is no matter what you do or how perfect a schedule is, the best solution is to find out as many of the player/coach team limitations or restrictions before you assign the playing days and time slots to any schedule. These unpredictable variables makes a mess out of a perfect schedule. People do get angry and complain when undesirable schedules are distributed. When a schedule is bad enough, some parents won't sign their youngster to play the following year. This happens when you have a young family with two or three small children, and Dad's work limits his ability to be there. When there is an early morning game, I think you can see the difficulty for Mom's when it all falls on her shoulders.
我的观点是,无论你做什么或时间表有多完美,最好的解决方案是在将比赛时间和时间段分配给任何赛程之前找出尽可能多的球员/教练团队限制或限制。这些不可预测的变量使得完美的时间表变得混乱。当分发不受欢迎的时间表时,人们会生气并抱怨。如果时间表足够糟糕,一些家长将不会在第二年签下他们的小伙伴。当你有一个有两三个小孩的年轻家庭时就会发生这种情况,爸爸的工作限制了他在那里的能力。当有一个清晨游戏时,我认为你可以看到妈妈的困难,当它全部落在她的肩膀上。
For those that are new to scheduling, stay with it. It gets better over time after you gain a little experience dealing with the problems. If you purchase a scheduling software program to calculate team pairs, be careful. Insist on seeing a full single round robin of the schedule they create. Check for the things described above (about balance and distribution).
对于那些不熟悉日程安排的人,请坚持下去。在获得处理问题的一点经验之后,它会随着时间的推移变得更好。如果您购买计划软件程序来计算团队对,请小心。坚持看到他们创建的计划的完整单循环。检查上述内容(关于平衡和分配)。
Bob R
#4
0
Given a table of teams, say team ( teamname );
给出一个团队表,比如团队(teamname);
And a table of fixtures
还有一个固定装置表
fixture ( date; );
夹具(日期;);
With the relationship decomposed by 'playing'
通过'播放'分解关系
playing ( fixture_date. teamname );
玩(fixture_date。队名);
Then it's would simply be a matter of iterating through each date, then team and selecting a team at random who does not already have a fixture for that date, and who have not played the selected team (or not played the selected team recently).
然后,只需要迭代每个日期,然后团队并随机选择一个团队,该团队尚未拥有该日期,并且没有参与所选团队(或者最近未参加选定的团队)。
Although a simpler solution would be to have team[n] (where n is 0....number of teams -1) play team[(n+(number of teams)) % X] for varying values of X.
虽然更简单的解决方案是让团队[n](其中n为0 ......团队数量-1)为不同的X值发挥团队[(n +(团队数量))%X]。