0

I am getting this error:

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

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

3
  • 2
    Which solver are you trying to use? Some solvers like Ipopt require the constraints/objective to be continuous and twice differentiable and therefore do not support abs(). Jun 11, 2019 at 14:20
  • 1
    Thank you Bethany Nicholson , it makes sense. I added a constraint to replicate the work of abs() in my optimization problem. It worked for me. Jun 11, 2019 at 21:37
  • Just be careful since adding a constraint on the part that you want to get the absolute value will only forbid to find a negative solution. I'll post a workaround for this. Jun 13, 2019 at 13:52

1 Answer 1

6

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.