Random Scheduling

cgreen

Active Member
Joined
Aug 14, 2002
Messages
293
I have 14 teams playing for 26 weeks. The teams play each other only twice in the 26-week frame. Is there a way to do a random schedule per week, then freeze (or do manual calculation) so that the schedule does not change for the 26 weeks unless I command it to? There will be 7 games per week, and again, each team can only play each other twice in the 26-week frame. The number listed first is understood to be where the game will be played, so it has to be random as well.

example(teams are identified as 1 thru 14):

Week One
2 and 13
3 and 4
11 and 6
5 and 14
1 and 7
8 and 12
10 and 9

Week Two
14 and 1
2 and 6
3 and 8
5 and 4
7 and 10
12 and 9
11 and 13
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi Mark,

I started it and walked away, 2 hrs later it was still shuffling but nothing had changed, so I stopped it. Before I went to bed I started it again, when I got up the next morning it was still running but nothing had changed. That's why I was wondering if the file is to big. I cut it down to 16 teams and it took 5 hours to run, but it worked.
 
Upvote 0
On 2002-11-12 07:34, cgreen wrote:
Hi Mark,

I started it and walked away, 2 hrs later it was still shuffling but nothing had changed, so I stopped it. Before I went to bed I started it again, when I got up the next morning it was still running but nothing had changed. That's why I was wondering if the file is to big. I cut it down to 16 teams and it took 5 hours to run, but it worked.

It's not that the file is too big... There simply are a lot of possible combinations with 17 weeks and 18 teams. It's like rolling all 1s with 8 dice as opposed to getting snake eyes with 2. That's the price for a random schedule of this size. I haven't taken any measurements of the time it takes to resolve a given schedule, but my impressions are that it takes less time to resolve subsequent schedules as the process progresses. So it would take the longest time to satisfy the 1st schedule.

I'm gonna let mine run all day and see if I'm luckier than you are. :)
This message was edited by Mark W. on 2002-11-12 09:33

BTW, after about 40 minutes runtime (in the background while I browsed MrExcel) I resolved week 17. Continuing...
This message was edited by Mark W. on 2002-11-12 09:58
 
Upvote 0
I don't where this topic stands, but developing a solution was an interesting exercise. So, here's mine.

The first point to note is that it is fast -- and I mean lightning fast. The 18 team solution takes a fraction of a second. It's been tested for 4, 6, 8, 14, and 18 teams, multiple times for the first 4.

To use the program, run the ScheduleManager subroutine. The output is pairs of teams, with each row representing one week. The output starts in the first empty cell in the current column. Any information in cells to the right is *destroyed*.

The idea behind the algorithm, if any one is interested, is based on analysis of a graph (a la Operations Research). Each team is a node. Arcs connecting the nodes represent legitimate possible pairings.

A valid pairing is found by selecting two nodes that have an arc between them. If all nodes are assigned to a solution, then that solution is feasible. If nodes are left in the graph and there are no arcs between those nodes, then the combinations used so far yield an infeasible solution and must be 'backed out.'

After a solution is found for a given week, the arcs representing the solution are deleted and the problem is solved again.

All possible solutions have been found when no arc remains in the graph.

In developing a solution for a given week, when a node is included in the solution, it (i.e., the node) is dropped from the graph all together. After an acceptable pair is found, the two nodes are removed from the graph, and the reduced problem is then solved.

How is a node-pair selected? The first node in a pair is the first available node in the graph. The second node in the pair is selected with the foll. technique:
First, select a random node that is still in the graph. If this combination results in an infeasible solution somewhere down the line, then revert to the sequential protocol.
If the first (random) selection fails, the second is a sequential selection process. Select, as the 2nd element of the pair, the first available node in the graph. If this results in an infeasible solution, select the next available node.

Implementation of the algorithm in VB:

The graph (nodes and arcs) is implemented by the TeamMap matrix. Since this is not a directed graph, only the upper half of the matrix is needed (but no memory optimization is done -- or needed).

During the development of a week's schedule, nodes are removed from (and, for infeasible solutions, added back to) the graph through the AvailTeams vector.

After a valid pair is found, the reduced graph is solved by the same procedure, which is called recursively.

<pre>
Option Explicit

Sub initializeSystem(ByRef TeamMap() As Byte, _
ByRef AvailTeams() As Byte)
Dim i As Integer, j As Integer
For i = 1 To UBound(TeamMap, 1)
For j = i + 1 To UBound(TeamMap, 1)
TeamMap(i, j) = 1
Next j
Next i
For i = 1 To UBound(AvailTeams)
AvailTeams(i) = 1
Next i
End Sub
Function FirstAvailCompetitor(TeamMap() As Byte, _
ByVal CurrTeam As Integer, AvailTeams() As Byte, _
ByVal StartAfter As Integer) _
As Integer
Dim i As Integer
If StartAfter = 0 Then StartAfter = CurrTeam
For i = StartAfter + 1 To UBound(TeamMap, 2)
If AvailTeams(i) And TeamMap(CurrTeam, i) = 1 Then
FirstAvailCompetitor = i
Exit Function '<<<<<
End If
Next i
End Function
Function FindRandomCompetitor(ByRef TeamMap() As Byte, _
CurrTeam As Integer, AvailTeams() As Byte) _
As Integer
Dim i As Integer, StartAt As Integer
StartAt = Fix(Rnd() * (UBound(TeamMap, 1) - CurrTeam)) _
+ CurrTeam + 1
i = StartAt
Do
If AvailTeams(i) And TeamMap(CurrTeam, i) = 1 Then
FindRandomCompetitor = i
Exit Function '<<<<<
End If
If i = UBound(TeamMap, 2) Then i = CurrTeam + 1 _
Else i = i + 1
Loop Until i = StartAt
End Function
Function FindCompetitor(ByRef TeamMap() As Byte, _
CurrTeam As Integer, AvailTeams() As Byte, _
StartAfter As Integer) As Integer
Dim i As Integer, StartAt As Integer
If StartAfter = 0 Then
FindCompetitor = FindRandomCompetitor( _
TeamMap, CurrTeam, AvailTeams)
Else
FindCompetitor = FirstAvailCompetitor( _
TeamMap, CurrTeam, AvailTeams)
End If
End Function
Function FirstAvailTeam(ByRef AvailTeams() As Byte) As Integer
Dim i As Integer
For i = 1 To UBound(AvailTeams)
If AvailTeams(i) = 1 Then
FirstAvailTeam = i
Exit Function '<<<<<
End If
Next i
End Function
Function schedAPair(ByVal nbrTeams As Integer, _
ByRef TeamMap() As Byte, AvailTeams() As Byte, _
RecursionDepth As Integer, _
ByRef RsltMap() As Integer) As Boolean
Dim T1 As Integer, T2 As Integer, _
Done As Boolean, Success As Boolean, _
FailureCount As Integer
T1 = FirstAvailTeam(AvailTeams)
If T1 = 0 Then 'all teams assigned
schedAPair = True
Exit Function
End If
T2 = FindRandomCompetitor(TeamMap, T1, AvailTeams)
Do
If T2 = 0 Then 'infeasible solution
Done = True
Else
AvailTeams(T1) = 0: AvailTeams(T2) = 0
RsltMap(RecursionDepth, 1) = T1
RsltMap(RecursionDepth, 2) = T2
Success = schedAPair(nbrTeams, TeamMap, AvailTeams, _
RecursionDepth + 1, RsltMap)
If Not Success Then 'Infeasible solution downstream
FailureCount = FailureCount + 1
AvailTeams(T1) = 1: AvailTeams(T2) = 1
RsltMap(RecursionDepth, 1) = 0
RsltMap(RecursionDepth, 2) = 0
T2 = FirstAvailCompetitor(TeamMap, T1, _
AvailTeams, IIf(FailureCount = 1, 0, T2))
End If
Done = Success
End If
Loop Until Done
schedAPair = Success
End Function
Sub printRslt(RsltMap() As Integer)
Dim dest As Range, i As Integer
Set dest = Cells(Cells.Rows.Count, Selection.Column).End(xlUp) _
.Offset(1, 0)
For i = 1 To UBound(RsltMap, 1)
dest.Offset(0, i - 1).Value = RsltMap(i, 1) _
& ", " & RsltMap(i, 2)
Next i
End Sub
Sub updateSystem(ByRef TeamMap() As Byte, _
ByRef RsltMap() As Integer, _
ByRef AvailTeams() As Byte)
Dim i As Integer
For i = 1 To UBound(RsltMap, 1)
TeamMap(RsltMap(i, 1), RsltMap(i, 2)) = 0
RsltMap(i, 1) = 0: RsltMap(i, 2) = 0
Next i
For i = 1 To UBound(AvailTeams)
AvailTeams(i) = 1
Next i
End Sub

