When I run this part of the code, I can get the value of the variable, like mentioned above

print("Obj:", m1.objVal)for w in m1.getVars():    print(f"{w.varName}：{round(w.x, 3)}")

But when I run this part of the code, it shows that the model is unbounded and does not produce the values of the variables.

if m1.status == GRB.status.OPTIMAL:    t1 = m1.runtime    t_MP.append(t1)    delta_sol = []    delta_sol.append(m1.getVarByName('delta').x)    b_sol = []    q_sol = []    x_sol = []    gamma_sol = []    eta_sol = []    psi_sol = []    for i in I:        b_sol.append(m1.getVarByName('b_%s' % i).x)        psi_sol.append(m1.getVarByName('psi_%s' % i).x)    for i in I:        q_sol.append([])        x_sol.append([])        gamma_sol.append([])        for t in T:            q_sol[i].append(m1.getVarByName('q_%s_%s' % (i, t)).x)            x_sol[i].append(m1.getVarByName('x_%s_%s' % (i, t)).x)            gamma_sol[i].append(m1.getVarByName('gamma_%s_%s' % (i, t)).x)    for i in I:        eta_sol.append([])        for j in I:            eta_sol[i].append(m1.getVarByName('eta_%s_%s' % (i, j)).x)    print(t1, t_MP, b_sol, q_sol, x_sol, gamma_sol, psi_sol, delta_sol, eta_sol)else:    print('Optimization was stopped with status %d' % m1.status)
• Gurobi Staff

In this part

print("Obj:", m1.objVal)for w in m1.getVars():    print(f"{w.varName}：{round(w.x, 3)}")

you don't check the status of your model. However, you are lucky, because Gurobi finds a feasible solution before declaring the model as unbounded. Thus you can print the solution point values of the last feasible solution found.

In your second code snippet, you check whether the model status is OPTIMAL. However, the model status is not OPTIMAL, thus you jump into the $$\texttt{else}$$ case and only get the printed message.

If you want to always print the solution point values when a solution point is available independent of the model status, you can check the SolCount attribute

if m1.SolCount > 0:  # print solution

Best regards,
Jaromił

Hi, Jaromił

The model is unbounded, can the attribute 'x' be used in the first part of the code above? Isn't the attribute 'x' only available in the optimal status?

• Gurobi Staff

HI Xiaona,

The attribute X can be retrieved whenever Gurobi finds a feasible solution. In your case, Gurobi finds a feasible solution before declaring the model as unbounded. Thus, the X attribute is available although the model is declared as unbounded. When your model is declared as unbounded, then the solution point values probably don't have any meaning for your application but nevertheless, you can query them.

Best regards,
Jaromił

Hi, Jaromił,

Thank you very much for your answer, which is  helpful to me. I think I should change  the unbounded status of the model.