首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在 scipy.minimize不满足约束条件,输出结果与原数据相同?

在 scipy.minimize不满足约束条件,输出结果与原数据相同?

提问于 2024-07-06 00:31:50
回答 0关注 0查看 5

我试图找到x,y和f,s的最小偏差,设置了以下最小化函数来进行实现,下面是应该满足的约束条件

代码如下所示,它使用了Scipy.minimize,但例程似乎不满足约束3。下面的代码显示最小化成功收敛,但我可以看到结果是不满足约束的。

非常感谢您的帮助

代码语言:txt
复制
import numpy as np
from scipy.optimize import minimize

# 定义给定的 (fij, sij) 数据
data = [[(0.5, 8), (0.3, 8), (0.4, 6), (0.2, 5)],
        [(0.7, 6), (0.5, 8), (0.7, 8), (0.4, 5)],
        [(0.6, 8), (0.3, 7), (0.5, 8), (0.9, 5)],
        [(0.8, 7), (0.6, 8), (0.1, 8), (0.5, 8)]]

# 提取fij和sij
fij = np.array([[data[i][j][0] for j in range(4)] for i in range(4)])
sij = np.array([[data[i][j][1] for j in range(4)] for i in range(4)])


# 目标函数
def objective(vars):
    xij = vars[:16].reshape(4, 4)
    yij = vars[16:].reshape(4, 4)
    return 0.457 * (1 / 16) * np.sum(np.abs(fij - xij)) + 0.04 * (1 / 16) * np.sum(np.abs(sij - yij))


# 约束条件1: xij + xji = 1
def constraint1(vars):
    xij = vars[:16].reshape(4, 4)
    cons = []
    for i in range(4):
        for j in range(4):
            if i != j:
                cons.append(xij[i][j] + xij[j][i] - 1)
    return cons


# 约束条件2: yij = yji
def constraint2(vars):
    yij = vars[16:].reshape(4, 4)
    cons = []
    for i in range(4):
        for j in range(4):
            if i != j:
                cons.append(yij[i][j] - yij[j][i])
    return cons


# 约束条件3: [1/288 * ((fij-0.5) * sij - (fkj-0.5) * skj - (fik - 0.5)) * sik] <= 0.2
def constraint3(vars):
    xij = vars[:16].reshape(4, 4)
    yij = vars[16:].reshape(4, 4)
    total_sum = 0
    for i in range(4):
        for j in range(4):
            for k in range(4):
                if i != j and j != k and i != k:
                    Aij, Bij = xij[i][j], yij[i][j]
                    Akj, Bkj = xij[k][j], yij[k][j]
                    Aik, Bik = xij[i][k], yij[i][k]

                    term1 = (Aij - 0.5) * Bij
                    term2 = (Akj - 0.5) * Bkj
                    term3 = (Aik - 0.5) * Bik

                    total_sum += abs(term1 - term2 - term3)
    return 1 - (1 / 288) * total_sum - 0.8


# 约束条件4: xij > 0
def constraint4(vars):
    xij = vars[:16].reshape(4, 4)
    return xij.flatten()


# 约束条件5: yij ∈ (0, 8)
def constraint5(vars):
    yij = vars[16:].reshape(4, 4)
    cons = []
    cons.extend(yij.flatten() - 0)  # yij > 0
    cons.extend(8 - yij.flatten())  # yij < 8
    return cons


# 初始猜测值
x0 = np.hstack((fij.flatten(), sij.flatten()))
# 定义约束条件列表
cons = [{'type': 'ineq', 'fun': constraint3},
        {'type': 'eq', 'fun': constraint1},
        {'type': 'eq', 'fun': constraint2},

        {'type': 'ineq', 'fun': constraint4},
        {'type': 'ineq', 'fun': constraint5}]
# 优化求解
solution = minimize(objective, x0,  constraints=cons, method='SLSQP', options={'disp': True}, tol=1e-1)

# 获取最优解
vars_opt = solution.x
xij_opt = vars_opt[:16].reshape(4, 4)
yij_opt = vars_opt[16:].reshape(4, 4)

print("Optimal xij:")
print(xij_opt)
print("Optimal yij:")
print(yij_opt)

# 输出结果的结构与输入的矩阵结构相同
result = [[(xij_opt[i][j], yij_opt[i][j]) for j in range(4)] for i in range(4)]
print("Result:")
print(result)
sum_term = 0
for i in range(4):
    for j in range(4):
        for k in range(4):
            if i != j and j != k and i != k:
                sum_term += abs((xij_opt[i][j] - 0.5) * yij_opt[i][j] - (xij_opt[k][j] - 0.5) * yij_opt[k][j] - (
                        xij_opt[i][k] - 0.5) * yij_opt[i][k])
constraint_val = 1 - (1 / 288) * sum_term
if constraint_val < 0.8:
    print(f"Constraint not satisfied: {constraint_val}")
print(constraint_val)

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档