使用Scipy Optimize解决运输问题错误退出模式6

2024-05-28 19:23:51 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我试图使用Scipy.optimize解决一个简单的运输行程时间优化问题,但在使用SLSQP时,我得到了一个错误(LSQ子问题(退出模式6)中的奇异矩阵C)

问题相当简单,需要2500人在2个点之间旅行,但每条路线的通行能力仅限于1800人。每条路线上有多少人。假设每个人都完全了解给定输入速度、距离和;容量

有两条可能的路线 1号线为链路1,距离3oom 路线2为连接线2+连接线3,总距离为3000m

限制设置为2500次行程和链路2&;三者应该相等

我已将下面的代码简化为3链接问题

每个路径/路线的解决方案应为1250,因为行程时间相同

import networkx as nx
import numpy as np
import pandas as pd
from scipy.optimize import minimize
%matplotlib notebook

G1 = nx.Graph()
G1.add_edges_from([(1, 3),
                   (1, 2),
                   (2, 3)])
        
nx.draw_networkx(G1)
                  
d1 = 3000 # Distance Link 1
s1 = 30   #Speed Link 1
c1 = 1800 #Capacity Link 1 
alpha1 = 4

d2 = 1500
s2 = 30
c2 = 1800
alpha2 = 4

d3 = 1500
s3 = 30
c3 = 1800
alpha3 = 4

t01 = (d1/1000.0)/s1
t02 = (d2/1000.0)/s2
t03 = (d3/1000.0)/s3

def objective2(x):
    x1 = x[0]
    x2 = x[1]
    x3 = x[2]
    obj_fun = t01*(1.0+(x1/c1)**alpha1) + t02*(1.0+(x2/c2)**alpha2) + t03*(1.0+(x3/c3)**alpha3)
    return obj_fun
    
#Sum of Path Volumes should not exceed O/D demand. 
#O/D Demand set to 2500 Trips from A to B 
def constraint1(x):
    return x[0]+x[1]-2500.0
def constraint2(x):
    return x[0]+x[2]-2500.0

#sum of multple paths between same O/D should not exceed O/D Demand
def constraint3(x):
    return x[1]-x[2]

#Set Bound for Link Flows to be above 0 foir all links
b = (0.0, 500000)
bnds  = (b,b,b)

# initial guesses
n = 3
x0 = np.zeros(n)
x0[0] = 2300.0
x0[1] = 200.0
x0[2] = 200.0

con1 = {'type': 'eq', 'fun': constraint1}
con2 = {'type': 'eq', 'fun': constraint2}
con3 = {'type': 'eq', 'fun': constraint3}
cons = ([con1,con2,con3])

solution = minimize(fun = objective2,x0=x0,method='SLSQP',\
                    bounds=bnds, constraints=cons, options={'disp':True, 'ftol':1.0e-4})
x = solution.x

# show final objective
print('Final Objective: ' + str(objective2(x)))

# print solution
print('Solution')
print('x1 = ' + str(x[0]))
print('x2 = ' + str(x[1]))
print('x3 = ' + str(x[2]))

Tags: fromimport距离returndefaslink路线

热门问题