@@ -15,7 +15,7 @@ Returns a flow matrix, flow[i,j] corresponds to the flow on the (i,j) arc.
1515
1616### Usage Example: 
1717
18- ```jldoctest  
18+ ```julia  
1919julia> g = lg.DiGraph(6) # Create a flow-graph 
2020julia> lg.add_edge!(g, 5, 1) 
2121julia> lg.add_edge!(g, 5, 2) 
@@ -45,47 +45,37 @@ function mincost_flow(g::lg.DiGraph,
4545		source:: Int  =  - 1 , #  if source and/or sink omitted or not in nodes, circulation problem 
4646		sink:: Int  =  - 1 ,
4747		solver:: AbstractMathProgSolver  =  GLPKSolverLP ())
48- 	m =  JuMP. Model (solver =  solver)
49- 	flow =  JuMP. @variable (m, x[1 : lg. nv (g),1 : lg. nv (g)] >=  0. )
50- 	JuMP. @constraint (m,
51- 		[i= 1 : lg. nv (g),j= 1 : lg. nv (g)],
52- 		flow[i,j] -  demand[i,j] >=  0. 
53- 	)
54- 	JuMP. @constraint (m,
55- 		[i= 1 : lg. nv (g),j= 1 : lg. nv (g)],
56- 		capacity[i,j] -  flow[i,j] >=  0. 
57- 	)
58- 	for  n1 in  1 : lg. nv (g)
59- 		for  n2 in  1 : lg. nv (g)
60- 			if  ! lg. has_edge (g,n1,n2)
61- 				JuMP. @constraint (m, flow[n1,n2] <=  0. )
62- 			end 
48+ 	flat_cap =  collect (Iterators. flatten (capacity))
49+ 	flat_dem =  collect (Iterators. flatten (demand))
50+ 	flat_cost =  collect (Iterators. flatten (cost))
51+ 	n =  lg. nv (g)
52+ 	b =  zeros (n+ n* n)
53+ 	A =  spzeros (n+ n* n,n* n)
54+ 	sense =  [' =' for  _ in  1 : n]
55+ 	append! (sense, [' >' for  _ in  1 : n* n])
56+ 	for  node in  1 : n
57+ 		if  sink ==  node
58+ 			sense[sink] =  ' >' 
59+ 		elseif  source ==  node
60+ 			sense[source] =  ' <' 
6361		end 
64- 	end 
65- 	#  flow conservation constraints
66- 	for  node in  1 : lg. nv (g)
67- 		if  node !=  source &&  node !=  sink
68- 			JuMP. @constraint (m,
69- 				sum (flow[node,j] for  j in  1 : lg. nv (g)) - 
70- 				sum (flow[i,node] for  i in  1 : lg. nv (g)) ==  0 
71- 			)
62+ 		col_idx =  (node- 1 )* n+ 1 : node* n
63+ 		line_idx =  node: n: n* n
64+ 		for  jdx in  col_idx
65+ 			A[node,jdx] =  A[node,jdx]+ 1.0 
66+ 		end 
67+ 		for  idx in  line_idx
68+ 			A[node,idx] =  A[node,idx]- 1.0 
7269		end 
7370	end 
74- 	#  source is a net flow producer
75- 	if  source ∈  1 : lg. nv (g)
76- 		JuMP. @constraint (m,
77- 			sum (flow[source,j] for  j in  1 : lg. nv (g)) - 
78- 			sum (flow[i,source] for  i in  1 : lg. nv (g)) >=  0 
79- 		)
80- 	end 
81- 	#  source is a net flow consumer
82- 	if  sink ∈  1 : lg. nv (g)
83- 		JuMP. @constraint (m,
84- 			sum (flow[sink,j] for  j in  1 : lg. nv (g)) - 
85- 			sum (flow[i,sink] for  i in  1 : lg. nv (g)) <=  0 
86- 		)
71+ 	for  src in  1 : n
72+ 		for  dst in  1 : n
73+ 			if  lg. Edge (src,dst) ∉  lg. edges (g)
74+ 				A[n+ src+ n* (dst- 1 ),src+ n* (dst- 1 )] =  1 
75+ 				sense[n+ src+ n* (dst- 1 )] =  ' <' 
76+ 			end 
77+ 		end 
8778	end 
88- 	JuMP. @objective (m, :Min , sum (flow[i,j]* cost[i,j] for  i= 1 : lg. nv (g),j= 1 : lg. nv (g)))
89- 	solution =  JuMP. solve (m)
90- 	return  JuMP. getvalue (flow)
79+ 	sol =  linprog (flat_cost, A, sense, b, flat_dem, flat_cap, solver)
80+ 	[sol. sol[idx +  n* (jdx- 1 )] for  idx= 1 : n,jdx= 1 : n]
9181end 
0 commit comments