Skip to main content

Calculating the Obj Value manually

Answered

Comments

4 comments

  • Official comment
    Simranjit Kaur
    Gurobi Staff Gurobi Staff
    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?.
  • Eli Towle
    Gurobi Staff Gurobi Staff

    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
  • Mohammad Borhani
    Gurobi-versary
    First Comment
    First Question

    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
  • Eli Towle
    Gurobi Staff Gurobi Staff

    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.