Calculating the Obj Value manually in Gurobi

I have been working on the problem on Facility Location Problem with Gurobi using the example provided by the Gurobi team.


m = gp.Model('facility_location')

select = m.addVars(num_facilities, vtype=GRB.BINARY, name='Select') # y_i
assign = m.addVars(cartesian_prod, ub=1, vtype=GRB.BINARY, name='Assign') # x_ij

m.addConstrs((assign[(c,f)] <= select[f] for c,f in cartesian_prod), name='Setup2ship')
m.addConstrs((gp.quicksum(assign[(c,f)] for f in range(num_facilities)) == 1 for c in range(num_customers)), name='Demand')
m.addConstr(gp.quicksum(select[f] for f in range(num_facilities)) == 9 , name='numberoffacilities')

m.setObjective(select.prod(setup_cost)+assign.prod(shipping_cost), GRB.MINIMIZE)

m.optimize()



Everything is fine with the optimization programming and the result. However, I am designing an approximation algorithm to solve the problem for large instances.

In Gurobi, by using

obj = m.getObjective()

print(obj.getValue())


I can access to obj value after the accomplishment of the optimization. Moreover, the numeric result for decision variables is also provided by

m.getVars()


Now, my problem is that my approximation algorithm outputs different values for decisions variable ( different select and assignment values for nodes).

May I use the Gurobi so as to calculate the obj value of the optimization problem using the output of my approximation algorithm? In other words, I want to pass the value of the decision variables manually to Gurobi and get the obj value of the input.

• Fix variables to your heuristic values and solve. Jul 28 at 8:37

I think one way to accomplish what you want is to pass your solution as a MIP Start to gurobi. If it's feasible, gurobi will report the objective value of that solution. Here is Gurobi's documentation on MIP Start. From the link:

When you run the example, the MIP solver reports that the start produced a feasible initial solution:

User MIP start produced solution with objective 210500 (0.01s)