简体   繁体   中英

Gurobi with Python (Optimization Iteration/Simulation)

I built an optimization model with stochastic random demand (~N(100,40)). My optimization model itself gave me the results which look promising. Now what I try to do as a next step is looping (simulating) this whole optimization problem by generating different normally distributed random demands. But it was not able to append the objective values that I need to derive an expected value after the simulation.

The error code is: Unable to retrieve attribute 'objVal' Any help will be so much appreciated!

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)

You should check the solution status of the optimization process. You can only query the objective value if the optimization terminated successfully:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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