# Failed optimization with function

**URL:** <https://discourse.julialang.org/t/failed-optimization-with-function/114290>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, optimization, function\
**Created:** [May 15, 2024, 8:53am UTC](https://discourse.julialang.org/t/failed-optimization-with-function/114290 "2024-05-15T08:53:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nouha941](https://avatars.discourse-cdn.com/v4/letter/n/3e96dc/32.png) [@nouha941](https://discourse.julialang.org/u/nouha941)\
**Post date:** [May 15, 2024, 8:53am UTC](https://discourse.julialang.org/t/failed-optimization-with-function/114290/1 "2024-05-15T08:53:21Z")

</div>

Hello everyone, I have a problem with optimizing my function. I keep getting an error. Could you please help me?

```julia
function optimize_Cs(Q_e, PT501, PT401, P_HP, R_m, R_p, Qp, Pe)
    #calcul initial de Cs
    Pe = (( ((Q_e * 0.000277778)*((PT501*1e5) - (PT401*1e5))) - ((Q_e * 0.000277778)*((PT501*1e5) - (P_HP*1e5))) )/ (R_m*R_p)) * (1/1000)
    Cs = Pe / Qp

    Pe = Model(Ipopt.Optimizer)
     #Declaring variables
    @variable(Pe, 350<= Q_e <= 450) 
    @variable(Pe, 2e5<= PT401 <= 5e5) 
    @variable(Pe, 45e5<= PT501 <= 70e5) 
    @variable(Pe, 25e5<= P_HP <= 42.1e5) 
    @variable(Pe, 125<= Qp <= 220)
    @variable(Pe, 0.7<= R_m <= 0.85) 
    @variable(Pe, 0.7<= R_p <= 0.9) 
    @variable(Pe, 45<= PT502 <= 56) 

    #Optimisation de CS
    if Cs > 3.5
        println(" The specific consumption value exceeds the average value!!!")
       
        #setting the objecjtive
        @objective(Pe, Min, (( ((Q_e * 0.000277778)*((PT501*1e5) - (PT401*1e5))) - ((Q_e * 0.000277778)*((PT501*1e5) - (P_HP*1e5))) )/ (R_m*R_p)) * (1/1000))
        
    elseif Cs < 3.5
        println(" The specific consumption value is under the average value!!!")

        #setting the objecjtive
        @objective(Pe, Max, (( ((Q_e * 0.000277778)*((PT501*1e5) - (PT401*1e5))) - ((Q_e * 0.000277778)*((PT501*1e5) - (P_HP*1e5))) )/ (R_m*R_p)) * (1/1000))
        
    else
        return Cs
 #Adding constraints
    @constraint(Pe, constraint1, Cs-3.5 == 0)
    @constraint(Pe, constraint2, PT502-PT501 <= 4e5)
        
    end
    #printing the optimization problem
    print(Pe)
    print(Cs)

    #solving the optimization problem
    JuMP.optimiza!(Pe)
    
end

```

the erreur:

```Julia
optimize_Cs (generic function with 1 method)

```

PS: The programme asks the user to enter their own values at the beginning

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 15, 2024, 8:59am UTC](https://discourse.julialang.org/t/failed-optimization-with-function/114290/2 "2024-05-15T08:59:19Z")

</div>

> [@nouha941](#):
>
> the erreur:
> 
> ```julia
> optimize_Cs (generic function with 1 method)
> 
> ```

That’s not an error, that just tells you that you’ve defined a function:

```julia
julia> function optimize_Cs(Qₑ, PT501, PT401, P_HP, Rₘ, Rₚ, Qp, Pe)
           1 + 1
       end
optimize_Cs (generic function with 1 method)

```

Which can then be called:

```julia
julia> optimize_Cs(1,2,3,4,5,6,7,8)
2

```

---

<div class="post-metadata">

**Author:** ![nouha941](https://avatars.discourse-cdn.com/v4/letter/n/3e96dc/32.png) [@nouha941](https://discourse.julialang.org/u/nouha941)\
**Post date:** [May 15, 2024, 9:03am UTC](https://discourse.julialang.org/t/failed-optimization-with-function/114290/3 "2024-05-15T09:03:22Z")

</div>

Thank you very much, sir.

---

<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:** [May 15, 2024, 10:17am UTC](https://discourse.julialang.org/t/failed-optimization-with-function/114290/4 "2024-05-15T10:17:45Z")

</div>

Hi @nouha941, I’ve edited your post to put it in triple-backtircks

````julia
```julia
code goes here
```

````

I think you should take another look at your code:

1. As @nilshg says, You have defined a function `optimize_Cs`, but not called it. There is no error, Julia is just telling you that you have defined a function.
2. You have defined some inputs to your function, like `Q_e`, which are constants that you then overwrite as optimization variables. What are the constant data in your problem and what are the decision variables?
3. You have an early `return Cs` in the `else` block. Did you mean to have that? It will skip the `## Adding constraints` section, because these are inside the `else` and occur after `return`

---

<div class="post-metadata">

**Author:** ![nouha941](https://avatars.discourse-cdn.com/v4/letter/n/3e96dc/32.png) [@nouha941](https://discourse.julialang.org/u/nouha941)\
**Post date:** [May 15, 2024, 4:49pm UTC](https://discourse.julialang.org/t/failed-optimization-with-function/114290/5 "2024-05-15T16:49:53Z")

</div>

I am very grateful for your help, Sir. Thank you very much for your valuable advice.
