Minimization with constraint involving logs in Julia
AnsweredI want to solve \(\frac{1}{N} \sum_{i=1}^N x_i^2\) subject to \(\frac{1}{N} \sum_{i=1}^N \log x_i \geq 0\).
In the above, N is the size of the vector x whose \(i^{th}\) element is \(x_i\). I think the solution should be \(x_j = 1\), for all \(j=1,\ldots, N\). How do I code this in Julia? I appreciate that I could replace the constraint involving a sum by a product but in the true application I have in mind (which involves logs) this may not be possible. Basically I want to minimize a function which is convex and straightforward but with a constraint involving logs. I tried the following but it doesn't work. In the below, ncol is the size of n (which is denoted x above)
model = Model(Gurobi.Optimizer)
@variable(model, 1000.0 >= n[1:ncol] >= 0.001)
@objective(model, Min, sum(n[i]^2 for i in eachindex(n)) / ncol)
@expression(model, expr, sum(log(n[i]) for i in 1:ncol))
@NLconstraint(model, expr >= 0.0)
optimize!(model)
Can anyone tell me how I could incorporate a constraint on the log of x? The code is Julia.
Thank you.
-
Official comment
This post is more than two 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. -
See a similar post in the Julia discourse. In your case, you can use the GRBaddgenconstrLog which is documented here: GRBaddgenconstrLog - C API.
Cheers,
David0
Post is closed for comments.
Comments
2 comments