View Single Post
  #112   Spotlight this post!  
Unread 21-04-2016, 18:06
sdangelo sdangelo is offline
Registered User
FTC #10842 (Train of Thought)
Team Role: Alumni
 
Join Date: Sep 2015
Rookie Year: 2009
Location: State College, PA
Posts: 40
sdangelo has a spectacular aura aboutsdangelo has a spectacular aura about
Re: The 2056 Streak has ended!

Quote:
Originally Posted by Richard Wallace View Post
Can anyone provide a script to calculate OP numbers for all currently active FRC teams?*
Here's what I have, in Python. I've also never used an API before, so huge thank you to plnyyanks for posting his breach & capture rates code and for helping me figure out an error.

I separated out the code for making the list of winning alliances simply because it was too much of a hassle to run every time I needed to test something with the algorithm:
Spoiler for TeamListWriter.py:
Code:
import json
import urllib2

BASE_URL = 'http://www.thebluealliance.com/api/v2/'
APP_HEADER = 'X-TBA-App-Id'
APP_ID = 'sdangelo:sixdegrees:v0.1'

def fetch_endpoint(endpoint):
    #Checks Blue Alliance for data
    full_url = BASE_URL + endpoint
    url = urllib2.Request(full_url, headers={APP_HEADER: APP_ID, 'User-agent': 'Mozilla/5.0'})
    response = urllib2.urlopen(url)
    return json.loads(response.read())


def fetch_event_keys_in_year(year):
    #Finds all event keys for a single year
    api_events = fetch_endpoint("events/" + str(year))
    return [event["key"] for event in api_events]


def fetch_event_winners(event_key):
    #builds list of single event winners
    event_winners = []
    awardees = fetch_endpoint("event/"+ str(event_key) + "/awards")
    for award in awardees:
        if award["award_type"] == 1:
            for team in award["recipient_list"]:
                event_winners.append(team["team_number"])            
    print "wrote" + str(event_key) + "winners: "
    print event_winners
    return event_winners

def alliance_list():
    #Builds list of event keys from every year        
    allWinners = open('AllWinners.txt', 'a')
    event_keys = []
    for year in range(1992, 2017):
        yearly_keys = fetch_event_keys_in_year(year)
        for key in yearly_keys:
            event_keys.append(key)
        print "Found" + str(year) + "events"

    #writes list of all winners
    for event_key in event_keys:
        event_winners = fetch_event_winners(event_key)
        string = ""
        for winner in event_winners:
            string += str(winner) + ","
        if str != "" and str != ",":
            string = string[:-1]
            string += "\n"
            allWinners.write(string)
    allWinners.close()

if __name__ == "__main__":
    #makes file of all winners
    alliance_list()
    print "Done"

I wasn't sure if I was supposed to be writing something to find the OP numbers for every team, or for any team, so I did both. And I only ran the every team code with all the teams who had ever won a regional, because it was easier and any other teams obviously won't have an OP number.

Spoiler for ListAllTeams.py:
Code:
def get_alliances(filename):
    allAlliances = open(filename, 'r')
    alliancelist = [line.rstrip('\n').split(",") for line in allAlliances]
    allAlliances.close()
    return alliancelist

def get_winners(alliances):
    team_list = []
    for competition in alliances:
        for team in competition:
            if team_list.count(team) == 0:
                team_list.append(team)
    return team_list

def knows(team1, team2, alliances):
    for competition in alliances:
        if competition.count(team1) > 0 and competition.count(team2) > 0 and team1  != team2:
            return True
    return False
            
def find_degrees(all_winners, alliance_list):
    testing_teams = ["2056"]
    known_teams = []
    unknown_teams = all_winners
    teams_with_numbers = []
    more = True
    while more == True:
        teams_by_numbers.append(testing_teams)
        for team in testing_teams:
            for other_team in unknown_teams:
                if knows(team, other_team, alliance_list):
                    known_teams.append(other_team)
                    unknown_teams.remove(other_team)
        testing_teams = known_teams
        known_teams = []
        if testing_teams == []:
            more = True
    teams_by_numbers.append(unknown_teams)
    return teams_by_numbers

if __name__ == "__main__":
    allies = get_alliances("AllWinners.txt")
    winners = get_winners(allies)
    degrees_list = find_degrees(winners, allies)

    for degree in range(0, len(degrees_list) - 1):
        print "Number of teams at degree" + str(degree) + ": " + str(len(degrees_list[degree]))
        print "Teams at degree " + str(degree) + ": " + str(degrees_list[degree])
    print "Number of island teams:" + str(len(degrees_list[-1]))
    print "Island teams: " + str(degrees_list[-1])