Sub ScheduleManager()
Dim nbrTeams As Integer, SchedRslt As Boolean
nbrTeams = Application.InputBox("Enter number of teams (must be an even number)", , 4, , , , , 1)
If nbrTeams Mod 2 = 1 Then
MsgBox "Please enter an even number (2, 4, 6, etc.)"
Exit Sub '<<<<<
End If
ReDim TeamMap(1 To nbrTeams, 1 To nbrTeams) As Byte, _
RsltMap(1 To nbrTeams / 2, 1 To 2) As Integer, _
AvailTeams(1 To nbrTeams) As Byte
'actually need only upper triangle matrix
'(above the main diagonal)
initializeSystem TeamMap, AvailTeams
'Establishes all valid combinations
Do
SchedRslt = schedAPair(nbrTeams, TeamMap, AvailTeams, 1, RsltMap)
If SchedRslt Then
printRslt RsltMap()
updateSystem TeamMap, RsltMap, AvailTeams
End If
Loop While SchedRslt

End Sub
</pre>
 
Upvote 0
But is this random (in the statistical sense)?

Is there a way to do a random schedule...

The approach that we're currently using is a Monte Carlo simulation.
This message was edited by Mark W. on 2002-11-13 17:41
 
Upvote 0
No comments eon the statistical issues, but nice code! Ran in about 4 sconds for 30 teams.

paddy
 
Upvote 0
On 2002-11-13 17:39, Mark W. wrote:
But is this random (in the statistical sense)?

Is there a way to do a random schedule...

The approach that we're currently using is a Monte Carlo simulation.
This message was edited by Mark W. on 2002-11-13 17:41

I don't know what 'random in a statistical sense' means. Especially, given that computers can generate, at best, pseudo-random numbers *and* given, as some (many?) claim, the weaknesses in the uniform random number generator in XL and VBA.

One way to test randomness -- I suppose -- would be to call the ScheduleManager from a loop in another subroutine and see when it yields a duplicate solution.

My take on the subject is that the result of the algorithm is as random as one can get in a practical sense. The way I thought of the role of chance in this problem is as follows.

As more and more teams are assigned to a given solution, the degrees of freedom drop. In the case where all but 2 teams have been assigned, there is no chance left in the system. The algorithm should leverage that knowledge by dropping the random search in this case.

Similarly, the presence of an infeasible solution 'downstream' reduces the 'degrees of freedom' for the pair selection at that point. Rather than incorporate this gradual reduction in the (random) search space, I abruptly drop out of random mode. One could -- without too much work -- use a gradual reduction is the search space, but given the chance element in prior decisions -- and in subsequent decisions, it is hard to imagine any discernible benefit to the more sophisticated approach.

I haven't followed the rather long discussion on the subject, but it would seem that the algorithm I posted can be considered a simulation that incorporates knowledge about previous solutions and possible pitfalls a priori to a decision rather than testing feasibility ex post.

Personally, I find that a 'pure' (for want of a better word) Monte Carlo simulation is not the best tool for finding feasible solutions -- especially when other methods are available. Just as a simulation is not the best way to find an optimal solution if an alternative method is available.
 
Upvote 0
Can't disagree with anything that you stated; however, the OP requested a random solution -- not an optimal one. If he had wanted an optimal one I would have suggested the use of Solver. The approach that we've been using randomly assigns (to the best of Excel's ability) team pairings and the week in which they'll meet. Of course, one could question how important it is for this particular application to be random. I merely accepted the OP's requirement as it was stated.

If I were building such a schedule I'd create a single, reuseable week/pairing framework, and then randomly assign team names to the team numbers used in the pairing framework. I suggested something along this line back on 29 August. But, hey, who am I?
This message was edited by Mark W. on 2002-11-13 18:53
 
Upvote 0
On 2002-11-13 18:28, Mark W. wrote:
Can't disagree with anything that you stated; however, the OP requested a random solution -- not an optimal one.
...
You've been working on this longer than I, so it is possible that I'm missing something, Mark, but the solution is a random one. Run the program multiple times and the result will be different each time. Further, knowing the past set of solutions doesn't help predict the next solution (see the FindRandomCompetitor subroutine).
 
Upvote 0

Forum statistics

Threads
1,216,087
Messages
6,128,740
Members
449,466
Latest member
Peter Juhnke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top