• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python networkx.network_simplex函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中networkx.network_simplex函数的典型用法代码示例。如果您正苦于以下问题:Python network_simplex函数的具体用法?Python network_simplex怎么用?Python network_simplex使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了network_simplex函数的18个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_negative_selfloops

    def test_negative_selfloops(self):
        """Negative selfloops should cause an exception if uncapacitated and
        always be saturated otherwise.
        """
        G = nx.DiGraph()
        G.add_edge(1, 1, weight=-1)
        assert_raises(nx.NetworkXUnbounded, nx.network_simplex, G)
        assert_raises(nx.NetworkXUnbounded, nx.capacity_scaling, G)
        G[1][1]['capacity'] = 2
        flowCost, H = nx.network_simplex(G)
        assert_equal(flowCost, -2)
        assert_equal(H, {1: {1: 2}})
        flowCost, H = nx.capacity_scaling(G)
        assert_equal(flowCost, -2)
        assert_equal(H, {1: {1: 2}})

        G = nx.MultiDiGraph()
        G.add_edge(1, 1, 'x', weight=-1)
        G.add_edge(1, 1, 'y', weight=1)
        assert_raises(nx.NetworkXUnbounded, nx.network_simplex, G)
        assert_raises(nx.NetworkXUnbounded, nx.capacity_scaling, G)
        G[1][1]['x']['capacity'] = 2
        flowCost, H = nx.network_simplex(G)
        assert_equal(flowCost, -2)
        assert_equal(H, {1: {1: {'x': 2, 'y': 0}}})
        flowCost, H = nx.capacity_scaling(G)
        assert_equal(flowCost, -2)
        assert_equal(H, {1: {1: {'x': 2, 'y': 0}}})
开发者ID:ProgVal,项目名称:networkx,代码行数:28,代码来源:test_mincost.py


示例2: test_digraph1

    def test_digraph1(self):
        # From Bradley, S. P., Hax, A. C. and Magnanti, T. L. Applied
        # Mathematical Programming. Addison-Wesley, 1977.
        G = nx.DiGraph()
        G.add_node(1, demand=-20)
        G.add_node(4, demand=5)
        G.add_node(5, demand=15)
        G.add_edges_from([(1, 2, {'capacity': 15, 'weight': 4}),
                          (1, 3, {'capacity': 8, 'weight': 4}),
                          (2, 3, {'weight': 2}),
                          (2, 4, {'capacity': 4, 'weight': 2}),
                          (2, 5, {'capacity': 10, 'weight': 6}),
                          (3, 4, {'capacity': 15, 'weight': 1}),
                          (3, 5, {'capacity': 5, 'weight': 3}),
                          (4, 5, {'weight': 2}),
                          (5, 3, {'capacity': 4, 'weight': 1})])
        flowCost, H = nx.network_simplex(G)
        soln = {1: {2: 12, 3: 8},
                2: {3: 8, 4: 4, 5: 0},
                3: {4: 11, 5: 5},
                4: {5: 10},
                5: {3: 0}}
        assert_equal(flowCost, 150)
        assert_equal(nx.min_cost_flow_cost(G), 150)
        assert_equal(H, soln)
        assert_equal(nx.min_cost_flow(G), soln)
        assert_equal(nx.cost_of_flow(G, H), 150)

        flowCost, H = nx.capacity_scaling(G)
        assert_equal(flowCost, 150)
        assert_equal(H, soln)
        assert_equal(nx.cost_of_flow(G, H), 150)
开发者ID:ProgVal,项目名称:networkx,代码行数:32,代码来源:test_mincost.py


