Objective function with Log and implementation of addGenConstrLog
AnsweredDear all,
I am a bit new to GUROBI and i want to implement this objective function in python:

The main objective function is a weighted sum approach and y_5 is one part of it.
I know I have to implement addGenConstrLog and to define auxiliary variables for this but I am not sure if I have the correct approach. I think my definition of the auxiliary variables in the loops is not correct. Can anyone help?
This is my code so far:
model = Model()
x = model.addVars(C, M, vtype = GRB.CONTINUOUS, name = "x")
model.addConstrs(sum(x[c,m] for m in M) == 1 for c in C)
for t in T:
for c in C:
for m in M:
for s in range(len(area_share.loc[(area_share.i == cells[c]) & (area_share.m == m) , T[t]].values)):
x_log = model.addVars(C, M, vtype = GRB.CONTINUOUS, name = "x_log")
model.addConstrs(sum(x_log[c,m] for m in M) == 1 for c in C)
x5 = model.addVar()
model.addConstr(x5 == area_share.loc[(area_share.i == cells[c])
& (area_share.m == m) , T[t]].values[s]*x_log[c, m])
y5_log = model.addVar()
model.addGenConstrLog(x5, y5_log)
y5 = model.addVar()
model.addConstr(y5 == area_share.loc[(area_share.i == cells[c])
& (area_share.m == m) , T[t]].values[s]*x[c, m]*y5_log)
y1 = sum(sum(cf.loc[(cf.i == cells[c]) & (cf.m == m) , T[t]].values*x[c, m])
for c in C for m in M for t in T)
y2 = sum(sum(timber_harvested.loc[(timber_harvested.i == cells[c]) & (timber_harvested.m == m) , T[t]].values*x[c, m])
for c in C for m in M for t in T)
y3 = sum(sum(c_binding.loc[(c_binding.i == cells[c]) & (c_binding.m == m) , T[t]].values*x[c, m])
for c in C for m in M for t in T)
y4 = sum(sum(water.loc[(water.i == cells[c]) & (water.m == m) , T[t]].values*x[c, m])
for c in C for m in M for t in T)
y1_w = 0.6
y2_w = 0.1
y3_w = 0.1
y4_w = 0.1
y5_w = 0.1
model.setObjective(
y1_w*y1 + y2_w*y2 + y3_w*y3 + y4_w*y4 - y5_w*y5,
GRB.MAXIMIZE
)
model.optimize()
model.printAttr("x")
model.ObjVal
0
-
Official comment
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. -
Hi Robert,
Your auxiliary variables look fine.
Note that you redefine \(\texttt{y5}\) in every \(\texttt{for}\)-loop iteration. Meaning that the last \(\texttt{y5}\) value is used in your objective function. I would guess that you are looking for something like
y5 = QuadExpr(0)
for
for
for
for
#[...]
y5.add(x[c, m]*y5_log, area_share.loc[(area_share.i == cells[c]) & (area_share.m == m) , T[t]].values[s])Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments