 Welcome to another optoplanar example. This time we'll be looking at tennis club scheduling and more specifically we'll be looking at the feature of fairness constraints where we want to make sure that in a certain solution something is fairly distributed amongst the persons involved in the solution, right? So let's take a look at the tennis club, tennis club scheduling problem. In this problem we have a number of days a number of here we have actually 18 days where the tennis club has two courts available and they actually play only once a week so it's 18 weeks actually and since they have two courts available four teams can actually play, right? Now the problem is there are actually more teams or actually as you can see over here seven teams going from Misha up to Tobias so only four of those teams can play on a certain date so we have to sign four teams to come play on the date and then they play around Robin against all the other teams that are available on that date so they basically play three matches something like that, right? Okay that in itself would be pretty easy to schedule of course, right? So there are a few extra constraints. So one of the constraints is that certain teams cannot make it on certain dates so for example Irene's team cannot play that first day zero and Tobias team cannot play on this day one, right? And so forth and as you can see we have we can have multiple of these unavailability days for certain teams on a certain day. So let's see what happens when we start planning this right? So let me just plan a little bit and I'll stop it for a second now. So here we have a schedule where all the teams play a number of times and you can see that on every day we have four teams at play so for example on day zero Misha's, Susie's, Christina's and Tobias team play and no team that is unavailable plays on any date so that that's a good thing, right? And no team plays twice on the same day so that basically means that none of the hard constraints are broken. So let's take a look at how those constraints are actually implemented, right? So okay, here we have the first one that only one team plays on a certain date so what happens is if you have two team assignments, right, of two teams playing on the same day so if you have a team assignment for a certain team on a certain day and we have another one on the for the same team for on the same day then we have a problem we're going to add a hard constraint of minus one. Now you can also see we use the ID trick to make sure that we're not punishing a team when it's with the combination with itself and we also make sure that we punish every combination only once again using the ID trick basically. Okay, so that's this constraint make sure that no teams play, no two teams play on the same day. Then we have the unavailability constraint so when there is a certain unavailability for certain team on a certain day so for example if you look back at the solution that was Irene cannot play on day zero so if you have that case so an unavailability of Irene for day zero then if you have an assignment of that team so Irene's team on day zero then we're going to again add a hard constraint and again we're going to do minus one. Now you can see if we look at the solution here that as you can see at the bottom here too by the way that no hard constraints are broken so this is a feasible solution. Okay, now on top of that there are two other constraints let me start with the first one which is basically the fairness constraint right. So each team plays for a number of days and we've actually calculated the total here at the end so you can see that for example Misha's team is in total playing 10 times right and as you can see we are going to try to balance this out make this fair so all teams should play 10 or 11 days. It's obvious of course that we cannot play all that I'm all play the exact same number of times because of the fact that the number of days that are available divided by the number of teams that are playing is not a discrete number so we need to some teams just play one day one time one day more right there just lucky and in this case Susie's and Christina's teams are lucky to play an extra day more. Okay, so how do we implement this constraint right so there are different ways you can do that one way would be to say okay I know the number of days that we're going that are available so I know the number of teams that will show up so I'll just simply calculate okay we need an average of about 10 days and I'll try to get everyone to get this to be as close to that average as possible right. The problem with this type of implementing it this way is that when more days are added you need this you need to do this calculation differently of course right and so it can get quite it's annoying to have to do that pre-calculation and more importantly you are have no guarantee that you'll be actually be able to find a feasible solution which does this fairly let me just show that or just explain it anyway let's suppose Irene team Irene's team for currently has one two three four five days that she doesn't want to play so there are still 12 days open where she wants to play so actually 13 days open where she wants to play so we can give her 10 days. Suppose she was 10 days and available in that case we would want to be eight days available for her to play so it would be impossible to give her 10 days her team 10 days to play because they were she was only available for eight days so in that case you would we'd always you never get 10 days for Irene in the feasible solution right and then of course the heart constraint that she is not that her team is not able to join is more important than this whole fairness thing right but even if this happens you want things to be fair but the other teams and related to her team right so the way to do this is basically with the fairness trick we shall explain now how was the how does the fairness trick work okay so it's shown with a different use case but but the thing is the same thing we want to basically in this tennis club example we want to load balance the number of times a team plays in this case we want to load balance then this is for employee rostering we want to load balance the number of shifts that every employee has to work we want to make sure that that equals out right so that's that they all work about the same number of shifts okay so what you do is you basically so in this case in the first solution over here we have employee x working 5 shifts employee y 4 shifts and employee z 1 shifts so you can easily see that this is rather unfair this one has to work 5 times as much so the purple one so worker 5 times as much as the blue one right now now one interesting thing to note there is that even though in this case the thing we're distributing is a bad thing they don't they don't want more shifts and in tennis club example it is a good thing they want to play more that we're distributing it's the same rule it's the same trick and it works it's exactly the same thing you just make sure that they get an even portion of the number of things that we were actually dividing right so what we do is we just take the we just count the number of shifts for x and we take the we square that and take the negative of that so there are 5 shifts so the negative of the square of that is minus 25 right so minus 25 soft that we add we do the same for employee y where we have 4 so minus 16 and same for one which ends up for the blue one which ends up as minus 1 if you all count those soft scores together we get a score of minus 42 right so now let's take a look at when we change something in this doesn't really matter what we changed let's just change the the I1 so this one will go to the blue one so the green so the yellow one will only have three shifts and the blue will have two shifts and the purple will stay the same and then if you do the calculation again we get purple stays the same minus 25 here we go from minus 4 to minus 16 to minus 9 so that's a gain of 7 points but the blue one there will go from minus 1 to minus 4 that's a loss of 3 points as a result of which we actually increase our score with 4 points so this is a better solution you can easily see that this is a better solution of course because it's more fair between blue and yellow where here blue only had one shift and yellow four now it's almost the same two and three right one difference again if you do the same thing and we do we now take one of the purple ones and move them into the yellow ones and then we have 4 4 2 we get a score of minus 36 and if you then balances out a little bit even more which is actually the best way we can do it do this with 10 shifts where we have 4 3 and 3 then you actually see that this is the the best scoring you actually see that this are every time this improves every time we do this every time we balance it even out more and more now the nice thing about this trick is that if the last two solutions were not feasible for whatever reason so let's for example say that shift a b c d and e can only be done by employee x because only he has the skills for that right then both these bottom solutions would not be feasible even in that case it will try to at least make it as fair as possible between y and z it it will be unfair because for employee x but yeah he's the only one who has that skill so there's no other way to do that but at least it will make it fair between y and z and actually give see some more work and why some less work right and it will converge on the solution okay so that does the fairness trick and we can basically use this for our for making sure that in in tennis club scheduling the number of days are actually the same so how do we do that pretty simple we have a rule that for each team we're going to basically count the number of times it's playing so we're just going to count the number of team assignments as you can see here I'm just going to count team assignments and what we're done going to do is we're going to basically like I said before and add a negative of the square of that value so we take the total and we just multiply it by itself which is a square of course and we take a negative of that all right so what you do see as a result of this is that the score of the media I've actually done this in this case I don't use it as the medium score now I'll explain in a minute why is that the medium score is never zero it's impossible to get that score but it's at this number which is it converges at this number and which is the more fair fair history of doing this right okay now there's one extra constraint on top of this and which is actually softer than the than the constraint of being fair so that's why the medium constraint was made it was must made so that's why the fairness constraint was actually being made in a medium constraint we added in another constraint which is the soft constraint in this case which is less important than fairness and that's that that's about the number of confrontations so for example on day zero Misha will play against Susie Irene sorry Susie Christina and Tobias we want to make sure that every team plays against every other team a nickel amount of times right so let's take a look there at the confrontations so for example Misha of course she's never playing against herself it's playing against Angelica four times against Catherine five times Susie five Irene five and then Christina six times and then Tobias five times right now let's take a look there so we were basically counting the number of times that a certain team plays against another team on the same date right so it is playing on the same date as another team right so interesting here is we have one going actually up to seven here Susie and Katrina play against our seven times and the lowest number is four we would actually like those to be much closer to more closer together right so let's see if we give up to a little bit more time see what happens right look make sure you look at the score here at the bottom if you give it a little bit more time pretty quickly I actually finds finds a better solution and that's actually conversion on the solution so I'll just stop it right so let's take a look at the confrontations again there are no sevens anymore nobody's playing seven times against another team maximum six now there are still teams playing four times against each other so we have Susie and Irene playing four times against each other and that they're so they're only two fours because these are just of course the mirror all of that of course right so that this is actually a better solution than one we had before right and would be even nice if we could actually trade out these get rid of these two fours and then reduce at least two of these sixes to five but due to the other constraints mainly the fairness constraint but also the unavailability constraints it's actually not possible now how do we implement the confrontations thing well actually we use the fairness trick again it's pretty simple again we basically take any two combination of any two teams where we have any two teams and we say when we have a team assignment for the first team and for the second team right and they're both on the same day and again we use on the same day and again we use the ID trick to make sure that they're different and we don't count them too much then of course we have a problem right and we're just so we just count the number of times actually plague these two team plays to those two team play against each other and we again take the negative of the square of that number right so it is basically the same thing as as a fair fairness constraints which we had here and again this results into a quite a high a nice number there quite a high number there but it doesn't matter it actually even the doubt gives us a very fair distribution right now the interesting to note here is that this is actually a real-world problem one of my colleagues had this problem and he did this on paper he solves on paper for a couple of night evenings he told me and the solution he came up with was actually on this part actually worse than than the one we just generated so yeah that it's pretty nice of course right and by the way the implementation of this the original implementation of this took a little bit over an hour which includes a rudimentary gui gui and a way to read and write from these XML files but I did improve the gui a little bit more thanks for watching this demonstration if you want to know more about Oploplanner just go to the website oploplanner.org and if you want to try this example yourself just download the zip and zip it and run the examples thanks for watching bye