示例3: test_digon

    def test_digon(self):
        """Check if digons are handled properly. Taken from ticket
        #618 by arv."""
        nodes = [(1, {}),
                 (2, {'demand': -4}),
                 (3, {'demand': 4}),
                 ]
        edges = [(1, 2, {'capacity': 3, 'weight': 600000}),
                 (2, 1, {'capacity': 2, 'weight': 0}),
                 (2, 3, {'capacity': 5, 'weight': 714285}),
                 (3, 2, {'capacity': 2, 'weight': 0}),
                 ]
        G = nx.DiGraph(edges)
        G.add_nodes_from(nodes)
        flowCost, H = nx.network_simplex(G)
        soln = {1: {2: 0},
                2: {1: 0, 3: 4},
                3: {2: 0}}
        assert_equal(flowCost, 2857140)
        assert_equal(nx.min_cost_flow_cost(G), 2857140)
        assert_equal(H, soln)
        assert_equal(nx.min_cost_flow(G), soln)
        assert_equal(nx.cost_of_flow(G, H), 2857140)

        flowCost, H = nx.capacity_scaling(G)
        assert_equal(flowCost, 2857140)
        assert_equal(H, soln)
        assert_equal(nx.cost_of_flow(G, H), 2857140)
开发者ID:ProgVal,项目名称:networkx,代码行数:28,代码来源:test_mincost.py


示例4: run_random_trial

    def run_random_trial(self):
        G = nx.MultiDiGraph()
        nodes = range(1, 10)
        edges = generate_edges(nodes, 50)
        G.add_edges_from(edges)
        source = random.choice(G.nodes())
        while True:
            target = random.choice(G.nodes())
            if target != source:
                break
        amount = random.randint(1, 10)
        G.node[source]['demand'] = -amount
        G.node[target]['demand'] = amount

        cost, flow_dict, exception = None, None, None
        try:
            cost, flow_dict = min_cost_flow(G)
        except nx.NetworkXException as e:
            exception = e

        H = unmulti(G)
        try:
            cost2, flow_dict2 = nx.network_simplex(H)
        except nx.NetworkXException as e:
            self.assertEquals(type(e) != type(exception))
            self.assertEquals(cost, None)
        else:
            self.assertEquals(cost, cost2)
            self.assertEquals(exception, None)
开发者ID:allinfinite,项目名称:villagescc,代码行数:29,代码来源:tests.py


示例5: test_zero_capacity_edges

    def test_zero_capacity_edges(self):
        """Address issue raised in ticket #617 by arv."""
        G = nx.DiGraph()
        G.add_edges_from([(1, 2, {'capacity': 1, 'weight': 1}),
                          (1, 5, {'capacity': 1, 'weight': 1}),
                          (2, 3, {'capacity': 0, 'weight': 1}),
                          (2, 5, {'capacity': 1, 'weight': 1}),
                          (5, 3, {'capacity': 2, 'weight': 1}),
                          (5, 4, {'capacity': 0, 'weight': 1}),
                          (3, 4, {'capacity': 2, 'weight': 1})])
        G.nodes[1]['demand'] = -1
        G.nodes[2]['demand'] = -1
        G.nodes[4]['demand'] = 2

        flowCost, H = nx.network_simplex(G)
        soln = {1: {2: 0, 5: 1},
                2: {3: 0, 5: 1},
                3: {4: 2},
                4: {},
                5: {3: 2, 4: 0}}
        assert_equal(flowCost, 6)
        assert_equal(nx.min_cost_flow_cost(G), 6)
        assert_equal(H, soln)
        assert_equal(nx.min_cost_flow(G), soln)
        assert_equal(nx.cost_of_flow(G, H), 6)

        flowCost, H = nx.capacity_scaling(G)
        assert_equal(flowCost, 6)
        assert_equal(H, soln)
        assert_equal(nx.cost_of_flow(G, H), 6)
开发者ID:ProgVal,项目名称:networkx,代码行数:30,代码来源:test_mincost.py


示例6: test_large

 def test_large(self):
     fname = os.path.join(os.path.dirname(__file__), 'netgen-2.gpickle.bz2')
     G = nx.read_gpickle(fname)
     flowCost, flowDict = nx.network_simplex(G)
     assert_equal(6749969302, flowCost)
     assert_equal(6749969302, nx.cost_of_flow(G, flowDict))
     flowCost, flowDict = nx.capacity_scaling(G)
     assert_equal(6749969302, flowCost)
     assert_equal(6749969302, nx.cost_of_flow(G, flowDict))
开发者ID:ProgVal,项目名称:networkx,代码行数:9,代码来源:test_mincost.py


示例7: test_multidigraph

    def test_multidigraph(self):
        """Multidigraphs are acceptable."""
        G = nx.MultiDiGraph()
        G.add_weighted_edges_from([(1, 2, 1), (2, 3, 2)], weight='capacity')
        flowCost, H = nx.network_simplex(G)
        assert_equal(flowCost, 0)
        assert_equal(H, {1: {2: {0: 0}}, 2: {3: {0: 0}}, 3: {}})

        flowCost, H = nx.capacity_scaling(G)
        assert_equal(flowCost, 0)
        assert_equal(H, {1: {2: {0: 0}}, 2: {3: {0: 0}}, 3: {}})
开发者ID:ProgVal,项目名称:networkx,代码行数:11,代码来源:test_mincost.py


示例8: processWiki

def processWiki(page, p=None, ignoreConditionalProbabilities=False):
    if p == None: p = readDict("data/wpairs")
    pe = readCounts("data/unigram-counts/" + page + ".en.tok.ucounts")
    pf = readCounts("data/unigram-counts/" + page + ".fr.tok.ucounts")

    G = nx.DiGraph()
    # 1 is source (F), 2 is sink (E)
    # 3 is F-null, 4 is E-null

    G.add_node(1, {'demand': -1, 'name': '*SOURCE*'})
    G.add_node(2, {'demand':  1, 'name': '*SINK*'})
    G.add_node(3, {'name': '*F-NULL*'})
    G.add_node(4, {'name': '*E-NULL*'})

    G.add_edge(1, 3, {'capacity': 1, 'weight': 100000})
    G.add_edge(4, 2, {'capacity': 1, 'weight': 100000})

    nCnt = 5

    fID = {}
    for f in pf.iterkeys():
        fID[f] = nCnt
        G.add_node(nCnt, {'name': f, 'lang': 'f'})
        G.add_edge(1, nCnt, {'capacity': pf[f], 'weight': 1})
        G.add_edge(nCnt, 4, {'capacity': 1, 'weight': 100000})
        nCnt += 1

    eID = {}
    for e in pe.iterkeys():
        eID[e] = nCnt
        G.add_node(nCnt, {'name': e, 'lang': 'e'})
        G.add_edge(nCnt, 2, {'capacity': pe[e], 'weight': 1})
        G.add_edge(3, nCnt, {'capacity': 1, 'weight': 100000})
        nCnt += 1

    for f in pf.iterkeys():
        nf = fID[f]
        if p.has_key(f):
            for e in p[f].iterkeys():
                if eID.has_key(e):
                    ne = eID[e]
                    cap = p[f][e]
                    if ignoreConditionalProbabilities: cap = 1
                    G.add_edge(nf, ne, {'capacity': cap, 'weight': 1})
                    
    flowCost,flowDict = nx.network_simplex(G)

    return G,flowDict
开发者ID:TPNguyen,项目名称:IntrinsicPSDEvaluation,代码行数:48,代码来源:subtract_distribution.py


示例9: match

    def match(self, riders, ridees):
        graph = nx.DiGraph()
        for rider in riders:
            graph.add_node(rider.household_id, demand = - rider.capacity)

        for ridee in ridees:
            graph.add_node(ridee.household_id, demand = 1)

        for rider in riders:
            for ridee in ridees:
                distance = this._get_distance(rider.location, ridee.location)
                graph.add_edge(rider.household_id, ridee.household_id, weight = distance, capacity = 1)

        flowCost, flowDict = nx.network_simplex(graph)

        return flowDict
开发者ID:chrisjung,项目名称:rhok,代码行数:16,代码来源:match.py


示例10: nxMCF

def nxMCF(startNodes, endNodes, capacities, costs, supplies):
    G = nx.DiGraph()
    for n, s in enumerate(supplies):
        G.add_node(n, demand=-s)

    for edgei in range(len(startNodes)):
        G.add_edge(startNodes[edgei],
                   endNodes[edgei],
                   weight=costs[edgei],
                   capacity=capacities[edgei])

    cost, flow = nx.network_simplex(G, demand='demand', capacity='capacity', weight='weight')
    totalFlow = 0
    for k1,v1 in flow.items():
        for k2,v2 in v1.items():
            totalFlow += v2
    return cost, totalFlow
开发者ID:agartland,项目名称:utils,代码行数:17,代码来源:polyfunctionality-checkpoint.py


示例11: test_transshipment

    def test_transshipment(self):
        G = nx.DiGraph()
        G.add_node('a', demand=1)
        G.add_node('b', demand=-2)
        G.add_node('c', demand=-2)
        G.add_node('d', demand=3)
        G.add_node('e', demand=-4)
        G.add_node('f', demand=-4)
        G.add_node('g', demand=3)
        G.add_node('h', demand=2)
        G.add_node('r', demand=3)
        G.add_edge('a', 'c', weight=3)
        G.add_edge('r', 'a', weight=2)
        G.add_edge('b', 'a', weight=9)
        G.add_edge('r', 'c', weight=0)
        G.add_edge('b', 'r', weight=-6)
        G.add_edge('c', 'd', weight=5)
        G.add_edge('e', 'r', weight=4)
        G.add_edge('e', 'f', weight=3)
        G.add_edge('h', 'b', weight=4)
        G.add_edge('f', 'd', weight=7)
        G.add_edge('f', 'h', weight=12)
        G.add_edge('g', 'd', weight=12)
        G.add_edge('f', 'g', weight=-1)
        G.add_edge('h', 'g', weight=-10)
        flowCost, H = nx.network_simplex(G)
        soln = {'a': {'c': 0},
                'b': {'a': 0, 'r': 2},
                'c': {'d': 3},
                'd': {},
                'e': {'r': 3, 'f': 1},
                'f': {'d': 0, 'g': 3, 'h': 2},
                'g': {'d': 0},
                'h': {'b': 0, 'g': 0},
                'r': {'a': 1, 'c': 1}}
        assert_equal(flowCost, 41)
        assert_equal(nx.min_cost_flow_cost(G), 41)
        assert_equal(H, soln)
        assert_equal(nx.min_cost_flow(G), soln)
        assert_equal(nx.cost_of_flow(G, H), 41)

        flowCost, H = nx.capacity_scaling(G)
        assert_equal(flowCost, 41)
        assert_equal(nx.cost_of_flow(G, H), 41)
        assert_equal(H, soln)
开发者ID:ProgVal,项目名称:networkx,代码行数:45,代码来源:test_mincost.py


示例12: test_simple_digraph

 def test_simple_digraph(self):
     G = nx.DiGraph()
     G.add_node('a', demand = -5)
     G.add_node('d', demand = 5)
     G.add_edge('a', 'b', weight = 3, capacity = 4)
     G.add_edge('a', 'c', weight = 6, capacity = 10)
     G.add_edge('b', 'd', weight = 1, capacity = 9)
     G.add_edge('c', 'd', weight = 2, capacity = 5)
     flowCost, H = nx.network_simplex(G)
     soln = {'a': {'b': 4, 'c': 1},
             'b': {'d': 4},
             'c': {'d': 1},
             'd': {}}
     assert_equal(flowCost, 24)
     assert_equal(nx.min_cost_flow_cost(G), 24)
     assert_equal(H, soln)
     assert_equal(nx.min_cost_flow(G), soln)
     assert_equal(nx.cost_of_flow(G, H), 24)
开发者ID:mshelton,项目名称:networkx,代码行数:18,代码来源:test_mincost.py


示例13: test_bone_shaped

 def test_bone_shaped(self):
     # From #1283
     G = nx.DiGraph()
     G.add_node(0, demand=-4)
     G.add_node(1, demand=2)
     G.add_node(2, demand=2)
     G.add_node(3, demand=4)
     G.add_node(4, demand=-2)
     G.add_node(5, demand=-2)
     G.add_edge(0, 1, capacity=4)
     G.add_edge(0, 2, capacity=4)
     G.add_edge(4, 3, capacity=4)
     G.add_edge(5, 3, capacity=4)
     G.add_edge(0, 3, capacity=0)
     flowCost, H = nx.network_simplex(G)
     assert_equal(flowCost, 0)
     assert_equal(
         H, {0: {1: 2, 2: 2, 3: 0}, 1: {}, 2: {}, 3: {}, 4: {3: 2}, 5: {3: 2}})
     flowCost, H = nx.capacity_scaling(G)
     assert_equal(flowCost, 0)
     assert_equal(
         H, {0: {1: 2, 2: 2, 3: 0}, 1: {}, 2: {}, 3: {}, 4: {3: 2}, 5: {3: 2}})
开发者ID:ProgVal,项目名称:networkx,代码行数:22,代码来源:test_mincost.py


示例14: range

    nz_idx = pd.Series(np.nonzero(C_transfers.values)[0])
    for i in range(abs(dif)):
        C_transfers.iloc[nz_idx[i]] -= direct 

for i in CG.nodes():
    CG.node[i]['load'] = C_loads[i]
    CG.node[i]['gen'] = C_gen[i]
    CG.node[i]['trans'] = C_transfers[i]

for i in CG.nodes():
    CG.node[i]['MW_net'] = CG.node[i]['gen'] - CG.node[i]['load'] - CG.node[i]['trans']

DG = CG.to_directed()


NS = nx.network_simplex(DG, demand='MW_net', weight='R', capacity='tot_MW_cap')
#### EVERYTHING BELOW DOESN'T WORK!!!

for i in G.nodes():
    kv_list = [G.adj[i][j]['tot_kv'] for j in G.adj[i].keys() if isinstance(j, int)]
    kv_max, kv_min = max(kv_list), min(kv_list)
    G[i]['max_volt'] = kv_max
    G[i]['min_volt'] = kv_min

#### GET GRID VOLTAGES FROM EIA FORM DATA

mwkv = pd.DataFrame(np.zeros(len(G.nodes())), index=G.nodes())

for x in ['load', 'gen', 'trans', 'min_volt', 'max_volt']:
    mwkv_col = pd.DataFrame(np.vstack([tuple([i, G[i][x]]) for i in G.nodes() if x in G[i].keys()])).rename(columns={1 : x}).set_index(0)
    mwkv = pd.concat([mwkv, mwkv_col], axis=1)
开发者ID:deisenb,项目名称:RIPS,代码行数:31,代码来源:network_messy.py


示例15:

G.add_node(1, demand=-2)
G.add_node(2, demand=-5)
# Adiciona nos de demanda
G.add_node(3, demand=1)
G.add_node(4, demand=3)
G.add_node(5, demand=3)
# Adiciona os arcos
G.add_edge(1, 3, weight=3)
G.add_edge(2, 3, weight=7)
# ###################################
G.add_edge(1, 4, weight=4)
G.add_edge(2, 4, weight=2)
# ###################################
G.add_edge(1, 5, weight=1)
G.add_edge(2, 5, weight=5)
flowCost, flowDict = nx.network_simplex(G)
print flowCost
# print flowDict
k,w = 0,0
mat = np.zeros([2,3])
cost = np.zeros([2,3])
total = 0
for i in flowDict:
    #print "i:%d\n"%i
    w = 0
    for j in flowDict[i]:
        # print flowDict[i][j],
        mat[k,w] = flowDict[i][j]
        cost[k,w] = G[i][j]["weight"]
        w += 1
        total += flowDict[i][j]*G[i][j]["weight"]
开发者ID:caioau,项目名称:personal,代码行数:31,代码来源:ex3.py


示例16: rig_component

