Skip to main content

Round all the elements in a gurobi MVar

Answered

Comments

2 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • Riley Clement
    • Gurobi Staff

    Hi Rahul,

    Please see the following example

    import numpy as np

    m = gp.Model()
    x = m.addMVar((5,5))

    x_up = m.addMVar((5,5), vtype="I")
    x_nearest = m.addMVar((5,5), vtype="I")
    x_down = m.addMVar((5,5), vtype="I")

    m.addConstr(x == np.random.randint(0,1000, (5,5))/100)

    epsilon = 1e-5

    # round up
    m.addConstr(x <= x_up)
    m.addConstr(x_up - 1 + epsilon <= x)

    # round nearest
    m.addConstr(x - 0.5 + epsilon <= x_nearest)
    m.addConstr(x_nearest - 0.5 <= x)

    # round down
    m.addConstr(x <= x_down + 1 - epsilon)
    m.addConstr(x_down <= x)

    m.optimize()

    print("x\n", x.X)
    print("x rounded up\n", x_up.X)
    print("x rounded nearest\n", x_nearest.X)
    print("x rounded down\n", x_down.X)

    Does this make sense?

    - Riley

    0

Post is closed for comments.