# Problem with implementing the objective function with minimum and maximum functions simultaneously!

**URL:** <https://discourse.julialang.org/t/problem-with-implementing-the-objective-function-with-minimum-and-maximum-functions-simultaneously/72917>\
**Category:** Optimization (Mathematical)\
**Created:** [December 10, 2021, 11:55pm UTC](https://discourse.julialang.org/t/problem-with-implementing-the-objective-function-with-minimum-and-maximum-functions-simultaneously/72917 "2021-12-10T23:55:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahar/32/31475_2.png) [@Sahar](https://discourse.julialang.org/u/Sahar)\
**Post date:** [December 10, 2021, 11:55pm UTC](https://discourse.julialang.org/t/problem-with-implementing-the-objective-function-with-minimum-and-maximum-functions-simultaneously/72917/1 "2021-12-10T23:55:51Z")

</div>

Hi  
Good morning!

I am working on an optimization problem and it has a user defined objective function. the problem is now working well with a simple cost objective function, but I am not able to change it to the attached picture;

![Untitled](https://global.discourse-cdn.com/julialang/original/3X/3/2/328f8d88cf6ade8fc8de8ed94dec18c27781ef93.png)

The e(t) is a an unknown variable of the problem which is a vector of symbolic variables for 24 hours like [e1,e2,e3,e4,…,e24]. Pb and Qb are also the same. Could you please help me to find out how can I define such objective function in julia which has maximum and minimum functions simultaneously! I was trying to use findmax() for finding the maximum variable but it is not working well and it is mentioned that the method is not matched.

I am using Ipopt.Optimizer!  
Thanks in advance for your kind support.

---

<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 11, 2021, 12:42am UTC](https://discourse.julialang.org/t/problem-with-implementing-the-objective-function-with-minimum-and-maximum-functions-simultaneously/72917/2 "2021-12-11T00:42:28Z")

</div>

To model `min { max_{t in T} f(t)}`, you can use the trick

```nohighlight
T = 3
model = Model()
@variable(model, f_t[1:T])
@variable(model, f_t_max)
# Assume f_t[t] is defined by other constraints
@constraint(model, [t=1:T], f_t_max >= f_t[t])
@objective(model, Min, f_t_max)

```

you can use that for your two `max` terms. Then the `-min f(x)` term can be converted into `max -f(x)`.

If you get stuck, read [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) and then provide an example of the JuMP code you are working on.

---

<div class="post-metadata">

**Author:** ![Sahar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahar/32/31475_2.png) [@Sahar](https://discourse.julialang.org/u/Sahar)\
**Post date:** [December 11, 2021, 2:31pm UTC](https://discourse.julialang.org/t/problem-with-implementing-the-objective-function-with-minimum-and-maximum-functions-simultaneously/72917/3 "2021-12-11T14:31:49Z")

</div>

Thank you so much for your kind support. I implemented the objective function and my code is running well now but only with quadratic form of it!

```julia

@variable(bb, e_t_max);	
@constraint(bb, [t=1:24], e_t_max >= e[t])
@variable(bb, e_t_min);	
@constraint(bb, [t=1:24], e_t_min <= e[t])
@variable(bb, Pb_max);	
@constraint(bb, [t=1:24],Pb_max >= Pb[t])

@objective(bb, Min, 0.4*(e_t_max-e_t_min)^2 + 0.3* Pb_max for t in LoadData.Hour))

```
