Manisha Maharjan
Manisha Maharjan

Reputation: 11

Is it possible to include abs() in the objective function to be solved by pyomo solver?

I am getting this error:

ValueError: Cannot load a SolverResults object with bad status: error " 

whenever I use abs() function in the objective function.

Upvotes: 0

Views: 3832

Answers (1)

V. Brunelle
V. Brunelle

Reputation: 1068

Before beginning, I will use the words "non-negative" and "non-positive" instead of "positive" and "negative". This is because that in Pyomo, there is a distinction between the two, it is that "positive" and "negative" domains exclude 0, while "non-positive" and "non-negative" include it).

Suppose that you have the following objective function:

def obj_f(model):
    return abs(model.x)

where model.x is a variable in your model that can take positive and negative values.

In order to make your model work, you can split model.x into two variables, let's say model.x_pos and model.x_neg. This means you will have a variable for the positive part of model.x (model.x_pos) and another variable for the negative part of model.x (model.x_neg).

model.x_pos can only take non-negative values and model.x_neg can only take non-positive values. So, model.x can be transformed into model.x_pos + model.x_neg. You will have to add constraints to make sure that model.x_pos is always non-negative and to make sure that model.x_neg is always non-positive. This can be done by setting the domain when you create the variable, or by adding more constraints to your model.

This way, you can formulate your objective function this way:

def obj_f(model):
    return model.x_pos - model.x_neg

(Note: Since the model.x_neg variable is already negative or 0, we have to use the - sign in front of it to make it positive)

This should be the equivalent of using an abs() function in your objective function.

Upvotes: 7

Related Questions