<s> import tab_logic <EOL> from mittab . apps . tab . models import * <EOL> import mwmatching <EOL> import random <EOL> def perfect_pairing ( list_of_teams ) : <EOL> """<STR_LIT>""" <EOL> graph_edges = [ ] <EOL> for i in range ( len ( list_of_teams ) ) : <EOL> for j in range ( len ( list_of_teams ) ) : <EOL> if i > j : <EOL> wt = calc_weight ( list_of_teams [ i ] , <EOL> list_of_teams [ j ] , i , j , <EOL> list_of_teams [ len ( list_of_teams ) - i - <NUM_LIT:1> ] , <EOL> list_of_teams [ len ( list_of_teams ) - j - <NUM_LIT:1> ] , <EOL> len ( list_of_teams ) - i - <NUM_LIT:1> , <EOL> len ( list_of_teams ) - j - <NUM_LIT:1> ) <EOL> graph_edges += [ ( i , j , wt ) ] <EOL> pairings_num = mwmatching . maxWeightMatching ( graph_edges , maxcardinality = True ) <EOL> all_pairs = [ ] <EOL> for pair in pairings_num : <EOL> if pair < len ( list_of_teams ) : <EOL> if [ list_of_teams [ pairings_num . index ( pair ) ] , list_of_teams [ pair ] ] not in all_pairs and [ list_of_teams [ pair ] , list_of_teams [ pairings_num . index ( pair ) ] ] not in all_pairs : <EOL> all_pairs += [ [ list_of_teams [ pairings_num . index ( pair ) ] , list_of_teams [ pair ] ] ] <EOL> return determine_gov_opp ( all_pairs ) <EOL> def calc_weight ( team_a , <EOL> team_b , <EOL> team_a_ind , <EOL> team_b_ind , <EOL> team_a_opt , <EOL> team_b_opt , <EOL> team_a_opt_ind , <EOL> team_b_opt_ind ) : <EOL> """<STR_LIT>""" <EOL> all_settings = dict ( [ ( ts . key , ts . value ) for ts in TabSettings . objects . all ( ) ] ) <EOL> def try_get ( key , default = None ) : <EOL> try : <EOL> return int ( all_settings [ key ] ) <EOL> except : <EOL> return default <EOL> current_round = try_get ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> tot_rounds = try_get ( "<STR_LIT>" , <NUM_LIT:5> ) <EOL> power_pairing_multiple = try_get ( "<STR_LIT>" , - <NUM_LIT:1> ) <EOL> high_opp_penalty = try_get ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> high_gov_penalty = try_get ( "<STR_LIT>" , - <NUM_LIT:100> ) <EOL> high_high_opp_penalty = try_get ( "<STR_LIT>" , - <NUM_LIT:10> ) <EOL> same_school_penalty = try_get ( "<STR_LIT>" , - <NUM_LIT:1000> ) <EOL> hit_pull_up_before = try_get ( "<STR_LIT>" , - <NUM_LIT> ) <EOL> hit_team_before = try_get ( "<STR_LIT>" , - <NUM_LIT> ) <EOL> if current_round == <NUM_LIT:1> : <EOL> wt = power_pairing_multiple * ( abs ( team_a_opt . seed - team_b . seed ) + abs ( team_b_opt . seed - team_a . seed ) ) / <NUM_LIT> <EOL> else : <EOL> wt = power_pairing_multiple * ( abs ( team_a_opt_ind - team_b_ind ) + abs ( team_b_opt_ind - team_a_ind ) ) / <NUM_LIT> <EOL> half = int ( tot_rounds / <NUM_LIT:2> ) + <NUM_LIT:1> <EOL> if tab_logic . num_opps ( team_a ) >= half and tab_logic . num_opps ( team_b ) >= half : <EOL> wt += high_opp_penalty <EOL> if tab_logic . num_opps ( team_a ) >= half + <NUM_LIT:1> and tab_logic . num_opps ( team_b ) >= half + <NUM_LIT:1> : <EOL> wt += high_high_opp_penalty <EOL> if tab_logic . num_govs ( team_a ) >= half and tab_logic . num_govs ( team_b ) >= half : <EOL> wt += high_gov_penalty <EOL> if team_a . school == team_b . school : <EOL> wt += same_school_penalty <EOL> if ( tab_logic . hit_pull_up ( team_a ) and tab_logic . tot_wins ( team_b ) < tab_logic . tot_wins ( team_a ) ) or ( tab_logic . hit_pull_up ( team_b ) and tab_logic . tot_wins ( team_a ) < tab_logic . tot_wins ( team_b ) ) : <EOL> wt += hit_pull_up_before <EOL> if tab_logic . hit_before ( team_a , team_b ) : <EOL> wt += hit_team_before <EOL> return wt <EOL> def listToPair ( listOfTeams ) : <EOL> pair = [ None ] * ( len ( listOfTeams ) / <NUM_LIT:2> ) <EOL> for i in range ( len ( listOfTeams ) / <NUM_LIT:2> ) : <EOL> pair [ i ] = [ listOfTeams [ i ] , listOfTeams [ ( len ( listOfTeams ) - <NUM_LIT:1> - i ) ] ] <EOL> return pair <EOL> def totPairs ( teams ) : <EOL> tot = <NUM_LIT:1> <EOL> for i in range ( len ( teams ) ) : <EOL> if i / <NUM_LIT:2> != i / <NUM_LIT> : <EOL> tot *= i <EOL> return tot <EOL> def determine_gov_opp ( all_pairs ) : <EOL> final_pairings = [ ] <EOL> for p in all_pairs : <EOL> if tab_logic . num_govs ( p [ <NUM_LIT:0> ] ) < tab_logic . num_govs ( p [ <NUM_LIT:1> ] ) : <EOL> final_pairings += [ [ p [ <NUM_LIT:0> ] , p [ <NUM_LIT:1> ] ] ] <EOL> elif tab_logic . num_govs ( p [ <NUM_LIT:1> ] ) < tab_logic . num_govs ( p [ <NUM_LIT:0> ] ) : <EOL> final_pairings += [ [ p [ <NUM_LIT:1> ] , p [ <NUM_LIT:0> ] ] ] <EOL> elif tab_logic . num_opps ( p [ <NUM_LIT:0> ] ) < tab_logic . num_opps ( p [ <NUM_LIT:1> ] ) : <EOL> final_pairings += [ [ p [ <NUM_LIT:1> ] , p [ <NUM_LIT:0> ] ] ] <EOL> elif tab_logic . num_opps ( p [ <NUM_LIT:1> ] ) < tab_logic . num_opps ( p [ <NUM_LIT:0> ] ) : <EOL> final_pairings += [ [ p [ <NUM_LIT:0> ] , p [ <NUM_LIT:1> ] ] ] <EOL> elif random . randint ( <NUM_LIT:0> , <NUM_LIT:1> ) == <NUM_LIT:0> : <EOL> final_pairings += [ [ p [ <NUM_LIT:0> ] , p [ <NUM_LIT:1> ] ] ] <EOL> else : <EOL> final_pairings += [ [ p [ <NUM_LIT:1> ] , p [ <NUM_LIT:0> ] ] ] <EOL> return final_pairings </s>
