Example #1
0
def createRandomGridGraph(n):
    """creates a graph with random neighbors, at most 4"""
    length = index = 0
    graph = GridGraph()

    for i in range(0, n, 1):
        for j in range(0, n, 1):
            graph.addGridNode(j, i, index)
            index += 1
    lst = graph.adj_matrix

    for graphNode in lst:
        x = graphNode.x
        y = graphNode.y
        lstOfALLvertices = []

        name = graphNode.name
        size = n * n
        #since the possible neighbors are only 4
        #it checks accordingly with the helper of other helper methods
        #to find
        n1 = getNextNeighbor(name, "-1", n)
        if GridGraph.getNeighborValidity(n1, size):
            checkY = lst[n1].y
            if y is checkY:
                lstOfALLvertices.append(n1)
                length += 1
        n2 = getNextNeighbor(name, "1", n)
        if GridGraph.getNeighborValidity(n2, size):
            checkY = lst[n2].y
            if y is checkY:
                lstOfALLvertices.append(n2)
                length += 1
        n3 = getNextNeighbor(name, "n", n)
        if GridGraph.getNeighborValidity(n3, size):
            checkX = lst[n3].x
            if x is checkX:
                lstOfALLvertices.append(n3)
                length += 1
        n4 = getNextNeighbor(name, "-n", n)
        if GridGraph.getNeighborValidity(n4, size):
            checkX = lst[n4].x
            if x is checkX:
                lstOfALLvertices.append(n4)
                length += 1

        for v in lstOfALLvertices:
            #acoord = lst[v]
            #if acoord not in lst[v].vertices:
            if lst[v] not in graphNode.vertices and graphNode not in lst[
                    v].vertices:
                randname = random.randint(0, 1)
                if randname <= 0:
                    pass
                else:
                    c1 = lst[v]
                    graphNode.vertices.append(c1)
                    lst[v].vertices.append(graphNode)
    graph.adj_matrix = lst
    return graph
Example #2
0
def getRandomGraph(n):
    graph = GridGraph(n)
    for x in range(0,n):
        for y in range(0,n):
            #should only check to add edges to previous nodes already made
            graph.addNode(x,y,1) #in this unweighted graph the weight of any node is just 1
            if(y > 0 and coinFlip()):
                graph.addEdge(graph.nodes[x][y], graph.nodes[x][y-1])
            if(x > 0 and coinFlip()):
                graph.addEdge(graph.nodes[x][y], graph.nodes[x-1][y])
    return graph
Example #3
0
def getWeightedRandomGraph(n):
    graph = GridGraph(n)
    for x in range(0,n):
        for y in range(0,n):
            w = random.randint(1,6)
            graph.addNode(x,y,w)
            if(y > 0 and coinFlip()):
                graph.addEdge(graph.nodes[x][y], graph.nodes[x][y-1])
            if(x > 0 and coinFlip()):
                graph.addEdge(graph.nodes[x][y], graph.nodes[x-1][y])
    return graph
Example #4
0
def createRandomGridGraph(n : int) -> GridGraph:
  g = GridGraph()
  for row in range(n):
    for col in range(n):
      g.addGridNode(col, row, "{},{}".format(col, row))
  nodes = g.getAllNodes()
  while len(nodes) > 1:
    curr = nodes.pop()
    sampleSet = g.getAllNodes()
    for node in sampleSet:
      if not g.isNeighbor(curr, node):
        continue
      coin = randint(0,1)
      if coin == 1:
        g.addUndirectedEdge(curr, node)

  return g
Example #5
0
def createRandomGridGraph(n):
    graph = GridGraph()
    for y in range(0,n):
        for x in range(0,n):
            graph.addGridNode(x,y,0)

            adj = graph.getNeighboringNodes(graph.grid[y][x])
            for i in adj:
                rng = random.randint(1,100)
                if rng%2 == 0:
                    graph.addUndirectedEdge(graph.grid[y][x],i)

    return graph
Example #6
0
def createRandomGridGraph(n):
    """Creates n^2 random nodes with randomly assigned unweighted, bidirectional edges."""
    graph = GridGraph()
    choices = [True, False]

    for y in range(n):
        for x in range(n):
            graph.addGridNode(x, y, "(%i,%i)" % (x, y))

            if x != 0:
                if random.choice(choices):
                    graph.addUndirectedEdge("(%i,%i)" % (x, y), "(%i,%i)" % (x - 1, y))

            if y != 0:
                if random.choice(choices):
                    graph.addUndirectedEdge("(%i,%i)" % (x, y), "(%i,%i)" % (x, y - 1))

    return graph
