# Equality constrain to NLopt

**URL:** https://discourse.julialang.org/t/equality-constrain-to-nlopt/101852
**Category:** Optimization (Mathematical)
**Tags:** question
**Created:** [July 20, 2023, 7:56pm UTC](https://discourse.julialang.org/t/equality-constrain-to-nlopt/101852 "2023-07-20T19:56:59Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mary/32/53139_2.png) [@mary](https://discourse.julialang.org/u/mary)
#### Post date: [July 20, 2023, 7:56pm UTC](https://discourse.julialang.org/t/equality-constrain-to-nlopt/101852/1 "2023-07-20T19:56:59Z")

</div>

Hi, I am doing topology optimization. I have seen some tutorials. I want to add volume constrain to my model as below:

```julia
v/v0=f

```

which v is material volume and v0 is domain volume and f is volume fraction.  
could you please tell me how can i add this to my code

```julia
using NLopt

function gf_p_optimize(p_init; r, β, η, TOL = 1e-6, MAX_ITER = 700, fem_params)
    ##################### Optimize #################
    opt = Opt(:LD_CCSAQ, fem_params.np)
    opt.lower_bounds = 0
    opt.upper_bounds = 1
    opt.ftol_rel = TOL
    opt.maxeval = MAX_ITER
    opt.min_objective = (p0, grad) -> gf_p(p0, grad; r, β, η, fem_params)
    
    (g_opt, p_opt, ret) = optimize(opt, p_init)
    @show numevals = opt.numevals # the number of function evaluations
    return g_opt, p_opt

end

```

thank you

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [July 20, 2023, 8:19pm UTC](https://discourse.julialang.org/t/equality-constrain-to-nlopt/101852/2 "2023-07-20T20:19:39Z")

</div>

CCSAQ doesn’t support equality constraints — the CCSA algorithm requires the feasible set to have a non-empty interior in general.

Normally if you are doing structural optimization you can replace volume constraints with an inequality constraint V ≤ f V\_0, since the optimization naturally “wants” the structure to use as much material as possible (i.e. the constraint will be active at the optimum, so you will end up with V = f V\_0).

---

<div class="post-metadata">

### Author: ![mary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mary/32/53139_2.png) [@mary](https://discourse.julialang.org/u/mary)
#### Post date: [July 20, 2023, 8:44pm UTC](https://discourse.julialang.org/t/equality-constrain-to-nlopt/101852/3 "2023-07-20T20:44:54Z")

</div>

so it should be work by adding

```julia
    inequality_constraint!(opt, sum(p0) - 0.4*fem_params.np<0)

```

what if i dont implement constrain in my model?
