如何格式化分区问题的幸福来读取幸福表?

问题描述

我正在尝试专门修改链接 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 (将#修改为@)