多目标优化示例Pyomo

时间:2018-06-07 13:51:48

标签: minimize maximize pyomo ipopt moo

Pyomo的多目标优化的任何例子?

我正在尝试最小化4个目标(非线性),我想使用pyomo和ipopt。也可以访问Gurobi。

我想看到一个非常简单的例子,我们尝试针对一个决策变量列表(不仅仅是一个维度,但可能是一个向量)优化两个或更多个目标(一个最小化和一个最大化)。

我拥有的Pyomo书(https://link.springer.com/content/pdf/10.1007%2F978-3-319-58821-6.pdf)没有提供一个明确的线索。

3 个答案:

答案 0 :(得分:0)

据我所知,虽然Pyomo支持表达具有多个目标的模型,但它还没有自动模型转换来为您生成常见的多目标优化公式。

也就是说,你仍然可以自己制作这些配方。对于一些想法,请看一下epsilon约束,1范数和无穷大规范。

答案 1 :(得分:0)

使用Pyomo,您必须自己实现它。我现在正在做。最好的方法是增强ε约束方法。它将始终高效,并且始终找到全局最佳状态。最好的例子在这里: Effective implementation of the epsilon-constraint method in Multi-Objective Mathematical Programming problems, Mavrotas, G, 2009

编辑:在这里,我在pyomo中编写了上述论文中的示例: 它将首先对f1最大化,然后对f2最大化。然后将应用正常的epsilon约束并绘制低效的Pareto前沿,然后将其应用到增强的epsilon约束中,这最终是一种方法!

from pyomo.environ import *
import matplotlib.pyplot as plt


# max f1 = X1 <br>
# max f2 = 3 X1 + 4 X2 <br>
# st  X1 <= 20 <br>
#     X2 <= 40 <br>
#     5 X1 + 4 X2 <= 200 <br>

model = ConcreteModel()

model.X1 = Var(within=NonNegativeReals)
model.X2 = Var(within=NonNegativeReals)

model.C1 = Constraint(expr = model.X1 <= 20)
model.C2 = Constraint(expr = model.X2 <= 40)
model.C3 = Constraint(expr = 5 * model.X1 + 4 * model.X2 <= 200)

model.f1 = Var()
model.f2 = Var()
model.C_f1 = Constraint(expr= model.f1 == model.X1)
model.C_f2 = Constraint(expr= model.f2 == 3 * model.X1 + 4 * model.X2)
model.O_f1 = Objective(expr= model.f1  , sense=maximize)
model.O_f2 = Objective(expr= model.f2  , sense=maximize)

model.O_f2.deactivate()

solver = SolverFactory('cplex')
solver.solve(model);

print( '( X1 , X2 ) = ( ' + str(value(model.X1)) + ' , ' + str(value(model.X2)) + ' )')
print( 'f1 = ' + str(value(model.f1)) )
print( 'f2 = ' + str(value(model.f2)) )
f2_min = value(model.f2)


# ## max f2

model.O_f2.activate()
model.O_f1.deactivate()

solver = SolverFactory('cplex')
solver.solve(model);

print( '( X1 , X2 ) = ( ' + str(value(model.X1)) + ' , ' + str(value(model.X2)) + ' )')
print( 'f1 = ' + str(value(model.f1)) )
print( 'f2 = ' + str(value(model.f2)) )
f2_max = value(model.f2)


# ## apply normal $\epsilon$-Constraint

model.O_f1.activate()
model.O_f2.deactivate()

model.e = Param(initialize=0, mutable=True)

model.C_epsilon = Constraint(expr = model.f2 == model.e)

solver.solve(model);

print('Each iteration will keep f2 lower than some values between f2_min and f2_max, so ['       + str(f2_min) + ', ' + str(f2_max) + ']')

n = 4
step = int((f2_max - f2_min) / n)
steps = list(range(int(f2_min),int(f2_max),step)) + [f2_max]

x1_l = []
x2_l = []
for i in steps:
    model.e = i
    solver.solve(model);
    x1_l.append(value(model.X1))
    x2_l.append(value(model.X2))
plt.plot(x1_l,x2_l,'o-.');
plt.title('inefficient Pareto-front');
plt.grid(True);


# ## apply augmented $\epsilon$-Constraint

# max   f2 + delta*epsilon <br>
#  s.t. f2 - s = e

model.del_component(model.O_f1)
model.del_component(model.O_f2)
model.del_component(model.C_epsilon)

model.delta = Param(initialize=0.00001)

model.s = Var(within=NonNegativeReals)

model.O_f1 = Objective(expr = model.f1 + model.delta * model.s, sense=maximize)

model.C_e = Constraint(expr = model.f2 - model.s == model.e)

x1_l = []
x2_l = []
for i in range(160,190,6):
    model.e = i
    solver.solve(model);
    x1_l.append(value(model.X1))
    x2_l.append(value(model.X2))
plt.plot(x1_l,x2_l,'o-.');
plt.title('efficient Pareto-front');
plt.grid(True);

答案 2 :(得分:0)

免责声明::我是pymoo(Python中的多目标优化框架)的主要开发人员。

您可能想考虑Python中其他关注多目标优化的框架。例如,在 pymoo 中,上述相当简单的测试问题的定义或多或少是简单的。您可以在下面找到它的实现。设计和目标空间中的结果如下:

Obtained non-dominated set of solutions using pymoo

pymoo有充分的文档记录,并提供了入门指南,该指南演示了如何定义自己的优化问题,获取一组近似最优的解决方案并进行分析:https://pymoo.org/getting_started.html

该框架的重点是与多目标优化有关的任何事情,包括可视化和决策。

import matplotlib.pyplot as plt
import numpy as np

from pymoo.algorithms.nsga2 import NSGA2
from pymoo.model.problem import Problem
from pymoo.optimize import minimize
from pymoo.visualization.scatter import Scatter


class MyProblem(Problem):
    def __init__(self):
        """
        max f1 = X1 <br>
        max f2 = 3 X1 + 4 X2 <br>
        st  X1 <= 20 <br>
            X2 <= 40 <br>
            5 X1 + 4 X2 <= 200 <br>
        """

        super().__init__(n_var=2,
                         n_obj=2,
                         n_constr=1,
                         xl=np.array([0, 0]),
                         xu=np.array([20, 40]))

    def _evaluate(self, x, out, *args, **kwargs):
        # define both objectives
        f1 = x[:, 0]
        f2 = 3 * x[:, 0] + 4 * x[:, 1]

        # we have to negate the objectives because by default we assume minimization
        f1, f2 = -f1, -f2

        # define the constraint as a less or equal to zero constraint
        g1 = 5 * x[:, 0] + 4 * x[:, 1] - 200

        out["F"] = np.column_stack([f1, f2])
        out["G"] = g1


problem = MyProblem()

algorithm = NSGA2()

res = minimize(problem,
               algorithm,
               ('n_gen', 200),
               seed=1,
               verbose=True)

print(res.X)
print(res.F)

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(12, 6))
Scatter(fig=fig, ax=ax1, title="Design Space").add(res.X, color="blue").do()
Scatter(fig=fig, ax=ax2, title="Objective Space").add(res.F, color="red").do()
plt.show()