Skip to main content

Model logs shows Numerical Issues how to handle this




  • David Torres Sanchez
    Gurobi Staff Gurobi Staff

    Can you try upgrading to v11.0.0 and re-running this test?


  • Sudheer Reddy
    First Comment

    David Torres Sanchez We do have v11.0 lic I will do. In general just want to understand below points

    Model contains large matrix coefficient range what is this meaning. How to know which one are large?  

    It also tells max constraint violation and max general constraint violation, how to know which constraints are violating. 

    The model log tells, Consider reformulating model. Here how to find which part of the constraint I need to reformulate. Is there any way to see which constraints are causing this issue? 



  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Sudheer,

    This is the code snippet I use (which uses Python and pandas)

    import pandas as pd
    import numpy as np

    # assumes your model is "m"

    A = np.abs(m.getA()).tocoo() # absolute-value coefficent matrix in scipy sparse coo_matrix form
    cons = np.array(m.getConstrs())
    df = pd.DataFrame({

    # try to find family of constraints with highest magnitude of coefficients
    #  (you obviously need to tailor this to the coefficients in your model)
    df["name"] = df["constr"].map(lambda c: c.constrname) # add names
    df["family"] = df["name"].map(lambda n: n.split("_")[0]) # assuming name is of the form myconstr_0_1
    df.query("abscoeff > 8e6")["family"].value_counts()

    # try to find family of constraints with smallest magnitude of coefficients
    df.query("abscoeff < 1e-3")["family"].value_counts()

    # investigate violated constraints
    # note this is only looking at linear constraints
    slacks = m.getAttr("Slack", cons)
    df2 = pd.DataFrame({
        "violation":-slacks[slacks < 0],
        "constr":cons[slacks < 0],

    # can sort df2, add names, count family etc

    For guidance on reducing the coefficient range please see the subsections of Tolerances and User Scaling (a section in our Guidelines for Numerical Issues chapter of our reference manual).

    - Riley



Please sign in to leave a comment.