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

Adding a constraint with min_ and MVars in Python

回答済み

コメント

2件のコメント

  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Ben,

    I would manually implement the min function.  This involves introducing a binary variable for each x variable:

    b = m.addMVar(x.shape, vtype=gp.GRB.BINARY)

    then introducing the following constraints:

    m.addConstr(x <= params)
    m.addConstr(x <= y)
    m.addConstr(x >= y - y_ub*b)
    m.addConstr(x >= params*b)

    where v_ub is an array of upper bounds on the y variables.  You should aim to make these upper bounds as tight as possible to improve performance.

    Also consider whether there is naturally "upwards pressure" on the x variables, i.e. will they always take the largest value they can in an optimal solution?  If so then you only need the first two constraint families.

    Also note since params is constant you could alternatively enforce the first constraints in the variable definition and this may save some time:

    x = m.addMVar((100, 1), ub=params, vtype=gp.GRB.CONTINUOUS)

    You would of course need to define params before x to use this approach.

    - Riley

     

    0
  • Ben King
    Gurobi-versary
    First Comment
    First Question

    Hi Riley,

    Thank you for your reply, I've implemented this and its working. For my problem there is "downwards pressure" so the lower bound is important but this 4-constraint formulation is great for that.

    Best,
    Ben

    0

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