メインコンテンツへスキップ

Round all the elements in a gurobi MVar

回答済み

コメント

1件のコメント

  • 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

サインインしてコメントを残してください。