Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 组合太多_Python_Combinations_Permutation_Itertools_Largenumber - Fatal编程技术网

Python 组合太多

Python 组合太多,python,combinations,permutation,itertools,largenumber,Python,Combinations,Permutation,Itertools,Largenumber,嗨,我正在尝试生成所有可能的建筑工人组合。(让我解释一下我的情况): 我在玩minecraft。在这个mod中,你有殖民者,他们可以被分配到建筑物中工作。这些工人拥有技能,并为他们分配了分数。(比如敏捷度:20,力量:5等等)如果指派一个技能与之相得益彰的殖民者,建筑物的工作表现会更好 所以我创建了一个所有工人和建筑物的数据库,希望优化哪些工人在哪些建筑物工作 buildings\u dict={1:['Strength','Focus'], 2:[“适应性”、“竞技性”], 3:[“知识”、“

嗨,我正在尝试生成所有可能的建筑工人组合。(让我解释一下我的情况):

我在玩minecraft。在这个mod中,你有殖民者,他们可以被分配到建筑物中工作。这些工人拥有技能,并为他们分配了分数。(比如敏捷度:20,力量:5等等)如果指派一个技能与之相得益彰的殖民者,建筑物的工作表现会更好

所以我创建了一个所有工人和建筑物的数据库,希望优化哪些工人在哪些建筑物工作

buildings\u dict={1:['Strength','Focus'],
2:[“适应性”、“竞技性”],
3:[“知识”、“灵巧”],
4:[“适应性”、“知识”],
6:[“耐力”、“运动”],
5:[“田径”、“耐力”],
7:[“专注”、“敏捷”],
8:[“灵巧”、“创意”],
9:[“力量”、“焦点”],
10:[“适应性”、“耐力”],
11:[“敏捷性”、“适应性”],
12:[“法力”、“知识”],
13:[“力量”、“耐力”],
14:[“田径”、“力量”],
15:[“创造力”、“灵巧性”],
16:[“知识”、“法力”],
17:[“敏捷性”、“适应性”]}
工人们:30岁,
"聚焦":1,,
"敏捷":3,,
"耐力":3,,
“知识”:30,
“力量”:8,
"田径":13,,
“灵巧”:6,
"创意":10,,
"适应性":10,,
“智力”:10},
4:{'Mana':29,
"焦点":32,,
“敏捷”:22,
"耐力":28,,
“知识”:21,
“力量”:30,
"田径":20,,
“灵巧”:31,
"创意":31,,
"适应性":8,,
“智力”:18},
5:{'Mana':13,
"聚焦":1,,
“敏捷”:9,
"耐力":27,,
“知识”:9,
“力量”:13,
"田径":15,,
“灵巧”:21,
"创意":16,,
"适应性":13,,
“智力”:28},
6:{'Mana':17,
"聚焦":14,,
“敏捷”:10,
"耐力":17,,
“知识”:13,
“力量”:5,
"田径":10,,
“灵巧”:15,
"创意":一,,
"适应性":11,,
“智力”:4},
7:{'Mana':1,
"聚焦":8,,
"敏捷":6,,
"耐力":27,,
“知识”:11,
“力量”:17,
"田径":30,,
“灵巧”:1,
"创意":五,,
"适应性":11,,
“智力”:5},
8:{'Mana':6,
"聚焦":1,,
“敏捷”:12,
"耐力":30,,
“知识”:20,
“力量”:15,
"田径":30,,
“灵巧”:9,
"创意":17,,
"适应性":30,,
“智力”:19},
9:{'Mana':5,
"聚焦":7,,
“敏捷”:19,
"耐力":5,,
“知识”:22,
“力量”:18,
"田径":26,,
“灵巧”:10,
"创意":24,,
"适应性":20,,
“智力”:22},
10:{'Mana':8,
"聚焦":12,,
“敏捷”:27,
"耐力":3,,
“知识”:17,
"力量":1,,
"田径":5,,
“灵巧”:9,
"创意":7,,
"适应性":29,,
“智能”:1},
11:{'Mana':1,
"聚焦":4,,
“敏捷”:5,
"耐力":30,,
“知识”:16,
“力量”:11,
"田径":28,,
“灵巧”:11,
"创意":五,,
"适应性":12,,
“智力”:4},
12:{'Mana':7,
"聚焦":1,,
“敏捷”:17,
"耐力":25,,
“知识”:23,
"力量":4,,
"田径":8,,
“灵巧”:26,
"创意":15,,
"适应性":29,,
“智力”:22},
13:{'Mana':2,
"聚焦":1,,
“敏捷”:5,
"耐力":21,,
“知识”:24,
“力量”:18,
"田径":20,,
“灵巧”:10,
"创意":12,,
"适应性":30,,
“智力”:5},
14:{'Mana':9,
"聚焦":16,,
“敏捷”:14,
"耐力":25,,
“知识”:14,
“力量”:24,
"田径":30,,
“灵巧”:9,
"创意":19,,
"适应性":23,,
“智力”:18},
15:{'Mana':23,
"聚焦":15,,
“敏捷”:5,
"耐力":12,,
“知识”:24,
“力量”:12,
"田径":20,,
“灵巧”:29,
"创意":五,,
"适应性":19,,
“智力”:12},
17:{'Mana':21,
"焦点":23,,
“敏捷”:30,
"耐力":18岁,,
“知识”:27,
“力量”:7,
"田径":30,,
“灵巧”:10,
"创意":五,,
"适应性":22,,
“智力”:18},
18:{'Mana':11,
"聚焦":11,,
"敏捷":4,,
"耐力":7,,
“知识”:28,
“力量”:11,
"田径":20,,
“灵巧”:28,
"创意":13,,
"适应性":12,,
“智力”:30},
19:{'Mana':11,
"聚焦":11,,
"敏捷":4,,
"耐力":7,,
“知识”:28,
“力量”:11,
"田径":20,,
“灵巧”:28,
"创意":13,,
"适应性":12,,
“智力”:30},
20:{'Mana':15,
"焦点":20,,
"敏捷":28,,
"耐力":22,,
“知识”:18,
“力量”:15,
"田径":23,,
“灵巧”:19,
"创意":20,,
"适应性":27,,
“智力”:20},
21:{'Mana':30,
"聚焦":7,,
“敏捷”:9,
"耐力":7,,
“知识”:30,
"力量":3,,
"田径":6,,
“灵巧”:17,
"创意":四,,
"适应性":11,,
“智力”:28},
22:{'Mana':9,
"聚焦":10,,
"敏捷":28,,
"耐力":26,,
“知识”:1,
“力量”:8,
"田径":5,,
“灵巧”:26,
"创意":一,,
"适应性":14,,
“智力”:16},
23:{'Mana':4,
"聚焦":14,,
“敏捷”:19,
"耐力":5,,
"知识":21,,
"实力":25,,
"田径":12,,
“灵巧”:23,
"创意":26,,
"适应性":21,,
“智力”:22},
24:{'Mana':1,
"聚焦":1,,
“敏捷”:18,
"耐力":24,,
"知识":25,,
"实力":20,,
"田径":9,,
“灵巧”:14,
"创意":19,,
"适应性":30,,
“智力”:7},
25:{'Mana':12,
"聚焦":13,,
"敏捷":21,,
"耐力":23,,
"知识":11,,
"实力":16,,
"田径":18岁,,
“灵巧”:24,
"创意":一,,
"适应性":20,,
“智能”:1},
26:{'Mana':10,
"聚焦":14,,
“敏捷”:12,
"耐力":27,,
"知识":17,,
"实力":24,,
"田径":23,,
“灵巧”:21
import pandas as pd
import numpy as np
# !pip install pulp
import pulp

df_buildings = pd.DataFrame(buildings_dict).T
df_workers = pd.DataFrame(workers_dict).T

# there are a few typos, e.g. Strenght vs. Strength and Knowlege vs. Knowledge
# let's fix this first
df_workers.Knowledge.fillna(df_workers.Knowlege, inplace=True)
df_workers.Strength.fillna(df_workers.Strenght, inplace=True)
del df_workers["Strenght"], df_workers["Knowlege"]

# fixing some notation
workers = df_workers.index.tolist() # list of workers
buildings =  df_buildings.index.tolist() # list of building

# next, we define production matrix
# production[i,j] will contain the productivity of 
# worker i when assigned to building j
# you could vectorize this step, though it seems fast enough here
production = pd.DataFrame(index=workers, columns=buildings)
for i in df_workers.index:
  for j in df_buildings.index:
    production.loc[i, j] = df_workers.loc[i, df_buildings.loc[j]].sum()

print(production.head())
#    1   2   3   4   6   5   7   8   9   10  11  12  13  14  15  16  17
# 3   9  23  36  40  16  16   4  16   9  13  13  60  11  21  16  60  13
# 4  62  28  52  29  48  48  54  62  62  36  30  50  58  50  62  50  30
# 5  14  28  30  22  42  42  10  37  14  40  22  22  40  28  37  22  22
# 6  19  21  28  24  27  27  24  16  19  28  21  30  22  15  16  30  21
# 7  25  41  12  22  57  57  14   6  25  38  17  12  44  47   6  12  17
prob = pulp.LpProblem("MineColoniesProblem", pulp.LpMaximize)

# in the solved problem, assignment[i,j] == 1 whenever i is assigned to j
assignment = pulp.LpVariable.dicts("Assignment", (workers, buildings), cat="Binary")

# our objective is to maximize the sum of production
prob += sum(assignment[i][j] * production.loc[i,j]
            for i in workers for j in buildings)

# each worker can be assigned to at most one building:
for i in workers:
  prob += sum(assignment[i][j] for j in buildings) <= 1

prob.solve()
# make sure that we got an optimal solution
assert prob.status == 1

# generically, we get an integer solution
assignment_dict = {i: j for i in workers for j in buildings
                   if assignment[i][j].varValue == 1}
print(f"Total production is {prob.objective.value()}") # 1401

# here is the the solution
# assignment_dict_saved = {3: 12, 4: 1, 5: 5, 6: 16, 7: 5, 8: 6, 9: 2, 10: 17, 11: 6, 12: 10, 13: 4, 14: 6, 15: 3, 17: 7, 18: 3, 19: 3, 20: 11, 21: 12, 22: 17, 23: 15, 24: 4, 25: 10, 26: 13, 27: 9, 28: 7, 29: 7, 30: 2}
prob = pulp.LpProblem("MineColoniesProblem", pulp.LpMaximize)

# in the solved problem, assignment[i,j] == 1 whenever i is assigned to j
assignment = pulp.LpVariable.dicts("Assignment", (workers, buildings), cat="Binary")

# our objective is to maximize the sum of production
prob += sum(assignment[i][j] * production.loc[i,j]
            for i in workers for j in buildings)

# each worker can be assigned to at most one building:
for i in workers:
  prob += sum(assignment[i][j] for j in buildings) <= 1

# each building has at most one worker
for j in buildings:
  prob += sum(assignment[i][j] for i in workers) <= 1

prob.solve()
# make sure that we got an optimal solution
assert prob.status == 1

# generically, we get an integer solution
assignment_dict = {i: j for i in workers for j in buildings
                   if assignment[i][j].varValue == 1}
# assignment_dict_saved = {3: 16, 4: 9, 7: 5, 8: 2, 10: 11, 11: 6, 12: 10, 14: 14, 18: 3, 19: 8, 20: 17, 21: 12, 23: 15, 24: 4, 26: 13, 27: 1, 29: 7}
print(f"Total production is {prob.objective.value()}") # 929