Exemple #1
0
    def display_cars(self, cars):

        car_lines = []

        for n in range(0, len(cars)):
            car_lines.append(random.choice(self.road))

        for n in range(0, len(self.totalRoads)):

            road = self.road[n]
            line_data = road.get_data()

            x_min, x_max = self.lineX(line_data)
            y_min, y_max = self.lineY(line_data)

            locX = (x_max - x_min) / 2 + x_min
            locY = (y_max - y_min) / 2 + y_min

            plot.plotLineTxt(locX, locY, n + 1)

        # temporal variable to hold values of cars
        points = [[], []]

        # get X cars in the graph
        for n in range(0, len(cars)):

            random_index = randrange(0, len(car_lines))
            self.currentRoad[cars[n]] = random_index
            car_line = car_lines[random_index]
            point = car_line.get_xydata()[0]  # first point in the graph

            # calculate the angle
            line_data = car_line.get_data()
            ang = self.calculateAngle(line_data)

            self.cars[cars[n]] = self.carProperties(point, ang, x_min, x_max)

            # for the even cars shift angle to negative
            # so that it goes in opposite direction from car1
            i = self.cars.keys().index(cars[n])
            if i % 2 == 0:
                ang = ang + math.pi
                point = car_line.get_xydata()[-1]  # for this car get the last point as positions

            x_min, x_max = self.lineX(line_data)

            self.initial[cars[n]] = self.carPoint(point)

            # add scatter
            points[0].append(point[0])
            points[1].append(point[1])

            self.speed(cars[n])  # Get Speed

            # Useful to Graph
            plot.instantiateCircle(cars[n])
            plot.instantiateAnnotate(cars[n])

        # plot the cars
        self.scatter = plot.plotScatter(points[0], points[1])
Exemple #2
0
    def display_cars(self, cars):
    
        car_lines = []
        
        for n in range(0,len(cars)):
            car_lines.append(random.choice(self.road))
        
        for n in range(0, len(self.totalRoads)):
            
            road = self.road[n]            
            line_data = road.get_data()
            
            x_min, x_max = self.lineX(line_data)
            y_min, y_max = self.lineY(line_data)

            locX = (x_max - x_min)/2 + x_min
            locY = (y_max - y_min)/2 + y_min
            
            plot.plotLineTxt(locX, locY, n+1)
            
        #temporal variable to hold values of cars
        points = [[],[]]
        
        #get X cars in the graph
        for n in range(0,len(cars)):
            
            random_index = randrange(0,len(car_lines))            
            self.currentRoad[cars[n]] = random_index            
            car_line = car_lines[random_index]
            point = car_line.get_xydata()[0] #first point in the graph
            
            #calculate the angle
            line_data = car_line.get_data()            
            ang = self.calculateAngle(line_data)
            
            self.cars[cars[n]] = self.carProperties(point, ang, x_min, x_max)
            
            #for the even cars shift angle to negative
            #so that it goes in opposite direction from car1
            i = self.cars.keys().index(cars[n])
            if i%2==0:
                ang = ang + math.pi
                point = car_line.get_xydata()[-1] #for this car get the last point as positions
    
            x_min, x_max = self.lineX(line_data)
            
            self.initial[cars[n]] = self.carPoint(point)
             
            #add scatter
            points[0].append(point[0])
            points[1].append(point[1])
                        
            self.speed(cars[n]) # Get Speed
            
            #Useful to Graph
            plot.instantiateCircle(cars[n])
            plot.instantiateAnnotate(cars[n])
            
        #plot the cars
        self.scatter = plot.plotScatter(points[0],points[1])
Exemple #3
0
    def simulate_car_movement(self, scatter, com_lines):
        # temporal variables
        points = [[], []]
        scatter.remove()

        nodes = dict(self.cars.items() + self.bss.items())

        # print initial
        while com_lines:
            com_lines[0].remove()
            del(com_lines[0])

        # iterate over each car
        for car in self.cars:
            # get all the properties of the car
            velocity = round(np.random.uniform(self.velo[car][0], self.velo[car][1]))
            position_x = self.cars[car][0]
            position_y = self.cars[car][1]

            car.params['position'] = position_x, position_y, 0
            angle = self.cars[car][2]

            # calculate new position of the car
            position_x = position_x + velocity * cos(angle) * self.time_per_iteraiton
            position_y = position_y + velocity * sin(angle) * self.time_per_iteraiton

            # check if car gets out of the line
            # no need to check for y coordinates as car follows the line
            if position_x < self.cars[car][3] or position_x > self.cars[car][4]:
                self.repeat(car)
                points[0].append(self.initial[car][0])
                points[1].append(self.initial[car][1])
            else:
                self.cars[car][0] = position_x
                self.cars[car][1] = position_y
                points[0].append(position_x)
                points[1].append(position_y)

                for node in nodes:
                    if nodes[node] == nodes[car]:
                        continue
                    else:
                        # compute to see if vehicle is in range
                        inside = math.pow((nodes[node][0] - position_x), 2) + math.pow((nodes[node][1] - position_y), 2)
                        if inside <= math.pow(node.params['range'] + 30, 2):
                            line = plot.plotLine2d([position_x, nodes[node][0]], [position_y, nodes[node][1]], color='r')
                            com_lines.append(line)
                            plot.plotLine(line)

            plot.drawTxt(car)
            plot.drawCircle(car)

        scatter = plot.plotScatter(points[0], points[1])
        plot.plotDraw()

        return [scatter, com_lines]
