Calculating the Obj Value manually
AnsweredHello,
I have been working on the problem on Facility Location Problem with Gurobi using the example provided by the Gurobi team in the below link.
# MIP model formulation
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.
Sincerely,
Mohammad
-
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 why not try our AI Gurobot?. -
The easiest way to handle this is to write your own Python function that evaluates the objective function for any solution. For example:
def evaluate_obj(ysol, xsol):
"""Evaluate objective function for dicts of select and assign values"""
obj = 0
for i, yval in ysol.items():
obj += yval*setup_cost[i]
for ij, xval in xsol.items():
obj += xval*shipping_cost[ij]
return obj
# Example usage with dummy solution
yvals = {i: random.random() for i in range(num_facilities)}
xvals = {(i, j): random.random() for i in range(num_customers) for j in range(num_facilities)}
print(evaluate_obj(yvals, xvals))1 -
Dear Eli,
Thank you for your suggestion. It seems to be a logical approach. Moreover, I conducted a deep search through the Gurobi documentation.
I believe that passing my solution to gurobi as a MIP Start is another approach to achieve my goal.
If it's possible, gurobi will publish the solution's objective value.
The documentation for MIP Start by Gurobi may be found here.User MIP start produced solution with objective 210500 (0.01s) Loaded user MIP start with objective 210500
I have only one question then. When I assigned the start value to the decision variable (using a python script and fetching the final result from approximation algorithm), i.e., assign.start and select.start), Is it possible to print the initial value assignment to decision variable so as to check that everything is correct before start optimizing?
When I try to print, for instance, the assign variable, I got the following info only:
for p in assign:
print(assign[p])# Output
<gurobi.Var *Awaiting Model Update*>0 -
Is it possible to print the initial value assignment to decision variable so as to check that everything is correct before start optimizing?
Unfortunately not. Gurobi does not process the MIP start until the user calls Model.optimize().
After starting the optimization process, you can examine the solution Gurobi constructs from your MIP start with one of the following:
- Call Model.cbGetSolution() inside of a MIPSOL callback. This is done in the tsp.py example.
- Set the SolFiles parameter. This directs Gurobi to immediately write new incumbent solutions to disk, including those produced by user-provided MIP starts.
0
Post is closed for comments.
Comments
4 comments