示例#1
0
    def predict_daily_points(database_session, day=None):
        if day is None:
            day = date.today()
        #hitter_regression = HitterRegressionTrainer()
        hitter_regression = HitterRegressionForestTrainer()
        hitter_regression.train_network()
        #pitcher_regression = PitcherRegressionTrainer()
        pitcher_regression = PitcherRegressionForestTrainer()
        pitcher_regression.train_network()
        daily_entries = PregameHitterGameEntry.get_all_daily_entries(
            database_session, day)
        for daily_entry in daily_entries:
            predicted_points = hitter_regression.get_prediction(
                daily_entry.to_input_vector())

            if predicted_points < 0:
                predicted_points = 0
            daily_entry.predicted_draftkings_points = predicted_points
            database_session.commit()

        daily_entries = PregamePitcherGameEntry.get_all_daily_entries(
            database_session, day)
        for daily_entry in daily_entries:
            predicted_points = pitcher_regression.get_prediction(
                daily_entry.to_input_vector())
            if predicted_points < 0:
                predicted_points = 0
            daily_entry.predicted_draftkings_points = predicted_points
            database_session.commit()
示例#2
0
    def train_network(self):
        """ Pure virtual method for training the network
        """
        db_query = self._database_session.query(PregameHitterGameEntry)
        mlb_training_data, mlb_evaluation_data = self.get_train_eval_data(
            db_query, 0.8)
        X_train, Y_train = self.get_stochastic_batch(mlb_training_data,
                                                     self.SIZE_TRAINING_BATCH)
        self._decision_tree.fit(X_train, Y_train)
        dot_data = StringIO()
        tree.export_graphviz(
            self._decision_tree,
            out_file=dot_data,
            feature_names=PregameHitterGameEntry.get_input_vector_labels())
        graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
        graph.write_pdf("hitter_tree.pdf")
        x_test_actual = list()
        y_test_actual = list()
        for data in mlb_evaluation_data:
            postgame_entry = self._database_session.query(
                PostgameHitterGameEntry).get(data.rotowire_id, data.game_date)
            if postgame_entry is None:
                print "Ignoring hitter %s since his postgame stats were not found." % data.rotowire_id
                continue

            y_test_actual.append([postgame_entry.actual_draftkings_points])
            x_test_actual.append(data.to_input_vector())

        self._database_session.close()
示例#3
0
    def predict_daily_points(database_session, day=None):
        if day is None:
            day = date.today()
        #hitter_regression = HitterRegressionTrainer()
        hitter_regression = HitterRegressionForestTrainer()
        hitter_regression.train_network()
        #pitcher_regression = PitcherRegressionTrainer()
        pitcher_regression = PitcherRegressionForestTrainer()
        pitcher_regression.train_network()
        daily_entries = PregameHitterGameEntry.get_all_daily_entries(database_session, day)
        for daily_entry in daily_entries:
            predicted_points = hitter_regression.get_prediction(daily_entry.to_input_vector())

            if predicted_points < 0:
                predicted_points = 0
            daily_entry.predicted_draftkings_points = predicted_points
            database_session.commit()

        daily_entries = PregamePitcherGameEntry.get_all_daily_entries(database_session, day)
        for daily_entry in daily_entries:
            hitter_array = daily_entry.get_opponent_vector(database_session)
            final_array = np.concatenate([daily_entry.to_input_vector(), hitter_array])
            predicted_points = pitcher_regression.get_prediction(final_array)
            if predicted_points < 0:
                predicted_points = 0
            daily_entry.predicted_draftkings_points = predicted_points
            database_session.commit()
    def train_network(self):
        """ Pure virtual method for training the network
        """
        db_query = self._database_session.query(PregameHitterGameEntry)
        mlb_training_data, mlb_evaluation_data = self.get_train_eval_data(db_query, 0.8)
        X_train, Y_train = self.get_stochastic_batch(mlb_training_data, self.SIZE_TRAINING_BATCH)
        self._decision_tree.fit(X_train, Y_train)
        dot_data = StringIO()
        tree.export_graphviz(self._decision_tree, out_file=dot_data,
                             feature_names=PregameHitterGameEntry.get_input_vector_labels())
        graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
        graph.write_pdf("hitter_tree.pdf")
        x_test_actual = list()
        y_test_actual = list()
        for data in mlb_evaluation_data:
            try:
                postgame_entry = self._database_session.query(PostgameHitterGameEntry).filter(PostgameHitterGameEntry.rotowire_id == data.rotowire_id,
                                                                                              PostgameHitterGameEntry.game_date == data.game_date).one()
                y_test_actual.append([postgame_entry.actual_draftkings_points])
                x_test_actual.append(data.to_input_vector())
            except NoResultFound:
                print "Ignoring hitter %s since his postgame stats were not found." % data.rotowire_id
                continue

        self._database_session.close()
    def runTest(self):
        # Sanity check of the relationship of our parameters
        self.assertLess(TestAddPlayerSecondary.CANDIDATE_POINTS, OptimalLineupTest.OPTIMAL_LINEUP_SS_POINTS)
        self.assertGreater(TestAddPlayerSecondary.CANDIDATE_POINTS, OptimalLineupTest.OPTIMAL_LINEUP_2B_POINTS)
        self.assertLess(TestAddPlayerSecondary.CANDIDATE_SALARY / float(TestAddPlayerSecondary.CANDIDATE_POINTS),
                        OptimalLineupTest.OPTIMAL_LINEUP_2B_SALARY / float(OptimalLineupTest.OPTIMAL_LINEUP_2B_POINTS))
        self.assertLessEqual(OptimalLineupTest.OPTIMAL_LINEUP_TOTAL_SALARY - OptimalLineupTest.OPTIMAL_LINEUP_2B_SALARY +
                             TestAddPlayerSecondary.CANDIDATE_SALARY, Draftkings.CONTEST_SALARY)

        player = PregameHitterGameEntry()
        player.rotowire_id = TestAddPlayerSecondary.CANDIDATE_ID
        player.primary_position = TestAddPlayerSecondary.CANDIDATE_PRIMARY_POS
        player.secondary_position = TestAddPlayerSecondary.CANDIDATE_SECONDARY_POS
        player.predicted_draftkings_points = TestAddPlayerSecondary.CANDIDATE_POINTS
        player.draftkings_salary = TestAddPlayerSecondary.CANDIDATE_SALARY
        self.optimal_lineup.add(player)
        self.assertEqual(self.optimal_lineup.position_map[TestAddPlayerSecondary.CANDIDATE_PRIMARY_POS]._player.rotowire_id,
                         OptimalLineupTest.OPTIMAL_LINEUP_SS_ID)
        self.assertEqual(self.optimal_lineup.position_map[TestAddPlayerSecondary.CANDIDATE_SECONDARY_POS]._player.rotowire_id,
                         TestAddPlayerSecondary.CANDIDATE_ID)

        self.assertEqual(self.optimal_lineup.get_total_salary(),
                         OptimalLineupTest.OPTIMAL_LINEUP_TOTAL_SALARY -
                         OptimalLineupTest.OPTIMAL_LINEUP_2B_SALARY +
                         TestAddPlayerSecondary.CANDIDATE_SALARY)
