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

Using dictionaries as objective coefficients

回答済み

コメント

2件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three 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.
  • Matthias Miltenberger
    • 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

投稿コメントは受け付けていません。