在以后的方法中添加先前方法调用的变量

时间:2014-05-24 12:56:24

标签: python python-2.7 networkx

运行此代码的示例输出也会显示,脚本也会运行,您可以运行它以了解它的作用。我的问题是,对route()函数进行了三次不同的调用,我可以添加哪些代码来获取所有先前目标函数值的列表,即第79行的obj变量。

 desired output = obj_rslt = [20.989285714285714, 21.166176470588233, 25.8656 ]

我曾尝试使用copy.copy()但它不起作用,我需要一个如上所示的所有值的列表,以便在另一个函数中进一步工作。谢谢。

#import statements
import copy
import networkx as nx
import random as rand

#Define nodes and Edges
pos = {1001:(-42503,-3748871),1002:(-42267,-3749806),1003:(-40938,-3750235),1004: (-39452,-3750624),1005:(-39985,-3749564),1006:(-38473,-3749615),1007:(-41714,-3747171),1008:(-42279,-3745275),1009:(-41853,-3744185),1010:(-42000,-3746561),1011:(-42651,-3746188),1012:(-42195,-3747788),1013:(-41498,-3748890),1014:(-40366,-3748684),1015:(-43036,-3750284)}

edge = [(1001, 1003,{'length':0.35}),(1001, 1004,{'length':0.46}),(1001, 1009,{'length':0.49}),(1002, 1007,{'length':0.22}),(1002, 9972,{'length':0.54}),(1002, 1013,{'length':0.59}),(1003, 1014,{'length':0.25}),(1004, 1010,{'length':0.29}),(1004, 1013,{'length':0.57}),(1004, 1003,{'length':0.43}),(1004, 1006,{'length':0.37}),(1005, 1002,{'length':0.23}),(1005, 14566,{'length':0.72}),(1006, 1005,{'length':0.6}),(1007, 1003,{'length':0.39}),(1007, 1010,{'length':0.11}),(1009, 1001,{'length':0.51}),(1010, 1005,{'length':0.2}),(1011, 1004,{'length':0.37}),(1012, 1006,{'length':0.17}),(1013, 1005,{'length':0.19}),(1013, 1007,{'length':0.21}),(1014, 1005,{'length':0.35}),(1014, 1009,{'length':0.51})]

#Create the graph and add the nodes and edges
X = nx.MultiDiGraph()
X.add_nodes_from(pos.keys())
X.add_edges_from(edge)

def routes():

    """ This function cretaes busroutes """

    individual = []
    shortest_path_length = []
    num_routes = 3

    #Generate the bus routes
    for i in xrange(num_routes):

            while True:

                try:

                    A = int(rand.choice(pos.keys()))
                    B = int(rand.choice(pos.keys()))
                    path = nx.dijkstra_path(X,A,B,weight='length')
                    individual.append(path)
                    pathlength = round(nx.dijkstra_path_length(X,A,B),2)

                    if pathlength > 1:
                        shortest_path_length.append(pathlength)
                        break

                    else: pathlength

                except:pass

    # Loop through the list of shortest path nodes to get the nodes for the bus route
    #bus_route_nodes = [map(lambda x: str(x) + '.0', l) for l in individual]

    veh_spid = []
    veh_hdw = []
    obj_rslt = []
    for ind in individual:

        try:
            headway = rand.randint(2, 30)
            veh_hdw.append(headway)
            speed = rand.choice([2,15,66])
            veh_spid.append(speed)

        except: pass

    # Print bus route features
    print 'OUTPUTS:'
    print 'Route Name:', str(ind[0]) + ' ' + '-' + ' ' + str(ind[-1])
    print 'Route Number:', individual.index(ind) + 1
    print 'Route headway = ',headway
    print 'Route speed = ',speed
    print 'shortest path length', shortest_path_length

    # Average network characteristics gotten by taken the mean of individual characteristics that make up each network
    ntwk_len = sum(shortest_path_length)
    ntwk_spid =  sum(veh_spid)/len(veh_spid)
    ntwk_hdwy = sum(veh_hdw )/len(veh_hdw)

    #Calculate objective function values
    obj = [0]
    obj = copy.copy(obj)
    obj = ( (ntwk_len/ntwk_spid) + 5 * (60/ntwk_hdwy) + (ntwk_len) )
    obj_rslt.append(obj)

    print 'obj_rslt', obj_rslt
    print

    return individual

#Three distinct method calls
routes()
routes()
routes()

OUTPUTS:
Route Name: 1014 - 1001
Route Number: 6
Route headway =  29
Route speed =  2
shortest path length [1.8, 2.77, 1.02]
obj_rslt [20.989285714285714]

OUTPUTS:
Route Name: 1003 - 1007
Route Number: 9
Route headway =  5
Route speed =  66
shortest path length [2.37, 2.57, 1.05]
obj_rslt [21.166176470588233]

OUTPUTS:
Route Name: 1012 - 1013
Route Number: 6
Route headway =  6
Route speed =  66
shortest path length [2.2, 1.85, 1.59]
obj_rslt [25.8656]

desired output = obj_rslt = [20.989285714285714, 21.166176470588233, 25.8656 ]

1 个答案:

答案 0 :(得分:2)

问题是您始终将obj_rslt设置为[]obj_rslt = []pos的定义之后将作业edge移出方法,那么你应该没问题

#...your code
obj_results = []
def routes():
    #do some computation and assume you store it in
    #a variable called res
    obj_results.append(res)
    return res

routes()
routes()
routes()

print obj_results

输出将是包含所有三个结果的列表