コード例 #1
0
ファイル: test_optimization.py プロジェクト: Abelarm/AIrsenal
def test_single_transfer():
    """
    mock squad with all players predicted 2 points, and potential transfers
    with higher scores, check we get the best transfer.
    """
    t = generate_dummy_squad()
    position_points_dict = {
        "GK": {0: 2, 1: 2, 100: 0, 101: 0, 200: 3, 201: 2},  # in the orig squad
        "DEF": {
            2: 2,
            3: 2,
            4: 2,
            5: 2,
            6: 2,  # in the orig squad
            103: 0,
            104: 0,
            105: 5,
            106: 2,
            107: 2,
            203: 0,
            204: 0,
            205: 1,
            206: 2,
            207: 2,
        },
        "MID": {
            7: 2,
            8: 2,
            9: 2,
            10: 2,
            11: 2,  # in the orig squad
            108: 2,
            109: 2,
            110: 3,
            111: 3,
            112: 0,
            208: 2,
            209: 2,
            210: 3,
            211: 3,
            212: 0,
        },
        "FWD": {12: 2, 13: 2, 14: 2, 113: 6, 114: 3, 115: 7},  # in the orig squad
    }
    mock_pred_points = predicted_point_mock_generator(position_points_dict)

    with mock.patch(
        "airsenal.framework.optimization_utils.get_predicted_points",
        side_effect=mock_pred_points,
    ):
        new_squad, pid_out, pid_in = make_optimum_transfer(t, "DUMMY", [1])
        ## we should expect - player 115 to be transfered in, and to be captain.
    assert pid_in[0] == 115
    for p in new_squad.players:
        if p.player_id == 115:
            assert p.is_captain == True
        else:
            assert p.is_captain == False
    ## expected points should be 10*2 + 7*2 = 34
    assert new_squad.get_expected_points(1, "DUMMY") == 34
コード例 #2
0
ファイル: api_utils.py プロジェクト: shashanksub42/AIrsenal
def best_transfer_suggestions(n_transfer, session_id, dbsession=DBSESSION):
    """
    Use our predicted playerscores to suggest the best transfers.
    """
    n_transfer = int(n_transfer)
    if not n_transfer in range(1,3):
        raise RuntimeError("Need to choose 1 or 2 transfers")
    if not validate_session_squad(session_id, dbsession):
        raise RuntimeError("Cannot suggest transfer without complete squad")

    budget = get_session_budget(session_id, dbsession)
    players = [p["id"] for p in get_session_players(session_id, dbsession)]
    t = Team(budget)
    for p in players:
        added_ok = t.add_player(p)
        if not added_ok:
            raise RuntimeError("Cannot add player {}".format(p))
    pred_tag = get_latest_prediction_tag()
    gw=get_next_gameweek(CURRENT_SEASON, dbsession)
    if n_transfer == 1:
        new_team, pid_out, pid_in = make_optimum_transfer(t, pred_tag)
    elif n_transfer == 2:
        new_team, pid_out, pid_in = make_optimum_double_transfer(t, pred_tag)
    return {
        "transfers_out": pid_out,
        "transfers_in": pid_in
    }