Skip to content

Data Converter (drive, bike, walk) #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions dataConverter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 30 12:45:06 2020

@author: ma076216
"""

# Read a csv file with source, target, and length and generate a standard graph
import csv
import json
Expand All @@ -8,8 +15,9 @@
dFile = []

# loading the graph from the json file located in .\graph folder
dirName = path.dirname(__file__)
fileName = path.join(dirName, 'graph/TestData.csv')
#dirName = path.dirname(__file__)
dirName = "C:/Users/ma076216/Desktop/PhD/research paper/python_code"
fileName = path.join(dirName, 'graph/graph_avg.csv')

# open the CSV input file
with open(fileName, newline='') as csvfile:
Expand All @@ -26,7 +34,7 @@
index = 0
while index < len(nodes):
graph.append({'id': index, 'gid': nodes[index],
'lat': 0, 'lon': 0, 'nbrs': [], 'dists': []})
'lat': 0, 'lon': 0, 'nbrs': [], 'dists': [],'TT':[],'bike':[],'walk':[]})
index += 1

# update the neighbor nodes in the graph
Expand All @@ -40,12 +48,15 @@
if (t != s): # edges starting and ending at the same node are useless
if (t not in graph[s]['nbrs']):
graph[s]['nbrs'].append(t)
graph[s]['dists'].append(float(row['dist']))
graph[s]['dists'].append(float(row['length_m']))
if (s not in graph[t]['nbrs']):
graph[t]['nbrs'].append(s)
graph[t]['dists'].append(float(row['dist']))
graph[t]['dists'].append(float(row['length_m']))
graph[t]['TT'].append(float(row['tt_thodaysec']))
graph[t]['bike'].append(float(row['Biking travel time']))
graph[t]['walk'].append(float(row['walking travel time_sec']))

# stroing graph.json file in the graph folder. It will overwrite existing files
fileName = path.join(dirName, 'graph/graph.json')
fileName = path.join(dirName, 'graph/graph1.json')
with open(fileName, 'w') as fp:
json.dump(graph, fp)
json.dump(graph, fp)
80 changes: 80 additions & 0 deletions hybalg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 12 11:03:56 2020

@author: ma076216
"""
import math
import random
demand_points_rnd = random.sample(range(1,19835),500)
demand_levels =[]
for i in range (len(demand_points_rnd)):

demand_levels.append(random.randint(1,100))

demand_set = list(zip(demand_points_rnd,demand_levels))

number_clusters = 5
capacity = 100
from clusalg import *


clusters_f = findCluster(demand_points_rnd, demand_levels, number_clusters, .5)
clusters_list = []
for i in range (number_clusters):
clusters_list.append(clusters_f[0][i]['dps'])

total_demand_clusters = []
number_modules = []
utilization = []
for i in range (number_clusters):
demand_sum = 0

j=0
for j in range(len(clusters_list[i])):

demand_sum = demand_sum+clusters_list[i][j][1]

total_demand_clusters.append(demand_sum)
number_modules.append(demand_sum/capacity)
utilization.append (number_modules[i]- math.floor(number_modules[i]))

uti_sort = sorted(utilization,reverse=True)
sim_thrishold = 7000
i = 0
dedicated_mod = []
shared_mod = []
remainder_mod=[]

while len(uti_sort)>0:
if uti_sort[i]== 1:
dedicated_mod.append(utilization.index(uti_sort[i],0, number_clusters-1))
uti_sort.pop(0)
continue
if uti_sort[i]>=0.50:
uti_shared = uti_sort[i]
sh_l = []
sh_l.append(utilization.index(uti_sort[i])) # get index of module with utilization >0.5 store it in shl
j=i+1
while j in range (1,len(uti_sort)-1): # iterate through remainng elements to find shared modules
uti_shared = uti_shared + uti_sort[j]
if uti_shared > 1: # if a combination is greater than 100 utilization do not combine them and move on
uti_shared = uti_shared - uti_sort[j]
j+=1
continue
sh_l.append(utilization.index(uti_sort[j])) # if two modules can be combined, do it and get their indecies
uti_sort.pop(j) # reomve the item combined from sorted utilization list
if len(sh_l)>1: #if 2 or more modules combined add them to the shared modules list, otherwise to dedicated list
shared_mod.append(sh_l)
uti_sort.pop(i)
continue
else:
dedicated_mod.append(sh_l)
uti_sort.pop(i)
continue
if uti_sort[i]<0.50:
remainder_mod.append(utilization.index(uti_sort[i]))
uti_sort.pop(i)



59 changes: 47 additions & 12 deletions pMedian.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 25 18:09:26 2021

@author: ma076216
"""

# -*- coding: utf-8 -*-
"""
Created on Fri Oct 30 12:45:49 2020

@author: ma076216
"""

# Multi Threaded Dijkstra's (MTD) algorithm
import random
import heapq
from os import path
from operator import itemgetter
Expand All @@ -8,13 +23,15 @@

# loading the graph from the json file located in .\graph folder
dirName = path.dirname(__file__)
dirName = "C:/Users/ma076216/Desktop/PhD/research paper/python_code"

fileName = path.join(dirName, 'graph/graph.json')
graph = pd.read_json(fileName)
print('Graph has '+str(len(graph))+' nodes.')
graph = graph[['id', 'nbrs', 'dists', 'lat', 'lon']]


def findCluster(nodes, weights, p, pec, maxItr=1000):
def findCluster(nodes, weights, p, pec, maxItr=10):
pec = int(pec*p)
dps = list(zip(nodes, weights)) # make a tuple from the demand points
dps.sort(key=itemgetter(1), reverse=False) # sorting the demand points in ascending order
Expand All @@ -28,16 +45,17 @@ def findCluster(nodes, weights, p, pec, maxItr=1000):
bestDist = float('inf')
bestId = -1
src = dps.pop()
for cluster in clusters:
dist = findSDist((graph['lon'][src[0]], graph['lon'][src[0]]), cluster['median'])
for cluster in clusters: #this loop iterates through all clusters to find the closet cluster for point src
# first modification
dist = findSDist((graph['lon'][src[0]], graph['lat'][src[0]]), cluster['median'])
if dist < bestDist:
bestDist = dist
bestId = cluster['id']
clusters[bestId]['dps'].append(src) # add the node to the closest cluster
clusters[bestId]['median'] = findCOM(clusters[bestId]['dps']) # update the center of mass

distMat = [[0 for i in range(p)] for j in range(p)] # initialize the distance matrix
for (i, j) in [(i, j) for i in range(3) for j in range(3)]:
for (i, j) in [(i, j) for i in range(p) for j in range(p)]:
if i < j:
distMat[i][j] = distMat[j][i] = findSDist(
clusters[i]['median'], clusters[j]['median'])
Expand All @@ -49,27 +67,36 @@ def findCluster(nodes, weights, p, pec, maxItr=1000):
for cluster in clusters:
if len(cluster['dps']) < 2: # skip clusters with one point
continue
pq = [] # create a list of nearby clusters
for j in range(p):
if cluster['id'] != j:
heapq.heappush(pq, (distMat[cluster['id']][j], j))
bestMove = {'src': -1, 'dst': -1, 'dp': -1, 'costDiff': 0}

for i in range(len(cluster['dps'])):
#5th modification reini
bestMove = {'src': -1, 'dst': -1, 'dp': -1, 'costDiff': 0}
pq = [] # create a list of nearby clusters
for j in range(p):
if cluster['id'] != j:
heapq.heappush(pq, (distMat[cluster['id']][j], j))

# second modification j=0
j=0
while j < pec:
tmpSrc = (graph['lon'][cluster['dps'][i][0]],
graph['lat'][cluster['dps'][i][0]])
tmpDst = heapq.heappop(pq)
# weighted distance between the demand point and new cluster median
newCost = findDist(
#third modification FindSdistance
newCost = findSDist(
tmpSrc, clusters[tmpDst[1]]['median']) * cluster['dps'][i][1]

# weighted distance between the demand point and curretn cluster median
curCost = findDist(tmpSrc, cluster['median']) * cluster['dps'][i][1]
curCost = findSDist(tmpSrc, cluster['median']) * cluster['dps'][i][1]

# update bestMove if we have a cost saving move
if bestMove['costDiff'] < curCost - newCost:
bestMove = {'src': cluster['id'], 'dst': tmpDst[1],
'dp': cluster['dps'][i], 'costDiff': curCost - newCost}
j += 1
if bestMove['costDiff'] > 0: # if we have a cost saving move, do it!

exchange = True
clusters[bestMove['src']]['dps'].remove(
bestMove['dp']) # remove the dp from the src cluster
Expand All @@ -80,6 +107,7 @@ def findCluster(nodes, weights, p, pec, maxItr=1000):
# update the center of mass of the dst cluster
clusters[bestMove['dst']]['median'] = findCOM(clusters[bestMove['dst']]['dps'])
Itr += 1

return (clusters, Itr)


Expand All @@ -99,6 +127,13 @@ def findCOM(nodes): # finds the geometric center of mas of a set of graph nodes
w += j
return (x/w, y/w)

#select demand points randomly and assign weight 1 to all points
demand_points_rnd = random.sample(range(1,19835),500)
demand_levels =[1]*500 # intialize a list of demand levels for all demand points

# this is how to call the clustering algorithm
print(findCluster([1453, 10, 456, 178, 876, 8743], [1, 2, 3, 4, 5, 6], 3, .5))
print(findCluster([1453, 10, 456, 178, 876, 8743], [1, 2, 3, 4, 5, 6], 3, .8))

print(findCluster(demand_points_rnd, demand_levels , 6, .7))