Exemple #1
0
 def __init__(self, screenSize, network=None):
     self.position = 0
     self.screenSize = screenSize
     self.network = network
     self.tree = QTree(
         pygame.Rect(-MapManager.mapSize / 2, -MapManager.mapSize / 2,
                     MapManager.mapSize, MapManager.mapSize))
	def __init__(self):
		""" Initialize a QTree and populate it with randomized stars. """
		self.tree = QTree()
		for _ in xrange(settings.N_PARTICLES):
			p = Vector(gauss(P_MU, P_STD), gauss(P_MU, P_STD))
			v = Vector(uniform(-V_MAX, V_MAX), uniform(-V_MAX, V_MAX))
			tree.insert(Particle(p, v))
Exemple #3
0
class MapManager:

    mapSize = 32768

    def __init__(self, screenSize, network=None):
        self.position = 0
        self.screenSize = screenSize
        self.network = network
        self.tree = QTree(
            pygame.Rect(-MapManager.mapSize / 2, -MapManager.mapSize / 2,
                        MapManager.mapSize, MapManager.mapSize))

    def addNode(self, node):
        self.tree.insert(node)

    def setNetwork(self, network):
        self.network = network

    def setPosition(self, position):
        self.position = position

    def incrementPosition(self, delta):
        self.position[0] += delta[0]
        self.position[1] += delta[1]

    def getPosition(self, position):
        return self.position

    def getAllNodes(self):
        return self.tree.getAllNodes()

    def getNodesAndConnectionsOnScreen(self):
        relevantNodes = []
        screenRect = pyGame.Rect(self.position[0] - self.screenSize[0],
                                 self.position[1] - self.screenSize[1],
                                 self.screenSize[0], self.screenSize[1])
        relevantNodes.extend(self.tree.getChildrenInRegion(screeRect))

        for node in relevantNodes:
            for connection in node.getConnections():
                if not connection in relevantNodes:
                    relevantNodes.append(connection)

        return relevantNodes

    def getQTree():
        return self.tree
Exemple #4
0
class MapManager:

	mapSize = 32768

	def __init__(self, screenSize, network = None):
		self.position = 0
		self.screenSize = screenSize
		self.network = network
		self.tree = QTree(pygame.Rect(-MapManager.mapSize/2, -MapManager.mapSize/2, 
									   MapManager.mapSize, MapManager.mapSize))

	def addNode(self, node):
		self.tree.insert(node)

	def setNetwork(self, network):
		self.network = network

	def setPosition(self, position):
		self.position = position

	def incrementPosition(self, delta):
		self.position[0] += delta[0]
		self.position[1] += delta[1]

	def getPosition(self, position):
		return self.position

	def getAllNodes(self):
		return self.tree.getAllNodes()

	def getNodesAndConnectionsOnScreen(self):
		relevantNodes = []
		screenRect = pyGame.Rect(self.position[0] 	 - 	self.screenSize[0],
								 self.position[1] 	 - 	self.screenSize[1],
								 self.screenSize[0], 	self.screenSize[1]); 
		relevantNodes.extend(self.tree.getChildrenInRegion(screeRect))

		for node in relevantNodes:
			for connection in node.getConnections():
				if not connection in relevantNodes:
					relevantNodes.append(connection)

		return relevantNodes

	def getQTree():
		return self.tree
Exemple #5
0
def main():
    N = 2000
    L = 10

    root = QTree(Point(0, 0), L, None)
    points = [Point(x, y) for x, y in numpy.random.uniform(0, L, size=(N, 2))]

    for p in points:
        root.insert_point(p)

    tree = []
    get_tree(root, tree)

    colors = pyplot.cm.jet(numpy.linspace(0, 1, len(tree)))

    pyplot.figure()
    for i, quad in enumerate(tree):
        print(len(quad.points), len(quad.children))
        rect = Rectangle(quad.position,
                         quad.length,
                         quad.length,
                         facecolor=colors[i],
                         zorder=-1,
                         alpha=0.1,
                         edgecolor="black")
        pyplot.gca().add_patch(rect)
        for x, y in quad.points:
            pyplot.plot(x, y, ".k", ms=1, zorder=100)

    pyplot.plot(*points[-1], "o", ms=10)
    pyplot.xlim(-0.5, L + 0.5)
    pyplot.ylim(-0.5, L + 0.5)
    pyplot.gca().set_aspect("equal")
    pyplot.tight_layout()
    pyplot.savefig("mientras.pdf")
    pyplot.close()
Exemple #6
0
	def __init__(self, screenSize, network = None):
		self.position = 0
		self.screenSize = screenSize
		self.network = network
		self.tree = QTree(pygame.Rect(-MapManager.mapSize/2, -MapManager.mapSize/2, 
									   MapManager.mapSize, MapManager.mapSize))
Exemple #7
0
from QTree import QTree, Point, Node
import random
from distances import *

my_tree = QTree()

#get the list of kids and create a list of points in the plane
with open('nice_list.txt', "r") as file:
    points = []
    for line in file:
        line = line.split(';')
        lonlat = get_lon_lat(line[2], line[1])
        points.append(Point(lonlat[0], lonlat[1], int(line[3]), int(line[0])))

#add all points to the Quad Tree
for p in points:
    my_tree.add_point(p)

#create the divisions inside the Quad Tree (the implementation divides nodes when the sum of all weights is above 10 tons)
my_tree.subdivide()

#remove duplicate nodes and calculate total weight of each trip. We'll use this to see if we can make two or more trips without returning to base
leaf_nodes = my_tree.get_leaves()

all_children = set()
results = []
for node in leaf_nodes:
    trip = []
    for p in node.get_points():
        if (p not in all_children):
            trip.append(p)
class Stars():
	""" Collection of stars and functions to manipulate them. """
	#####################
	### Magic Methods ###
	#####################
	def __init__(self):
		""" Initialize a QTree and populate it with randomized stars. """
		self.tree = QTree()
		for _ in xrange(settings.N_PARTICLES):
			p = Vector(gauss(P_MU, P_STD), gauss(P_MU, P_STD))
			v = Vector(uniform(-V_MAX, V_MAX), uniform(-V_MAX, V_MAX))
			tree.insert(Particle(p, v))

	######################
	### Public Methods ###
	######################
	def compute_step(self):
		""" Recompute pos/vel vectors for all stars. """
		# Compute new velocity/position for each particle, but don't change 
		# the particles' attributes until after all stars are finished
		save_for_later = []
		for star in self.tree.traverse_all():
			# Save neighbours into memory for speed. Change this later? 
			clusters = list(self.tree.traverse_neighbours(star, 5.0))
			# Compute Runge Kutta coefficients
			k1v = G * sum(cluster[1] * self._invrsquared(star.p, cluster[0]) for cluster in clusters)
			k1r = star.v
			k2v = G * sum(cluster[1] * self._invrsquared(star.p + 0.5*k1r, cluster[0]) for cluster in clusters)
			k2r = P1.v * k1v * 0.5
			k3v = G * sum(cluster[1] * self._invrsquared(star.p + 0.5*k2r, cluster[0]) for cluster in clusters)
			k3r = P1.v * k2v * 0.5
			k4v = G * sum(cluster[1] * self._invrsquared(star.p + k3r, cluster[0]) for cluster in clusters)
			k4r = P1.v * k3v
			# Compute Runge Kutta final steps (save for later)
			v_inc += (k1v + 2*k2v + 2*k3v + k4v) / 6.0
			p_inc += (k1r + 2*k2r + 2*k3r + k4r) / 6.0
			save_for_later.append(dict(star=star, v_inc=v_inc, p_inc=p_inc))
		# Modify particles' attributes
		for save in save_for_later:
			save.star.p += save.p_inc
			save.star.v += save.v_inc


	#######################
	### Private Methods ###
	#######################	
	def _invrsquared(self, p1, p2):
		""" 
		Returns vector gravitation on p1 from p2 = (p2 - p1) / r^3 
		CONSTANTS G, M NOT TAKEN INTO ACCOUNT!!
		"""
		try:
			pos1 = p1.p
		except AttributeError:
			pos1 = p1
		try:
			pos2 = p2.p
		except AttributeError:
			pos2 = p2
		rvec = pos2 - pos1
		return rvec / (rvec.get_x()**2 + rvec.get_y()**2)**(1.5)