Objective function gives 0 (Integer problem)

403 views
Skip to first unread message

khan

unread,
May 30, 2018, 7:00:55 AM5/30/18
to Gurobi Optimization
Hi everyone,

Can somebody tell me why my objective function is giving 0 in the output. In fact, everything is zero in the output. Here is my Python code.


 m = Model("mip1")
    var = [ "a", "b", "c", "d", "e", "f" ]
    order = [ "c", "a", "v", "d", "c", "a", "b", "b", "f", "a", "d", "f" ]
    p = {}
    q = {}
    perms = {}
    temp = list(permutations(var))
   
    for i in range(0, len(temp)):
    perms[i] = list(temp[i])

    # Create variables
    for i in range(len(perms)):
      for j in range(len(var)):
    perms[i][j] = m.addVar(lb=0, vtype=GRB.CONTINUOUS, name="v%d%d" % (i,j))
   
    for i in range(len(order)):
      order[i] = m.addVar(lb=0, vtype=GRB.CONTINUOUS, name="s%d" % i)
   
    for i in range(len(order)):
      p[i] = m.addVar(lb=0, vtype=GRB.CONTINUOUS, name="p%d" % i)
   
    for i in range(len(order)):
      q[i] = m.addVar(lb=0, vtype=GRB.CONTINUOUS, name="q%d" % i)
  
   # set model sense
    m.ModelSense = GRB.MINIMIZE
   
    # set objective function
    for p_i in range(len(perms)):
      m.setObjective(quicksum( p[k] + q[k] for k in range(1,len(order)-1)))
     
      #constraints
      for k in range(1,len(order)-1):
    m.addConstr( (p[k] >= 0) , "c0")
        m.addConstr( (q[k] >= 0), "c1")
        m.addConstr( (perms[p_i].index(order[k]) - perms[p_i].index(order[k-1]) + p[k] - q[k] == 0), "c2")
   
    # optimize the model
    m.optimize()

    for v in m.getVars():
        print('%s %g' % (v.varName, v.x))

    print('Obj: %g' % m.objVal)

Any help/comment on this will ber very much appreciated.
Thanks.

Khan

Tobias Achterberg

unread,
May 30, 2018, 7:10:48 AM5/30/18
to gur...@googlegroups.com
You are minimizing the sum of non-negative variables, and the all-zero solution is
feasible for all of your constraints. Hence, it is optimal. Why do you think a different
solution should be optimal to your problem?

Tobias

Asif Ali Khan Lecturer DCSE

unread,
May 31, 2018, 11:09:37 AM5/31/18
to gur...@googlegroups.com
Hi Tobia,

Thank you for your feedback. Constraint 2 "c2" in my model makes sure the solution is not 0. I am subtracting two indices and to avoid negative, I have introduced p and q one of which must always be non-zero in order to satisfy c2.
In the objective function, I am adding p and q. That's why 0 should not be the solution.
correct me if I am wrong, please.

Here is my mathematical formulation.


s[i] refers to the index of s[i] in list servers (see python code for details please).

Khan

On Wed, May 30, 2018 at 1:10 PM, 'Tobias Achterberg' via Gurobi Optimization <gur...@googlegroups.com> wrote:
You are minimizing the sum of non-negative variables, and the all-zero solution is feasible for all of your constraints. Hence, it is optimal. Why do you think a different solution should be optimal to your problem?

Tobias


--

--- You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tobias Achterberg

unread,
Jun 1, 2018, 11:59:13 AM6/1/18
to gur...@googlegroups.com
Ahh... you are trying to use something like the "element" constraint from Constraint
Programming. This does not work in MIP. I wonder why you do not get an error message in
your code.

When you write "perms[p_i].index(order[k])" with "order[k]" being a Gurobi model variable,
I guess you are hoping that this yields the index in the perms array that corresponds to
the MIP solution value of variable "order[k]".

This is not supported. You cannot pass Gurobi model variables to those general purpose
Python methods. You can only use them to create constraints and to query solution values
after you have called optimize().

Regards,

Tobias
Reply all
Reply to author
Forward
0 new messages