How to add log and exponential term in objective function?
Answeredhi,
I am very new to using gurobi and have the following issue.
I would like to add log(1+exp((-1)^y_i * (sum over k=1 for K (beta_k * a _ik)) to my objective function.
To be precise, I would like to minimize over beta while summing over i in range(n)(log(1+exp((-1)^y_i * (sum over k=1 for K (beta_k * a _ik)) - y-i (sum over k in K) beta_k*a_ik)
y is a [n,1] vector
a is a [n,k] matrix where i in range(n)
b is a vector representing coefficients with [n,k] for k in range(K)
I know, that log and exp do not exist in gurobi per se but can be achieved by auxiliary variables. The following approach has not been successful:
#z = exp(w)
w = (quicksum ([beta[k]*a[i,k]for k in range(K)]) for i in range(n))
gz = m.addGenConstrExp(beta, w)
I appreciate your help! Thank you.
-
Hi Lena,
Gurobi does not natively support nonlinear function such as \(\log, \exp\). However, supports piecewise-linear approximations of these functions. You can add a constraint \(y = \log(x)\) via the addGenConstrLog method and a constraint \(y = \exp(x)\) via the addGenConstrExpr method. Note that both method accept only a single variable as input and output. This means that you have to introduce auxiliary variables in order to work with general constraints. For example, in order to implement \(\min x + \exp(\sum_i y_i)\), you have to add 2 auxiliary variables and 2 constraints to model it as
\[\begin{align*}
\min\,\,\, &x + z\\
\text{s.t.}\,\,\, & z = \exp(w)\\
& w = \sum_i y_i
\end{align*}\]The only issue I see is the modeling of the term \((-1)^{y}\). Gurobi currently does not support a power term with negative base, cf. addGenConstrExpA method. So unless you can reformulate your objective expression, it is not possible to model it in Gurobi at the moment.
Best regards,
Jaromił0 -
Hi Jaromił,
Thanks for making this post. I am struggling to find any documentation for how to add this constraint (addGenConstrExpA) in Julia (JuMP specifically). Is there a page that I am missing?
Thanks
0 -
Hi Ryan,
I am not a Julia or JuMP user. However, according to the github repo of Gurobi.jl, the function addGenConstrExpA is supported, cf. Gurobi.jl/libgrb_api.jl.
Best regards,
Jaromił0
Please sign in to leave a comment.
Comments
3 comments