# Entropy maximization with nonlinear constraint

**URL:** https://discourse.julialang.org/t/entropy-maximization-with-nonlinear-constraint/73104
**Category:** Optimization (Mathematical)
**Tags:** jump, optimization, nonlinear, nlopt
**Created:** [December 14, 2021, 11:35pm UTC](https://discourse.julialang.org/t/entropy-maximization-with-nonlinear-constraint/73104 "2021-12-14T23:35:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mvsoom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mvsoom/32/31051_2.png) [@mvsoom](https://discourse.julialang.org/u/mvsoom)
#### Post date: [December 14, 2021, 11:35pm UTC](https://discourse.julialang.org/t/entropy-maximization-with-nonlinear-constraint/73104/1 "2021-12-14T23:35:24Z")

</div>

I’m trying to solve the following entropy maximization problem:

\max\_p -\sum\_{ij} p\_{ij} \log p\_{ij}

s.t.

p \in \mathbb{R}^{n \times n} \\ p\_{ij} = p\_{ji} \\ 0 \leq p\_{ij} \leq 1 ~ \forall (i,j) \\ \sum\_{ij} p\_{ij} = 1 \\ \frac{\sum\_i p\_{ii} - \sum\_i (\sum\_j p\_{ij} )^2}{1-\sum\_i (\sum\_j p\_{ij} )^2} = r

The problem depends on the given parameter r \in [0,1] which governs the nonlinear constraint.

The objective function is bounded, n is on the order of a few dozens, I have a heuristic which provides a starting point, and I’m happy with a local optimum, so I guess this should be a feasible problem.

In the JuMP docs there is an example of [entropy maximization](https://jump.dev/JuMP.jl/stable/tutorials/conic/tips_and_tricks/#Example:-Entropy-Maximization), but this uses a linear constraint of the form A p \leq b. I couldn’t succeed in adapting it to the nonlinear constraint with `NLopt`. Before I dig in deeper, I wanted to ask: is this type of problem even possible in JuMP? Thank you!

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [December 14, 2021, 11:36pm UTC](https://discourse.julialang.org/t/entropy-maximization-with-nonlinear-constraint/73104/2 "2021-12-14T23:36:52Z")

</div>

> is this type of problem even possible in JuMP?

Yes, see [Nonlinear Modeling (Legacy) · JuMP](https://jump.dev/JuMP.jl/stable/manual/nlp/)

---

<div class="post-metadata">

### Author: ![mvsoom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mvsoom/32/31051_2.png) [@mvsoom](https://discourse.julialang.org/u/mvsoom)
#### Post date: [December 14, 2021, 11:41pm UTC](https://discourse.julialang.org/t/entropy-maximization-with-nonlinear-constraint/73104/3 "2021-12-14T23:41:23Z")

</div>

Thanks for the answer. Is it expected that I can use the exponential cone transformation as in the [entropy maximization example](https://jump.dev/JuMP.jl/stable/tutorials/conic/tips_and_tricks/#Example:-Entropy-Maximization) (mentioned above) with the nonlinear constraint, or would it only apply to conic programming?

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [December 14, 2021, 11:47pm UTC](https://discourse.julialang.org/t/entropy-maximization-with-nonlinear-constraint/73104/4 "2021-12-14T23:47:43Z")

</div>

NLopt doesn’t support the entropic cone, so you’ll have to write that as a nonlinear constraint.

Just write the JuMP model exactly as you formulated above. Perhaps:

```nohighlight
using JuMP, Ipopt
N = 3
model = Model(Ipopt.Optimizer)
@variable(model, 0 <= p[1:N, 1:N] <= 1, Symmetric, start = 1 / N)
@NLobjective(model, Max, -sum(p[i, j] * log(p[i, j]) for i in 1:N, j in 1:N))
@constraint(model, sum(p) == 1)
nl_expr = @NLexpression(model, sum(sum(p[i,j] for j in 1:N)^2 for i in 1:N))
@NLconstraint(model, sum(p[i, i] for i in 1:N) - nl_expr == r * (1 - nl_expr))
optimize!(model)

```

---

<div class="post-metadata">

### Author: ![mvsoom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mvsoom/32/31051_2.png) [@mvsoom](https://discourse.julialang.org/u/mvsoom)
#### Post date: [December 14, 2021, 11:55pm UTC](https://discourse.julialang.org/t/entropy-maximization-with-nonlinear-constraint/73104/5 "2021-12-14T23:55:50Z")

</div>

Oh my. Thank you, kind stranger 😀