Exemple #4
0
   def simulate_car_movement(self,scatter,com_lines):
       #temporal variables
       points= [[],[]]
       scatter.remove()
       
       nodes = dict(self.cars.items() + self.bss.items())
       
       #print initial
       while com_lines:
           com_lines[0].remove()
           del(com_lines[0])
       
       #iterate over each car
       for car in self.cars:  
           #get all the properties of the car
           velocity = round(np.random.uniform(self.velo[car][0],self.velo[car][1]))
           position_x = self.cars[car][0]
           position_y = self.cars[car][1]
           
           car.params['position'] = position_x, position_y, 0
           angle = self.cars[car][2]
          
           #calculate new position of the car
           position_x = position_x + velocity*cos(angle)*self.time_per_iteraiton
           position_y = position_y + velocity*sin(angle)*self.time_per_iteraiton
          
           #check if car gets out of the line
           # no need to check for y coordinates as car follows the line
           if position_x < self.cars[car][3] or position_x> self.cars[car][4]:
               self.repeat(car)
               points[0].append(self.initial[car][0])
               points[1].append(self.initial[car][1])
           else:
               self.cars[car][0] = position_x
               self.cars[car][1] = position_y
               points[0].append(position_x)
               points[1].append(position_y)
              
               for node in nodes:
                   if nodes[node] == nodes[car]:
                       continue
                   else:
                       #compute to see if vehicle is in range
                       inside = math.pow((nodes[node][0]-position_x),2) + math.pow((nodes[node][1]-position_y),2)
                       if inside <= math.pow(node.range+30,2):
                           line = plot.plotLine2d([position_x,nodes[node][0]],[position_y,nodes[node][1]], color='r')
                           com_lines.append(line)
                           plot.plotLine(line)
 
           plot.drawTxt(car)
           plot.drawCircle(car)   
         
       scatter = plot.plotScatter(points[0],points[1])        
       plot.plotDraw()
   
       return [scatter,com_lines]
Exemple #5
0
    def display_grid(self, baseStations, nroads):

        for n in range(0, nroads):
            if n == 0:
                p = ginp(2)
                self.points[n] = p
                self.all_points = p
            else:
                p = ginp(1)
                self.points[n] = p
                self.all_points.append(p[0])

            x1 = [x[0] for x in self.points[n]]
            y1 = [x[1] for x in self.points[n]]
            if n == 0:
                self.points[n] = self.get_line(
                    int(x1[0]), int(y1[0]), int(x1[1]),
                    int(y1[1]))  # Get all the points in the line
            else:
                self.points[n] = self.get_line(
                    int(self.all_points[n][0]), int(self.all_points[n][1]),
                    int(p[0][0]),
                    int(p[0][1]))  # Get all the points in the line

            x1 = [x[0] for x in self.points[n]]
            y1 = [x[1] for x in self.points[n]]

            self.interX[n] = x1
            self.interY[n] = y1

            self.road[n] = plot.plotLine2d(
                x1, y1, color='g'
            )  # Create a line object with the x y values of the points in a line
            plot.plotLine(self.road[n])
            #plot.plotDraw()

        for i in range(len(baseStations)):
            self.bss[baseStations[i]] = ginp(1)[0]
            bs_x = self.bss[baseStations[i]][0]
            bs_y = self.bss[baseStations[i]][1]
            self.scatter = plot.plotScatter(bs_x, bs_y)
            baseStations[i].params['position'] = bs_x, bs_y, 0
            plot.instantiateAnnotate(baseStations[i])
            plot.instantiateCircle(baseStations[i])
            plot.drawTxt(baseStations[i])
            plot.drawCircle(baseStations[i])
            plot.plotDraw()
Exemple #6
0
 def display_grid(self, baseStations, nroads):
     
     for n in range(0, nroads):
         if n == 0:
             p = ginp(2)
             self.points[n] = p
             self.all_points = p
         else:
             p = ginp(1)
             self.points[n] = p
             self.all_points.append(p[0])
             
         x1 = [x[0] for x in self.points[n]]
         y1 = [x[1] for x in self.points[n]]
         if n == 0:
             self.points[n] = self.get_line(int(x1[0]),int(y1[0]),int(x1[1]),int(y1[1])) # Get all the points in the line
         else:
             self.points[n] = self.get_line(int(self.all_points[n][0]),int(self.all_points[n][1]),int(p[0][0]),int(p[0][1])) # Get all the points in the line
         
         x1 = [x[0] for x in self.points[n]]
         y1 = [x[1] for x in self.points[n]]
         
         self.interX[n] = x1
         self.interY[n] = y1            
         
         self.road[n] =  plot.plotLine2d(x1,y1, color='g') # Create a line object with the x y values of the points in a line
         plot.plotLine(self.road[n])
         #plot.plotDraw()
     
     for i in range(len(baseStations)):
         self.bss[baseStations[i]] = ginp(1)[0]
         bs_x = self.bss[baseStations[i]][0]
         bs_y = self.bss[baseStations[i]][1]
         self.scatter = plot.plotScatter(bs_x, bs_y) 
         baseStations[i].params['position'] = bs_x, bs_y, 0
         plot.instantiateAnnotate(baseStations[i])
         plot.instantiateCircle(baseStations[i])
         plot.drawTxt(baseStations[i])
         plot.drawCircle(baseStations[i])
         plot.plotDraw()