324 lines
14 KiB
Python
Executable File
324 lines
14 KiB
Python
Executable File
# %%
|
||
PROJECT_PATH = '/home/md/Work/ligalytics/leagues_develop/'
|
||
import os, sys
|
||
sys.path.insert(0, PROJECT_PATH)
|
||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "leagues.settings")
|
||
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
|
||
|
||
from leagues import settings
|
||
settings.DATABASES['default']['NAME'] = PROJECT_PATH+'/db.sqlite3'
|
||
# settings.DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2'
|
||
# settings.DATABASES['default']['HOST'] = '0.0.0.0'
|
||
# settings.DATABASES['default']['PORT'] = '5432'
|
||
# settings.DATABASES['default']['USER'] = 'postgres'
|
||
# settings.DATABASES['default']['PASSWORD'] = 'secret123'
|
||
# settings.DATABASES['default']['NAME'] = 'mypgsqldb'
|
||
# settings.DATABASES['default']['ATOMIC_REQUESTS'] = False
|
||
# settings.DATABASES['default']['AUTOCOMMIT'] = True
|
||
# settings.DATABASES['default']['CONN_MAX_AGE'] = 0
|
||
# settings.DATABASES['default']['CONN_HEALTH_CHECKS'] = False
|
||
# settings.DATABASES['default']['OPTIONS'] = {}
|
||
|
||
os.environ["XPRESSDIR"] = "/opt/xpressmp"
|
||
os.environ["XPRESS"] = "/opt/xpressmp/bin"
|
||
os.environ["LD_LIBRARY_PATH"] = os.environ["XPRESSDIR"] + "/lib"
|
||
os.environ["DYLD_LIBRARY_PATH"] = os.environ["XPRESSDIR"] + "/lib"
|
||
os.environ["SHLIB_PATH"] = os.environ["XPRESSDIR"] + "/lib"
|
||
os.environ["LIBPATH"] = os.environ["XPRESSDIR"] + "/lib"
|
||
os.environ["PYTHONPATH"] = os.environ["XPRESSDIR"] + "/lib"
|
||
os.environ["CLASSPATH"] = os.environ["XPRESSDIR"] + "/lib/xprs.jar"
|
||
os.environ["CLASSPATH"] = os.environ["XPRESSDIR"] + "/lib/xprb.jar" + os.pathsep + os.environ["CLASSPATH"]
|
||
os.environ["CLASSPATH"] = os.environ["XPRESSDIR"] + "/lib/xprm.jar" + os.pathsep + os.environ["CLASSPATH"]
|
||
os.environ["PATH"] = os.environ["XPRESSDIR"] + "/bin" + os.pathsep + os.environ["PATH"]
|
||
|
||
|
||
import django
|
||
django.setup()
|
||
|
||
from scheduler.models import *
|
||
import pulp
|
||
from pulp import lpSum, value, XPRESS, GUROBI, PULP_CBC_CMD
|
||
from django.db.models import Q
|
||
from django.template.loader import render_to_string
|
||
|
||
from qualifiers.models import *
|
||
from common.models import GlobalTeam, GlobalCountry
|
||
from scheduler.models import Season, Scenario, Team, Day, CountryClash, Country
|
||
|
||
from qualifiers.draws import groupTeams, optimize_inversions4
|
||
from scheduler.solver.tasks.optimize import optimize
|
||
from common.functions import distanceInMilesByGPS
|
||
import datetime
|
||
|
||
|
||
from referees.helpers import use_referees, import_deb_delegates, seed_rounds_from_days, generate_distance_matrix
|
||
|
||
from referees.models import *
|
||
|
||
import random
|
||
import time
|
||
import json
|
||
import csv
|
||
import networkx as nx
|
||
import matplotlib.pyplot as plt
|
||
from dateutil import parser
|
||
from datetime import timedelta
|
||
import googlemaps
|
||
import pandas as pd
|
||
# %%
|
||
|
||
gmaps = googlemaps.Client(key='AIzaSyB76EhR4OqjdXHQUiTkHZC0Svx_7cPGqyU')
|
||
scenario = Scenario.objects.get(id=4)
|
||
season= scenario.season
|
||
|
||
# %%
|
||
|
||
|
||
# def seed_stadiums():
|
||
|
||
# df = pd.read_excel('referees/fixtures/wales/List_of_stadiums1700843463152.xlsx')
|
||
|
||
# Location.objects.filter(season=scenario.season).delete()
|
||
# for index, row in df.iterrows():
|
||
# name = row['Name']
|
||
# country = row['Country']
|
||
# city= row['Place']
|
||
# longitude = row['invisible_label_longitude']
|
||
# latitude = row['invisible_label_latitude']
|
||
# country_obj = Country.objects.filter(season=scenario.season,name=country).first()
|
||
|
||
# if not pd.isna(row['invisible_label_latitude']) and not pd.isna(row['invisible_label_longitude']):
|
||
# Location.objects.create(name=row['Name'],city=city,country=country_obj, latitude=row['invisible_label_latitude'], longitude=row['invisible_label_longitude'], season=season)
|
||
# else:
|
||
# geocode_result = gmaps.geocode(f"{country}+{city}+{name}")
|
||
# if len(geocode_result) > 0:
|
||
# lat = geocode_result[0]['geometry']['location']['lat']
|
||
# lng = geocode_result[0]['geometry']['location']['lng']
|
||
# Location.objects.create(name=row['Name'],city=city,country=country_obj, latitude=lat, longitude=lng, season=season)
|
||
|
||
# seed_stadiums()
|
||
# %%
|
||
|
||
# def seed_delegates():
|
||
# df = pd.read_excel('referees/fixtures/wales/Anonymised Referee Registrations for Ligalytics.xlsx')
|
||
# country = Country.objects.get(season=season,name='Wales')
|
||
|
||
# Delegate.objects.filter(season=season).delete()
|
||
# Role.objects.filter(season=season).delete()
|
||
# Role.objects.create(order=1,name='Referee', season=season)
|
||
# Role.objects.create(order=2,name='Assistant Referee 1', season=season)
|
||
# Role.objects.create(order=3,name='Assistant Referee 2', season=season)
|
||
# Role.objects.create(order=4,name='Fourth Official', season=season)
|
||
# for index, row in df.iterrows():
|
||
# name = row['Dummy ID']
|
||
# city = row['Home Town']
|
||
# category = row['Refеree category']
|
||
|
||
|
||
# cat = Category.objects.filter(name=category, season=season).first()
|
||
# if not cat:
|
||
# cat = Category.objects.create(name=category, season=season)
|
||
|
||
# location = Location.objects.filter(name=city, country=country).first()
|
||
|
||
# if not location:
|
||
# geocode_result = gmaps.geocode(f"{country}+{city}")
|
||
|
||
# if len(geocode_result) > 0:
|
||
# lat = geocode_result[0]['geometry']['location']['lat']
|
||
# lng = geocode_result[0]['geometry']['location']['lng']
|
||
# location = Location.objects.create(name=city,city=city,country=country, latitude=lat, longitude=lng, season=season, type=3)
|
||
# else:
|
||
# print("No geocode for ", city)
|
||
|
||
|
||
# # role = Role.objects.filter(season=season,name=category).first()
|
||
# # if not role:
|
||
# # role = Role.objects.create(name=category, season=season)
|
||
|
||
# delegate = Delegate.objects.create(name=name, country=country, location=location, category=cat, season=season)
|
||
|
||
# seed_delegates()
|
||
# %%
|
||
|
||
# def seed_teams_and_games():
|
||
# matches = pd.read_excel('referees/fixtures/wales/v2/Matches Test December 2023.xlsx') #.set_index('Name')
|
||
# country = Country.objects.get(season=season,name='Wales')
|
||
# Team.objects.filter(season=season).delete()
|
||
# Day.objects.filter(season=season).delete()
|
||
# Round.objects.filter(season=season).delete()
|
||
# Game.objects.filter(season=season).delete()
|
||
# RefGame.objects.filter(scenario=scenario).delete()
|
||
# Competition.objects.filter(season=season).delete()
|
||
# for index, row in matches.iterrows():
|
||
# if pd.isna(row['ID']):
|
||
# continue
|
||
|
||
# id = row['ID']
|
||
# descr = row['Match description']
|
||
# home = row['Home team']
|
||
# away = row['Away team']
|
||
|
||
# date_format = '%d.%m.%Y %H:%M:%S'
|
||
# matchdate = datetime.datetime.strptime(row['Match date (GMT)'], date_format)
|
||
# competition_name = row['Name']
|
||
# rank = row['Rank']
|
||
# round = row['Round']
|
||
# facility = row['Facility']
|
||
# place = row['Facility place name']
|
||
# if pd.isna(rank):
|
||
# rank = 0
|
||
|
||
# if len(descr.split('-')) > 4:
|
||
# print("No team for ", descr)
|
||
# continue
|
||
|
||
# competition = Competition.objects.filter(name=competition_name, season=season).first()
|
||
# if not competition:
|
||
# competition = Competition.objects.create(name=competition_name, season=season, rank=rank)
|
||
|
||
# location = Location.objects.filter(name=facility, season=season).first()
|
||
# if not location:
|
||
# print("No location for ", facility)
|
||
# geocode_result = gmaps.geocode(f"{country}+{place}+{facility}")
|
||
|
||
# if len(geocode_result) > 0:
|
||
# lat = geocode_result[0]['geometry']['location']['lat']
|
||
# lng = geocode_result[0]['geometry']['location']['lng']
|
||
# location = Location.objects.create(name=facility,city=place,country=country, latitude=lat, longitude=lng, season=season, type=1)
|
||
# else:
|
||
# print("No geocode for ", facility)
|
||
|
||
|
||
# matchdescr = descr.split(':')[0][:-1].strip()
|
||
|
||
# homeTeam = Team.objects.filter(name=matchdescr.split('-')[0], season=season).first()
|
||
# if not homeTeam:
|
||
# homeTeam = Team.objects.create(name=matchdescr.split('-')[0],shortname=home, country=Country.objects.get(season=season,name='Wales'), location=location,latitude=location.latitude,longitude=location.longitude, season=season)
|
||
# awayTeam = Team.objects.filter(name=matchdescr.split('-')[1], season=season).first()
|
||
# if not awayTeam:
|
||
# awayTeam =Team.objects.create(name=matchdescr.split('-')[1],shortname=away, country=Country.objects.get(season=season,name='Wales'), season=season)
|
||
|
||
# day = Day.objects.filter(date=matchdate.date(), day=matchdate.strftime('%Y-%m-%d'), round=1, season=season).first()
|
||
# if not day:
|
||
# day = Day.objects.create(date=matchdate.date(), day=matchdate.strftime('%Y-%m-%d'), round=1, season=season)
|
||
|
||
# timeslot = TimeSlot.objects.filter(start=matchdate.time(), season=season).first()
|
||
# if not timeslot:
|
||
# timeslot = TimeSlot.objects.create(start=matchdate.time(), end=(matchdate+timedelta(hours=2)).time(), season=season, name=matchdate.time().strftime("%H%M"))
|
||
|
||
|
||
# roundObj = Round.objects.filter(name=round,number=round, season=season).first()
|
||
# if not roundObj:
|
||
# roundObj = Round.objects.create(name=round, number=round, season=season)
|
||
# roundObj.days.add(day)
|
||
|
||
|
||
# game = RefGame.objects.filter(location=location,round=round, homeTeam=homeTeam, awayTeam=awayTeam,scenario=scenario, competition=competition).first()
|
||
# if not game:
|
||
# RefGame.objects.create(location=location,round=round, homeTeam=homeTeam, awayTeam=awayTeam, day=day,timeslot=timeslot, scenario=scenario, competition=competition)
|
||
|
||
# seed_teams_and_games()
|
||
|
||
# # %%
|
||
# def seed_requirements():
|
||
# requirements = pd.read_excel('referees/fixtures/wales/v2/Competition Ranks.xlsx',skiprows=1)
|
||
|
||
# Requirement.objects.filter(season=season).delete()
|
||
# for index, row in requirements.iterrows():
|
||
# comp = row['Competition Name']
|
||
# rank = row['Competition Rank']
|
||
# ref = row['Referee']
|
||
# ref_cats = row['Category Required to Appoint As Ref']
|
||
# assistant1 = row['Assistant Referee 1']
|
||
# assistant2 = row['Assistant Referee 2']
|
||
# assistant_cats = row['Category Required to appoint as Assistant Referee']
|
||
# official = row['Fourth Official']
|
||
# official_cats = row['Category Required to Appoint as Fourth Official']
|
||
# max_dist = row['Maximum Distance for 1 Match Official to Travel (Miles)']
|
||
|
||
# try:
|
||
# max_dist = float(max_dist)
|
||
# except:
|
||
# max_dist = 0.0
|
||
|
||
# ref_categories = []
|
||
# if not pd.isna(ref_cats):
|
||
# ref_categories = [
|
||
# Category.objects.filter(name=cat, season=season).first() for cat in ref_cats.split(';')
|
||
# ]
|
||
|
||
# assistant_categories = []
|
||
# if not pd.isna(assistant_cats):
|
||
# assistant_categories = [
|
||
# Category.objects.filter(name=cat, season=season).first() for cat in assistant_cats.split(';')
|
||
# ]
|
||
|
||
# official_categories = []
|
||
# if not pd.isna(official_cats):
|
||
# official_categories = [
|
||
# Category.objects.filter(name=cat, season=season).first() for cat in official_cats.split(';')
|
||
# ]
|
||
|
||
|
||
# competition = Competition.objects.filter(name=comp, season=season).first()
|
||
|
||
# if competition:
|
||
# competition.rank = rank
|
||
# competition.save()
|
||
# if ref_categories:
|
||
# req = Requirement.objects.create(season=season, competition=competition, role=Role.objects.get(name='Referee', season=season), max_distance=max_dist)
|
||
# for cat in ref_categories:
|
||
# req.categories.add(cat)
|
||
|
||
# if assistant_categories:
|
||
# req1 = Requirement.objects.create(season=season, competition=competition, role=Role.objects.get(name='Assistant Referee 1', season=season), max_distance=max_dist)
|
||
# req2 = Requirement.objects.create(season=season, competition=competition, role=Role.objects.get(name='Assistant Referee 2', season=season), max_distance=max_dist)
|
||
# for cat in assistant_categories:
|
||
# req1.categories.add(cat)
|
||
# req2.categories.add(cat)
|
||
|
||
# if official_categories:
|
||
# req = Requirement.objects.create(season=season, competition=competition, role=Role.objects.get(name='Fourth Official', season=season), max_distance=max_dist)
|
||
# for cat in official_categories:
|
||
# req.categories.add(cat)
|
||
|
||
|
||
|
||
# seed_requirements()
|
||
|
||
|
||
# %%
|
||
|
||
|
||
|
||
availabilities = pd.read_excel('referees/fixtures/wales/Anonymised Referee Unavailability for Ligalytics.xlsx')
|
||
|
||
# %%
|
||
availabilities['Date from'] = pd.to_datetime(availabilities['Date from'], format='%d.%m.%Y', errors='coerce')
|
||
availabilities['Date to'] = pd.to_datetime(availabilities['Date to'], format='%d.%m.%Y', errors='coerce')
|
||
|
||
|
||
# %%
|
||
|
||
# %%
|
||
DelBlocking.objects.filter(season=season).delete()
|
||
for index, row in availabilities.iterrows():
|
||
start = row['Date from']
|
||
end = row['Date to']
|
||
dummy = row['Dummy ID']
|
||
if pd.isna(end):
|
||
end = start
|
||
if start >= datetime.datetime(2023,12,1) and end <= datetime.datetime(2024,1,1):
|
||
for i in range((end-start).days+1):
|
||
day = start + datetime.timedelta(days=i)
|
||
available = (row[day.weekday()+5] == 'X')
|
||
if not available:
|
||
delegate = Delegate.objects.filter(name=f"Person {dummy}", season=season).first()
|
||
dayObj = Day.objects.filter(day=day.strftime('%Y-%m-%d'), season=season).first()
|
||
if delegate and dayObj:
|
||
DelBlocking.objects.create(delegate=delegate, day=dayObj, season=season)
|
||
print(f"Blocked {delegate} on {dayObj}")
|
||
|
||
# %%
|