# How to treat variable as binary in some points of model?

**URL:** <https://discourse.julialang.org/t/how-to-treat-variable-as-binary-in-some-points-of-model/46100>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [September 5, 2020, 12:23pm UTC](https://discourse.julialang.org/t/how-to-treat-variable-as-binary-in-some-points-of-model/46100 "2020-09-05T12:23:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sogrbilja](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sogrbilja/32/17521_2.png) [@sogrbilja](https://discourse.julialang.org/u/sogrbilja)\
**Post date:** [September 5, 2020, 12:23pm UTC](https://discourse.julialang.org/t/how-to-treat-variable-as-binary-in-some-points-of-model/46100/1 "2020-09-05T12:23:47Z")

</div>

I have a decision variable PRODAMOUNT and it is of Int type. I need it as binary in some points of the model, like here in the objective function second PRODAMOUNT should be used as 1 when there is a positive value in it otherwise is should be 0 (as it is)

```julia
        @objective(
            premex,
            Min,
             sum((u["cap"] - PRODAMOUNT[op_k, u_k, t] * _PRODUCTs_ALL[op["product"]]["bagSize"]) * u["util_cost1"]
           + sum(t / _ORDERs_ALL[op["order"]]["details"]["deadline"] * PRODAMOUNT[op_k, u_k, t] * 10000
                for (op_k, op) in _ORDER_PRODUCTs_ALL, (u_k, u) in UNITS, t in TIME) 
)

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 5, 2020, 1:09pm UTC](https://discourse.julialang.org/t/how-to-treat-variable-as-binary-in-some-points-of-model/46100/2 "2020-09-05T13:09:06Z")

</div>

I am not sure if I understand the question, do you have a variable that should be a continuous or integer variable in the constraints, but in the objective function it should only matter if it has a positive value or not?

If I guessed right, one solution is: create a second variable, `PRODAMOUNT_IS_POSITIVE`, it is equal to `PRODAMOUNT` except it is binary, in the formulation add the constraint `PRODAMOUNT_IS_POSITIVE >= PRODAMOUNT/MAX_VALUE_PRODAMOUNT_CAN_ASSUME`. If `PRODAMOUNT` assumes any positive value, then `PRODAMOUNT_IS_POSITIVE` that is binary will be forced to assume value 1, if you do not divide `PRODAMOUNT` by the maximum value `PRODAMOUNT` may assume then the model may become unfeasible (as the binary variable cannot assume values higher than one). Use `PRODAMOUNT_IS_POSITIVE` in the objective function (and keep `PRODAMOUNT` in the constraints as it already is).

---

<div class="post-metadata">

**Author:** ![sogrbilja](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sogrbilja/32/17521_2.png) [@sogrbilja](https://discourse.julialang.org/u/sogrbilja)\
**Post date:** [September 5, 2020, 1:30pm UTC](https://discourse.julialang.org/t/how-to-treat-variable-as-binary-in-some-points-of-model/46100/3 "2020-09-05T13:30:22Z")

</div>

thank you for your answer, but I don’t know max value of PRODAMOUNT because that is the goal of the model itself. I will try to eliminate all dependent variables from the model and change the logic behind it. BTW I am working on the model that was already created as it is.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 5, 2020, 2:50pm UTC](https://discourse.julialang.org/t/how-to-treat-variable-as-binary-in-some-points-of-model/46100/4 "2020-09-05T14:50:16Z")

</div>

Note that if you do not know the max value of `PRODAMOUNT` you can try using the tighter upper bound you have.
