# How to make if in the objective function

**URL:** <https://discourse.julialang.org/t/how-to-make-if-in-the-objective-function/46859>\
**Category:** Optimization (Mathematical)\
**Created:** [September 18, 2020, 5:13pm UTC](https://discourse.julialang.org/t/how-to-make-if-in-the-objective-function/46859 "2020-09-18T17:13:26Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 18, 2020, 10:54pm UTC](https://discourse.julialang.org/t/how-to-make-if-in-the-objective-function/46859/11 "2020-09-18T22:54:33Z")

</div>

First let me say this is a lot more readable, and therefore much more accessible to folks eager to help!  
However, as @odow mentioned in the other thread, assuming `_PRODUCTs_ALL` or `PRODAMOUNT` contain variables, this is not possible. The result of `sum1` is an expression containing variables, and this expression is _not a number_, so when you compare it to 0, the answer is _always false_. This is clearly not the behavior you want. Unfortunately, what you want to have happen is entirely impossible. You need to introduce a binary variable as discussed in that thread that will determine which branch to take based on the value of the expression. This is sometimes called a Big-M formulation, and @odow gave an example of the approach [here](https://discourse.julialang.org/t/why-the-results-of-my-model-remain-the-same-even-if-the-objective-value-changes/46817/2)

---

_[View the full topic](https://discourse.julialang.org/t/how-to-make-if-in-the-objective-function/46859)._
