Python 的 Gurobi(优化 Iteration/Simulation)

Gurobi with Python (Optimization Iteration/Simulation)

我建立了一个具有随机随机需求 (~N(100,40)) 的优化模型。我的优化模型本身给了我看起来很有希望的结果。现在我下一步要做的是通过生成不同的正态分布随机需求来循环(模拟)整个优化问题。但是它无法附加 objective 值,我需要在模拟后得出预期值。

错误代码为:无法检索属性 'objVal' 非常感谢任何帮助!

for i in range(n_samples):
demand = np.random.normal(100, 40, 10)
capacity = np.tile(100, 10)
shortfall = 0 

    m = gp.Model("Chaining Network")

    Network = {}
    Fulfillment = {}
    Lostsale = {}

    for i in range (nr_supplier):
        for j in range (nr_retailer):
            Fulfillment[i,j] = m.addVar()
            Network[i,j] = m.addVar(vtype = GRB.BINARY)
            
    for j in range (nr_retailer):       
        Lostsale[j] = m.addVar()


    m.setObjective (gp.quicksum(Lostsale[j] for j in range(nr_retailer)), GRB.MINIMIZE)   


    m.addConstr(sum(Network[i,j] for i in range (nr_supplier) for j in range(nr_retailer)) <= maxnet)
 
    for i in range(nr_supplier):
        for j in range(nr_retailer):
            m.addConstr(Fulfillment[i,j] <= bigM*Network[i,j])
         

    for j in range (nr_retailer):
        m.addConstr(sum(Fulfillment[i,j] for i in range(nr_supplier)) + Lostsale[j] >= demand[j] )


    for i in range (nr_supplier):
        m.addConstr(sum(Fulfillment[i,j] for j in range(nr_retailer)) <= capacity[i] )

    for j in range(nr_retailer):
         m.addConstr(Lostsale[j] == demand[j] - sum(Fulfillment[i,j] for i in range(nr_supplier)))

    for i in range(nr_supplier):
        for j in range(nr_retailer):
            if i == j:
                m.addConstr(Fulfillment[i,j] == min(demand[j], capacity[i]))
        
    m.optimize()
    res = m.objVal
    shortfall =+ res

estimate = np.mean(shortfall)/n_samples
print(estimate)

您应该检查优化过程的解决方案状态。如果优化成功终止,您只能查询 objective 值:

if m.status == GRB.OPTIMAL:
    print('Optimal objective: %g' % m.objVal)
elif m.status == GRB.INF_OR_UNBD:
    print('Model is infeasible or unbounded')
    sys.exit(0)
elif m.status == GRB.INFEASIBLE:
    print('Model is infeasible')
    sys.exit(0)
elif m.status == GRB.UNBOUNDED:
    print('Model is unbounded')
    sys.exit(0)
else:
    print('Optimization ended with status %d' % m.status)
    sys.exit(0)