def rig_component(G, x, y, maxW):
    """ Compute the RIG metric on a single component.

    Parameters
    ----------
    G : nx.Graph
       A connected graph of weighted edges
    x : pd.Series
       Vector of nodes and their abundance in sample x
       within the connected graph G
    y : pd.Series
       Vector of nodes and their abundance in sample y
       within the connected graph G
    maxW : float
       The cost of an insertion step

    Returns
    -------
    float :
       Distance between sample x and sample y

    Note
    ----
    If x or y is None, then 1 will be added to the total distance.
    If they are both None, then the distance will be zero.
    Also, the weights of the edges must be contained in `'weight'`.
    """

    # Both samples don't contain any metabolites
    if len(x) == 0 and len(y) == 0:
        return 0
    xtotal = int(max([x.sum(), 1]))
    ytotal = int(max([y.sum(), 1]))

    # Convert everything to fractions
    if x.sum() != 0:
        x = pd.Series([Fraction(i, xtotal) for i in np.ravel(x.values)], index=x.index)
    if y.sum() != 0:
        y = pd.Series([Fraction(i, ytotal) for i in np.ravel(y.values)], index=y.index)
    if x.equals(y):
        return 0

    # The component being analyzed has only 1 node.
    # So the networkx simplex algorithm cannot be run
    # since there are no edges.
    if len(list(G.nodes())) == 1:
        return maxW

    cost = 0
    edges = G.edges(data="weight")

    # If there one of the samples doesn't have any metabolites
    # on the component, arbituarily pick two metabolites
    # and append them to the set.  This to address the issue of
    # measuring distance between unshared components.
    if len(x) == 0 or x.sum() == 0:
        x = pd.Series({y.index[0]: Fraction(1, 1)})
        weight = maxW
    elif len(y) == 0 or y.sum() == 0:
        y = pd.Series({x.index[0]: Fraction(1, 1)})
        weight = maxW
    else:
        weight = 0
    _G = copy.deepcopy(G)

    xarr = pd.Series({n: (x[n] if n in x else 0) for n in G.nodes()})
    yarr = pd.Series({n: (y[n] if n in y else 0) for n in G.nodes()})

    d = 0
    maxxD = max(map(lambda k: k.denominator, x))
    maxyD = max(map(lambda k: k.denominator, y))

    for node in _G.nodes():
        dmd = int((xarr[node] - yarr[node]) * xtotal * ytotal)
        _G.node[node]["demand"] = dmd
        d += dmd

    W, _ = nx.network_simplex(_G.to_directed())
    cost += W + weight
    return cost
开发者ID:mortonjt,项目名称:chemifrac,代码行数:80,代码来源:rig.py


示例17: min_cost_flow

def min_cost_flow(G, demand = 'demand', capacity = 'capacity',
                  weight = 'weight'):
    r"""Return a minimum cost flow satisfying all demands in digraph G.

    G is a digraph with edge costs and capacities and in which nodes
    have demand, i.e., they want to send or receive some amount of
    flow. A negative demand means that the node wants to send flow, a
    positive demand means that the node want to receive flow. A flow on
    the digraph G satisfies all demand if the net flow into each node
    is equal to the demand of that node.

    Parameters
    ----------
    G : NetworkX graph
        DiGraph on which a minimum cost flow satisfying all demands is
        to be found.

    demand : string
        Nodes of the graph G are expected to have an attribute demand
        that indicates how much flow a node wants to send (negative
        demand) or receive (positive demand). Note that the sum of the
        demands should be 0 otherwise the problem in not feasible. If
        this attribute is not present, a node is considered to have 0
        demand. Default value: 'demand'.

    capacity : string
        Edges of the graph G are expected to have an attribute capacity
        that indicates how much flow the edge can support. If this
        attribute is not present, the edge is considered to have
        infinite capacity. Default value: 'capacity'.

    weight : string
        Edges of the graph G are expected to have an attribute weight
        that indicates the cost incurred by sending one unit of flow on
        that edge. If not present, the weight is considered to be 0.
        Default value: 'weight'.

    Returns
    -------
    flowDict : dictionary
        Dictionary of dictionaries keyed by nodes such that
        flowDict[u][v] is the flow edge (u, v).

    Raises
    ------
    NetworkXError
        This exception is raised if the input graph is not directed or
        not connected.

    NetworkXUnfeasible
        This exception is raised in the following situations:

            * The sum of the demands is not zero. Then, there is no
              flow satisfying all demands.
            * There is no flow satisfying all demand.

    NetworkXUnbounded
        This exception is raised if the digraph G has a cycle of
        negative cost and infinite capacity. Then, the cost of a flow
        satisfying all demands is unbounded below.

    See also
    --------
    cost_of_flow, max_flow_min_cost, min_cost_flow_cost, network_simplex

    Examples
    --------
    A simple example of a min cost flow problem.

    >>> import networkx as nx
    >>> G = nx.DiGraph()
    >>> G.add_node('a', demand = -5)
    >>> G.add_node('d', demand = 5)
    >>> G.add_edge('a', 'b', weight = 3, capacity = 4)
    >>> G.add_edge('a', 'c', weight = 6, capacity = 10)
    >>> G.add_edge('b', 'd', weight = 1, capacity = 9)
    >>> G.add_edge('c', 'd', weight = 2, capacity = 5)
    >>> flowDict = nx.min_cost_flow(G)
    """
    return nx.network_simplex(G, demand = demand, capacity = capacity,
                              weight = weight)[1]
