简体   繁体   English

是否可以在目标函数中包含abs()以便由pyomo求解器求解?

[英]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. 每当我在目标函数中使用abs()函数时。

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). 这是因为在Pyomo中,两者之间存在区别,即“正”和“负”域排除 0,而“非正”和“非负”包括它。

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. 其中model.x是模型中可以取正值和负值的变量。

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 . 为了使您的模型工作,您可以将model.x拆分为两个变量,例如model.x_posmodel.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.xmodel.x_pos )的正面部分,另一个变量用于model.x的负面部分( model.x_neg )。

model.x_pos can only take non-negative values and model.x_neg can only take non-positive values. model.x_pos只能采用非负值,而model.x_neg只能采用非正值。 So, model.x can be transformed into model.x_pos + model.x_neg . 因此, model.x可以转换为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. 您必须添加约束以确保model.x_pos始终为非负数,并确保model.x_neg始终为非正数。 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) (注意:由于model.x_neg变量已经为负数或0,我们必须使用前面的-符号使其为正数)

This should be the equivalent of using an abs() function in your objective function. 这应该等同于在目标函数中使用abs()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM