1{
2 "cells": [
3  {
4   "cell_type": "markdown",
5   "id": "google",
6   "metadata": {},
7   "source": [
8    "##### Copyright 2021 Google LLC."
9   ]
10  },
11  {
12   "cell_type": "markdown",
13   "id": "apache",
14   "metadata": {},
15   "source": [
16    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
17    "you may not use this file except in compliance with the License.\n",
18    "You may obtain a copy of the License at\n",
19    "\n",
20    "    http://www.apache.org/licenses/LICENSE-2.0\n",
21    "\n",
22    "Unless required by applicable law or agreed to in writing, software\n",
23    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
24    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
25    "See the License for the specific language governing permissions and\n",
26    "limitations under the License.\n"
27   ]
28  },
29  {
30   "cell_type": "markdown",
31   "id": "basename",
32   "metadata": {},
33   "source": [
34    "# sports_schedule_sat"
35   ]
36  },
37  {
38   "cell_type": "markdown",
39   "id": "link",
40   "metadata": {},
41   "source": [
42    "<table align=\"left\">\n",
43    "<td>\n",
44    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/master/examples/notebook/contrib/sports_schedule_sat.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
45    "</td>\n",
46    "<td>\n",
47    "<a href=\"https://github.com/google/or-tools/blob/master/examples/contrib/sports_schedule_sat.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/github_32px.png\"/>View source on GitHub</a>\n",
48    "</td>\n",
49    "</table>"
50   ]
51  },
52  {
53   "cell_type": "markdown",
54   "id": "doc",
55   "metadata": {},
56   "source": [
57    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
58   ]
59  },
60  {
61   "cell_type": "code",
62   "execution_count": null,
63   "id": "install",
64   "metadata": {},
65   "outputs": [],
66   "source": [
67    "!pip install ortools"
68   ]
69  },
70  {
71   "cell_type": "code",
72   "execution_count": null,
73   "id": "code",
74   "metadata": {},
75   "outputs": [],
76   "source": [
77    "# Based on sports_scheduling_sat.cc, Copyright 2010-2021 Google LLC\n",
78    "#\n",
79    "# Translated to Python by James E. Marca August 2019\n",
80    "#\n",
81    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
82    "# you may not use this file except in compliance with the License.\n",
83    "# You may obtain a copy of the License at\n",
84    "#\n",
85    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
86    "#\n",
87    "# Unless required by applicable law or agreed to in writing, software\n",
88    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
89    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
90    "# See the License for the specific language governing permissions and\n",
91    "# limitations under the License.\n",
92    "\"\"\"Sports scheduling problem.\n",
93    "\n",
94    "We want to solve the problem of scheduling of team matches in a\n",
95    "double round robin tournament.  Given a number of teams, we want\n",
96    "each team to encounter all other teams, twice, once at home, and\n",
97    "once away. Furthermore, you cannot meet the same team twice in the\n",
98    "same half-season.\n",
99    "\n",
100    "Finally, there are constraints on the sequence of home or aways:\n",
101    " - You cannot have 3 consecutive homes or three consecutive aways.\n",
102    " - A break is a sequence of two homes or two aways, the overall objective\n",
103    "   of the optimization problem is to minimize the total number of breaks.\n",
104    " - If team A meets team B, the reverse match cannot happen less that 6 weeks\n",
105    "   after.\n",
106    "\n",
107    "Translation to python:\n",
108    "\n",
109    "This version is essentially a straight translation of\n",
110    "sports_scheduling_sat.cc from the C++ example, SecondModel version.\n",
111    "\n",
112    "This code gets an F from code climate for code quality and\n",
113    "maintainability.\n",
114    "\n",
115    "Originally developed with pool vs pool constraints on the mailing\n",
116    "list, but that has been dropped for this version to keep it in sync\n",
117    "with the C++ version.\n",
118    "\n",
119    "Added command line options to set numbers of teams, numbers of days,\n",
120    "etc.\n",
121    "\n",
122    "Added CSV output.\n",
123    "\n",
124    "For a version with pool constraints, plus tests, etc, see\n",
125    "https://github.com/jmarca/sports_scheduling\n",
126    "\n",
127    "\"\"\"\n",
128    "import argparse\n",
129    "import os\n",
130    "import re\n",
131    "import csv\n",
132    "import math\n",
133    "\n",
134    "from ortools.sat.python import cp_model\n",
135    "\n",
136    "\n",
137    "def csv_dump_results(solver, fixtures, num_teams, num_matchdays, csv_basename):\n",
138    "    matchdays = range(num_matchdays)\n",
139    "    teams = range(num_teams)\n",
140    "\n",
141    "    vcsv = []\n",
142    "    for d in matchdays:\n",
143    "        game = 0\n",
144    "        for home in range(num_teams):\n",
145    "            for away in range(num_teams):\n",
146    "                if solver.Value(fixtures[d][home][away]):\n",
147    "                    game += 1\n",
148    "                    # each row: day,game,home,away\n",
149    "                    row = {\n",
150    "                        'day': d + 1,\n",
151    "                        'game': game,\n",
152    "                        'home': home + 1,\n",
153    "                        'away': away + 1\n",
154    "                    }\n",
155    "                    vcsv.append(row)\n",
156    "\n",
157    "    # check for any existing file\n",
158    "    idx = 1\n",
159    "    checkname = csv_basename\n",
160    "    match = re.search(r\"\\.csv\", checkname)\n",
161    "    if not match:\n",
162    "        print(\n",
163    "            'looking for a .csv ending in passed in CSV file name.  Did not find it, so appending .csv to',\n",
164    "            csv_basename)\n",
165    "        csv_basename += \".csv\"\n",
166    "\n",
167    "    checkname = csv_basename\n",
168    "    while os.path.exists(checkname):\n",
169    "        checkname = re.sub(r\"\\.csv\", \"_{}.csv\".format(idx), csv_basename)\n",
170    "        idx += 1\n",
171    "        # or just get rid of it, but that is often undesireable\n",
172    "        # os.unlink(csv_basename)\n",
173    "\n",
174    "    with open(checkname, 'w', newline='') as csvfile:\n",
175    "        fieldnames = ['day', 'game', 'home', 'away']\n",
176    "        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)\n",
177    "\n",
178    "        writer.writeheader()\n",
179    "        for row in vcsv:\n",
180    "            writer.writerow(row)\n",
181    "\n",
182    "\n",
183    "def screen_dump_results(solver, fixtures, num_teams, num_matchdays):\n",
184    "    matchdays = range(num_matchdays)\n",
185    "    teams = range(num_teams)\n",
186    "\n",
187    "    total_games = 0\n",
188    "    for d in matchdays:\n",
189    "        game = 0\n",
190    "        for home in teams:\n",
191    "            for away in teams:\n",
192    "                match_on = solver.Value(fixtures[d][home][away])\n",
193    "                if match_on:\n",
194    "                    game += 1\n",
195    "                    print('day %i game %i home %i away %i' %\n",
196    "                          (d + 1, game, home + 1, away + 1))\n",
197    "        total_games += game\n",
198    "\n",
199    "\n",
200    "def assign_matches(num_teams,\n",
201    "                   num_matchdays,\n",
202    "                   num_matches_per_day,\n",
203    "                   max_home_stand,\n",
204    "                   time_limit=None,\n",
205    "                   num_cpus=None,\n",
206    "                   csv=None,\n",
207    "                   debug=None):\n",
208    "    \"\"\"Assign matches between teams in a league.\n",
209    "\n",
210    "    Keyword arguments:\n",
211    "    num_teams -- the number of teams\n",
212    "    num_matchdays -- the number of match days to play.  Should be greater than one day.  Note that if num_matchdays is exactly some multipe (`n`) of `num_teams - 1` then each team with play every other team exactly `n` times.  If the number of match days is less than or greater than a perfect multiple, then some teams will not play each other `n` times.\n",
213    "    num_matches_per_day -- how many matches can be played in a day.  The assumption is one match per day, and really this code was not tested with different values.\n",
214    "    max_home_stand -- how many home games are allowed to be in a row.\n",
215    "    time_limit -- the time in minutes to allow the solver to work on the problem.\n",
216    "    num_cpus -- the number of processors to use for the solution\n",
217    "    csv -- a file name to save the output to a CSV file\n",
218    "    debug -- boolean value stating whether to ask the solver to show its progress or not\n",
219    "\n",
220    "    \"\"\"\n",
221    "\n",
222    "    model = cp_model.CpModel()\n",
223    "\n",
224    "    print('num_teams', num_teams, 'num_matchdays', num_matchdays,\n",
225    "          'num_matches_per_day', num_matches_per_day, 'max_home_stand',\n",
226    "          max_home_stand)\n",
227    "\n",
228    "    matchdays = range(num_matchdays)\n",
229    "    matches = range(num_matches_per_day)\n",
230    "    teams = range(num_teams)\n",
231    "    # how many possible unique games?\n",
232    "    unique_games = (num_teams) * (num_teams - 1) / 2\n",
233    "\n",
234    "    # how many games are possible to play\n",
235    "    total_games = num_matchdays * num_matches_per_day\n",
236    "\n",
237    "    # maximum possible games versus an opponent.  example, if 20\n",
238    "    # possible total games, and 28 unique combinations, then 20 // 28\n",
239    "    # +1 = 1.  If 90 total games (say 5 per day for 18 match days) and\n",
240    "    # 10 teams for 45 possible unique combinations of teams, then 90\n",
241    "    # // 45 + 1 = 3. Hmm.  Should be 2\n",
242    "    matchups = int((total_games // unique_games) + 1)\n",
243    "    # print(matchups)\n",
244    "    # there is a special case, if total games / unique games == total\n",
245    "    # games // unique games, then the constraint can be ==, not <=\n",
246    "    matchups_exact = False\n",
247    "    if (total_games % unique_games == 0):\n",
248    "        matchups_exact = True\n",
249    "        matchups = int(total_games // unique_games)\n",
250    "\n",
251    "    print('expected matchups per pair', matchups, 'exact?', matchups_exact)\n",
252    "\n",
253    "    days_to_play = int(unique_games // num_matches_per_day)\n",
254    "    print('unique_games', unique_games, '\\nnum matches per day',\n",
255    "          num_matches_per_day, '\\ndays to play', days_to_play,\n",
256    "          '\\ntotal games possible', total_games)\n",
257    "\n",
258    "    fixtures = [\n",
259    "    ]  # all possible games, list of lists of lists: fixture[day][iteam][jteam]\n",
260    "    at_home = [\n",
261    "    ]  # whether or not a team plays at home on matchday, list of lists\n",
262    "\n",
263    "    # Does team i receive team j at home on day d?\n",
264    "    for d in matchdays:\n",
265    "        # hackity hack, append a new list for all possible fixtures for a team on day d\n",
266    "        fixtures.append([])\n",
267    "        for i in teams:\n",
268    "            # hackity hack, append a list of possible fixtures for team i\n",
269    "            fixtures[d].append([])\n",
270    "            for j in teams:\n",
271    "                # for each possible away opponent for home team i, add a fixture\n",
272    "                #\n",
273    "                # note that the fixture is not only that team i plays\n",
274    "                # team j, but also that team i is the home team and\n",
275    "                # team j is the away team.\n",
276    "                fixtures[d][i].append(\n",
277    "                    model.NewBoolVar(\n",
278    "                        'fixture: home team %i, opponent %i, matchday %i' %\n",
279    "                        (i, j, d)))\n",
280    "                if i == j:\n",
281    "                    # It is not possible for team i to play itself,\n",
282    "                    # but it is cleaner to add the fixture than it is\n",
283    "                    # to skip it---the list stays the length of the\n",
284    "                    # number of teams.  The C++ version adds a \"FalseVar\" instead\n",
285    "                    model.Add(fixtures[d][i][j] == 0)  # forbid playing self\n",
286    "\n",
287    "    # Is team t at home on day d?\n",
288    "    for d in matchdays:\n",
289    "        # hackity hack, append a new list for whether or not a team is at home on day d\n",
290    "        at_home.append([])\n",
291    "        for i in teams:\n",
292    "            # is team i playing at home on day d?\n",
293    "            at_home[d].append(\n",
294    "                model.NewBoolVar('team %i is home on matchday %i' % (i, d)))\n",
295    "\n",
296    "    # each day, team t plays either home or away, but only once\n",
297    "    for d in matchdays:\n",
298    "        for t in teams:\n",
299    "            # for each team, each day, list possible opponents\n",
300    "            possible_opponents = []\n",
301    "            for opponent in teams:\n",
302    "                if t == opponent:\n",
303    "                    continue\n",
304    "                # t is home possibility\n",
305    "                possible_opponents.append(fixtures[d][t][opponent])\n",
306    "                # t is away possibility\n",
307    "                possible_opponents.append(fixtures[d][opponent][t])\n",
308    "            model.Add(\n",
309    "                sum(possible_opponents) == 1)  # can only play one game per day\n",
310    "\n",
311    "    # \"Each fixture happens once per season\" is not a valid constraint\n",
312    "    # in this formulation.  in the C++ program, there are exactly a\n",
313    "    # certain number of games such that every team plays every other\n",
314    "    # team once at home, and once away.  In this case, this is not the\n",
315    "    # case, because there are a variable number of games.  Instead,\n",
316    "    # the constraint is that each fixture happens Matchups/2 times per\n",
317    "    # season, where Matchups is the number of times each team plays every\n",
318    "    # other team.\n",
319    "    fixture_repeats = int(math.ceil(matchups / 2))\n",
320    "    print('fixture repeats expected is', fixture_repeats)\n",
321    "\n",
322    "    for t in teams:\n",
323    "        for opponent in teams:\n",
324    "            if t == opponent:\n",
325    "                continue\n",
326    "            possible_days = []\n",
327    "            for d in matchdays:\n",
328    "                possible_days.append(fixtures[d][t][opponent])\n",
329    "            if matchups % 2 == 0 and matchups_exact:\n",
330    "                model.Add(sum(possible_days) == fixture_repeats)\n",
331    "            else:\n",
332    "                # not a hard constraint, because not exactly the right\n",
333    "                # number of matches to be played\n",
334    "                model.Add(sum(possible_days) <= fixture_repeats)\n",
335    "\n",
336    "    # Next, each matchup between teams happens at most \"matchups\"\n",
337    "    # times per season. Again this is different that C++ version, in\n",
338    "    # which the number of games is such that each team plays every\n",
339    "    # other team exactly two times.  Here this is not the case,\n",
340    "    # because the number of games in the season is not fixed.\n",
341    "    #\n",
342    "    # in C++ version, the season is split into two halves.  In this\n",
343    "    # case, splitting the season into \"Matchups\" sections, with each\n",
344    "    # team playing every other team once per section.\n",
345    "    #\n",
346    "    # The very last section of games is special, as there might not be\n",
347    "    # enough games for every team to play every other team once.\n",
348    "    #\n",
349    "    for t in teams:\n",
350    "        for opponent in teams:\n",
351    "            if t == opponent:\n",
352    "                continue\n",
353    "            prior_home = []\n",
354    "            for m in range(matchups):\n",
355    "                current_home = []\n",
356    "                pairings = []\n",
357    "                # if m = matchups - 1, then last time through\n",
358    "                days = int(days_to_play)\n",
359    "                if m == matchups - 1:\n",
360    "                    days = int(\n",
361    "                        min(days_to_play, num_matchdays - m * days_to_play))\n",
362    "                # print('days',days)\n",
363    "                for d in range(days):\n",
364    "                    theday = int(d + m * days_to_play)\n",
365    "                    # print('theday',theday)\n",
366    "                    pairings.append(fixtures[theday][t][opponent])\n",
367    "                    pairings.append(fixtures[theday][opponent][t])\n",
368    "                    # current_home.append(fixtures[theday][t][opponent])\n",
369    "                if m == matchups - 1 and not matchups_exact:\n",
370    "                    # in the last group of games, if the number of\n",
371    "                    # games left to play isn't quite right, then it\n",
372    "                    # will not be possible for each team to play every\n",
373    "                    # other team, and so the sum will be <= 1, rather\n",
374    "                    # than == 1\n",
375    "                    #\n",
376    "                    # print('last matchup',m,'relaxed pairings constraint')\n",
377    "                    model.Add(sum(pairings) <= 1)\n",
378    "                else:\n",
379    "                    # if it is not the last group of games, then every\n",
380    "                    # team must play every other team exactly once.\n",
381    "                    #\n",
382    "                    # print('matchup',m,'hard pairings constraint')\n",
383    "                    model.Add(sum(pairings) == 1)\n",
384    "\n",
385    "    # maintain consistency between fixtures and at_home[day][team]\n",
386    "    for d in matchdays:\n",
387    "        for t in teams:\n",
388    "            for opponent in teams:\n",
389    "                if t == opponent:\n",
390    "                    continue\n",
391    "                # if the [t][opp] fixture is true, then at home is true for t\n",
392    "                model.AddImplication(fixtures[d][t][opponent], at_home[d][t])\n",
393    "                # if the [t][opp] fixture is true, then at home false for opponent\n",
394    "                model.AddImplication(fixtures[d][t][opponent],\n",
395    "                                     at_home[d][opponent].Not())\n",
396    "\n",
397    "    # balance home and away games via the following \"breaks\" logic\n",
398    "    # forbid sequence of \"max_home_stand\" home games or away games in a row\n",
399    "    # In sports like baseball, homestands can be quite long.\n",
400    "    for t in teams:\n",
401    "        for d in range(num_matchdays - max_home_stand):\n",
402    "            model.AddBoolOr([\n",
403    "                at_home[d + offset][t] for offset in range(max_home_stand + 1)\n",
404    "            ])\n",
405    "            model.AddBoolOr([\n",
406    "                at_home[d + offset][t].Not()\n",
407    "                for offset in range(max_home_stand + 1)\n",
408    "            ])\n",
409    "            # note, this works because AddBoolOr means at least one\n",
410    "            # element must be true.  if it was just AddBoolOr([home0,\n",
411    "            # home1, ..., homeN]), then that would mean that one or\n",
412    "            # all of these could be true, and you could have an\n",
413    "            # infinite sequence of home games.  However, that home\n",
414    "            # constraint is matched with an away constraint.  So the\n",
415    "            # combination says:\n",
416    "            #\n",
417    "            # AddBoolOr([home0, ... homeN]) at least one of these is true\n",
418    "            # AddBoolOr([away0, ... awayN]) at least one of these is true\n",
419    "            #\n",
420    "            # taken together, at least one home from 0 to N is true,\n",
421    "            # which means at least one away0 to awayN is false.  At\n",
422    "            # the same time, at least one away is true, which means\n",
423    "            # that the corresponding home is false.  So together, this\n",
424    "            # prevents a sequence of one more than max_home_stand to\n",
425    "            # take place.\n",
426    "\n",
427    "    # objective using breaks concept\n",
428    "    breaks = []\n",
429    "    for t in teams:\n",
430    "        for d in range(num_matchdays - 1):\n",
431    "            breaks.append(\n",
432    "                model.NewBoolVar(\n",
433    "                    'two home or two away for team %i, starting on matchday %i'\n",
434    "                    % (t, d)))\n",
435    "\n",
436    "            model.AddBoolOr([at_home[d][t], at_home[d + 1][t], breaks[-1]])\n",
437    "            model.AddBoolOr(\n",
438    "                [at_home[d][t].Not(), at_home[d + 1][t].Not(), breaks[-1]])\n",
439    "\n",
440    "            model.AddBoolOr(\n",
441    "                [at_home[d][t].Not(), at_home[d + 1][t], breaks[-1].Not()])\n",
442    "            model.AddBoolOr(\n",
443    "                [at_home[d][t], at_home[d + 1][t].Not(), breaks[-1].Not()])\n",
444    "\n",
445    "            # I couldn't figure this out, so I wrote a little program\n",
446    "            # and proved it.  These effectively are identical to\n",
447    "            #\n",
448    "            # model.Add(at_home[d][t] == at_home[d+1][t]).OnlyEnforceIf(breaks[-1])\n",
449    "            # model.Add(at_home[d][t] != at_home[d+1][t]).OnlyEnforceIf(breaks[-1].Not())\n",
450    "            #\n",
451    "            # except they are a little more efficient, I believe.  Wrote it up in a blog post\n",
452    "            #\n",
453    "            # my write-up is at https://activimetrics.com/blog/ortools/cp_sat/addboolor/\n",
454    "\n",
455    "    # constrain breaks\n",
456    "    #\n",
457    "    # Another deviation from the C++ code.  In the C++, the breaks sum is\n",
458    "    # required to be >= (2 * num_teams - 4), which is exactly the number\n",
459    "    # of match days.\n",
460    "    #\n",
461    "    # I said on the mailing list that I didn't know why this was, and\n",
462    "    # Laurent pointed out that there was a known bound.  I looked it\n",
463    "    # up and found some references (see thread at\n",
464    "    # https://groups.google.com/d/msg/or-tools-discuss/ITdlPs6oRaY/FvwgB5LgAQAJ)\n",
465    "    #\n",
466    "    # The reference states that \"schedules with n teams with even n\n",
467    "    # have at least n − 2 breaks\", and further that \"for odd n\n",
468    "    # schedules without any breaks are constructed.\"\n",
469    "    #\n",
470    "    # That research doesn't *quite* apply here, as the authors were\n",
471    "    # assuming a single round-robin tournament\n",
472    "    #.\n",
473    "    # Here there is not an exact round-robin tournament multiple, but\n",
474    "    # still the implication is that the number of breaks cannot be\n",
475    "    # less than the number of matchdays.\n",
476    "    #\n",
477    "    # Literature aside, I'm finding in practice that if you don't have\n",
478    "    # an exact round robin multiple, and if num_matchdays is odd, the\n",
479    "    # best you can do is num_matchdays + 1.  If you have even days,\n",
480    "    # then you can do num_matchdays.  This can be tested for small\n",
481    "    # numbers of teams and days, in which the solver is able to search\n",
482    "    # all possible combinations in a reasonable time limit.\n",
483    "\n",
484    "    optimal_value = matchups * (num_teams - 2)\n",
485    "    if not matchups_exact:\n",
486    "        # fiddle a bit, based on experiments with low values of N and D\n",
487    "        if num_matchdays % 2:\n",
488    "            # odd number of days\n",
489    "            optimal_value = min(num_matchdays + 1, optimal_value)\n",
490    "        else:\n",
491    "            optimal_value = min(num_matchdays, optimal_value)\n",
492    "\n",
493    "    print('expected optimal value is', optimal_value)\n",
494    "    model.Add(sum(breaks) >= optimal_value)\n",
495    "\n",
496    "    model.Minimize(sum(breaks))\n",
497    "    # run the solver\n",
498    "    solver = cp_model.CpSolver()\n",
499    "    solver.parameters.max_time_in_seconds = time_limit\n",
500    "    solver.parameters.log_search_progress = debug\n",
501    "    solver.parameters.num_search_workers = num_cpus\n",
502    "\n",
503    "    # solution_printer = SolutionPrinter() # since we stop at first\n",
504    "    # solution, this isn't really\n",
505    "    # necessary I think\n",
506    "    status = solver.Solve(model)\n",
507    "    print('Solve status: %s' % solver.StatusName(status))\n",
508    "    print('Statistics')\n",
509    "    print('  - conflicts : %i' % solver.NumConflicts())\n",
510    "    print('  - branches  : %i' % solver.NumBranches())\n",
511    "    print('  - wall time : %f s' % solver.WallTime())\n",
512    "\n",
513    "    if status == cp_model.INFEASIBLE:\n",
514    "        return status\n",
515    "\n",
516    "    if status == cp_model.UNKNOWN:\n",
517    "        print('Not enough time allowed to compute a solution')\n",
518    "        print('Add more time using the --timelimit command line option')\n",
519    "        return status\n",
520    "\n",
521    "    print('Optimal objective value: %i' % solver.ObjectiveValue())\n",
522    "\n",
523    "    screen_dump_results(solver, fixtures, num_teams, num_matchdays)\n",
524    "\n",
525    "    if status != cp_model.OPTIMAL and solver.WallTime() >= time_limit:\n",
526    "        print('Please note that solver reached maximum time allowed %i.' %\n",
527    "              time_limit)\n",
528    "        print(\n",
529    "            'A better solution than %i might be found by adding more time using the --timelimit command line option'\n",
530    "            % solver.ObjectiveValue())\n",
531    "\n",
532    "    if csv:\n",
533    "        csv_dump_results(solver, fixtures, num_teams, num_matchdays, csv)\n",
534    "\n",
535    "    # # print break results, to get a clue what they are doing\n",
536    "    # print('Breaks')\n",
537    "    # for b in breaks:\n",
538    "    #     print('  %s is %i' % (b.Name(), solver.Value(b)))\n",
539    "\n",
540    "\n",
541    "\"\"\"Entry point of the program.\"\"\"\n",
542    "parser = argparse.ArgumentParser(\n",
543    "    description='Solve sports league match play assignment problem')\n",
544    "parser.add_argument('-t,--teams',\n",
545    "                    type=int,\n",
546    "                    dest='num_teams',\n",
547    "                    default=10,\n",
548    "                    help='Number of teams in the league')\n",
549    "\n",
550    "parser.add_argument(\n",
551    "    '-d,--days',\n",
552    "    type=int,\n",
553    "    dest='num_matchdays',\n",
554    "    default=2 * 10 - 2,\n",
555    "    help=\n",
556    "    'Number of days on which matches are played.  Default is enough days such that every team can play every other team, or (number of teams - 1)'\n",
557    ")\n",
558    "\n",
559    "parser.add_argument(\n",
560    "    '--matches_per_day',\n",
561    "    type=int,\n",
562    "    dest='num_matches_per_day',\n",
563    "    default=10 - 1,\n",
564    "    help=\n",
565    "    'Number of matches played per day.  Default is number of teams divided by 2.  If greater than the number of teams, then this implies some teams will play each other more than once.  In that case, home and away should alternate between the teams in repeated matchups.'\n",
566    ")\n",
567    "\n",
568    "parser.add_argument(\n",
569    "    '--csv',\n",
570    "    type=str,\n",
571    "    dest='csv',\n",
572    "    default='output.csv',\n",
573    "    help='A file to dump the team assignments.  Default is output.csv')\n",
574    "\n",
575    "parser.add_argument(\n",
576    "    '--timelimit',\n",
577    "    type=int,\n",
578    "    dest='time_limit',\n",
579    "    default=60,\n",
580    "    help='Maximum run time for solver, in seconds.  Default is 60 seconds.')\n",
581    "\n",
582    "parser.add_argument(\n",
583    "    '--cpu',\n",
584    "    type=int,\n",
585    "    dest='cpu',\n",
586    "    help=\n",
587    "    'Number of workers (CPUs) to use for solver.  Default is 6 or number of CPUs available, whichever is lower'\n",
588    ")\n",
589    "\n",
590    "parser.add_argument('--debug',\n",
591    "                    action='store_true',\n",
592    "                    help=\"Turn on some print statements.\")\n",
593    "\n",
594    "parser.add_argument(\n",
595    "    '--max_home_stand',\n",
596    "    type=int,\n",
597    "    dest='max_home_stand',\n",
598    "    default=2,\n",
599    "    help=\n",
600    "    \"Maximum consecutive home or away games.  Default to 2, which means three home or away games in a row is forbidden.\"\n",
601    ")\n",
602    "\n",
603    "args = parser.parse_args()\n",
604    "\n",
605    "# set default for num_matchdays\n",
606    "num_matches_per_day = args.num_matches_per_day\n",
607    "if not num_matches_per_day:\n",
608    "    num_matches_per_day = args.num_teams // 2\n",
609    "ncpu = 8\n",
610    "try:\n",
611    "    ncpu = len(os.sched_getaffinity(0))\n",
612    "except AttributeError:\n",
613    "    pass\n",
614    "cpu = args.cpu\n",
615    "if not cpu:\n",
616    "    cpu = min(6, ncpu)\n",
617    "    print('Setting number of search workers to %i' % cpu)\n",
618    "\n",
619    "if cpu > ncpu:\n",
620    "    print(\n",
621    "        'You asked for %i workers to be used, but the os only reports %i CPUs available.  This might slow down processing'\n",
622    "        % (cpu, ncpu))\n",
623    "\n",
624    "if cpu != 6:\n",
625    "    # don't whinge at user if cpu is set to 6\n",
626    "    if cpu < ncpu:\n",
627    "        print(\n",
628    "            'Using %i workers, but there are %i CPUs available.  You might get faster results by using the command line option --cpu %i, but be aware ORTools CP-SAT solver is tuned to 6 CPUs'\n",
629    "            % (cpu, ncpu, ncpu))\n",
630    "\n",
631    "    if cpu > 6:\n",
632    "        print(\n",
633    "            'Using %i workers.  Be aware ORTools CP-SAT solver is tuned to 6 CPUs'\n",
634    "            % cpu)\n",
635    "\n",
636    "# assign_matches()\n",
637    "assign_matches(args.num_teams, args.num_matchdays, num_matches_per_day,\n",
638    "               args.max_home_stand, args.time_limit, cpu, args.csv,\n",
639    "               args.debug)\n",
640    "\n"
641   ]
642  }
643 ],
644 "metadata": {},
645 "nbformat": 4,
646 "nbformat_minor": 5
647}
648