Skip to content

Commit

Permalink
Merge pull request #21 from Scratchcat1/AB-DynoGraph
Browse files Browse the repository at this point in the history
AB DynoGraph
  • Loading branch information
Scratchcat1 authored Nov 7, 2017
2 parents b671c35 + e44f9dc commit 82c9995
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 43 deletions.
103 changes: 61 additions & 42 deletions AATC_AStar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -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 Down
2 changes: 1 addition & 1 deletion AATC_NoFlyZoneGrapher.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def GetNoFlyZones(self):
return ProcessedData

def Make_Values(self,NoFlyZoneData):
graph = AATC_AStar.DynoGraph()
graph = AATC_AStar.DynoGraph(ABSlot = 1)
graph.ImportGraph()
Values = {}
for Zone in NoFlyZoneData:
Expand Down

0 comments on commit 82c9995

Please sign in to comment.