Skip to content

Instantly share code, notes, and snippets.

@kangeugine
Created August 9, 2018 02:40
Show Gist options
  • Select an option

  • Save kangeugine/db1e2f525aa417dc5f85d510832b6356 to your computer and use it in GitHub Desktop.

Select an option

Save kangeugine/db1e2f525aa417dc5f85d510832b6356 to your computer and use it in GitHub Desktop.

Revisions

  1. kangeugine created this gist Aug 9, 2018.
    24 changes: 24 additions & 0 deletions knapsack-lp.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    from scipy.optimize import linprog

    points = data['Points']
    cost = data['Cost']
    gk = data['Position'].apply(lambda x: 1 if x == 'GK' else 0)
    defe = data['Position'].apply(lambda x: 1 if x == 'DEF' else 0)
    mid = data['Position'].apply(lambda x: 1 if x == 'MID' else 0)
    stri = data['Position'].apply(lambda x: 1 if x == 'STR' else 0)
    xi = np.ones(data.shape[0])

    A_upperbounds = np.array([cost, defe, mid, stri])
    b_upperbounds = np.array([100, 5, 5, 3])
    A_equality = np.array([gk, xi])
    b_equality = np.array([1, 11])
    bounds = [(0, 1) for x in range(data.shape[0])]

    solution = linprog(
    c=-points,
    A_ub=A_upperbounds,
    b_ub=b_upperbounds,
    A_eq=A_equality,
    b_eq=b_equality,
    bounds=bounds
    )