gpt4 book ai didi

pyomo 隐式替换 Component 属性

转载 作者:行者123 更新时间:2023-12-02 20:24:08 25 4
gpt4 key购买 nike

我正在研究一个 pyomo 优化脚本。我的限制之一是隐式替换。寻找有关如何解决问题的想法。
数据通过 Python 列表和字典从数据库加载到模型对象中。以下是参数、变量和目标函数以及约束条件。有问题的约束是带有大写 ID 的。

#Sets
#clinics
model.C = Set(initialize=list(clinics.keys()))

#clients
model.B = Set(initialize=list(client_blocks.keys()))

#scalar params
model.clt_stf_max = 50
model.tt_max = 45
model.stf_max = sum(staff_cap.values())
model.tot_unsrvd_clients = sum(client_blocks.values())
model.z_M = 5000
model.e_M = 500

#indexed params
model.cnc_stf_cap = Param(C, initialize=staff_cap)
model.clt_blk = Param(B, initialize=client_blocks)
model.trav_time = Param(T, initialize=trav_time)

#decision vars
#x new staff at each clinic
model.new_stf = Var(model.C, domain=NonNegativeIntegers, initialize=0)

#y new clients at each clinic/block combo
model.new_clt = Var(model.C, model.B, domain=NonNegativeIntegers, initialize=0)

#z clinic has client(s)
model.z = Var(model.C, model.B, domain=Binary, initialize=0)

#e clinic has staff
model.e = Var(model.C, domain=Binary, initialize=0)

#objective function
def o_min_tt_rule(model):
return sum(model.trav_time[c,b]*model.new_clt[c,b] for c in model.C for b in model.B)
model.o_min_tt = Objective(rule=o_min_tt_rule, sense=minimize)

#constraints
# limit new clients at clinic to staff capacity
def limit_clients_to_clinic_staff_cap_rule(model, c):
return sum(model.new_clt[c,b] for c in model.C for b in model.B) <= (model.cnc_stf_cap[c] * model.clt_stf_max)
model.limit_clients_to_clinic_staff_cap = Constraint(model.C, rule=limit_clients_to_clinic_staff_cap_rule)

#total of new clients served in block should not exceed the number new clients in the block
def limit_newclient_block_rule(model, b):
return sum(model.new_clt[c,b] for c in model.C for b in model.B) <= (model.clt_blk[b])
model.limit_newclient_block = Constraint(model.B, rule=limit_newclient_block_rule)

#limit new clients to selected clinics
def client_to_selected_clinic_rule(model, c, b):
return model.new_clt[c,b] <= model.z[c,b] * model.z_M
model.client_to_selected_clinic = Constraint(model.C, model.B, rule=client_to_selected_clinic_rule)

#limit single client travel time to max travel time minutes
def limit_client_travtime_rule(model, c, b):
return (model.trav_time[c,b] * model.z[c,b]) <= model.tt_max
model.limit_client_travtime = Constraint(model.C, model.B, rule=limit_client_travtime_rule)

#limit selected clinics to max number
def staff_to_selected_clinic_rule(model):
return summation(model.e) <= model.selected_clinic_max
model.staff_to_selected_clinic = Constraint(rule=staff_to_selected_clinic_rule,)

#THIS CAUSES THE ERROR
#limit new staff to selected clinics
def staff_to_selected_clinic_rule(model, c):
return model.new_stf[c] <= model.e[c] * model.e_M
model.staff_to_selected_clinic = Constraint(model.C, rule=staff_to_selected_clinic_rule)

#limit new staff at clinic to clinic capacity
def limit_staff_to_clnic_cap_rule(model, c):
return model.new_stf[c] <= model.cnc_stf_cap[c]
model.limit_staff_to_clnic_cap = Constraint(model.C, rule=limit_staff_to_clnic_cap_rule)

#limit total new staff to staff max
def limit_tot_staff_rule(model):
return summation(model.new_stf) <= model.stf_max
model.limit_tot_staff = Constraint(rule=limit_tot_staff_rule)

最佳答案

问题是您有两个名为 model.staff_to_selected_clinic 的约束。只需更改其中之一的名称(连同相关规则)。

关于pyomo 隐式替换 Component 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50456873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com