def read_input(in_file): rides_list = [] with open(in_file, 'r') as file: r, c, f, n, b, t = [int(x) for x in file.readline().strip().split()] grid_layout = Grid(r, c, t, b) for n in range(n): x1, y1, x2, y2, t1, t2 = [int(x) for x in file.readline().strip().split()] rides_list.append(Ride(n, x1, y1, x2, y2, t1, t2)) vehicles_list = [Vehicle() for _ in range(f)] return grid_layout, rides_list, vehicles_list
def __init__(self, name, units, clock): self.clock = clock self.name = name self.units = [] self._health = None for unit in units: if unit["unit_type"] == "soldier": self.units.append( Soldier(unit["name"], unit["health"], unit["unit_type"], self.clock)) elif unit["unit_type"] == "vehicle": self.units.append( Vehicle(unit["name"], unit["health"], unit["unit_type"], unit["operators"], self.clock)) self.strategy = ""
def best_vehicle(rides, F, T): vehicles = [] for ride in rides: # create new vehicle if F > 0: vehicles.append(Vehicle(T)) F -= 1 # find best_vehicle best_vehicle = vehicles[0] for vehicle in vehicles: if best_vehicle.score(ride) < vehicle.score(ride): best_vehicle = vehicle best_vehicle.add_ride(ride) return vehicles
import time import random from cyclonedds.core import Qos, Policy from cyclonedds.domain import DomainParticipant from cyclonedds.pub import Publisher, DataWriter from cyclonedds.topic import Topic from cyclonedds.util import duration from vehicles import Vehicle qos = Qos(Policy.Reliability.BestEffort, Policy.Deadline(duration(microseconds=10)), Policy.Durability.Transient, Policy.History.KeepLast(10)) domain_participant = DomainParticipant(0) topic = Topic(domain_participant, 'Vehicle', Vehicle, qos=qos) publisher = Publisher(domain_participant) writer = DataWriter(publisher, topic) cart = Vehicle(name="Dallara IL-15", x=200, y=200) while True: cart.x += random.choice([-1, 0, 1]) cart.y += random.choice([-1, 0, 1]) writer.write(cart) print(">> Wrote vehicle") time.sleep(random.random() * 0.9 + 0.1)
SUMO.prec_train_amplitude = np.random.rand( number_episodes) * 20 / 3.6 SUMO.prec_train_mean = np.random.rand( number_episodes) * 20 / 3.6 + 10 / 3.6 else: SUMO.prec_train_amplitude = 25 / 3.6 # a=25/3.6 SUMO.prec_train_mean = 30 / 3.6 # c=30/3.6 SUMO.create_v_profile_prec(a=SUMO.prec_train_amplitude, c=SUMO.prec_train_mean) elif vehicle3_vprofile == 'emergstop': SUMO.create_v_profile_emerg_stop() else: raise NameError('No valid velocity profile selected') trafic.vehicle_ego = Vehicle(ego=True, ID='ego', RouteID='RouteID_ego', Route=trafic.Route_Ego, powertrain_concept='ICEV') trafic.dynamics_ego = Longitudinal_dynamics(tau=0.5) if trafic.vehicle2_exist: trafic.vehicle_2 = Vehicle(ego=False, ID='traffic.0', RouteID='traci_route_traffic.0', Route=trafic.Route_Traffic1) if trafic.vehicle3_exist: trafic.vehicle_3 = Vehicle(ego=False, ID='traffic.1', RouteID='traci_route_traffic.1', Route=trafic.Route_Traffic2) acc_controller = {} # if controller == 'ACC' or controller == 'hybrid_a' or controller == 'DDPG_v':
def createVehicle(self, vehicle_type, price): """ Create an Animal. """ self._db.create(Vehicle(vehicle_type, price))
def check_blind_robot(id_dynamics, dynamics): vehicle = Vehicle() vehicle.add_dynamics(id_dynamics, dynamics) world = Empty([[-10, 10], [-10, 10], [-10, 10]]) sim = VehicleSimulation(vehicle, world) check_simulation(sim, num_episodes=3, num_instants=10, dt=0.1)
import atexit from flask import Flask, request, jsonify from vehicles import Vehicle LOCATIONS = {"backLeft": 1, "backRight": 2, "frontLeft": 3, "frontRight": 4} vehicle = Vehicle() app = Flask(__name__) @atexit.register def release(): vehicle.release() def update_motor(location, motor): app.logger.info("Updating motor at %s with %s", location, motor) vehicle.update_motor(LOCATIONS[location], motor["command"]["value"], motor["speed"]) @app.route('/motors', methods=['PUT']) def drive(): for location in request.json: update_motor(location, request.json[location]) return jsonify({})
def SimInitializer(trafic, episode): error = False try: timestep = 1 trafic.number_episodes = 100000 feature_number = 4 trafic.training = True sample_generation = False trafic.TLS_virt_vehicle = True # Red and Yellow traffic lights are considered as preceding vehicles with v=0 trafic.TLS_ID = ['0', '1', '2', '3', '4'] trafic.evaluation = False trafic.vehicle2_exist = False # currently no meaningful trajectory / route - keep on False trafic.vehicle3_exist = True trafic.vehicle3_vprofile = 'sinus' # 'sinus', 'emergstop' liveplot = False # memory leak problem on windows when turned on trafic.Route_Ego = ['startedge', 'gneE01', 'gneE02', 'stopedge'] trafic.ego_depart_speed = np.ones( (trafic.number_episodes, )) * 0. # specific depart speed for ego vehicle when not training trafic.Route_Traffic1 = ['gneE01', 'junction_out'] # for vehicle2 trafic.Route_Traffic2 = ['gneE01', 'gneE02', 'stopedge'] # for vehicle3 trafic.state_empty = np.zeros([feature_number, 1]) timestep = 2 np.random.seed(42 + episode) SUMO2 = SUMO(trafic.Route_Ego, trafic.Route_Traffic1, trafic.Route_Traffic2, timestep=0.2) ## create velocity profile of preceding vehicle ## """Hier werden bei 'training = True' unterschiedliche Geschwindigkeitsprofile für das vorausfahrende Fahrzeug definiert. Für 'training = False' wird ein festes sinusförmiges Profil mit Mittelwert 30 km/h und Amplitude +- 25 km/h definiert.""" if trafic.vehicle3_exist: if trafic.vehicle3_vprofile == 'sinus': if trafic.training: # create random sinusodial velocity profiles for training SUMO2.prec_train_amplitude = np.random.rand( trafic.number_episodes) * 20 / 3.6 SUMO2.prec_train_mean = np.random.rand( trafic.number_episodes) * 20 / 3.6 + 10 / 3.6 else: SUMO2.prec_train_amplitude = 25 / 3.6 # a=25/3.6 SUMO2.prec_train_mean = 30 / 3.6 # c=30/3.6 SUMO2.create_v_profile_prec(a=SUMO.prec_train_amplitude, c=SUMO.prec_train_mean) elif vehicle3_vprofile == 'emergstop': SUMO2.create_v_profile_emerg_stop() else: raise NameError('No valid velocity profile selected') trafic.vehicle_ego = Vehicle(ego=True, ID='ego', RouteID='RouteID_ego', Route=trafic.Route_Ego, powertrain_concept='ICEV') trafic.dynamics_ego = Longitudinal_dynamics(tau=0.5) if trafic.vehicle2_exist: trafic.vehicle_2 = Vehicle(ego=False, ID='traffic.0', RouteID='traci_route_traffic.0', Route=trafic.Route_Traffic1) if trafic.vehicle3_exist: trafic.vehicle_3 = Vehicle(ego=False, ID='traffic.1', RouteID='traci_route_traffic.1', Route=trafic.Route_Traffic2) acc_controller = {} timestep = 3 # if trafic.training and liveplot: # fig_running, ax_running_1, ax_running_2, ax_running_3, ax_running_4 = plot_running_init(training) process_param = multiprocessing.Process() # print(process_param.name) traci.start(['sumo', '-c', 'SUMO_config.sumocfg', '--no-warnings'], label=str(process_param.name)) #, label=sim) simulation = traci.getConnection(process_param.name) simulation.route.add(trafic.vehicle_ego.RouteID, trafic.vehicle_ego.Route) if trafic.vehicle2_exist: simulation.route.add(trafic.vehicle_2.RouteID, trafic.vehicle_2.Route) if trafic.vehicle3_exist: simulation.route.add(trafic.vehicle_3.RouteID, trafic.vehicle_3.Route) simulation.vehicletype.setSpeedFactor(typeID='traffic_vehicle', factor=5.0) length_episode = np.zeros((trafic.number_episodes, 1)) restart_step = 0 # counter for calculating the reset timing when the simulation time gets close to 24 days evaluation = False if trafic.training: trafic.vehicle_ego.depart_speed = np.random.randint( 0, 30, size=trafic.number_episodes) else: trafic.vehicle_ego.depart_speed = ego_depart_speed simulation.trafficlight.setProgram(tlsID='junction1', programID=np.random.choice( trafic.TLS_ID)) timestep = 4 + episode if not trafic.training: simulation.trafficlight.setPhase('junction1', 0) if trafic.training and not trafic.evaluation and trafic.vehicle3_exist: trafic.vehicle3 = np.random.choice([True, False], p=[0.95, 0.05]) simulation.lane.setMaxSpeed( 'gneE01_0', np.random.choice([8.33, 13.89, 19.44, 25.])) simulation.lane.setMaxSpeed( 'gneE02_0', np.random.choice([8.33, 13.89, 19.44, 25.])) simulation.lane.setMaxSpeed( 'startedge_0', np.random.choice([8.33, 13.89, 19.44, 25.])) SUMO2.create_v_profile_prec(a=SUMO2.prec_train_amplitude[episode - 1], c=SUMO2.prec_train_mean[episode - 1]) else: trafic.vehicle3 = vehicle3_exist simulation.lane.setMaxSpeed('startedge_0', 13.89) # 13.89 simulation.lane.setMaxSpeed('gneE01_0', 19.44) # 19.44 simulation.lane.setMaxSpeed('gneE02_0', 13.89) # 13.89 simulation.lane.setMaxSpeed('stopedge_0', 8.33) # 8.33 trafic.episoden_variante = np.random.rand() * 240. return process_param.name, SUMO2, error except Exception as ex: template = "An exception of type {0} occurred. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) error = True traci.close()