示例#6
0
    def update_salaries(database_session, csv_dict=None, game_date=None):
        if game_date is None:
            game_date = date.today()
        if csv_dict is None:
            csv_dict = Draftkings.get_csv_dict()
        #Hitters
        pregame_hitters = PregameHitterGameEntry.get_all_daily_entries(
            database_session, game_date)
        for pregame_entry in pregame_hitters:
            # Lookup the player's name in the database
            # Lookup the name in the dictionary
            hitter_entry = database_session.query(HitterEntry).get(
                pregame_entry.rotowire_id)
            if hitter_entry is None:
                print "Player %s not found in the Draftkings CSV file. Deleting entry." % pregame_entry.rotowire_id
            try:
                csv_entry = csv_dict[(hitter_entry.first_name + " " +
                                      hitter_entry.last_name +
                                      hitter_entry.team).lower()]
                pregame_entry.draftkings_salary = int(csv_entry["Salary"])
                positions = csv_entry["Position"].split("/")
                pregame_entry.primary_position = positions[0]
                pregame_entry.secondary_position = positions[len(positions) -
                                                             1]
                database_session.commit()
            except KeyError:
                print "Player %s not found in the Draftkings CSV file. Deleting entry." % (
                    hitter_entry.first_name + " " + hitter_entry.last_name)
                database_session.delete(pregame_entry)
                database_session.commit()

        # Pitchers
        pregame_pitchers = PregamePitcherGameEntry.get_all_daily_entries(
            database_session, game_date)
        for pregame_entry in pregame_pitchers:
            # Lookup the player's name in the database
            # Lookup the name in the dictionary
            pitcher_entry = database_session.query(PitcherEntry).get(
                pregame_entry.rotowire_id)
            if pitcher_entry is None:
                print "Player %s not found in the Draftkings CSV file. Deleting entry." % pregame_entry.rotowire_id
            try:
                csv_entry = csv_dict[(pitcher_entry.first_name + " " +
                                      pitcher_entry.last_name +
                                      pitcher_entry.team).lower()]
                pregame_entry.draftkings_salary = int(csv_entry["Salary"])
                database_session.commit()
            except KeyError:
                print "Player %s not found in the Draftkings CSV file. Deleting entry." % (
                    pitcher_entry.first_name + " " + pitcher_entry.last_name)
                database_session.delete(pregame_entry)
                database_session.commit()
    def runTest(self):
        player = PregameHitterGameEntry()
        player.rotowire_id = TestAddPlayerWorse.CANDIDATE_ID
        player.primary_position = TestAddPlayerWorse.CANDIDATE_PRIMARY_POS
        player.secondary_position = TestAddPlayerWorse.CANDIDATE_SECONDARY_POS
        player.predicted_draftkings_points = TestAddPlayerWorse.CANDIDATE_POINTS
        player.draftkings_salary = TestAddPlayerWorse.CANDIDATE_SALARY
        self.optimal_lineup.add(player)
        self.assertEqual(self.optimal_lineup[TestAddPlayerWorse.CANDIDATE_PRIMARY_POS].rotowire_id,
                         OptimalLineupTest.OPTIMAL_LINEUP_SS_ID)
        self.assertEqual(self.optimal_lineup[TestAddPlayerWorse.CANDIDATE_SECONDARY_POS].rotowire_id,
                         OptimalLineupTest.OPTIMAL_LINEUP_3B_ID)

        self.assertEqual(self.optimal_lineup.get_total_salary(), OptimalLineupTest.OPTIMAL_LINEUP_TOTAL_SALARY)