Spoiler for Output:
Number of teams at degree0: 1
Teams at degree 0: ['2056']
Number of teams at degree1: 32
Teams at degree 1: ['254', '176', '330', '217', '118', '781', '1114', '492', '296', '1625', '910', '771', '2166', '1680', '2185', '2609', '3138', '1547', '1518', '3756', '4334', '1219', '2200', '4372', '3944', '1325', '4069', '5288', '2852', '5719', '2634', '4920']
Number of teams at degree2: 174
Teams at degree 2: ['144', '60', '25', '111', '349', '364', '33', '294', '256', '636', '115', '74', '56', '64', '233', '469', '865', '753', '852', '980', '955', '971', '766', '987', '22', '1887', '1662', '581', '4', '1425', '2056', '2454', '973', '3357', '3230', '649', '2848', '1678', '3704', '751', '1138', '2135', '1323', '5027', '3970', '175', '48', '69', '501', '1124', '1519', '1727', '67', '260', '359', '1212', '634', '1270', '696', '1216', '1717', '2403', '1515', '2761', '842', '2102', '1266', '4486', '3925', '148', '75', '85', '301', '65', '245', '522', '68', '1506', '1503', '174', '247', '440', '830', '3098', '3119', '51', '2960', '1188', '1551', '3096', '3539', '16', '548', '476', '647', '935', '231', '1745', '2194', '1477', '624', '1986', '3494', '1801', '1642', '1967', '3008', '3753', '2789', '1723', '2585', '5458', '1671', '2613', '3490', '4587', '746', '3735', '45', '177', '610', '2016', '3940', '4039', '4121', '311', '63', '229', '1024', '1305', '1281', '2041', '117', '1923', '1482', '1640', '3492', '4814', '900', '5136', '4903', '1031', '1983', '2046', '2471', '948', '2522', '3826', '1816', '1296', '2039', '2590', '3467', '2481', '2451', '639', '70', '862', '5193', '846', '1559', '4591', '1334', '2468', '2013', '1619', '4625', '3166', '4001', '1726', '4183', '20', '3339', '3710']
Number of teams at degree3: 435
Teams at degree 3: ['368', '1629', '337', '409', '968', '1046', '1006', '39', '1165', '2984', '232', '293', '343', '340', '103', '180', '494', '341', '102', '381', '195', '1279', '1302', '1522', '222', '1676', '3929', '71', '47', '112', '269', '537', '1756', '1710', '1850', '876', '2826', '1732', '2512', '2702', '4226', '2338', '201', '437', '57', '457', '1927', '3937', '3616', '1', '27', '182', '107', '888', '108', '388', '314', '1112', '977', '904', '1023', '2337', '2137', '4294', '1718', '4779', '5053', '4768', '5068', '100', '698', '279', '5124', '2122', '3688', '237', '365', '95', '165', '224', '395', '303', '41', '1796', '1370', '2577', '5895', '383', '578', '1527', '121', '759', '179', '1649', '1568', '1251', '86', '1592', '207', '768', '2377', '246', '4063', '4575', '4592', '4087', '859', '868', '703', '1918', '2834', '3548', '3175', '244', '2676', '4377', '3452', '833', '847', '2930', '3189', '3476', '3495', '5274', '1868', '190', '8', '1013', '1622', '1197', '3512', '2844', '2485', '4265', '2478', '5012', '3853', '3965', '1690', '1937', '488', '2557', '3711', '2928', '4488', '2811', '2002', '4469', '3786', '2659', '2363', '1836', '1243', '2054', '2959', '2771', '4409', '5502', '3245', '5931', '3037', '5057', '399', '4161', '295', '1538', '2543', '3310', '126', '133', '236', '58', '716', '1038', '1184', '1824', '1768', '839', '375', '291', '1902', '379', '2614', '1086', '2974', '125', '213', '558', '435', '230', '1276', '2642', '2067', '4828', '1786', '1307', '319', '5633', '1512', '3123', '122', '66', '302', '1126', '503', '274', '348', '858', '3095', '4003', '3546', '2851', '3656', '4405', '3707', '6086', '192', '527', '1747', '2467', '3880', '3239', '4218', '3990', '2175', '2444', '2502', '4253', '1572', '4009', '4508', '585', '835', '668', '1452', '4276', '2486', '997', '2130', '604', '2662', '3021', '4583', '215', '704', '418', '2354', '1817', '3481', '4090', '2022', '4610', '5726', '89', '203', '1272', '2645', '4327', '5462', '292', '188', '11', '2619', '2187', '1711', '5692', '4256', '694', '1635', '1241', '288', '5166', '1684', '620', '272', '191', '2053', '1507', '123', '4384', '3773', '4815', '3604', '3620', '312', '869', '1405', '967', '1706', '3352', '3784', '447', '4500', '2665', '6055', '322', '4395', '446', '1802', '34', '1108', '1158', '1775', '2410', '932', '4206', '499', '1421', '2169', '1429', '2173', '4353', '3612', '2972', '3728', '1806', '525', '2667', '1985', '4356', '2457', '2389', '3931', '4296', '1658', '4455', '2439', '4158', '4589', '1785', '5098', '930', '1444', '2171', '53', '306', '284', '5129', '4055', '138', '5686', '3609', '4914', '1310', '3560', '1418', '378', '3015', '334', '308', '157', '234', '1567', '545', '1626', '141', '1386', '2197', '4103', '1720', '1511', '155', '1218', '2607', '2729', '225', '5113', '1391', '3824', '2059', '1287', '1225', '957', '949', '2635', '4082', '4125', '2907', '4060', '4061', '3238', '3049', '4654', '2149', '3237', '5803', '1540', '360', '1510', '3674', '5468', '2903', '4915', '4304', '3663', '2976', '2415', '816', '3981', '3340', '3360', '5407', '5528', '2386', '5000', '4555', '2202', '2220', '4740', '5125', '1736', '1675', '2252', '451', '5084', '240', '3010', '3003', '5406', '4633', '5320', '4799', '4719', '4678', '1730', '4593', '1011', '3683', '4146', '181', '40', '228', '1574', '5654', '4744']
Number of teams at degree4: 317
Teams at degree 4: ['1280', '2348', '623', '836', '3941', '357', '460', '3473', '598', '1897', '255', '134', '193', '459', '186', '1261', '1319', '547', '1610', '1398', '4188', '59', '4930', '316', '79', '2815', '3627', '3932', '1065', '1180', '4956', '84', '1332', '3204', '486', '2559', '1495', '1257', '2487', '2370', '2168', '999', '2064', '3461', '354', '1743', '2753', '3059', '714', '1403', '5624', '173', '2604', '2709', '331', '663', '5', '1810', '4028', '3747', '2062', '1714', '3130', '2491', '2530', '171', '2574', '3996', '538', '3337', '3039', '541', '5907', '519', '93', '2586', '5610', '212', '1683', '2283', '384', '5081', '5712', '5784', '3617', '2620', '1089', '573', '1502', '2405', '3667', '2048', '1701', '1458', '2996', '1696', '3191', '3309', '498', '6128', '131', '209', '549', '358', '7', '1230', '1880', '555', '1989', '870', '1807', '3171', '1884', '2344', '1369', '1466', '4451', '1612', '4901', '3193', '1195', '1699', '1517', '3419', '5687', '4301', '1646', '216', '2474', '1677', '4835', '4381', '6116', '5501', '2489', '3255', '2526', '3132', '6220', '353', '250', '533', '61', '1516', '1099', '3205', '4473', '4576', '3843', '1576', '3351', '4050', '1569', '2990', '4038', '956', '4077', '4205', '2550', '5779', '4457', '2421', '3547', '4967', '4482', '4216', '3807', '2339', '840', '19', '1155', '782', '3464', '1831', '571', '1388', '2010', '88', '1922', '1058', '5563', '346', '2534', '5279', '744', '4026', '4118', '4910', '5594', '78', '4761', '401', '1262', '1991', '4908', '1311', '4935', '3661', '6161', '281', '5804', '5546', '818', '226', '1139', '1015', '326', '815', '3234', '5926', '3530', '1075', '4950', '5618', '4607', '3042', '3184', '2883', '5601', '3562', '6175', '2473', '3256', '3859', '1087', '5118', '2587', '2165', '1140', '423', '3538', '3602', '421', '2265', '4797', '5030', '2935', '4819', '128', '3951', '903', '4362', '2767', '5069', '415', '449', '3642', '1984', '829', '1501', '3284', '49', '135', '5188', '1977', '1997', '1987', '3160', '4522', '2992', '3018', '701', '3061', '931', '2518', '1208', '2345', '3081', '2052', '3244', '4215', '5172', '3397', '2438', '3323', '4124', '5076', '2912', '1088', '1555', '2081', '772', '3865', '1529', '204', '4460', '2495', '1731', '4073', '2640', '4288', '1318', '2915', '2147', '4495', '2923', '3789', '1584', '2395', '2660', '4559', '832', '2655', '1771', '2751', '587', '2648', '3102', '4330', '3414', '4550', '5039', '1474', '6153', '1657', '1950', '3316']
Number of teams at degree5: 120
Teams at degree 5: ['2443', '4541', '3260', '4821', '467', '2914', '606', '277', '4361', '1057', '801', '1414', '3329', '1746', '342', '1648', '1598', '1541', '4468', '422', '2549', '2970', '1772', '3280', '3146', '2274', '3314', '4653', '223', '28', '713', '313', '1030', '3038', '4011', '4143', '2470', '650', '3946', '4818', '168', '1912', '3794', '4977', '3322', '453', '3405', '5059', '263', '271', '339', '1583', '514', '3137', '3950', '1002', '1127', '1848', '1051', '1893', '4564', '1254', '4391', '5675', '1255', '4613', '55', '1713', '945', '2877', '3393', '4911', '2093', '5248', '5008', '5969', '3359', '4080', '515', '3509', '5203', '5155', '3387', '3533', '2177', '4778', '5232', '5222', '4988', '1025', '5016', '2775', '2408', '4269', '3301', '3660', '3364', '3692', '2259', '3313', '2445', '912', '2040', '3259', '4464', '5450', '3588', '1595', '2036', '3528', '238', '1533', '21', '3506', '5855', '3641', '1955', '2214', '2669', '3065']
Number of teams at degree6: 40
Teams at degree 6: ['4547', '5651', '5812', '3266', '5074', '643', '3278', '3044', '3718', '398', '2830', '755', '462', '3480', '5696', '386', '1156', '287', '2383', '1636', '569', '1539', '5265', '6035', '394', '1450', '1899', '3489', '3534', '5524', '3147', '2424', '2227', '3586', '3680', '5518', '5679', '5511', '2230', '2630']
Number of teams at degree7: 6
Teams at degree 7: ['2483', '2247', '329', '496', '4290', '1577']
Number of teams at degree8: 5
Teams at degree 8: ['1758', '2575', '2638', '1708', '4320']
Number of island teams:37
Island teams: ['73', '151', '15', '239', '692', '662', '1147', '982', '938', '1860', '1382', '2243', '2108', '540', '843', '1742', '1561', '2004', '884', '4476', '1285', '2198', '1143', '4285', '1228', '1735', '1100', '2523', '2537', '5115', '4456', '3535', '5505', '2832', '3158', '4403', '3527']


Spoiler for FindOneDegree.py:
Code:
def get_alliances(filename):
    allAlliances = open(filename, 'r')
    alliancelist = [line.rstrip('\n').split(",") for line in allAlliances]
    allAlliances.close()
    return alliancelist

def get_winners(alliances):
    team_list = []
    for competition in alliances:
        for team in competition:
            if team_list.count(team) == 0:
                team_list.append(team)
    return team_list

def knows(team1, team2, alliances):
    for competition in alliances:
        if competition.count(team1) > 0 and competition.count(team2) > 0 and team1  != team2:
            return True
    return False
            
def find_degrees(orig_team, all_winners, alliance_list):
    if orig_team == "2056":
        return 0
    elif knows(orig_team, "2056", alliance_list):
        teamups = 0
        for event in alliance_list:
            if event.count(orig_team) > 0 and event.count("2056") > 0:
                teamups += 1
        return 1.0 / teamups
    else:
        testing_teams = [orig_team]
        known_teams = []
        unknown_teams = all_winners
        more = True
        returnnum = 2
        while more == True:
            for team in testing_teams:
                for other_team in unknown_teams:
                    if knows(team, other_team, alliance_list):
                        if knows(other_team, "2056", alliance_list):
                            return returnnum
                        else:
                            known_teams.append(other_team)
                            unknown_teams.remove(other_team)
            returnnum += 1
            testing_teams = known_teams
            known_teams = []
            if testing_teams == []:
                more = False
    return -1

if __name__ == "__main__":
    allies = get_alliances("AllWinners.txt")
    winners = get_winners(allies)
    team = str(raw_input("Team Number: "))
    print find_degrees(team, winners, allies)
__________________
Reply With Quote