- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我正在尝试生成所有可能的 worker 到建筑物的组合。 (让我解释一下我的场景):
我在玩 MineColonies在我的世界。在这个模组中,你有殖民者,他们可以在建筑物中分配工作。这些 worker 拥有技能和分配给他们的分数。 (例如敏捷:20,力量:5 等)并且当分配到技能与之相称的殖民者时,建筑物的工作会得到更好的执行...
因此,我创建了一个包含所有 worker 和建筑物的数据库,并希望优化哪些 worker 在哪些建筑物内工作。
buildings_dict = {1: ['Strength', 'Focus'],
2: ['Adaptability', 'Athletics'],
3: ['Knowledge', 'Dexterity'],
4: ['Adaptability', 'Knowledge'],
6: ['Stamina', 'Athletics'],
5: ['Athletics', 'Stamina'],
7: ['Focus', 'Agility'],
8: ['Dexterity', 'Creativity'],
9: ['Strength', 'Focus'],
10: ['Adaptability', 'Stamina'],
11: ['Agility', 'Adaptability'],
12: ['Mana', 'Knowledge'],
13: ['Strength', 'Stamina'],
14: ['Athletics', 'Strength'],
15: ['Creativity', 'Dexterity'],
16: ['Knowledge', 'Mana'],
17: ['Agility', 'Adaptability']}
workers_dict = {3: {'Mana': 30,
'Focus': 1,
'Agility': 3,
'Stamina': 3,
'Knowlege': 30,
'Strenght': 8,
'Athletics': 13,
'Dexterity': 6,
'Creativity': 10,
'Adaptability': 10,
'Intelligence': 10},
4: {'Mana': 29,
'Focus': 32,
'Agility': 22,
'Stamina': 28,
'Knowlege': 21,
'Strenght': 30,
'Athletics': 20,
'Dexterity': 31,
'Creativity': 31,
'Adaptability': 8,
'Intelligence': 18},
5: {'Mana': 13,
'Focus': 1,
'Agility': 9,
'Stamina': 27,
'Knowlege': 9,
'Strenght': 13,
'Athletics': 15,
'Dexterity': 21,
'Creativity': 16,
'Adaptability': 13,
'Intelligence': 28},
6: {'Mana': 17,
'Focus': 14,
'Agility': 10,
'Stamina': 17,
'Knowlege': 13,
'Strenght': 5,
'Athletics': 10,
'Dexterity': 15,
'Creativity': 1,
'Adaptability': 11,
'Intelligence': 4},
7: {'Mana': 1,
'Focus': 8,
'Agility': 6,
'Stamina': 27,
'Knowlege': 11,
'Strenght': 17,
'Athletics': 30,
'Dexterity': 1,
'Creativity': 5,
'Adaptability': 11,
'Intelligence': 5},
8: {'Mana': 6,
'Focus': 1,
'Agility': 12,
'Stamina': 30,
'Knowlege': 20,
'Strenght': 15,
'Athletics': 30,
'Dexterity': 9,
'Creativity': 17,
'Adaptability': 30,
'Intelligence': 19},
9: {'Mana': 5,
'Focus': 7,
'Agility': 19,
'Stamina': 5,
'Knowlege': 22,
'Strenght': 18,
'Athletics': 26,
'Dexterity': 10,
'Creativity': 24,
'Adaptability': 20,
'Intelligence': 22},
10: {'Mana': 8,
'Focus': 12,
'Agility': 27,
'Stamina': 3,
'Knowlege': 17,
'Strenght': 1,
'Athletics': 5,
'Dexterity': 9,
'Creativity': 7,
'Adaptability': 29,
'Intelligence': 1},
11: {'Mana': 1,
'Focus': 4,
'Agility': 5,
'Stamina': 30,
'Knowlege': 16,
'Strenght': 11,
'Athletics': 28,
'Dexterity': 11,
'Creativity': 5,
'Adaptability': 12,
'Intelligence': 4},
12: {'Mana': 7,
'Focus': 1,
'Agility': 17,
'Stamina': 25,
'Knowlege': 23,
'Strenght': 4,
'Athletics': 8,
'Dexterity': 26,
'Creativity': 15,
'Adaptability': 29,
'Intelligence': 22},
13: {'Mana': 2,
'Focus': 1,
'Agility': 5,
'Stamina': 21,
'Knowlege': 24,
'Strenght': 18,
'Athletics': 20,
'Dexterity': 10,
'Creativity': 12,
'Adaptability': 30,
'Intelligence': 5},
14: {'Mana': 9,
'Focus': 16,
'Agility': 14,
'Stamina': 25,
'Knowlege': 14,
'Strenght': 24,
'Athletics': 30,
'Dexterity': 9,
'Creativity': 19,
'Adaptability': 23,
'Intelligence': 18},
15: {'Mana': 23,
'Focus': 15,
'Agility': 5,
'Stamina': 12,
'Knowlege': 24,
'Strenght': 12,
'Athletics': 20,
'Dexterity': 29,
'Creativity': 5,
'Adaptability': 19,
'Intelligence': 12},
17: {'Mana': 21,
'Focus': 23,
'Agility': 30,
'Stamina': 18,
'Knowlege': 27,
'Strenght': 7,
'Athletics': 30,
'Dexterity': 10,
'Creativity': 5,
'Adaptability': 22,
'Intelligence': 18},
18: {'Mana': 11,
'Focus': 11,
'Agility': 4,
'Stamina': 7,
'Knowlege': 28,
'Strenght': 11,
'Athletics': 20,
'Dexterity': 28,
'Creativity': 13,
'Adaptability': 12,
'Intelligence': 30},
19: {'Mana': 11,
'Focus': 11,
'Agility': 4,
'Stamina': 7,
'Knowlege': 28,
'Strenght': 11,
'Athletics': 20,
'Dexterity': 28,
'Creativity': 13,
'Adaptability': 12,
'Intelligence': 30},
20: {'Mana': 15,
'Focus': 20,
'Agility': 28,
'Stamina': 22,
'Knowlege': 18,
'Strenght': 15,
'Athletics': 23,
'Dexterity': 19,
'Creativity': 20,
'Adaptability': 27,
'Intelligence': 20},
21: {'Mana': 30,
'Focus': 7,
'Agility': 9,
'Stamina': 7,
'Knowlege': 30,
'Strenght': 3,
'Athletics': 6,
'Dexterity': 17,
'Creativity': 4,
'Adaptability': 11,
'Intelligence': 28},
22: {'Mana': 9,
'Focus': 10,
'Agility': 28,
'Stamina': 26,
'Knowlege': 1,
'Strenght': 8,
'Athletics': 5,
'Dexterity': 26,
'Creativity': 1,
'Adaptability': 14,
'Intelligence': 16},
23: {'Mana': 4,
'Focus': 14,
'Agility': 19,
'Stamina': 5,
'Knowledge': 21,
'Strength': 25,
'Athletics': 12,
'Dexterity': 23,
'Creativity': 26,
'Adaptability': 21,
'Intelligence': 22},
24: {'Mana': 1,
'Focus': 1,
'Agility': 18,
'Stamina': 24,
'Knowledge': 25,
'Strength': 20,
'Athletics': 9,
'Dexterity': 14,
'Creativity': 19,
'Adaptability': 30,
'Intelligence': 7},
25: {'Mana': 12,
'Focus': 13,
'Agility': 21,
'Stamina': 23,
'Knowledge': 11,
'Strength': 16,
'Athletics': 18,
'Dexterity': 24,
'Creativity': 1,
'Adaptability': 20,
'Intelligence': 1},
26: {'Mana': 10,
'Focus': 14,
'Agility': 12,
'Stamina': 27,
'Knowledge': 17,
'Strength': 24,
'Athletics': 23,
'Dexterity': 21,
'Creativity': 5,
'Adaptability': 5,
'Intelligence': 28},
27: {'Mana': 11,
'Focus': 23,
'Agility': 21,
'Stamina': 12,
'Knowledge': 15,
'Strength': 24,
'Athletics': 17,
'Dexterity': 12,
'Creativity': 1,
'Adaptability': 11,
'Intelligence': 9},
28: {'Mana': 7,
'Focus': 21,
'Agility': 22,
'Stamina': 21,
'Knowledge': 14,
'Strength': 15,
'Athletics': 9,
'Dexterity': 16,
'Creativity': 2,
'Adaptability': 11,
'Intelligence': 5},
29: {'Mana': 12,
'Focus': 25,
'Agility': 29,
'Stamina': 6,
'Knowledge': 7,
'Strength': 10,
'Athletics': 14,
'Dexterity': 15,
'Creativity': 6,
'Adaptability': 13,
'Intelligence': 29},
30: {'Mana': 21,
'Focus': 17,
'Agility': 8,
'Stamina': 21,
'Knowledge': 22,
'Strength': 22,
'Athletics': 26,
'Dexterity': 13,
'Creativity': 15,
'Adaptability': 24,
'Intelligence': 13}}
抱歉,代码块太长,是的,我意识到 ID 不一定正确(希望使其可重现)。
itertools.permutations
将所有 worker 组合到建筑物中:
import itertools
workers_ls = list(workers_dict.keys())
combinations = list(itertools.permutations(workers_ls, len(buildings_dict))
(我打算在之后对组合进行评分)
最佳答案
我假设你想最大化总产量的总和。例如,当没有分配 worker 时,总产量为零(或某个不依赖于 worker 分配的常数)。如果您将 worker 与 Agility
配对2 和 Focus
3 带建筑属性[Agility, Focus]
,您添加 2+3=5
到总产量。
像这样的问题通常用线性规划来解决。我会用 pulp
帮助制定线性规划问题。我还建议查看 Julia
包裹JuMP
.
计算总产量的实际规则可能更复杂。如果(1)您可以定义生产矩阵的一些模拟,并且(2)总产量可以表示为( worker ,建筑)对的生产总和,您仍然可以使用线性规划。
这里有两种方法可以解决这个问题。第一个允许每个建筑物有多个 worker ,第二个不允许。
设置
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
每栋楼允许多名 worker
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}
每栋楼最多一名 worker
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
关于python - 组合太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65992348/
我有一个使用 PHP 和 MariaDB 10.3 的小型大型数据库应用程序。 我有大约 100 个表,大约有 3,000 个 View 。 当超过 1,000 个 View 时,数据库架构就会崩溃并
parsing "\(|.*?)|)" - Too many )'s. 写这个的时候我收到这个错误... private static Regex resourceTextsREGEX = new
我有一个Powershell脚本,它会生成一个包含数据的JSON文件。 我对此文件有问题。它产生两倍的“\”! 你知道我该怎么解决吗? 这是我的生成JSON文件的代码: [ordered]@{ pcn
我不确定为什么会收到此错误,我在不同点使用 str.join() 和 os.path.join()在脚本中,这是原因吗? 使用os.path.join: from os.path import get
一段时间后,在我的应用程序中,似乎出现了一个大问题。有一个来自 Box2D 的 b2Bodys 的构建。我确实在我的应用程序中使用了一些 b2Body 来进行碰撞,但我会说屏幕上一次最多有 10 个。
我正在创建一个包含 6 种不同问题类型的简单数学程序。我想让程序随机显示6种类型中的一种,但有些问题应该出现得比较频繁。我使用加权数组,但从加权数组中选择问题类型后,如果不在 if 语句中使用 10
我想构建一个包含大约 400 个单元的 Controller ,4 列,每列 100 个单元。每个单元格都必须被绘制并响应鼠标事件。这个会不会太重了?我应该为每个单元使用另一种方法,如 CALayer
我是 Haskell 的新手,在编写小程序时,我通常会使用太多的 where 子句来检查函数中的许多内容,因此编写 where 子句是一种很好的做法,或者还有其他好的替代方法吗? 例如,在下面的代码中
我有一个 index.js,其中包含一些导出,每个导出仅包含一个函数。我尝试一次部署其中的几个,CLI 给我以下错误; Error: Too many arguments. Run firebase
我在正则表达式上挣扎了几个小时,似乎没有找到最后一点解决方案。我基本上是逐行解析 C 头文件以查找变量。 以下是我可能遇到的需要传递正则表达式的行的情况: //#define variable_nam
我有一个 html 表单,大约有 1500 个输入字段*(文本或隐藏)。form.action 是 POST 并且每个输入字段都有一个唯一的名称(没有 name=foo[])。 每当我在提交表单后尝试
我很困惑 一劳永逸 VS 添加引用(/net 选项卡)说 dll 的 gac 在这里: 我发现这个包含 GAC 的文件夹:(附注:为什么有 3 个 Gac 类型?) 还有这个包含 GAC 的文件夹:
我有一个实现Comparable的对象列表。 我想对此列表进行排序,这就是我使用Comparable的原因。 每个对象都有一个字段 weight,它由另外 3 个成员 int 变量组成。 对于具有最大
在我们的系统中,有多个“站点”通过 WCF 相互通信。每个站点通过 NetTCP 绑定(bind)公开约 20 个接口(interface)。 当一个站点使用对等站点的接口(interface)时,它
我已经从 http://boost.teeks99.com/ 下载了 boost 1.58.0(预编译,x86,VC 12.0)并安装到C:\local\boost_1_58_0(我也试过自己用msv
所以...我有一个查询,该查询返回在我的网站上使用相同的电子邮件地址、密码和其他信息创建的用户帐户(是的,实现不好,不要问)。它通过从另一个程序获取用户 ID 来实现这一点。 我的 SQL 是 SEL
我知道这是一个有点菜鸟的问题,但我只是想问一下,如果我有太多 Controller ,这是好事还是坏事。假设我有一个网络应用程序,它有大约 12 个 View 。每个 View 都有自己的 Contr
我认为我的项目做了一些可笑的错误。我正在制作一个项目,基本上是一组 View Controller ,其中一些 Controller 上有视频,其他 Controller 上有图像。我创建了一个模型,
嘿,我正在创建一个电子商店并显示类别树和所有产品及其多种价格变化,我制作了 150 多个 mysql_query("SELECT ..."); 在一页上查询。 (如果我计算“while”循环)。 是不
我在 JS 方面遇到了问题。我正在尝试制作按类型排序的三个成分列表(用于酿造药水),所有这些都是标签内的复选框。 您应该选择(选中)三个列表中每一个的一个元素才能酿造一剂药水。如果您选择正确的成分并按
我是一名优秀的程序员,十分优秀!