示例#8
0
    def update_salaries(database_session, csv_dict=None, game_date=None):
        if game_date is None:
            game_date = date.today()
        if csv_dict is None:
            csv_dict = Draftkings.get_csv_dict()
        #Hitters
        pregame_hitters = PregameHitterGameEntry.get_all_daily_entries(database_session, game_date)
        for pregame_entry in pregame_hitters:
            # Lookup the player's name in the database
            # Lookup the name in the dictionary
            hitter_entry = database_session.query(HitterEntry).get(pregame_entry.rotowire_id)
            if hitter_entry is None:
                print "Player %s not found in the Draftkings CSV file. Deleting entry." % pregame_entry.rotowire_id
            try:
                csv_entry = csv_dict[(hitter_entry.first_name + " " + hitter_entry.last_name + hitter_entry.team).lower()]
                pregame_entry.draftkings_salary = int(csv_entry["Salary"])
                positions = csv_entry["Position"].split("/")
                pregame_entry.primary_position = positions[0]
                pregame_entry.secondary_position = positions[len(positions) - 1]
                database_session.commit()
            except KeyError:
                print "Player %s not found in the Draftkings CSV file. Deleting entry." % (hitter_entry.first_name + " " + hitter_entry.last_name)
                database_session.delete(pregame_entry)
                database_session.commit()

        # Pitchers
        pregame_pitchers = PregamePitcherGameEntry.get_all_daily_entries(database_session, game_date)
        for pregame_entry in pregame_pitchers:
            # Lookup the player's name in the database
            # Lookup the name in the dictionary
            pitcher_entry = database_session.query(PitcherEntry).get(pregame_entry.rotowire_id)
            if pitcher_entry is None:
                print "Player %s not found in the Draftkings CSV file. Deleting entry." % pregame_entry.rotowire_id
            try:
                csv_entry = csv_dict[(pitcher_entry.first_name + " " + pitcher_entry.last_name + pitcher_entry.team).lower()]
                pregame_entry.draftkings_salary = int(csv_entry["Salary"])
                database_session.commit()
            except KeyError:
                print "Player %s not found in the Draftkings CSV file. Deleting entry." % (pitcher_entry.first_name + " " + pitcher_entry.last_name)
                database_session.delete(pregame_entry)
                database_session.commit()
    def runTest(self):
        # Add the mediocre player
        player = PregameHitterGameEntry()
        player.rotowire_id = TestAddOutfielder.CANDIDATE_ID
        player.primary_position = TestAddOutfielder.CANDIDATE_PRIMARY_POS
        player.secondary_position = TestAddOutfielder.CANDIDATE_SECONDARY_POS
        player.predicted_draftkings_points = TestAddOutfielder.CANDIDATE_POINTS
        player.draftkings_salary = TestAddOutfielder.CANDIDATE_SALARY
        self.optimal_lineup.add(player)
        worst_player = heapq.heappop(self.optimal_lineup.position_map[TestAddOutfielder.CANDIDATE_PRIMARY_POS]._position_heap)
        self.assertEqual(self.optimal_lineup.position_map[TestAddOutfielder.CANDIDATE_PRIMARY_POS]._position_heap[0][1].rotowire_id,
                         OptimalLineupTest.OPTIMAL_LINEUP_OF1_ID)
        heapq.heappush(self.optimal_lineup.position_map[TestAddOutfielder.CANDIDATE_PRIMARY_POS]._position_heap, worst_player)
        self.assertEqual(self.optimal_lineup.position_map[TestAddOutfielder.CANDIDATE_SECONDARY_POS]._player.rotowire_id,
                         OptimalLineupTest.OPTIMAL_LINEUP_1B_ID)

        self.assertEqual(self.optimal_lineup.get_total_salary(),
                         OptimalLineupTest.OPTIMAL_LINEUP_TOTAL_SALARY -
                         OptimalLineupTest.OPTIMAL_LINEUP_1B_SALARY)
示例#10
0
    def get_optimal_lineup(database_session, day=None):
        """ Get the optimal lineup of the players to choose for tonight
        :param database_session: SQLAlchemy database session
        :return: an OptimalLineupDict structure
        """
        if day is None:
            day = date.today()
        optimal_lineup = OptimalLineupDict()
        player_heap = list()

        # Look for the hitter entries
        for fielding_position in OptimalLineupDict.FieldingPositions:
            query_results = PregameHitterGameEntry.get_daily_entries_by_position(
                database_session, fielding_position, day)
            query_results = list(
                query_results.order_by(
                    desc(PregameHitterGameEntry.predicted_draftkings_points)))
            if fielding_position == "OF":
                while len(optimal_lineup["OF"]
                          ) < OptimalLineupDict.MAX_OUTFIELDERS and len(
                              query_results) > 0:
                    candidate_player = heapq.heappop(query_results)
                    if not optimal_lineup.is_in_dict(candidate_player):
                        optimal_lineup.add(candidate_player)
            else:
                try:
                    candidate_player = heapq.heappop(query_results)
                except IndexError:
                    print "Exception."
                if not optimal_lineup.is_in_dict(candidate_player):
                    optimal_lineup.add(candidate_player)
            for player in query_results:
                try:
                    heapq.heappush(
                        player_heap,
                        (-player.predicted_draftkings_points, player))
                except ZeroDivisionError:
                    continue

        # Look for pitchers
        query_results = PregamePitcherGameEntry.get_all_daily_entries(
            database_session, day)
        query_results = list(
            query_results.order_by(
                desc(PregamePitcherGameEntry.predicted_draftkings_points)))
        for i in range(0, OptimalLineupDict.MAX_PITCHERS):
            candidate_player = heapq.heappop(query_results)
            if not optimal_lineup.is_in_dict(candidate_player):
                optimal_lineup.add(candidate_player)

        for pitcher in query_results:
            try:
                heapq.heappush(player_heap,
                               (-pitcher.predicted_draftkings_points, pitcher))
            except ZeroDivisionError:
                continue

        # Replace players one by one who are "overpaid" based on predicted points per dollar
        while (optimal_lineup.get_total_salary() > Draftkings.CONTEST_SALARY and len(player_heap) > 0) or \
                not optimal_lineup.is_valid():
            next_player = heapq.heappop(player_heap)[1]
            if not optimal_lineup.is_in_dict(next_player):
                optimal_lineup.add(next_player)

        # Print out all the remaining players in order of their value
        print "Runner-up players"
        for player in player_heap:
            print player[1]
        print " "

        # Commit the prediction to the database
        lineup_db_entry = LineupEntry()
        lineup_db_entry.game_date = date.today()
        lineup_db_entry.game_time = datetime.now().strftime("%H:%M:%S")
        lineup_db_entry.starting_pitcher_1 = optimal_lineup["SP"][0][
            1].rotowire_id
        lineup_db_entry.starting_pitcher_2 = optimal_lineup["SP"][1][
            1].rotowire_id
        lineup_db_entry.catcher = optimal_lineup["C"].rotowire_id
        lineup_db_entry.first_baseman = optimal_lineup["1B"].rotowire_id
        lineup_db_entry.second_baseman = optimal_lineup["2B"].rotowire_id
        lineup_db_entry.third_baseman = optimal_lineup["3B"].rotowire_id
        lineup_db_entry.shortstop = optimal_lineup["SS"].rotowire_id
        lineup_db_entry.outfielder_1 = optimal_lineup["OF"][0][1].rotowire_id
        lineup_db_entry.outfielder_2 = optimal_lineup["OF"][1][1].rotowire_id
        lineup_db_entry.outfielder_3 = optimal_lineup["OF"][2][1].rotowire_id
        database_session.add(lineup_db_entry)
        database_session.commit()

        return optimal_lineup
    def setUp(self):
        self.optimal_lineup = OptimalLineupDict()
        player1 = PregameHitterGameEntry()
        player1.rotowire_id = OptimalLineupTest.OPTIMAL_LINEUP_SP1_ID
        player1.draftkings_salary = OptimalLineupTest.OPTIMAL_LINEUP_SP1_SALARY
        player1.predicted_draftkings_points = OptimalLineupTest.OPTIMAL_LINEUP_SP1_POINTS
        player1.primary_position = "SP"
        self.optimal_lineup.add(player1)

        player2 = PregameHitterGameEntry()
        player2.rotowire_id = OptimalLineupTest.OPTIMAL_LINEUP_SP2_ID
        player2.draftkings_salary = OptimalLineupTest.OPTIMAL_LINEUP_SP2_SALARY
        player2.predicted_draftkings_points = OptimalLineupTest.OPTIMAL_LINEUP_SP2_POINTS
        player2.primary_position = "SP"
        self.optimal_lineup.add(player2)

        player3 = PregameHitterGameEntry()
        player3.rotowire_id = OptimalLineupTest.OPTIMAL_LINEUP_C_ID
        player3.draftkings_salary = OptimalLineupTest.OPTIMAL_LINEUP_C_SALARY
        player3.predicted_draftkings_points = OptimalLineupTest.OPTIMAL_LINEUP_C_POINTS
        player3.primary_position = "C"
        self.optimal_lineup.add(player3)

        player4 = PregameHitterGameEntry()
        player4.rotowire_id = OptimalLineupTest.OPTIMAL_LINEUP_1B_ID
        player4.draftkings_salary = OptimalLineupTest.OPTIMAL_LINEUP_1B_SALARY
        player4.predicted_draftkings_points = OptimalLineupTest.OPTIMAL_LINEUP_1B_POINTS
        player4.primary_position = "1B"
        self.optimal_lineup.add(player4)

        player5 = PregameHitterGameEntry()
        player5.rotowire_id = OptimalLineupTest.OPTIMAL_LINEUP_2B_ID
        player5.draftkings_salary = OptimalLineupTest.OPTIMAL_LINEUP_2B_SALARY
        player5.predicted_draftkings_points = OptimalLineupTest.OPTIMAL_LINEUP_2B_POINTS
        player5.primary_position = "2B"
        self.optimal_lineup.add(player5)

        player6 = PregameHitterGameEntry()
        player6.rotowire_id = OptimalLineupTest.OPTIMAL_LINEUP_3B_ID
        player6.draftkings_salary = OptimalLineupTest.OPTIMAL_LINEUP_3B_SALARY
        player6.predicted_draftkings_points = OptimalLineupTest.OPTIMAL_LINEUP_3B_POINTS
        player6.primary_position = "3B"
        self.optimal_lineup.add(player6)

        player7 = PregameHitterGameEntry()
        player7.rotowire_id = OptimalLineupTest.OPTIMAL_LINEUP_SS_ID
        player7.draftkings_salary = OptimalLineupTest.OPTIMAL_LINEUP_SS_SALARY
        player7.predicted_draftkings_points = OptimalLineupTest.OPTIMAL_LINEUP_SS_POINTS
        player7.primary_position = "SS"
        self.optimal_lineup.add(player7)

        player8 = PregameHitterGameEntry()
        player8.rotowire_id = OptimalLineupTest.OPTIMAL_LINEUP_OF1_ID
        player8.draftkings_salary = OptimalLineupTest.OPTIMAL_LINEUP_OF1_SALARY
        player8.predicted_draftkings_points = OptimalLineupTest.OPTIMAL_LINEUP_OF1_POINTS
        player8.primary_position = "OF"
        self.optimal_lineup.add(player8)

        player9 = PregameHitterGameEntry()
        player9.rotowire_id = OptimalLineupTest.OPTIMAL_LINEUP_OF2_ID
        player9.draftkings_salary = OptimalLineupTest.OPTIMAL_LINEUP_OF2_SALARY
        player9.predicted_draftkings_points = OptimalLineupTest.OPTIMAL_LINEUP_OF2_POINTS
        player9.primary_position = "OF"
        self.optimal_lineup.add(player9)

        player10 = PregameHitterGameEntry()
        player10.rotowire_id = OptimalLineupTest.OPTIMAL_LINEUP_OF3_ID
        player10.draftkings_salary = OptimalLineupTest.OPTIMAL_LINEUP_OF3_SALARY
        player10.predicted_draftkings_points = OptimalLineupTest.OPTIMAL_LINEUP_OF3_POINTS
        player10.primary_position = "OF"
        self.optimal_lineup.add(player10)
    def runTest(self):
        # Add the mediocre player
        player = PregameHitterGameEntry()
        player.rotowire_id = TestAddOutfielder.CANDIDATE_ID
        player.primary_position = TestAddOutfielder.CANDIDATE_PRIMARY_POS
        player.secondary_position = TestAddOutfielder.CANDIDATE_SECONDARY_POS
        player.predicted_draftkings_points = TestAddOutfielder.CANDIDATE_POINTS
        player.draftkings_salary = TestAddOutfielder.CANDIDATE_SALARY
        self.optimal_lineup.add(player)
        self.assertEqual(self.optimal_lineup[TestAddOutfielder.CANDIDATE_PRIMARY_POS][TestAddOutfielder.CANDIDATE_INSERTION_POSITION][1].rotowire_id,
                         TestAddOutfielder.CANDIDATE_ID)
        self.assertEqual(self.optimal_lineup[TestAddOutfielder.CANDIDATE_SECONDARY_POS].rotowire_id,
                         OptimalLineupTest.OPTIMAL_LINEUP_1B_ID)

        self.assertEqual(self.optimal_lineup.get_total_salary(),
                         OptimalLineupTest.OPTIMAL_LINEUP_TOTAL_SALARY -
                         OptimalLineupTest.OPTIMAL_LINEUP_OF3_SALARY +
                         TestAddOutfielder.CANDIDATE_SALARY)

        # Add the best player
        best_player = PregameHitterGameEntry()
        best_player.rotowire_id = TestAddOutfielder.BEST_CANDIDATE_ID
        best_player.primary_position = TestAddOutfielder.BEST_CANDIDATE_PRIMARY_POS
        best_player.secondary_position = TestAddOutfielder.BEST_CANDIDATE_SECONDARY_POS
        best_player.predicted_draftkings_points = TestAddOutfielder.BEST_CANDIDATE_POINTS
        best_player.draftkings_salary = TestAddOutfielder.BEST_CANDIDATE_SALARY
        self.optimal_lineup.add(best_player)
        self.assertEqual(self.optimal_lineup[TestAddOutfielder.BEST_CANDIDATE_PRIMARY_POS][TestAddOutfielder.BEST_CANDIDATE_INSERTION_POSITION][1].rotowire_id,
                         TestAddOutfielder.BEST_CANDIDATE_ID)

        self.assertEqual(self.optimal_lineup.get_total_salary(),
                         OptimalLineupTest.OPTIMAL_LINEUP_TOTAL_SALARY -
                         OptimalLineupTest.OPTIMAL_LINEUP_OF3_SALARY +
                         TestAddOutfielder.CANDIDATE_SALARY -
                         OptimalLineupTest.OPTIMAL_LINEUP_OF2_SALARY +
                         TestAddOutfielder.BEST_CANDIDATE_SALARY)
    def get_optimal_lineup(database_session, day=None):
        """ Get the optimal lineup of the players to choose for tonight
        :param database_session: SQLAlchemy database session
        :return:
        """
        if day is None:
            day = date.today()
        optimal_lineup = OptimalLineupDict()
        player_heap = list()

        # Look for the hitter entries
        for fielding_position in OptimalLineupDict.FieldingPositions:
            query_results = PregameHitterGameEntry.get_daily_entries_by_position(database_session, fielding_position, day)
            query_results = list(query_results.order_by(desc(PregameHitterGameEntry.predicted_draftkings_points)))
            if fielding_position == "OF":
                while len(optimal_lineup["OF"]) < OptimalLineupDict.MAX_OUTFIELDERS and len(query_results) > 0:
                    candidate_player = heapq.heappop(query_results)
                    if not optimal_lineup.is_in_dict(candidate_player):
                        optimal_lineup.add(candidate_player)
            else:
                candidate_player = heapq.heappop(query_results)
                if not optimal_lineup.is_in_dict(candidate_player):
                    optimal_lineup.add(candidate_player)
            for player in query_results:
                try:
                    dollars_per_point = OptimalLineupDict.dollars_per_point(player)
                    if dollars_per_point > 0:
                        heapq.heappush(player_heap, (dollars_per_point, player))
                except ZeroDivisionError:
                    continue

        # Look for pitchers
        query_results = PregamePitcherGameEntry.get_all_daily_entries(database_session, day)
        query_results = list(query_results.order_by(desc(PregamePitcherGameEntry.predicted_draftkings_points)))
        for i in range(0, OptimalLineupDict.MAX_PITCHERS):
            candidate_player = heapq.heappop(query_results)
            if not optimal_lineup.is_in_dict(candidate_player):
                optimal_lineup.add(candidate_player)

        for pitcher in query_results:
            try:
                dollars_per_point = OptimalLineupDict.dollars_per_point(pitcher)
                if dollars_per_point > 0:
                    heapq.heappush(player_heap, (OptimalLineupDict.dollars_per_point(pitcher), pitcher))
            except ZeroDivisionError:
                continue

        # Replace players one by one who are "overpaid" based on predicted points per dollar
        while (optimal_lineup.get_total_salary() > Draftkings.CONTEST_SALARY and len(player_heap) > 0) or \
                not optimal_lineup.is_valid():
            # TODO: we should add the player back on the player heap when kicked out of lineup
            next_player = heapq.heappop(player_heap)[1]
            if not optimal_lineup.is_in_dict(next_player):
                optimal_lineup.add(next_player)

        # Print out all the remaining players in order of their value
        print "Runner-up players"
        for player in player_heap:
            print player[1]
        print " "

        return optimal_lineup
示例#14
0
    def get_optimal_lineup(database_session, day=None):
        """ Get the optimal lineup of the players to choose for tonight
        :param database_session: SQLAlchemy database session
        :return: an OptimalLineupDict structure
        """
        if day is None:
            day = date.today()
        optimal_lineup = OptimalLineupDict()
        player_heap = dict()

        # Look for the hitter entries
        for fielding_position in OptimalLineupDict.FieldingPositions:
            query_results = PregameHitterGameEntry.get_daily_entries_by_position(database_session, fielding_position, day)
            query_results = list(query_results.order_by(desc(PregameHitterGameEntry.predicted_draftkings_points)))
            query_result_heap = list()
            for query_result in query_results:
                heapq.heappush(query_result_heap, (-query_result.predicted_draftkings_points, query_result))
            while not optimal_lineup.position_map[fielding_position].is_valid() and len(query_results) > 0:
                candidate_player = heapq.heappop(query_result_heap)[1]
                optimal_lineup.add(candidate_player)

            player_heap[fielding_position] = list()
            while len(query_result_heap) > 0:
                player = heapq.heappop(query_result_heap)
                heapq.heappush(player_heap[fielding_position], player)

        # Look for pitchers
        query_results = PregamePitcherGameEntry.get_all_daily_entries(database_session, day)
        query_results = list(query_results.order_by(desc(PregamePitcherGameEntry.predicted_draftkings_points)))
        for query_result in query_results:
            heapq.heappush(query_result_heap, (-query_result.predicted_draftkings_points, query_result))
        while not optimal_lineup.position_map["SP"].is_valid() and len(query_results) > 0:
            candidate_player = heapq.heappop(query_result_heap)[1]
            optimal_lineup.add(candidate_player)

        player_heap["SP"] = list()
        while len(query_result_heap) > 0:
            player = heapq.heappop(query_result_heap)
            heapq.heappush(player_heap["SP"], player)

        # Replace players one by one who are "overpaid" based on predicted points per dollar
        while (optimal_lineup.get_total_salary() > Draftkings.CONTEST_SALARY and len(player_heap) > 0) or \
                not optimal_lineup.is_valid():
            worst_position = optimal_lineup.get_worst_position()
            next_player = heapq.heappop(player_heap[worst_position])[1]
            optimal_lineup.add(next_player)

        # Print out all the remaining players in order of their value
        runner_up_text = "Runner-up players\n"
        for fielding_position in OptimalLineupDict.FieldingPositions:
            runner_up_text += fielding_position + "\n"
            while len(player_heap[fielding_position]) > 0:
                player = heapq.heappop(player_heap[fielding_position])
                runner_up_text += "%s\n" % str(player[1])
            runner_up_text += "\n"

        runner_up_text += "SP\n"
        while len(player_heap["SP"]) > 0:
            player = heapq.heappop(player_heap["SP"])
            runner_up_text += "%s\n" % str(player[1])

        send_email(runner_up_text)
        print runner_up_text

        # Commit the prediction to the database
        lineup_db_entry = LineupEntry()
        lineup_db_entry.game_date = date.today()
        lineup_db_entry.game_time = datetime.now().strftime("%H:%M:%S")
        lineup_db_entry.starting_pitcher_1 = optimal_lineup.position_map["SP"]._position_heap[0][1].rotowire_id
        lineup_db_entry.starting_pitcher_2 = optimal_lineup.position_map["SP"]._position_heap[1][1].rotowire_id
        lineup_db_entry.catcher = optimal_lineup.position_map["C"]._player.rotowire_id
        lineup_db_entry.first_baseman = optimal_lineup.position_map["1B"]._player.rotowire_id
        lineup_db_entry.second_baseman = optimal_lineup.position_map["2B"]._player.rotowire_id
        lineup_db_entry.third_baseman = optimal_lineup.position_map["3B"]._player.rotowire_id
        lineup_db_entry.shortstop = optimal_lineup.position_map["SS"]._player.rotowire_id
        lineup_db_entry.outfielder_1 = optimal_lineup.position_map["OF"]._position_heap[0][1].rotowire_id
        lineup_db_entry.outfielder_2 = optimal_lineup.position_map["OF"]._position_heap[1][1].rotowire_id
        lineup_db_entry.outfielder_3 = optimal_lineup.position_map["OF"]._position_heap[2][1].rotowire_id
        database_session.add(lineup_db_entry)
        database_session.commit()

        return optimal_lineup