Minimum Cost Flow - network optimization in R

前端 未结 2 1126
天涯浪人
天涯浪人 2021-01-04 23:53

I am trying to implement a \"Minimum Cost Network Flow\" transportation problem solution in R.

I understand that this could be implemented from scratch

2条回答
  •  误落风尘
    2021-01-05 00:12

    I was looking for a function but didn't success. The original function calls another: res <- .Call("R_igraph_maxflow", graph, source - 1, target - 1, capacity, PACKAGE = "igraph")

    And I don't know how to deal with it.

    For the moment I inverted the cost path values in order to use the same function in oposite direction:

    E2 <- E # create another table
    E2[, 3] <- max(E2[, 3]) + 1 - E2[, 3] # invert values
    
    E2
         from to capacity
    [1,]    1  3        8
    [2,]    3  4       10
    [3,]    4  2        9
    [4,]    1  5       10
    [5,]    5  6        9
    [6,]    6  2        1
    
    g2 <- graph_from_data_frame(as.data.frame(E2)) # create 2nd graph
    
    # Get maximum flow
    m1 <- max_flow(g1, source=V(g1)["1"], target=V(g1)["2"])
    m2 <- max_flow(g2, source=V(g2)["1"], target=V(g2)["2"])
    
    m1$partition2 # Route on maximal cost
    + 4/6 vertices, named:
    [1] 4 5 6 2
    
    m2$partition2 # Route on minimal cost
    + 3/6 vertices, named:
    [1] 3 4 2
    

    I draw in paper the graph and my code agree with manual solution This method should be tested with real know values, as I mentioned in the comment

提交回复
热议问题