<commit_before>#!/anaconda/bin/python

"""
reqs
1. generate teams based on the following constraints
    -all teams get same number of games
    -reffing is as balanced as possible
    -reffing must before or after play
    -cannot play/ref in more than one court at a time

stretch objectives:
1. minimize:
    -[will require names to teams] downtime between activities (play or ref)
Design:
Objects:
    Facility[1] - retains data on where can be played
        days[days-per-schedule] - distinct days in the schedule
            slots[division] - array of court-time tuples for that division
            sheet[court][time] - array of game slots in sheet form
    Division[1] - total set of teams in the league
        teams[teams-per-division] - team object in league
            contains team history (times reffed, times played another team,etc)
    Schedule[1] - retain a specific schedule
        days[days-per-season] - contains games for a given day
            contains team, ref, division, court and time data for all games
    where does games-per-team go?
"""

from pprint import pprint

def epoch_now():
    '''this is a simple utility for getting the current epoch'''
    from calendar import timegm
    from time import gmtime
    return timegm(gmtime())

def compare_days(day1, day2):
    ''' this is a debugging and reporting tool'''
    print()
    for time in range(len(day1.courts[0])):
        game1_sum = ""
        game2_sum = ""
        for court in range(len(day1.courts)):
            game1 = day1.courts[court][time]
            game2 = day2.courts[court][time]
            game1_sum += game1.small_str()
            game2_sum += game2.small_str()
        print(game1_sum + "  " + game2_sum)

def make_schedule(team_counts, league, sch_tries=500, seed=None):
    from schedule import Schedule
    from random import choice, randrange
    import random
    import pickle
    import datetime
    facilities = league.days
    if seed != None:
        random.seed(seed)
    start = epoch_now()
    sch = Schedule(league, team_counts, facilities)
    sch.seed = seed
    for mut_idx in range(sch_tries):
        if True:
            if sch.daycount > 1:
                target1 = randrange(sch.daycount)
                target2 = (target1 + randrange(sch.daycount-1)) % sch.daycount
                target = [target1, target2]
                sch.try_remake_days(target)
            target = [randrange(sch.daycount)]
            fitness = sch.try_remake_days(target)
        if True:
            count = 1  # randrange(2)
            fitness = sch.remake_worst_day(count)
        # debug logic
        breakdown = sch.new_fitness_error_breakdown()
        print("value = %s while on mutation step %s: %s %s" %
              (fitness, mut_idx, sch.new_fitness_div_list(), breakdown))
        if (fitness == 0):
            print("correct schedule found!!!!!")
            break
    fitness = sch.fitness(league.games_per_div)
    end = epoch_now()
    print("total run time was %s second" % (float(end - start)))
    path = '/Users/coulter/Desktop/life_notes/2016_q1/scvl/'
    # todo, change this to use print(datetime.datetime.now().date())
    tag = '2016-07-18a_'
    sch.gen_csv(path + tag + "simple.csv")
    sch.write_audit_file(path + tag + "audit_2016_spr.csv")
    sch_py_obj = path + tag + 'python_file_obj.pickle'
    with open(sch_py_obj,'wb') as sch_file:
        pickle.dump(sch, sch_file)
    return sch

def make_regular_season(team_counts, ndays=9, sch_tries=500, seed=1):
    from facility import SCVL_Facility_Day
    from facility import League
    days = []
    import random
    random.seed(seed)
    league = League(ndivs=4, ndays=ndays, ncourts=5, ntimes=4,
                    team_counts=team_counts, day_type=SCVL_Facility_Day)
    if True:  # qwer  True   False
        for idx, day in enumerate(league.days):
            from facility import csv_str_to_fac_list_list, Facility_Day
            csv_str = day.csv()
            new_arrays = csv_str_to_fac_list_list(csv_str)
            new_day = Facility_Day(team_counts, csv_obj=new_arrays)
            league.days[idx] = new_day


    league.debug_print()
    sch = make_schedule(team_counts, league, sch_tries=sch_tries)
    return sch

def make_round_robin(team_counts, sch_tries=500, seed=1, save_progress=False,
                     total_sch=2):
    from facility import SCVL_Round_Robin, League<commit_msg>removing code to test that days can be sent to and from csv<commit_after>#!/anaconda/bin/python

"""
reqs
1. generate teams based on the following constraints
    -all teams get same number of games
    -reffing is as balanced as possible
    -reffing must before or after play
    -cannot play/ref in more than one court at a time

stretch objectives:
1. minimize:
    -[will require names to teams] downtime between activities (play or ref)
Design:
Objects:
    Facility[1] - retains data on where can be played
        days[days-per-schedule] - distinct days in the schedule
            slots[division] - array of court-time tuples for that division
            sheet[court][time] - array of game slots in sheet form
    Division[1] - total set of teams in the league
        teams[teams-per-division] - team object in league
            contains team history (times reffed, times played another team,etc)
    Schedule[1] - retain a specific schedule
        days[days-per-season] - contains games for a given day
            contains team, ref, division, court and time data for all games
    where does games-per-team go?
"""

from pprint import pprint

def epoch_now():
    '''this is a simple utility for getting the current epoch'''
    from calendar import timegm
    from time import gmtime
    return timegm(gmtime())

def compare_days(day1, day2):
    ''' this is a debugging and reporting tool'''
    print()
    for time in range(len(day1.courts[0])):
        game1_sum = ""
        game2_sum = ""
        for court in range(len(day1.courts)):
            game1 = day1.courts[court][time]
            game2 = day2.courts[court][time]
            game1_sum += game1.small_str()
            game2_sum += game2.small_str()
        print(game1_sum + "  " + game2_sum)

def make_schedule(team_counts, league, sch_tries=500, seed=None):
    from schedule import Schedule
    from random import choice, randrange
    import random
    import pickle
    import datetime
    facilities = league.days
    if seed != None:
        random.seed(seed)
    start = epoch_now()
    sch = Schedule(league, team_counts, facilities)
    sch.seed = seed
    for mut_idx in range(sch_tries):
        if True:
            if sch.daycount > 1:
                target1 = randrange(sch.daycount)
                target2 = (target1 + randrange(sch.daycount-1)) % sch.daycount
                target = [target1, target2]
                sch.try_remake_days(target)
            target = [randrange(sch.daycount)]
            fitness = sch.try_remake_days(target)
        if True:
            count = 1  # randrange(2)
            fitness = sch.remake_worst_day(count)
        # debug logic
        breakdown = sch.new_fitness_error_breakdown()
        print("value = %s while on mutation step %s: %s %s" %
              (fitness, mut_idx, sch.new_fitness_div_list(), breakdown))
        if (fitness == 0):
            print("correct schedule found!!!!!")
            break
    fitness = sch.fitness(league.games_per_div)
    end = epoch_now()
    print("total run time was %s second" % (float(end - start)))
    path = '/Users/coulter/Desktop/life_notes/2016_q1/scvl/'
    # todo, change this to use print(datetime.datetime.now().date())
    tag = '2016-07-18a_'
    sch.gen_csv(path + tag + "simple.csv")
    sch.write_audit_file(path + tag + "audit_2016_spr.csv")
    sch_py_obj = path + tag + 'python_file_obj.pickle'
    with open(sch_py_obj,'wb') as sch_file:
        pickle.dump(sch, sch_file)
    return sch

def make_regular_season(team_counts, ndays=9, sch_tries=500, seed=1):
    from facility import SCVL_Facility_Day
    from facility import League
    days = []
    import random
    random.seed(seed)
    league = League(ndivs=4, ndays=ndays, ncourts=5, ntimes=4,
                    team_counts=team_counts, day_type=SCVL_Facility_Day)
    league.debug_print()
    sch = make_schedule(team_counts, league, sch_tries=sch_tries)
    return sch

def make_round_robin(team_counts, sch_tries=500, seed=1, save_progress=False,
                     total_sch=2):
    from facility import SCVL_Round_Robin, League
{'fraction_non_alphanumeric': 0.09318155146109612, 'fraction_numerical': 0.015256425302194579, 'mean_word_length': 2.6217594560135997, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1732343', 'n_tokens_mistral': 2718, 'n_tokens_neox': 2632, 'n_words': 761}