简体   繁体   English

Python的纸浆不匹配变量dict值

[英]python pulp not matching value of variable dict

I am struggling to simplify the constraint for a few days now. 我正在努力简化约束几天。 still learning python. 还在学习python。 please help and thank you in advance. 请帮助并提前感谢您。

    Employees=['Paul', 'Ben', 'Nasim', 'Ceci', 'Victoria',]
    Days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',      
    'Saturday', 'Sunday']

    avail = pulp.LpVariable.dicts("on_off", ((employee, day) for     
    employee in Employees for day in Days), cat="Binary")

    requests={"Paul": {"Monday":1, "Tuesday":1, "Wednesday":1,   
    "Thursday":1, "Friday":1, "Saturday":1, "Sunday":1},
    "Ben": {"Monday":1, "Tuesday":1, "Wednesday":1, "Thursday":1, 
    "Friday":1, "Saturday":1, "Sunday":0},
    "Nasim": {"Monday":1, "Tuesday":1, "Wednesday":1, "Thursday":1, 
    "Friday":1, "Saturday":1, "Sunday":1},
    "Ceci": {"Monday":1, "Tuesday":1, "Wednesday":1, "Thursday":1, 
    "Friday":1,"Saturday":1, "Sunday":0},
    "Victoria": {"Monday":1, "Tuesday":1, "Wednesday":0, "Thursday":0, 
    "Friday":0, "Saturday":0, "Sunday":0}}



    for employee, day in avail:
            prob += avail[employee, day] == [requests[i][j] for i in    
            requests for j in requests[i]]

as an example, the first few constraints: 例如,前几个约束:

"_C13: on_off_('Paul',_'Monday') = 28
_C14: on_off_('Paul',_'Tuesday') = 28
_C15: on_off_('Paul',_'Wednesday') = 28
_C16: on_off_('Paul',_'Thursday') = 28
_C17: on_off_('Paul',_'Friday') = 28
_C18: on_off_('Paul',_'Saturday') = 28" 

("28" is just so happened to be the SUM of all the 1's and the 0's in my nested dict.) (在我的嵌套字典中,“ 28”恰好是所有1和0的总和。)

Instead i like to have each variable matching 1's and 0's from my nested dict. 相反,我喜欢让嵌套字典中的每个变量都匹配1和0。

"_C13: on_off_('Paul',_'Monday') = 1
_C14: on_off_('Paul',_'Tuesday') = 1
_C15: on_off_('Paul',_'Wednesday') = 1
_C16: on_off_('Paul',_'Thursday') = 1
_C17: on_off_('Paul',_'Friday') = 1
_C18: on_off_('Paul',_'Saturday') = 1"

After the == sign you want the value 1 or 0 from the dictionary, therefore you should use: 在==符号后,您希望字典中的值为1或0,因此应使用:

for employee, day in avail:
    prob += avail[employee, day] == requests[employee][day]

With requests[employee] you select the dictionary for that employee 通过请求[员工],您可以为该员工选择词典

requests['Paul'] = {"Monday":1, "Tuesday":1, "Wednesday":1,   
"Thursday":1, "Friday":1, "Saturday":1, "Sunday":1}

Then by adding [day] you select the day from that dictionary: 然后通过添加[day]从该词典中选择日期:

 requests['Paul']['Monday'] = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM