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

Using dictionaries as objective coefficients

回答済み

コメント

1件のコメント

  • Matthias Miltenberger
    • Gurobi Staff Gurobi Staff

    Hi Bing,

    This should do the trick:

    import gurobipy as gp

    m = gp.Model()

    d = {(111,222,333):20, (111,222,444):30, (111,222,555):10, (111,222,666):0}
    k1 = {(111,222,333):0.5, (111,222,444):0.1, (111,222,666):0.25, (111,222,555):0.3}
    k2 = {(111,222,333):0.1, (111,222,555):0.3, (111,222,666):0.1, (111,222,444):0.5}
    k3 = {(111,222,444):0.4, (111,222,555):0, (111,222,666):0.2, (111,222,333):0.6}

    x_d = m.addVars(d, name="d")
    x_d_abs = m.addVars(d, name="d_abs")
    x_k1 = m.addVars(k1, name="k1")
    x_k2 = m.addVars(k2, name="k2")
    x_k3 = m.addVars(k2, name="k3")

    for i in d.keys():
        m.addConstr(x_d[i] == d[i] - k1[i]*x_k1[i] - k2[i]*x_k2[i] - k3[i]*x_k3[i])
        m.addGenConstrAbs(x_d_abs[i], x_d[i])

    m.setObjective(gp.quicksum(x_d_abs))
    m.write("dict.lp")

    You just have to define a set of auxiliary variables to hold the absolute values for the objective.

    I added a call to write() so you can inspect the final model to see whether this is actually correct.

    Cheers,
    Matthias

    0

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