开发者ID:4c656554,项目名称:networkx,代码行数:81,代码来源:mincost.py


示例18: min_cost_flow_cost

def min_cost_flow_cost(G, demand='demand', capacity='capacity',
                       weight='weight'):
    r"""Find the cost of a minimum cost flow satisfying all demands in digraph G.

    G is a digraph with edge costs and capacities and in which nodes
    have demand, i.e., they want to send or receive some amount of
    flow. A negative demand means that the node wants to send flow, a
    positive demand means that the node want to receive flow. A flow on
    the digraph G satisfies all demand if the net flow into each node
    is equal to the demand of that node.

    Parameters
    ----------
    G : NetworkX graph
        DiGraph on which a minimum cost flow satisfying all demands is
        to be found.

    demand : string
        Nodes of the graph G are expected to have an attribute demand
        that indicates how much flow a node wants to send (negative
        demand) or receive (positive demand). Note that the sum of the
        demands should be 0 otherwise the problem in not feasible. If
        this attribute is not present, a node is considered to have 0
        demand. Default value: 'demand'.

    capacity : string
        Edges of the graph G are expected to have an attribute capacity
        that indicates how much flow the edge can support. If this
        attribute is not present, the edge is considered to have
        infinite capacity. Default value: 'capacity'.

    weight : string
        Edges of the graph G are expected to have an attribute weight
        that indicates the cost incurred by sending one unit of flow on
        that edge. If not present, the weight is considered to be 0.
        Default value: 'weight'.

    Returns
    -------
    flowCost : integer, float
        Cost of a minimum cost flow satisfying all demands.

    Raises
    ------
    NetworkXError
        This exception is raised if the input graph is not directed or
        not connected.

    NetworkXUnfeasible
        This exception is raised in the following situations:

            * The sum of the demands is not zero. Then, there is no
              flow satisfying all demands.
            * There is no flow satisfying all demand.

    NetworkXUnbounded
        This exception is raised if the digraph G has a cycle of
        negative cost and infinite capacity. Then, the cost of a flow
        satisfying all demands is unbounded below.

    See also
    --------
    cost_of_flow, max_flow_min_cost, min_cost_flow, network_simplex

    Notes
    -----
    This algorithm is not guaranteed to work if edge weights or demands
    are floating point numbers (overflows and roundoff errors can
    cause problems). As a workaround you can use integer numbers by
    multiplying the relevant edge attributes by a convenient
    constant factor (eg 100).

    Examples
    --------
    A simple example of a min cost flow problem.

    >>> import networkx as nx
    >>> G = nx.DiGraph()
    >>> G.add_node('a', demand = -5)
    >>> G.add_node('d', demand = 5)
    >>> G.add_edge('a', 'b', weight = 3, capacity = 4)
    >>> G.add_edge('a', 'c', weight = 6, capacity = 10)
    >>> G.add_edge('b', 'd', weight = 1, capacity = 9)
    >>> G.add_edge('c', 'd', weight = 2, capacity = 5)
    >>> flowCost = nx.min_cost_flow_cost(G)
    >>> flowCost
    24
    """
    return nx.network_simplex(G, demand=demand, capacity=capacity,
                              weight=weight)[0]
开发者ID:ProgVal,项目名称:networkx,代码行数:90,代码来源:mincost.py



注:本文中的networkx.network_simplex函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python networkx.node_connected_component函数代码示例发布时间:2022-05-27
下一篇:
Python networkx.neighbors函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap