Python 线性编程。不使用 scipy.optimize.linprog OverflowError: 无法将浮点无穷大转换为整数

问题描述 投票:0回答:1

我尝试不使用 scipy.optmize.lineprog 库直接实现这个运输问题: (交通问题)。数量 a1, a2, . 。 。 , 米 , 分别为 某种产品将从 m 个地点中的每一个地点发货并在 金额 b1、b2、. 。 。 , b n 分别位于 n 个目的地中的每一个。与相关 将一个产品单位从原产地 i 运送到目的地 j 的运费为 c i j 。这是 需要确定每个出发地-目的地之间要运输的数量 x i j 对 i = 1, 2, . 。 。 ,米; j = 1, 2, . 。 。 , n;以满足运输要求和 最大限度地减少运输总成本。 请原谅我的英语,我来自法语国家

import numpy as np
import matplotlib.pyplot as plt

# Données du problème
m = 3  # Nombre d'origines
n = 4  # Nombre de destinations
a = np.array([20, 30, 25])  # Offres des origines
b = np.array([10, 15, 25, 15])  # Demandes des destinations
c = np.array([
    [5, 4, 7, 6],
    [8, 6, 9, 7],
    [6, 3, 4, 5]
])  # Coûts d'expédition

# Matrice des variables de décision (quantités à expédier)
x = np.zeros((m, n))

# Algorithme du transport initial
while np.sum(x) < np.sum(b):
    min_supply = np.min(a)
    min_demand = np.min(b)
    
    i, j = np.unravel_index(c.argmin(), c.shape)
    quantity = min(min_supply, min_demand)
    
    x[i, j] = quantity
    a[i] -= quantity
    b[j] -= quantity
    c[i,j]= np.inf

# Affichage de la solution
print("Solution optimale (quantités à expédier) :")
print(x)

# Affichage du graphique de la solution
plt.imshow(x, cmap='Blues', interpolation='nearest')
plt.colorbar()
plt.title("Quantités à expédier")
plt.xlabel("Destinations")
plt.ylabel("Origines")
plt.xticks(range(n), [f"Destination {i+1}" for i in range(n)])
plt.yticks(range(m), [f"Origine {i+1}" for i in range(m)])
plt.show()

这是发生的错误

OverflowError                             Traceback (most recent call last)
Cell In[2], line 29
     27     a[i] -= quantity
     28     b[j] -= quantity
---> 29     c[i,j]= np.inf
     31 # Affichage de la solution
     32 print("Solution optimale (quantités à expédier) :")

OverflowError: cannot convert float infinity to integer

numpy matplotlib numpy-ndarray
1个回答
0
投票

使用较大的值来表示类似无穷大的条件,例如:

x[i, j] = quantity
a[i] -= quantity
b[j] -= quantity
c[i, j] = 9999999
© www.soinside.com 2019 - 2024. All rights reserved.