限制0-1背包项目/人数问题?

时间:2019-02-28 09:10:19

标签: python-3.x dynamic knapsack-problem

我现在面对这个问题已有一段时间了。我不了解我如何限制此程序中的人员/项目。我必须从技能最佳,薪水低的团队中选出6个人。在数组中,第一项是名称,第二项是薪水,最后一项是技能得分。我必须在1500000薪水中选择6名技能得分最高的人。在这里,我以薪金为重。你能帮我吗?

try:
    xrange
except:
    xrange = range

def totalvalue(comb):
    totwt = totval = 0
    for item, wt, val in comb:
        totwt  += wt
        totval += val
    return (totval, -totwt) if totwt <= 1500000 else (0, 0)

items = [
['Eva Battolla', 15000, 130], ['Sandra Rubesam', 20000, 150], 
['Frida Ronning', 20000, 100], ['Stanislav Sehnal', 15000, 50], 
['Jackson Frew', 30000, 92], ['Miranda Miller', 100000, 285], ['Alia Marcellini', 50000, 135], ['Marcelo Gutierrez', 200000, 537], 
['Loris Revelli', 50000, 132], ['Rachel Atherton', 675000, 1770], ['Mark Wallace', 225000, 588], ['Eleonora Farina', 125000, 315], 
['Reece Wilson', 200000, 504], ['Magnus Manson', 50000, 124], ['Carina Cappellari', 175000, 430], ['Remi Thirion', 250000, 613], 
['Tahnee Seagrave', 625000, 1520], ['Bryn Dickerson', 50000, 121], ['Marine Cabirou', 375000, 900], ['Noel Niederberger', 20000, 48], 
['Loris Vergier', 450000, 1066], ['Connor Fearon', 275000, 649], ['Alex Marin', 75000, 177], ['Charlie Harrison', 225000, 528], 
['Troy Brosnan', 450000, 1040], ['Danny Hart', 475000, 1065], ['Baptiste Pierron', 125000, 280], ['Myriam Nicole', 450000, 990], 
['Sam Blenkinsop', 250000, 545], ['Adam Brayton', 150000, 327], ['Faustin Figaret', 100000, 216], ['Laurie Greenland', 410000, 880], 
['Forrest Riesco', 10000, 21], ['Tracey Hannah', 550000, 1130], ['Slawomir Lukasik', 30000, 61], ['Amaury Pierron', 610000, 1240], 
['Benoit Coulanges', 100000, 203], ['Michael Jones', 125000, 253], ['Monika Hrastnik', 375000, 745], ['David Trummer', 75000, 149], 
['Taylor Vernon', 60000, 119], ['Veronika Widmann', 175000, 345], ['Bernard Kerr', 150000, 293], ['Thomas Estaque', 150000, 288], 
['Jacob Dickson', 100000, 190], ['Dylan Levesque', 30000, 57], ['Phil Atwill', 150000, 281], ['Joe Breeden', 30000, 56], 
['Loic Bruni', 550000, 998], ['Brook Macdonald', 375000, 679], ['Neko Mulally', 150000, 271], ['Joe Smith', 125000, 224], 
['Matt Walker', 275000, 490], ['Dean Lucas', 275000, 487], ['Brendan Fairclough', 100000, 176], ['Gee Atherton', 325000, 563], 
['Gaetan Vige', 40000, 69], ['Emilie Siegenthaler', 325000, 550], ['Charlie Hatton', 100000, 160], ['Florent Payet', 100000, 157], 
['Gaetan Ruffin', 20000, 31], ['Luca Shaw', 425000, 652], ['Edward Masters', 175000, 267], ['Dakotah Norton', 250000, 379], 
['Mariana Salazar', 300000, 445], ['Finnley Iles', 350000, 514], ['Thibault Laly', 50000, 70], ['Martin Maes', 400000, 550], 
['Greg Williamson', 150000, 195], ['Wyn Masters', 75000, 96], ['Matthew Walker', 125000, 149], ['Jack Reading', 75000, 89], 
['Isak Leivsson', 50000, 59], ['Aaron Gwin', 600000, 683], ['Jack Moir', 375000, 410], ['Michael Hannah', 150000, 151], 
['Jure Jabjek', 50000, 49], ['Alexandre Fayolle', 150000, 142], ['Greg Minnaar', 375000, 268], ['Rupert Chapman', 50000, 32], 
['Thibault Ruffin', 75000, 45], ['Cecille Ravanel', 500000, 0], ['Katy Curd', 215000, 0], ['Nina Hoffman', 100000, 0], 
["Sian A'hern", 100000, 0], ['Kate Weatherly', 25000, 0], ['Camille Balanche', 20000, 0], ['Melanie Chappaz', 5000, 0], 
['Virginia Armstrong', 5000, 0], ['Angel Suarez', 250000, 0], ['George Brannigan', 150000, 0], ['Kade Edwards', 125000, 0], 
['Henry Fitzgerald', 75000, 0], ['Hugo Frixtalon', 75000, 0], ['Joshua Barth', 50000, 0], ['Antoine Pierron', 50000, 0], 
['Charly Dipasquale', 50000, 0], ['Sam Dale', 50000, 0], ['Max Hartenstern', 50000, 0], ['Johannes Von Klebelsberg', 50000, 0], 
['George Gannicott', 50000, 0], ['Andreas Kolb', 50000, 0], ['Rafael Gutierrez', 50000, 0], ['Valentin Chatanay', 25000, 0], 
['Kevin Marry', 20000, 0], ['Kaos Seagrave', 15000, 0], ['Edgar Gonzalez', 15000, 0], ['Miran Vauh', 10000, 0]]


def knapsack01_dp(items, limit):
    table = [[0 for w in range(limit + 1)] for j in xrange(len(items) + 1)]

    for j in xrange(1, len(items) + 1):
        item, wt, val = items[j-1]
        for w in xrange(1, limit + 1):
            if wt > w:
                table[j][w] = table[j-1][w]
            else:
                table[j][w] = max(table[j-1][w],
                                  table[j-1][w-wt] + val)

    result = []
    w = limit
    for j in range(len(items), 0, -1):
        was_added = table[j][w] != table[j-1][w]

        if was_added:
            item, wt, val = items[j-1]
            result.append(items[j-1])
            w -= wt

    return result


bagged = knapsack01_dp(items, 1500000)
print("Selected the following persons\n  " +
  '\n  '.join(sorted(item for item,_,_ in bagged)))
val, wt = totalvalue(bagged)
print("for a total skill score of %i and a total salary of %i" % (val, -wt))

谢谢。

0 个答案:

没有答案