Failed optimization with function

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

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:

optimize_Cs (generic function with 1 method)

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

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

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> optimize_Cs(1,2,3,4,5,6,7,8)
2
1 Like

Thank you very much, sir.

1 Like

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

```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
2 Likes

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

1 Like