The solution returned by GUROBI is not optimal
AnsweredI try to solve my Optimization problem using Gurobi in Python but the answer does not make sense. I solve the problem using MATLAB CVX library with MSOEK solver to get an answer (solution should be around 60; it will vary around 60 as D is a random sample).

I am wondering what could be the reason behind that:
Please see my Python code below
import gurobipy as gp
from gurobipy import GRB
import numpy as np
from numpy.random import randint
# index
Ind=np.linspace(0, 999,num=1000, dtype = int)
DD=np.linspace(0, 100,num=101)
d = randint(0, 100, 1000)
D=DD[d]
eta=50
c=10
p=15
s=7
c_=c-s
p_=p-s
lamda=0.2
B=1/2
pro=1/1000
m=gp.Model()
x=m.addVar(vtype='c',lb=0, name='x')
w=m.addVars(Ind,vtype='c',lb=0, name='w')
v=m.addVars(Ind,vtype='c',lb=0, name='v')
Z=m.addVars(Ind,vtype='c', name='Z')
obj=pro*gp.quicksum( Z[k] for k in Ind)-lamda*pro*gp.quicksum((1-B)*w[k]+B*v[k] for k in Ind)
m.setObjective(obj, GRB.MAXIMIZE)
m.addConstrs(Z[k]== eta+v[k]-w[k] for k in Ind)
m.addConstrs(Z[k]<=-c_*x+p_*D[k] for k in Ind)
m.addConstrs(Z[k]<=-c_*x+p_*x for k in Ind)
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
By default, variables added via Model.addVar() or Model.addVars() have a lower bound of \( 0 \). If the \( Z \) variables are free, you should define them as follows:
Z = m.addVars(Ind, lb=-GRB.INFINITY, vtype='C', name='Z')
With this change, the optimal value of \( x \) is around \( 60 \).
The only other difference I see between the mathematical formulation and your code is that the formulation includes \( p_k \) in the objective function summations, where your code instead uses a single \( \texttt{pro} \) value. Of course, this isn't an issue if all of the \( p_k \) are equal to \( \texttt{pro} \).
1 -
Thank you so much. That fixes the problem!
Yes, pro is constant as they have a similar probability.
0
Post is closed for comments.
Comments
3 comments