如何解决如何格式化分区问题的幸福来读取幸福表?
我正在尝试专门修改此链接 https://www.coin-or.org/PuLP/CaseStudies/a_set_partitioning_problem.html 中的代码,我不知道如何格式化 happiness
定义,以便它可以读取每个分区的附加表以获得快乐。>
代码如下:
import pulp
max_tables = 5
max_table_size = 4
guests = 'A B C D E F G I J K L M N O P Q R'.split()
def happiness(table):
"""
Find the happiness of the table
- by calculating the maximum distance between the letters
"""
return abs(ord(table[0]) - ord(table[-1]))
#create list of all possible tables
possible_tables = [tuple(c) for c in pulp.allcombinations(guests,max_table_size)]
#create a binary variable to state that a table setting is used
x = pulp.LpVariable.dicts('table',possible_tables,lowBound = 0,upBound = 1,cat = pulp.LpInteger)
seating_model = pulp.LpProblem("Wedding Seating Model",pulp.LpMinimize)
seating_model += sum([happiness(table) * x[table] for table in possible_tables])
#specify the maximum number of tables
seating_model += sum([x[table] for table in possible_tables]) <= max_tables,\
"Maximum_number_of_tables"
#A guest must seated at one and only one table
for guest in guests:
seating_model += sum([x[table] for table in possible_tables
if guest in table]) == 1,"Must_seat_%s"%guest
seating_model.solve()
print "The choosen tables are out of a total of %s:"%len(possible_tables)
for table in possible_tables:
if x[table].value() == 1.0:
print table
我正在想办法为幸福做些什么。就我而言,我在 excel 中有下表。在表格中显示顾客对什么感到满意。 2s必须在一起,1s可以在一起,空白不能在一起:
Components | A | B | C | D | E | F | G |
-----------+---+---+---+---+---+---+---+
A | | | 1 | 1 | 2 | 1 | |
-----------+---+---+---+---+---+---+---+
B | | | | | 1 | | 1 |
-----------+---+---+---+---+---+---+---+
C | 1 | | | | | 1 | |
-----------+---+---+---+---+---+---+---+
D | 1 | | | | | 1 | 1 |
-----------+---+---+---+---+---+---+---+
E | 2 | 1 | | | | | 1 |
-----------+---+---+---+---+---+---+---+
F | 1 | | 1 | 1 | | | 1 |
-----------+---+---+---+---+---+---+---+
G | | 1 | | 1 | 1 | 1 | |
-----------+---+---+---+---+---+---+---+
现在我正在考虑为每位顾客列出一份清单。例如;
A = [0,1,2,0]
B = [0,1]
c = [1,0]
...
return sum(A,B,C...)
或者我在想像字典一样的东西,每个顾客都嵌套在一起;
d = ["A":[0,0],"B";[0,1],"C";[1,...}
return sum(d)
在这两种情况下,它通过查看相关深度的值来查找幸福感并返回要求和的值。例如:通过分区 E 查找分区 A 的幸福度,我们在与分区 A 关联的列表中找到深度为 4(从 0 开始计数)处的 2。
有没有更简单或更好的方法来做到这一点?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。