Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
-Adding A/B.. compartmentalisation. Systems can run on different datasets without interfering i.e. obtaining a write lock on a file.
  • Loading branch information
Scratchcat1 authored Nov 3, 2017
1 parent b46b68b commit 5c986f5
Showing 1 changed file with 72 additions and 50 deletions.
122 changes: 72 additions & 50 deletions AATC_AStar.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os,pickle,heapq,time,math,hashlib
import os,pickle,time,math,hashlib
from AATC_Coordinate import *
try:
try:
import PriorityQueue.PriorityQueueC as PriorityQueue
except:
print("PriotityQueueC not available, defaulting to pure python")
import PriorityQueue.PriorityQueue
import PriorityQueue.PriorityQueue as PriorityQueue

except:
print("AStarPQ not available")
Expand All @@ -26,7 +26,7 @@ class DynoGraph:
"""
def __init__(self,BlockSize = 500,FolderName = "GraphFolder",GraphFileName = "Graph",GraphFileSuffix = ".graph",BlockFileName = "GraphBlock",BlockFileSuffix = ".blk",Node_Cache_BlockSize = 10000000):
def __init__(self,BlockSize = 500,FolderName = "GraphFolder",GraphFileName = "Graph",GraphFileSuffix = ".graph",BlockFileName = "GraphBlock",BlockFileSuffix = ".blk",Node_Cache_BlockSize = 10000000, ABInterval = 36000, ABSlot = 0, ABSlots = 2):
self.Nodes = {}
self.BlockSize = BlockSize
self.cwd = os.getcwd()
Expand All @@ -37,6 +37,10 @@ def __init__(self,BlockSize = 500,FolderName = "GraphFolder",GraphFileName = "Gr
self.BlockFileName = BlockFileName
self.BlockFileSuffix = BlockFileSuffix
self.Node_Cache_BlockSize = Node_Cache_BlockSize

self.ABInterval = ABInterval
self.ABSlot = ABSlot
self.ABSlots = ABSlots


def Size(self,xSize,ySize,zSize):
Expand All @@ -50,13 +54,13 @@ def Get_Size(self):
def add_node(self,node):
self.Nodes[node.Get_NodeID()] = node

def clean_edges(self):
print("Cleaning edges...")
for item in self.Nodes.values():
for num in item.Friends:
friend = self.Nodes[num]
if item.Get_NodeID() not in friend.Friends:
friend.add_friend(item.Get_NodeID())
## def clean_edges(self):
## print("Cleaning edges...")
## for item in self.Nodes.values():
## for num in item.Friends:
## friend = self.Nodes[num]
## if item.Get_NodeID() not in friend.Friends:
## friend.add_friend(item.Get_NodeID())


def Add_Edges(self,xRange,yRange,zRange):
Expand Down Expand Up @@ -184,23 +188,24 @@ def Save_Node_Cache(self):
if r not in Sets:
Sets[r] = {}
Sets[r][Key] = self.Node_Cache[Key] #Adds the item to the set

print("Saving Node Cache. Sets:",len(Sets))
for Set in Sets:
filename = os.path.join(self.cwd,self.FolderName,self.BlockFileName+"NC"+str(Set)+self.BlockFileSuffix)
file = open(filename,"wb")
data = Sets[Set]
pickle.dump(data,file,protocol = pickle.HIGHEST_PROTOCOL)
file.close()
for Letter in self.GetFolderNames():
for Set in Sets:
filename = os.path.join(self.cwd,self.FolderName,Letter,self.BlockFileName+"NC"+str(Set)+self.BlockFileSuffix)
data = Sets[Set]
with open(filename,"wb") as file:
pickle.dump(data,file,protocol = pickle.HIGHEST_PROTOCOL)


def Get_Node_Cache(self,x,y,z):
Key = (x,y,z)
if Key not in self.Node_Cache:
NCBlockID = self.Node_Cache_Hash(Key)
try:
filename = os.path.join(self.cwd,self.FolderName,self.BlockFileName+"NC"+str(NCBlockID)+self.BlockFileSuffix)
file = open(filename,"rb")
block = pickle.load(file)
file.close()
filename = os.path.join(self.cwd,self.FolderName,self.CurrentFolderName(),self.BlockFileName+"NC"+str(NCBlockID)+self.BlockFileSuffix)
with open(filename,"rb") as file:
block = pickle.load(file)
self.Node_Cache.update(block)

except Exception as e:
Expand Down Expand Up @@ -232,7 +237,7 @@ def Find_NodeID(self,x,y,z):
return NodeID

def Obj_Find_NodeID(self,Obj):
x,y,z = Obj.Coords.x,Obj.Coords.y,Obj.Coords.z
x,y,z = Obj.Coords.Get_X(),Obj.Coords.Get_Y(),Obj.Coords.Get_Z()
NodeID = self.Find_NodeID(x,y,z)
return NodeID

Expand All @@ -241,29 +246,31 @@ def Obj_Find_NodeID(self,Obj):

def SaveGraph(self,AutoNodeSave = True,AutoNodeCacheSave = True):
print("Saving graph...")
for Letter in self.GetFolderNames():
os.makedirs(os.path.join(os.getcwd(),self.FolderName,Letter),exist_ok = True)
if AutoNodeSave:
self.SaveNodes()
if AutoNodeCacheSave:
self.Save_Node_Cache()

self.Nodes = {}
self.Node_Cache = {}
try:
filename = os.path.join(self.cwd,self.FolderName,self.GraphFileName+self.GraphFileSuffix)
file = open(filename,"wb")
pickle.dump(self,file,protocol = pickle.HIGHEST_PROTOCOL)
file.close()
print("Saved graph sucessfully")
except Exception as e:
print("Error occured while saving graph file ",e)
for Letter in self.GetFolderNames():
try:
filename = os.path.join(self.cwd,self.FolderName,Letter,self.GraphFileName+self.GraphFileSuffix)
with open(filename,"wb") as file:
pickle.dump(self,file,protocol = pickle.HIGHEST_PROTOCOL)
print("Saved graph sucessfully")
except Exception as e:
print("Error occured while saving graph file ",e)

def ImportGraph(self):
print("Importing graph")
try:
filename = os.path.join(os.getcwd(),self.FolderName,self.GraphFileName+self.GraphFileSuffix)
file = open(filename,"rb")
ImportFile = pickle.load(file)
file.close()
filename = os.path.join(os.getcwd(),self.FolderName,"A",self.GraphFileName+self.GraphFileSuffix) #MUST ALWAYS HAVE ATLEAST THE FOLDER "A" in order to load the configuration
with open(filename,"rb") as file:
ImportFile = pickle.load(file)
self.__dict__.update(ImportFile.__dict__)
print("Imported graph sucessfully")
except Exception as e:
Expand All @@ -277,10 +284,10 @@ def GetNode(self,NodeID):
if NodeID not in self.Nodes:
BlockID = self.Hash(NodeID)
try:
filename = os.path.join(self.cwd,self.FolderName,self.BlockFileName+"N"+str(BlockID)+self.BlockFileSuffix)
file = open(filename,"rb")
block = pickle.load(file)
file.close()
filename = os.path.join(self.cwd,self.FolderName,self.CurrentFolderName(),self.BlockFileName+"N"+str(BlockID)+self.BlockFileSuffix)
with open(filename,"rb") as file:
block = pickle.load(file)

self.Nodes.update(block)


Expand All @@ -303,21 +310,33 @@ def SaveNodes(self):
r = self.Hash(node.Get_NodeID())
Sets[r][node.Get_NodeID()] = node


for Set in Sets: #Set = BlockID
if len(Sets[Set]) != 0: #If set is not empty. Empty sets may cause issues with delta Node change saving.
filename = os.path.join(self.cwd,self.FolderName,self.BlockFileName+"N"+str(Set)+self.BlockFileSuffix)
file = open(filename,"wb")
data = Sets[Set]
pickle.dump(data,file,protocol = pickle.HIGHEST_PROTOCOL)
file.close()
for Letter in self.GetFolderNames():
for Set in Sets: #Set = BlockID
if len(Sets[Set]) != 0: #If set is not empty. Empty sets may cause issues with delta Node change saving.
filename = os.path.join(self.cwd,self.FolderName,Letter,self.BlockFileName+"N"+str(Set)+self.BlockFileSuffix)
data = Sets[Set]
with open(filename,"wb") as file:
pickle.dump(data,file,protocol = pickle.HIGHEST_PROTOCOL)

def EvictNode(self,NodeID): #Removes a node from the Nodes dict
if NodeID in self.Nodes:
self.Nodes.pop(NodeID)
return True
else:
return False

#######################################

def GetFolderNames(self):
names = []
for x in range(self.ABSlots):
names.append(chr(65+x))
return names

def CurrentFolderName(self):
char = chr( int(65+ ((time.time()+self.ABInterval*self.ABSlot)//self.ABInterval)%self.ABSlots))
return char




Expand All @@ -340,12 +359,15 @@ def Get_Coords(self):
def Get_Cost(self):
return self.Cost

def Set_Cost(self,cost):
self.Cost = cost

def EstimateDistance(Node,Target,xSize,ySize,zSize):
Node_Coords = Node.Get_Coords()
Target_Coords = Target.Get_Coords()
return abs(Node_Coords.Get_X()-Target_Coords.Get_X())/xSize+abs(Node_Coords.Get_Y()-Target_Coords.Get_Y())/ySize+abs(Node_Coords.Get_Z()-Target_Coords.Get_Z())/zSize

def AStarPQ(graph,start,target,xSize=1,ySize=1,zSize = 1): # Set all g to node_count + 1
def AStarPQ(graph,start,target): # Set all g to node_count + 1
StartTime = time.time()

xSize,ySize,zSize = graph.Get_Size()
Expand Down Expand Up @@ -387,9 +409,9 @@ def AStarPQ(graph,start,target,xSize=1,ySize=1,zSize = 1): # Set all g to node
cameFrom[NodeID] = current
g[NodeID] = tScore
fp.remove((f[NodeID],NodeID))
x = g[NodeID] + EstimateDistance(NewNode,graph.GetNode(target),xSize,ySize,zSize)
f[NodeID] = x
fp.put((x,NodeID))
fTemp = g[NodeID] + EstimateDistance(NewNode,graph.GetNode(target),xSize,ySize,zSize)
f[NodeID] = fTemp
fp.put((fTemp,NodeID))

f.pop(current) #These values will not be refered to again since the current NodeID has been moved to the closed set . This therefore reduces memory usage very slightly
g.pop(current)
Expand All @@ -404,7 +426,7 @@ def AStarPQ(graph,start,target,xSize=1,ySize=1,zSize = 1): # Set all g to node
print(FindPath(cameFrom,current))
return None

def AStar2(graph,start,target,xSize=1,ySize=1,zSize = 1): # Set all g to node_count + 1
def AStar2(graph,start,target): # Set all g to node_count + 1
StartTime = time.time()

xSize,ySize,zSize = graph.Get_Size()
Expand Down

0 comments on commit 5c986f5

Please sign in to comment.