2016-04-14 18 views
0

Belirli grafiklerde max_flow_min_cost işlevini kullanmaya çalışıyorum, ancak bazen algoritma sonsuza kadar sürüyor (veya en azından çok uzun bir süre) gibi görünüyor ve bunun nedenini anlamıyorum. dava. İşte bu kadar uzun süre çalışmasına neden olan bir grafik. Yukarıdaki grafik değiştirme ancak 2 ya da 3 batmaya hedef kapasiteli yaparsanız Hepsi aynı düğümleriPython NetworkX max_flow_min_cost işlevi sonsuza kadar koşuyor

nodes = [ 
    ('1in', {'y': -1, 'x': -1, 'type': 'passenger in', 'number': 1}), 
    ('2out', {'y': 1, 'x': -1, 'type': 'passenger out', 'number': 2}), 
    ('destination', {'y': 0, 'x': 0, 'type': 'destination'}), 
    ('2in', {'y': 1, 'x': -1, 'type': 'passenger in', 'number': 2}), 
    ('source', {'type': 'meta'}), 
    ('4in', {'y': -1, 'x': 1, 'type': 'passenger in', 'number': 4}), 
    ('1out', {'y': -1, 'x': -1, 'type': 'passenger out', 'number': 1}), 
    ('4out', {'y': -1, 'x': 1, 'type': 'passenger out', 'number': 4}), 
    ('sink', {'type': 'meta'}), 
    ('3in', {'y': 1, 'x': 1, 'type': 'passenger in', 'number': 3}), 
    ('3out', {'y': 1, 'x': 1, 'type': 'passenger out', 'number': 3}) 
] 

edges = [ 
    ('1in', '1out', {'cost': 0, 'capacity': 1}), 
    ('2out', '4in', {'cost': -9.48, 'capacity': 1}), 
    ('2out', 'destination', {'cost': -10.9, 'capacity': 1}), 
    ('2out', '3in', {'cost': -10.31, 'capacity': 1}), 
    ('destination', 'sink', {'cost': 0, 'capacity': 1}), 
    ('2in', '2out', {'cost': 0, 'capacity': 1}), 
    ('source', '2in', {'cost': 0, 'capacity': 1}), 
    ('source', '4in', {'cost': 0, 'capacity': 1}), 
    ('source', '1in', {'cost': 0, 'capacity': 1}), 
    ('source', '3in', {'cost': 0, 'capacity': 1}), 
    ('4in', '4out', {'cost': 0, 'capacity': 1}), 
    ('1out', '2in', {'cost': -10.31, 'capacity': 1}), 
    ('1out', '4in', {'cost': -10.31, 'capacity': 1}), 
    ('1out', 'destination', {'cost':-10.9, 'capacity': 1}), 
    ('1out', '3in', {'cost': -9.48, 'capacity': 1}), 
    ('4out', 'destination', {'cost': -10.9, 'capacity': 1}), 
    ('3in', '3out', {'cost': 0, 'capacity': 1}), 
    ('3out', '4in', {'cost': -10.31, 'capacity': 1}), 
    ('3out', 'destination', {'cost': -10.9, 'capacity': 1}) 
] 

sahip, aynı zamanda sonsuza kadar çalışır. Kapasite 4'e eşitse, algoritma düzgün çalışır. Bu tam olarak arandı:

nx.max_flow_min_cost(G,'source','sink',weight='cost') 

Teşekkür ederiz! Herhangi bir yardım takdir edilecektir. G'nin kodlarıyla ilgili bir sorun olması durumunda, NetworkX projesinde bir Yönlendirilmiş Grafik (DiGraph)

Düzenleme: I opened an issue olduğunu belirtmekte yarar var.

+0

networkx web sitesinde cevap soruya bakın o kodu gibi görünmesi amacıyla 4 boşluklu kod tablolaştırıyoruz. –

+0

@imaluengo Teşekkür ederiz! Editörde bir satır gösterdi, bu yüzden korktum ve yapmadım. – Arya

+0

Evet, sadece 1 satırdı, onu birkaç satıra (düğüm ve kenar başına bir tane) kırdım ve 4 boşlukla tablo haline getirdim. 1 satır korkutucuydu! –

cevap

1

Benim için çalışıyor:

>>> G = nx.Graph() 
>>> G.add_edges_from([ 
    ('1in', '1out', {'cost': 0, 'capacity': 1}), 
    ('2out', '4in', {'cost': -9.48, 'capacity': 1}), 
    ('2out', 'destination', {'cost': -10.9, 'capacity': 1}), 
    ('2out', '3in', {'cost': -10.31, 'capacity': 1}), 
    ('destination', 'sink', {'cost': 0, 'capacity': 1}), 
    ('2in', '2out', {'cost': 0, 'capacity': 1}), 
    ('source', '2in', {'cost': 0, 'capacity': 1}), 
    ('source', '4in', {'cost': 0, 'capacity': 1}), 
    ('source', '1in', {'cost': 0, 'capacity': 1}), 
    ('source', '3in', {'cost': 0, 'capacity': 1}), 
    ('4in', '4out', {'cost': 0, 'capacity': 1}), 
    ('1out', '2in', {'cost': -10.31, 'capacity': 1}), 
    ('1out', '4in', {'cost': -10.31, 'capacity': 1}), 
    ('1out', 'destination', {'cost':-10.9, 'capacity': 1}), 
    ('1out', '3in', {'cost': -9.48, 'capacity': 1}), 
    ('4out', 'destination', {'cost': -10.9, 'capacity': 1}), 
    ('3in', '3out', {'cost': 0, 'capacity': 1}), 
    ('3out', '4in', {'cost': -10.31, 'capacity': 1}), 
    ('3out', 'destination', {'cost': -10.9, 'capacity': 1}) 
]) 

>>> nx.max_flow_min_cost(G, 'source', 'sink', weight='cost') 
{'1in': {'1out': 0, 'source': 0}, 
'1out': {'1in': 0, '2in': 1, '3in': 1, '4in': 1, 'destination': 1}, 
'2in': {'1out': 1, '2out': 1, 'source': 0}, 
'2out': {'2in': 0, '3in': 1, '4in': 1, 'destination': 1}, 
'3in': {'1out': 1, '2out': 1, '3out': 0, 'source': 0}, 
'3out': {'3in': 0, '4in': 1, 'destination': 1}, 
'4in': {'1out': 1, '2out': 1, '3out': 1, '4out': 0, 'source': 0}, 
'4out': {'4in': 0, 'destination': 1}, 
'destination': {'1out': 1, '2out': 0, '3out': 1, '4out': 1, 'sink': 1}, 
'sink': {'destination': 0}, 
'source': {'1in': 0, '2in': 1, '3in': 0, '4in': 0}} 
+0

Üzgünüz! G'nin bir DiGraph – Arya

+1

@Arya'nın düzenlenmiş olduğunu söylemeyi unuttum! Kaynağa yönlendirilen kenarların "kapasitesi" olmadan ve batırmak mükemmel çalışır. Networkx belgelerinin örneği, bu niteliklere sahip değildir. –

+0

Bunun için bir neden var mı? Uygulamaya çalıştığım algoritmaya göre, hedeften batığa farklı bir kapasiteye sahip bir grafik çizmeliyim ve ardından en düşük maliyetle bir tane döndürmeliyim. Örneğin, bu yönlendirilmiş grafik, hedefin batması için 1 ve 7 arasındaki her kapasite için mükemmel bir şekilde çalışır: http://pastebin.com/J1tk5xCD – Arya

İlgili konular