def poi_value_update(n_step): # Handle POI value update. if (n_step % config.n_poi_value_update_rate == 0): global L_POIS for poi in L_POIS: # Value increases over time poi.increaseValueBy(config.f_poi_value_inc_amt) # Make sure that the value can't increase over max if (poi.getValue() > config.f_poi_value_max): poi.setValue(config.f_poi_value_max) # Update color to reflect value # We want low values to be blue and high values to be green # Colors are (red, green, blue, alpha) n_color_intensity = int((poi.getValue() / config.f_poi_value_max) * 255) if (poi.getValue() < 0): traci.poi.setColor(poi.getID(),(255,0,0,0)) elif (poi.getValue() > 255): traci.poi.setColor(poi.getID(),(0,255,0,0)) else: traci.poi.setColor(poi.getID(),(255-n_color_intensity,0+n_color_intensity,0,0)) # end for o_poi in L_POIS: # end if (n_step % config.n_poi_value_update_rate): del n_step
def handle_vehicles(n_step): global L_VEHICLES global L_POIS for veh in L_VEHICLES: # If the vehicle has arrived at it's destination if traci.vehicle.getRoadID(veh.get_id()) == veh.get_next_dest_edge_id(): # Locate the POI that we arrived at. Find the POI that is # nearest to the edge we're on. for poi in L_POIS: if (veh.get_next_dest_edge_id() == poi.getClosestEdge()[0]): # "Hit" the POI poi.vehicleHit(n_step,veh.get_id()) # Add the POI to list of visited POIs. veh.add_visited_pois(poi.getID()) # Reduce Capacity n_amt = int(poi.getValue() * 100.0) veh.increase_capacity(0-n_amt) # Update vehicle Color update_vehicle_color(veh) break # End for # Is the vehicle full (capacity = 0) or has it visted all the POIs? # We'll use some flags to reduce redundant checks is_full = False is_bored = False if veh.get_capacity() == 0: is_full = True elif len(veh.get_visited_pois()) == len(L_POIS): is_bored = True if is_full or is_bored : # Go home traci.vehicle.changeTarget( veh.get_id(), veh.get_final_dest_edge_id()) # Change color so we can see when cars go home. The colors will # vary by reason for going home. # REASON: No more capacity. Cars will turn CYAN. if is_full: traci.vehicle.setColor(veh.get_id(),(0,255,255,0)) # REASON: Visited every unique POI. Cars will turn GREEN elif is_bored: traci.vehicle.setColor(veh.get_id(),(0,255,0,0)) # REASON: Unknown. Cars will turn DARK GREY # If cars are grey, there's a logic problem! else: traci.vehicle.setColor(veh.get_id(),(30,30,30,0)) # remove the vehicle from the list since we no longer need to # track it. L_VEHICLES.remove(veh) # Otherwise send it to another poi else: # Reroute vehicles to another POI go_to_poi(veh)
def handle_vehicles(n_step): global L_VEHICLES global L_POIS for veh in L_VEHICLES: # If the vehicle has arrived at it's destination if traci.vehicle.getRoadID( veh.get_id()) == veh.get_next_dest_edge_id(): # Locate the POI that we arrived at. Find the POI that is # nearest to the edge we're on. for poi in L_POIS: if (veh.get_next_dest_edge_id() == poi.getClosestEdge()[0]): # "Hit" the POI poi.vehicleHit(n_step, veh.get_id()) # Add the POI to list of visited POIs. veh.add_visited_pois(poi.getID()) # Reduce Capacity n_amt = int(poi.getValue() * 100.0) veh.increase_capacity(0 - n_amt) # Update vehicle Color update_vehicle_color(veh) break # End for # Is the vehicle full (capacity = 0) or has it visted all the POIs? if (veh.get_capacity() == 0) or len( veh.get_visited_pois()) == len(L_POIS): # Go home traci.vehicle.changeTarget(veh.get_id(), veh.get_final_dest_edge_id()) # Change the color to blue so we can recognize accomplished cars traci.vehicle.setColor(veh.get_id(), (0, 255, 255, 0)) # remove the vehicle from the list since we no longer need to # track it. L_VEHICLES.remove(veh) # Otherwise send it to another poi else: # Reroute vehicles to another POI go_to_poi(veh)
def go_to_poi(veh): # Send it to the POI with the most value global L_POIS n_max_val = -1 l_poi_max = [] s_dest_edge = "" for poi in L_POIS: # If we've already visited it, don't check it. if poi.getID() in veh.get_visited_pois(): continue # Find the POI with the most value elif poi.getValue() >= n_max_val: l_poi_max.append(poi) # Compare vs the record list if there's more than one item in it. if len(l_poi_max) > 1: # If the recorded pois are less than the current, remove it # from the tracking list. for poi_max in l_poi_max: if poi_max.getValue() < poi.getValue(): l_poi_max.remove(poi_max) # End elif # End for # If there are more than one candidates, meaning that at least two # POIs have the same value, we'll compare distances. if len(l_poi_max) > 1: f_distance = -1.0 f_closest_dist = -1.0 l_poi_closest = [] for poi_max in l_poi_max: # We can determine the distance by assigning a route to each poi # and then calling getDrivingDistance to find the distance retured # as a double s_dest_edge = poi_max.getClosestEdge()[0] traci.vehicle.changeTarget(veh.get_id(),s_dest_edge) f_distance = traci.vehicle.getDrivingDistance(veh.get_id(),s_dest_edge,1.0) # Compare distances # We automatically consider the first one if f_closest_dist == -1.0: f_closest_dist = f_distance l_poi_closest.append([poi_max,f_distance]) # We check the other ones in the list else: # Add possible contendors to list. if f_distance <= f_closest_dist: f_closest_dist = f_distance l_poi_closest.append([poi_max,f_distance]) # Remove non-contendors from the list for poi_closest in l_poi_closest: if poi_closest[1] > f_distance: l_poi_closest.remove(poi_closest) # end for poi_closest in l_poi_closest: # end if f_distance <= f_closest_dist: # end else # end for poi_max in l_poi_max: # If the two values are the same, one of POIs will be picked at random. if len(l_poi_closest) > 1: n_random_int = random.randint(0,len(l_poi_closest)) s_dest_edge = l_poi_closest[n_random_int][0] del n_random_int # end if len(l_poi_closest) > 1: else: s_dest_edge = l_poi_closest[0][0].getClosestEdge()[0] del f_distance del f_closest_dist del l_poi_closest # end if len(l_poi_max) > 1: else: s_dest_edge = l_poi_max[0].getClosestEdge()[0] # Send it to the POI traci.vehicle.changeTarget(veh.get_id(),s_dest_edge) # Store the current destination edge veh.set_next_dest_edge_id(s_dest_edge) # Add it to L_VEHICLES to be tracked. global L_VEHICLES L_VEHICLES.append(veh) del veh del n_max_val del l_poi_max del s_dest_edge