从目标函数Pyomo打印求解器项的结果

时间:2018-08-06 13:13:42

标签: python python-3.x solver pyomo

当我想使用规划求解结果打印一些具体值时,我会遇到一些问题。

这是我的目标功能:

def obj2_rule(model):
    return sum(sum(model.htotal1[k] + model.htotal2[k] + model.htotal3[k] - sum(sum(model.hreq1[p,j] * (sum(model.x[p,k,j] for k in sequence(t)) - sum(model.x[p,k,j] for k in sequence(t - model.time[p,j]))) + model.hreq2[p,j] * (sum(model.x[p,k,j] for k in sequence(t)) - sum(model.x[p,k,j] for k in sequence(t - model.time[p,j]))) + model.hreq3[p,j] * (sum(model.x[p,k,j] for k in sequence(t)) - sum(model.x[p,k,j] for k in sequence(t - model.time[p,j]))) for j in model.MODELS) for p in jobs_per_station[k]) for k in model.ESTATIONS) for t in model.PERIODS)
model.obj2 = Objective(rule=obj2_rule)

在该目标函数中,我想单独打印此“额外变量”,因为目标函数针对模型中的每个t返回它们的和。PERIODS:

h_idle[t] = sum(model.htotal1[k] + model.htotal2[k] + model.htotal3[k] - sum(sum(model.hreq1[p,j] * (sum(model.x[p,k,j] for k in sequence(t)) - sum(model.x[p,k,j] for k in sequence(t - model.time[p,j]))) + model.hreq2[p,j] * (sum(model.x[p,k,j] for k in sequence(t)) - sum(model.x[p,k,j] for k in sequence(t - model.time[p,j]))) + model.hreq3[p,j] * (sum(model.x[p,k,j] for k in sequence(t)) - sum(model.x[p,k,j] for k in sequence(t - model.time[p,j]))) for j in model.MODELS) for p in jobs_per_station[k]) for k in model.STATIONS)

对于模型中的每个t。PERIODS

我要打印这些“变量”:h_idle[1], h_idle[2] ....
拜托,有什么想法吗? 预先感谢。

1 个答案:

答案 0 :(得分:1)

调用求解器后,结果将自动加载回模型中的变量中。您可以使用value函数打印单个值。

for t in model.PERIODS:
    h_idle_t = YOUR LONG EXPRESSION
    print('h_idle[' + str(t) + '] = ', value(h_idle_t))