Example #7
0
    def createRandomGridGraph(n):
        graph = GridGraph()
        node = 0
        for newX in range(n):
            for newY in range(n):
                graph.addGridNode(newX, newY, node)
                node = node + 1

        nodelist = graph.getAllNodes()
        for x in nodelist:
            for y in nodelist:
                if random.randint(0, 1) is 0:
                    graph.addUndirectedEdge(x, y)

        return graph
Example #8
0
def createRandomGridGraph(n):
    g = GridGraph()
    cols = n  # floor(sqrt(n)) # Pick how long each row is
    rowcount = colcount = 0
    randomNums = list(range(1, n**2 + 1))  # randomize range list 1..n^2
    shuffle(randomNums)
    for idx in randomNums:
        if colcount == cols:
            colcount = 0
            rowcount += 1
        x = rowcount
        y = colcount
        g.addGridNode(x, y, createLabel(idx))  # Insert a node at (x,y)
        nodes = g.getAllNodes()  # Get the updated node grid
        if colcount > 0:  # If its not the leftmost node in grid
            if randint(0,
                       2):  # 2/3 chance of creating a node, 50% was too little
                g.addUndirectedEdge(nodes[x][y - 1], nodes[x][y])
        if idx > cols:  # If its not the first row
            if randint(0,
                       2):  # 2/3 chance of creating a node, 50% was too little
                g.addUndirectedEdge(nodes[x - 1][y], nodes[x][y])
        colcount += 1
    return g
Example #9
0
def createRandomGridGraph(n):
    graph = GridGraph(n)

    nodeCount = 0
    for x in range(0, n):
        for y in range(0, n):
            graph.addGridNode(x, y, nodeCount)

            if x is not 0:
                if random.randint(0, 1) is 1:
                    graph.addUndirectedEdge(graph.adjMatrix[x][y],
                                            graph.adjMatrix[x - 1][y])

            if y is not 0:
                if random.randint(0, 1) is 1:
                    graph.addUndirectedEdge(graph.adjMatrix[x][y],
                                            graph.adjMatrix[x][y - 1])

            nodeCount += 1

    return graph
Example #10
0
def launch_simulation(size_scene,obstacles,exits,agents,time_simulation,dt):
    
    history_agents = []
    
    print "Initialization of the agents..."
    #create navigation field for each agent
    precision = 0.5#nodes per unity
    
    navigation_maps = {}
    
    for agent in agents:
        if agent.size not in navigation_maps:
            debug_precision = precision
            #to make sure that precision*agent.size is an integer
            if not precision*agent.size==int(precision*agent.size):
                debug_precision = 1
            graph = GridGraph(size_scene,debug_precision)
            graph.prepare_graph_for_fast_marching(obstacles,exits,agent)
            fast_marching_method(graph, graph.to_node(agent.position))
            navigation_maps[agent.size] = graph
            #shows the distance map to the exit after applying the fast marching method
            #imshow(graph.distances,interpolation='nearest',origin='lower')
            #show()
    for agent in agents:
        agent.navigation_map = navigation_maps[agent.size]
        
    
    loading_bar = 0
    print "Simulation running..."
    
    #start simulation
    for t in range(int(time_simulation/dt)):
        
        #loading bar
        if int(t/float(int(time_simulation/dt))*100)==loading_bar:
            print "#",
            loading_bar = loading_bar+10
        
        #initialize patches for the agents for one frame of the animation
        patches_agents = []
        
        numpy.random.seed(1)
        pick_agent = numpy.random.choice(len(agents),len(agents),replace=False)
        for i in pick_agent:
            #update the speed of the agent
            
            agents[i].update_speed(agents,obstacles)
            #update the position of the agent
            agents[i].update_position(agents,obstacles,exits,dt,size_scene)
            
            #add patch of the agent for one frame
            patches_agents.append(patches.Circle(agents[i].position,agents[i].size,color=[agents[i].get_color_agent(),0,0]))
            #
        
        history_agents.append(patches_agents)
        
        agents = [v for i, v in enumerate(agents) if not v.has_reached_exit]
        
        if not agents:
            break;
    
    print ''
    print 'Simulation finished at time '+str((t+1)*dt)+'s'
    
    display_simulation(history_agents, obstacles, exits, size_scene,dt,3)