# Set fixed values in optimization

**URL:** <https://discourse.julialang.org/t/set-fixed-values-in-optimization/101337>\
**Category:** Optimization (Mathematical)\
**Tags:** optimization\
**Created:** [July 8, 2023, 12:20am UTC](https://discourse.julialang.org/t/set-fixed-values-in-optimization/101337 "2023-07-08T00:20:46Z")\
**Posts on this page:** 6\
**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 8, 2023, 12:20am UTC](https://discourse.julialang.org/t/set-fixed-values-in-optimization/101337/1 "2023-07-08T00:20:46Z")

</div>

Hi, I am doing topology optimization using Nlopt. it seems that it incorrectly delete material under load line? do you know how can i silve this issue? also how can i set constant values in my domain. for example force the model to put material under load line. and this is the code

```julia
using NLopt

function gf_p_optimize(p_init; r, β, η,TOL = 1e-4, MAX_ITER = 500, fem_params)
    ##################### Optimize #################
    opt = Opt(:LD_MMA, 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

p_opt = fill(0.4, fem_params.np) # Initial guess
β_list = [4, 8, 16]
g_opt = 0
# g_opt = Inf

TOL = 1e-8
MAX_ITER = 200
for bi = 1 : 3
    β = β_list[bi]
    g_opt, p_temp_opt = gf_p_optimize(p_opt; r, β, η,TOL, MAX_ITER, fem_params)
    global p_opt = p_temp_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 8, 2023, 12:26am UTC](https://discourse.julialang.org/t/set-fixed-values-in-optimization/101337/2 "2023-07-08T00:26:52Z")

</div>

> [@mary](#):
>
> also how can i set constant values in my domain.

You can just set the lower bound equal to the upper bound at those points.

---

<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 8, 2023, 12:33am UTC](https://discourse.julialang.org/t/set-fixed-values-in-optimization/101337/3 "2023-07-08T00:33:02Z")

</div>

@stevengj thank you. is there any examples on this to help how to define that?

---

<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:** [July 9, 2023, 2:24am UTC](https://discourse.julialang.org/t/set-fixed-values-in-optimization/101337/4 "2023-07-09T02:24:04Z")

</div>

> [@mary](#):
>
> `opt.lower_bounds = 0`

Use `opt.lower_bounds = [0, 0, 0, 0]` instead to set a different lower bound for each variable.

---

<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 9, 2023, 6:08pm UTC](https://discourse.julialang.org/t/set-fixed-values-in-optimization/101337/5 "2023-07-09T18:08:20Z")

</div>

I just have one variable (density). but i want to make this variable in loadline as 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 9, 2023, 8:25pm UTC](https://discourse.julialang.org/t/set-fixed-values-in-optimization/101337/6 "2023-07-09T20:25:15Z")

</div>

I just did for those points but as i want to plot both togatehr it gives me error because number of nodes are different in 2 optimizations.

```julia
writevtk(Ω,"shapmbb",cellfields=["p_opt"=>p_opt, "p_opt2"=>p_opt2])

```

error

```julia
AssertionError: 

You are trying to build a CellField from an array of length 20
on a Triangulation with 14573 cells. The length of the given array
and the number of cells should match.

Stacktrace:
 [1] macro expansion
   @ C:\Users\marya\.julia\packages\Gridap\971dU\src\Helpers\Macros.jl:60 [inlined]
 [2] CellField(f::Vector{Float64}, trian::BodyFittedTriangulation{2, 2, UnstructuredDiscreteModel{2, 2, Float64, NonOriented}, UnstructuredGrid{2, 2, Float64, NonOriented, Nothing}, Gridap.Arrays.IdentityVector{Int64}}, domain_style::ReferenceDomain)
   @ Gridap.CellData C:\Users\marya\.julia\packages\Gridap\971dU\src\CellData\CellFields.jl:90
 [3] CellField
   @ C:\Users\marya\.julia\packages\Gridap\971dU\src\CellData\CellFields.jl:104 [inlined]
 [4] _prepare_pdata(trian::BodyFittedTriangulation{2, 2, UnstructuredDiscreteModel{2, 2, Float64, NonOriented}, UnstructuredGrid{2, 2, Float64, NonOriented, Nothing}, Gridap.Arrays.IdentityVector{Int64}}, cellfields::Vector{Pair{String, Vector{Float64}}}, samplingpoints::Gridap.Arrays.CompressedArray{Vector{VectorValue{2, Float64}}, 1, Vector{Vector{VectorValue{2, Float64}}}, Vector{Int8}})
   @ Gridap.Visualization C:\Users\marya\.julia\packages\Gridap\971dU\src\Visualization\VisualizationData.jl:140
 [5] visualization_data(trian::BodyFittedTriangulation{2, 2, UnstructuredDiscreteModel{2, 2, Float64, NonOriented}, UnstructuredGrid{2, 2, Float64, NonOriented, Nothing}, Gridap.Arrays.IdentityVector{Int64}}, filebase::String; order::Int64, nsubcells::Int64, celldata::Dict{Any, Any}, cellfields::Vector{Pair{String, Vector{Float64}}})
   @ Gridap.Visualization C:\Users\marya\.julia\packages\Gridap\971dU\src\Visualization\VisualizationData.jl:47
 [6] writevtk(::BodyFittedTriangulation{2, 2, UnstructuredDiscreteModel{2, 2, Float64, NonOriented}, UnstructuredGrid{2, 2, Float64, NonOriented, Nothing}, Gridap.Arrays.IdentityVector{Int64}}, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Vector{Pair{String, Vector{Float64}}}, Tuple{Symbol}, NamedTuple{(:cellfields,), Tuple{Vector{Pair{String, Vector{Float64}}}}}})
   @ Gridap.Visualization C:\Users\marya\.julia\packages\Gridap\971dU\src\Visualization\Vtk.jl:4
 [7] top-level scope
   @ In[137]:1

```

and p\_opt2 are optimization in those special points.
