Condition in @objective

I want to set an if statement in @objective macro. This is how it looks like now:

        @variable(premex, PRODAMOUNT[op_k in keys(_ORDER_PRODUCTs_ALL), u_k in keys(UNITS), t in TIME], Int, lower_bound = 0)

        @objective(
            premex,
            Min,
            sum(
                sum(
                    u["util_cost1"] * 
                    (
                        u["cap"]  -  
                        sum(
                            PRODAMOUNT[op_k, u_k, t]  * _PRODUCTs_ALL[op["product"]]["bagSize"]
                            for (op_k, op) in _ORDER_PRODUCTs_ALL
                            )
                    )
                    for (u_k, u) in UNITS
                    )
                for t in TIME
                ) 
         _

Now my goal is to check if sum(PRODAMOUNT…) == 0 then use instead u[“cap”], otherwise use sum(PRODAMOUNT…) as it is.

Not sure to understand, if the sum is zero, you want to remove it from the objective? If it is zero, it does not affect the objective right?

Yes, exactly like that. If inner sum == 0 then all this should be zero as well. But I am not sure how to accomplish this. I have tried with iszero function but I am getting strange results.

I have a feeling that SCIP is giving some strange results, no matter how I construct the @objective function the results that are given are so wrong that I can easily see it and change it by hand.

I’ve changed the objective function to minimize TIME needed for the model to finish and it didn’t do anything to minimize it.

            sum(
                iszero(
                sum(
                        PRODAMOUNT[op_k, u_k, t]  
                        for op_k in keys(_ORDER_PRODUCTs_ALL), u_k in keys(UNITS)
                    )) ? 0 : 1
                for t in TIME
                ) 
             }

A simple reproducible example will show you what is happening. iszero evaluates on the objective expression and returns false, which gets promoted to 0.

julia> using JuMP

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x)
x

julia> iszero(x)
false

julia> @objective(model, Min, iszero(x))
false

julia> objective_function(model)
0

Please ask more targeted questions with simplified reproducible examples.

1 Like

The problem here I face is that the model that I am building is not behaving as expected, so I struggle to “force” it working the right way. For the moment we will move away from this kind of modeling because it simply doesn’t give us any meaningful results and start with a custom build heuristic.

Thank you all for participating in the topics I’ve created regarding this and other questions I had, I appreciate that. It was worthy to try Julia, I’ve learned some new things, and who knows maybe we will cross our paths again.