Appending variables from previous method calls in later ones - python

Hi a sample output from running this code is shown as well, The script runs as well, you could run it to get a sense of what it does. My problem, is given the three different calls to the route() function what codes can I add to get a list of all previous objective function values ie obj variable on line 79.
desired output = obj_rslt = [20.989285714285714, 21.166176470588233, 25.8656 ]
I have tried to use the copy.copy() but it does not work, I need a list of all values as shown above for further work in another function. Thank you.
#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 ]

The problem is that you always set obj_rslt to []
Move the assignment obj_rslt = [] out of the method after the definition of pos and edge then you should be fine
#...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
The ouput will be a list with all three results

Related

How to fill quantity values into network in python with networkx?

The main question is, it's possible distribute a determinate quantity in a network satisfying the nodal equations in every node?
e.g: adding quantity values in a network(mesh) {GIF}
**Nodal equations: Incoming flow (quantity) = Outgoing flow (quantity) for every node.
import networkx as nx
inputName = [('D','H'),('G','E'),('B','F'),('H','C'),('E','B'),('C','G'),('H','A'),('C','E'),('A','G'),('D','A'),('F','D')]
quantity = [0,0,0,0,0,0,0,0,0,0,0]
resistance = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,1.1]
fan = [0,0,0,0,0,0,2000,0,0,0,0]
def createDirectedGraph(inputName):
ventilationNetwork = nx.DiGraph()
i = 0
for edge in inputName:
node_from = edge[0]
node_to = edge[1]
ventilationNetwork.add_edge(node_from, node_to)
ventilationNetwork.edges[node_from, node_to]['quantity'] = quantity[i]
ventilationNetwork.edges[node_from, node_to]['resistance'] = resistance[i]
ventilationNetwork.edges[node_from, node_to]['fan'] = fan[i]
i += 1
return ventilationNetwork
I was trying the following:
count = 1
for node in ventilationNetwork.nodes:
if count == 1:
quantity = 100 # Any quantity != 0
for node_from in ventilationNetwork.predecessors(node):
ventilationNetwork.edges[node_from, node]['quantity'] = quantity/(len(list(ventilationNetwork.predecessors(node))))
for node_to in ventilationNetwork.successors(node):
ventilationNetwork.edges[node, node_to]['quantity'] = quantity/(len(list(ventilationNetwork.successors(node))))
else:
sumQ = 0
for node_from in ventilationNetwork.predecessors(node):
sumQ += ventilationNetwork.edges[node_from, node]['quantity']
ventilationNetwork.edges[node_from, node]['quantity'] = sumQ/(len(list(ventilationNetwork.predecessors(node))))
for node_to in ventilationNetwork.successors(node):
sumQ += ventilationNetwork.edges[node, node_to]['quantity']
ventilationNetwork.edges[node, node_to]['quantity'] = sumQ/(len(list(ventilationNetwork.successors(node))))
count += 1
but it's not working. It's not satisfying the nodal equations.
Any idea how to solve this? With NetworkX or without NetworkX.

How to use for loop inside a capacitated vehicle routing problem (Google OR tools)

I want to find the shortest route distance and vehicle load carrying capacity a vehicle can have. It is a capacitated vehicle routing problem (Google OR tools). I want to use a for- loop in between the code so that it gives the distance_optimization for all the district which gets saved in one location.
for example : out_1 = get_sol( "banswara",1) shows the route for the fos (vehicle) for just "banswara" district. I want to write a for-loop so that output shows the same result for all the districts.
my code:
dist = DistanceMetric.get_metric('haversine')
def create_data_model(df_user,no_fos):
"""Stores the data for the problem."""
data = {}
data['distance_matrix'] = dist.pairwise(df_user [['lat','lon']].to_numpy())*6373
data['num_vehicles'] = no_fos
data['depot'] = 0
return data
def print_solution(data, manager, routing, solution):
"""Prints solution on console."""
max_route_distance = 0
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
plan_output = 'Route for FOS {}:\n'.format(vehicle_id)
route_distance = 0
while not routing.IsEnd(index):
plan_output += ' {} -> '.format(manager.IndexToNode(index))
previous_index = index
index = solution.Value(routing.NextVar(index))
route_distance += routing.GetArcCostForVehicle(
previous_index, index, vehicle_id)
plan_output += '{}\n'.format(manager.IndexToNode(index))
plan_output += 'Distance of the route: {}m\n'.format(route_distance)
print(plan_output)
max_route_distance = max(route_distance, max_route_distance)
print('Maximum of the route distances: {}Km'.format(max_route_distance))
def distance(x,y,data):
dis = data['distance_matrix'][x][y]
return dis
def get_routes(solution, routing, manager,df_user,data):
"""Get vehicle routes from a solution and store them in an array."""
# Get vehicle routes and store them in a two dimensional array whose
# i,j entry is the jth location visited by vehicle i along its route.
routes = []
#routes_dist = []
for route_nbr in range(routing.vehicles()):
index = routing.Start(route_nbr)
route = [manager.IndexToNode(index)]
while not routing.IsEnd(index):
index = solution.Value(routing.NextVar(index))
route.append(manager.IndexToNode(index))
routes.append(route)
#routes = get_routes(solution, routing, manager)
routes_t = pd.DataFrame(routes).T
col_to_iter = routes_t.columns
routes_t['route_info'] = routes_t.index
routes_t = pd.melt(routes_t, id_vars=['route_info'], value_vars=col_to_iter)
routes_t = routes_t.drop_duplicates(subset='value', keep="first")
df_user['value'] = df_user.index
df_user_out = pd.merge(df_user, routes_t, on="value")
df_user_out = df_user_out.sort_values(by=['variable','route_info'])
df_user_out['route_lag'] = df_user_out.groupby('variable')['value'].shift(-1).fillna(0)
df_user_out['route_lag'] = df_user_out['route_lag'].astype(np.int64)
df_user_out['route_info'] = df_user_out['route_info'].astype(np.int64)
df_user_out['dist'] = df_user_out.apply(lambda row: distance(row['route_lag'], row['value'],data), axis=1)
return df_user_out
def get_sol(sub_dist_fil,fos_cnt):
df_user_org_sub = df_user_org[(df_user_org.sub_district == sub_dist_fil) ]
df_user_org_sub.reset_index( inplace=True,drop=True)
fos_cnt=fos_cnt
data = create_data_model(df_user_org_sub,fos_cnt)
# Create the routing index manager.
manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),
data['num_vehicles'], data['depot'])
# Create Routing Model.
routing = pywrapcp.RoutingModel(manager)
# Create and register a transit callback.
def distance_callback(from_index, to_index):
"""Returns the distance between the two nodes."""
# Convert from routing variable Index to distance matrix NodeIndex.
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['distance_matrix'][from_node][to_node]
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
# Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
# Add Distance constraint.
dimension_name = 'Distance'
routing.AddDimension(
transit_callback_index,
0, # no slack
3000, # vehicle maximum travel distance
True, # start cumul to zero
dimension_name)
distance_dimension = routing.GetDimensionOrDie(dimension_name)
distance_dimension.SetGlobalSpanCostCoefficient(100)
# Setting first solution heuristic.
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
# Solve the problem.
solution = routing.SolveWithParameters(search_parameters)
out_df = get_routes(solution, routing, manager,df_user_org_sub,data)
# Print solution on console.
if solution:
print_solution(data, manager, routing, solution)
return out_df
out_1 = get_sol( "banswara",1)
The output is as follows:
Route for FOS 0:
0 -> 14 -> 1 -> 9 -> 8 -> 7 -> 10 -> 11 -> 16 -> 13 -> 15 -> 12 -> 4 -> 3 -> 2 -> 5 -> 6 -> 0
Distance of the route: 217m
Maximum of the route distances: 217Km
So, the above output is just for banswara district, but i want the same result for all the district by writing a for loop in between.

Breadth's Algorithm Runs Forever Despite Possible Ending

So I saw this coding interview question and tried to solve it. I am trying to employ Breadth's Path Finding Algorithm to find the optimum flight routes from a given airport to all other airports; given the list of all airports and routes. An element in routes means that there is a one way flight from the first airport to the second one.
I got here, this was supposed to find the shortest routes to all other airports from a given airport. But when I run it never ends.
I figured that my algorithm doesn't append all possible next airports to my paths, but everything seems o.k to me.
'''
import queue
airports = ["BGI", "CDG", "DEL", "DOH", "DSM", "EWR", "EYW", "HND", "ICN", "JFK", "LGA",
"LHR", "ORD", "SAN", "SFO", "SIN", "TLV", "BUD"]
routes = [["DSM", "ORD"], ["ORD", "BGI"], ["BGI", "LGA"], ["SIN", "CDG"], ["CDG", "SIN"],
["CDG", "BUD"], ["DEL", "DOH"], ["DEL", "CDG"], ["TLV", "DEL"], ["EWR", "HND"],
["HND", "ICN"], ["HND", "JFK"], ["ICN", "JFK"], ["JFK", "LGA"], ["EYW", "LHR"],
["LHR", "SFO"], ["SFO", "SAN"], ["SFO", "DSM"], ["SAN", "EYW"], ["LGA", "EYW"]]
main = "LGA"
def done(moves, aim):
if moves == "":
return False
elif moves[-1][1] == aim:
return True
return False
def valid(moves, put):
if moves == "":
return False
if moves[-1][1] == put[0]:
return True
return False
def available_starts(start, pos):
anfaenge = list()
for i in pos:
if i[0] == start:
anfaenge.append(i)
return anfaenge
#MAIN ALGORITHM
kurzeste_moeglichkeiten = [] """all shortest possibilities"""
for port in airports:
nums = queue.Queue()
nums.put("")
add = ""
start = main
if port != start:
anfaenge = available_starts(start, routes) """possible startings"""
for anfang in anfaenge:
anfang = [anfang]
nums.put(anfang)
while not done(add, port):
add = nums.get()
for got in routes:
if valid(add, got):
t = add
t.append(got)
nums.put(t)
kurzeste_moeglichkeiten.append(add)
for eine in kurzeste_moeglichkeiten:
print(eine)
'''
I've managed to represent your graph with the modules networkx and matplotlib. As you can see, the airports are splitted in 2 groups.
As mentionned, I do believe 2 of your routes ["SIN", "CDG"] and ["CDG", "SIN"] are the reverse ones and are not OK with the rest of your data.
NOTE: the code I've used to show the graph:
import networkx as nx
import matplotlib.pyplot as plt
airports = ["BGI", "CDG", "DEL", "DOH", "DSM", "EWR", "EYW", "HND", "ICN", "JFK", "LGA",
"LHR", "ORD", "SAN", "SFO", "SIN", "TLV", "BUD"]
legs = [["DSM", "ORD"], ["ORD", "BGI"], ["BGI", "LGA"], ["SIN", "CDG"], ["CDG", "SIN"],
["CDG", "BUD"], ["DEL", "DOH"], ["DEL", "CDG"], ["TLV", "DEL"], ["EWR", "HND"],
["HND", "ICN"], ["HND", "JFK"], ["ICN", "JFK"], ["JFK", "LGA"], ["EYW", "LHR"],
["LHR", "SFO"], ["SFO", "SAN"], ["SFO", "DSM"], ["SAN", "EYW"], ["LGA", "EYW"]]
main = "LGA"
G = nx.Graph()
for node in airports:
G.add_node(node)
for leg in legs:
G.add_edge(leg[0], leg[1])
plt.subplot(121)
nx.draw(G, with_labels=True)

How do I raise the memory limit in python?

I have written a python (2.7) script but it use a lot of memory so I get a out of memory error. Is it possible to use memory?
My code (or the github):
from itertools import combinations
import numpy
# Find the unused members and put this in a other group
def findMembers(listIn,listMembers):
lengthlist2 = (len(listMembers)-len(listIn[0]))
group2 = [0] * lengthlist2 #making the other groups based on the length of the first group
for i in listIn:
wichRow = 0
for x in listMembers:
if not (x in i) :
group2[wichRow] = x
wichRow += 1
listIn.append(group2)
return listIn
#you give a list of members and the numbers of groups
#you get back all the possibilities of combinations
def findCombinations(listMembers,numbersOfGroups):
groupTemp = [] #list needed to save correctly all the combinations
group = [] #list needed for keep it simple
newGroup = [] #list that will be returned
for listPossibilities in combinations(listMembers,(len(listMembers)/numbersOfGroups)):
groupTemp.append(list(listPossibilities))
group.append(groupTemp) #saving all the possibilities
groupTemp = []
for k in group:
# place the unused members in group2
k = (findMembers(k,listMembers))
if numbersOfGroups > 2:
groupTemp = []
groupTemp = findCombinations(k[1],numbersOfGroups-1)
for i in groupTemp:
listTemp = []
listTemp.append(k[0])
listTemp.extend(i)
newGroup.append(listTemp)
else:
newGroup = group
return newGroup
# Calculate the happiness of the group
def findHappiness(tabel,listIn):
happiness = 0
for i in listIn:
for j in i:
for k in i:
happiness += tabel[j][k]
return happiness
def buildTabel(members): #build a random survey
tabel = numpy.random.random((members,members))
return tabel
def calculateHappiness(group):
print "Finding all the happiness: "
maxhappiness = 0
i = 0
for x in group:
happiness = findHappiness(tabel,x)
if happiness > maxhappiness:
maxhappiness = happiness
y = x
progress = int(round((((i)*1.0/(len(group)))*100.0)))
update_progress(progress)
i += 1
print "\n Best solution: ", y, " with: ", maxhappiness, " happiness"
def update_progress(progress):
print '\r[{0}] {1}%'.format('#'*(progress/5), progress),
if __name__ == "__main__":
members = 24 # members of the group
numbersOfGroups = 3
tabel = buildTabel(members) #preferences will be stored here
listMembers = (range(members)) #members of the group that need to be divided
print "Searching all the combinations..."
group = findCombinations(listMembers,numbersOfGroups) #find all the combinations (recursive)
print len(group)," combinations"
calculateHappiness(group) #calculate the most happiest group and print
the error:
Searching all the combinations...
Traceback (most recent call last):
File "main.py", line 75, in <module>
calculateHappiness(group) #calculate the most happiest group and print
File "main.py", line 38, in findCombinations
newGroup = group
MemoryError
I'm using windows 10 64bit with 6gb ram. Is it possible to use virtual ram or disk space of mine hard drive disk?

NLTK output changes when using the same input on the same machine

The next piece of code returns different output with the same input(self.SynSets)
Why can it be happening? Am I doing something wrong? or is it caused by python?
def FilterSynSets(self):
self.filteredSysNets = {}
for synset in self.SysNets:
for subsynset in self.SysNets:
length = wn.path_similarity(synset,subsynset)
if not length is None\
and length !=1\
and length >0:
target = synset.__str__().replace("'","")
source =subsynset.__str__().replace("'","")
connection="\"{}\"->\"{}\" [label={}]".format(
target,
source,
str(round(length,3)))
self.filteredSysNets[connection] = length
oldLength = len(self.filteredSysNets)
avarageVal = sum(self.filteredSysNets.values())/len(self.filteredSysNets)
self.filteredSysNets = {k: v for k,v in self.filteredSysNets.items() if v>=avarageVal}
newLength = len(self.filteredSysNets)
prt = newLength/oldLength*100
print ("avr -{}\n old -{}\n new - {}\n prec={}".format(avarageVal,oldLength,newLength,prt))
return self
Outputs:
http://screencast.com/t/eFMOROfkPXR
http://screencast.com/t/Fdd6ufhA

Categories