Skip to content

Instantly share code, notes, and snippets.

@QuantumFractal
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuantumFractal/6c7e011c3f5966653ddd to your computer and use it in GitHub Desktop.
Save QuantumFractal/6c7e011c3f5966653ddd to your computer and use it in GitHub Desktop.
Dynamic Graphs // A Sketch
import inspect
""" Dynamic weighted graphs
tmoll 2015
"""
class DynamicWeight(object):
"""docstring for DynamicWeight"""
def __init__(self, weight, weight_func):
self.weight_func = weight_func
self.func_string = inspect.getsource(weight_func).split('lambda')[1]
self._weight = weight
@property
def weight(self):
return self._weight
def __str__(self):
return "Weight: "+str(self._weight)+" Function:"+self.func_string
def traverse(self):
self._weight = self.weight_func(self._weight)
return self.weight
class Vertex(object):
"""Vertex
Contains dict of neighbors with dynweights
"""
def __init__(self, node):
self.id = node
self.adjacent = {}
def add_neighbor(self, neighbor, dynamicweight):
self.adjacent[neighbor.id] = dynamicweight
def get_id(self):
return self.id
def get_weight(self, neighbor):
return self.adjacent[neighbor].weight
def traverse(self, to):
return self.adjacent[to].traverse()
class Graph(object):
""" Graph
Holds dict of verticies
"""
def __init__(self):
self.vert_dict = {}
self.size = 0
def get_vertex(self, n):
if n in self.vert_dict:
return self.vert_dict[n]
else:
return None
def add_vertex(self, node):
self.size += 1
new_vert = Vertex(node)
self.vert_dict[node] = new_vert
return new_vert
def add_edge(self, to, frm, dynweight):
if frm not in self.vert_dict:
self.add_vertex(frm)
if to not in self.vert_dict:
self.add_vertex(to)
self.vert_dict[frm].add_neighbor(self.vert_dict[to], dynweight)
self.vert_dict[to].add_neighbor(self.vert_dict[frm], dynweight)
def get_edge(self, a, b):
return self.vert_dict[a].adjacent[b]
def traverse(self, a, b):
return self.vert_dict[a].traverse(b)
if __name__ == '__main__':
g = Graph()
g.add_edge('a', 'b', DynamicWeight(0, lambda x: x+2))
print g.get_edge('a', 'b')
while g.traverse('a', 'b') < 20:
continue
print g.get_edge('a', 'b').weight
import inspect
""" Dynamic weighted graphs
tmoll 2015
"""
class DynamicWeight(object):
"""docstring for DynamicWeight"""
def __init__(self, weight, weight_func):
self.weight_func = weight_func
self.func_string = inspect.getsource(weight_func).split('lambda')[1]
self._weight = weight
@property
def weight(self):
return self._weight
def __str__(self):
return "Weight: "+str(self._weight)+" Function:"+self.func_string
def traverse(self):
self._weight = self.weight_func(self._weight)
return self.weight
class Vertex(object):
"""Vertex
Contains dict of neighbors with dynweights
"""
def __init__(self, node):
self.id = node
self.adjacent = {}
def add_neighbor(self, neighbor, dynamicweight):
self.adjacent[neighbor.id] = dynamicweight
def get_id(self):
return self.id
def get_weight(self, neighbor):
return self.adjacent[neighbor].weight
def traverse(self, to):
if to.id in self.adjacent.keys():
return self.adjacent[to.id].traverse()
return False
class Graph(object):
""" Graph
Holds dict of verticies
"""
def __init__(self):
self.vert_dict = {}
self.size = 0
def get_vertex(self, n):
if n in self.vert_dict:
return self.vert_dict[n]
else:
return None
def vertices(self):
""" returns the vertices of a graph """
return list(self.vert_dict)
def add_vertex(self, node):
self.size += 1
new_vert = Vertex(node)
self.vert_dict[node] = new_vert
return new_vert
def add_edge(self, to, frm, dynweight):
if frm not in self.vert_dict:
self.add_vertex(frm)
if to not in self.vert_dict:
self.add_vertex(to)
self.vert_dict[frm].add_neighbor(self.vert_dict[to], dynweight)
self.vert_dict[to].add_neighbor(self.vert_dict[frm], dynweight)
def get_edge(self, a, b):
return self.vert_dict[a].adjacent[b]
def traverse(self, a, b):
self.vert_dict[a].traverse(b)
class Realm(Graph):
def __init_(self):
super(Realm, self).__init__()
def add_village(self, name, startingpop):
self.size += 1
new_vert = Village(name, startingpop)
new_vert.set_realm(self)
self.vert_dict[name] = new_vert
return new_vert
def get_villages(self):
for x in self.vertices():
print x
class Village(Vertex):
def __init__(self, node, startingpop):
super(Village, self).__init__(node)
self.population = startingpop
def set_realm(self, realm):
self.realm = realm
def send_party(self, amount, destination):
if destination is self:
print "Can't send people to yourself, ya dingus"
return
if amount > self.population:
print "Not enough people"
return
# People die :(
self.population -= amount
weight = self.traverse(destination)
print weight,"People Died"
destination.population += max(0, amount - weight)
if __name__ == '__main__':
r = Realm()
village1 = r.add_village('village1', 500)
village2 = r.add_village('village2', 200)
r.add_edge('village1', 'village2', DynamicWeight(30, lambda x: x-5))
def status(): print village1.population, village2.population
status()
village1.send_party(30, village2)
status()
village2.send_party(100, village1)
status()
village1.send_party(100, village2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment