Best Python code snippet using locust
runner_old.py
Source:runner_old.py
1# @file runner.py2# @author Davide3# @date 2021-04-304from __future__ import absolute_import5from __future__ import print_function6from copy import copy7from pathlib import Path8import random9import math10import os11import sys12import optparse13import csv14# we need to import python modules from the $SUMO_HOME/tools directory15if 'SUMO_HOME' in os.environ:16 tools = os.path.join(os.environ['SUMO_HOME'], 'tools')17 sys.path.append(tools)18else:19 sys.exit("please declare environment variable 'SUMO_HOME'")20from sumolib import checkBinary21import traci22import sumolib23from classes.Simulator import Simulator24from init import *25# Create driver - add vehicle in the network. Simulate a driver that becomes active26def create_driver(timestamp,area_id):27 #print("create_driver")28 global driver_id_counter29 driver_id = f"driver_{driver_id_counter}"30 num_random_routes = net_info["num_random_routes"]31 route_num = random.randrange(0,num_random_routes)32 route_id = f"area_{area_id}_route_{route_num}"33 traci.vehicle.add(driver_id, route_id, "driver", depart=f'{timestamp}', departPos="random", line="taxi")34 areas[area_id]["drivers"].append(driver_id)35 areas[area_id]["drivers_counter"] += 136 driver_id_counter += 137 drivers_list.append({38 "driver_id": driver_id,39 "area_id": area_id,40 "state": "active",41 "start": timestamp,42 "last_ride": timestamp,43 "personality": assign_personality(areas[area_id]["driver_personality_probability_distribution"])44 })45def move_driver_to_different_area(driver_id,area_id):46 #print("move_driver_to_different_area")47 min_edge = areas[area_id]["edges"][0]48 max_edge = areas[area_id]["edges"][1]49 from_edge = traci.vehicle.getRoadID(driver_id)50 prefix_to = "" if random_choice(0.5) else "-"51 edge_prefix = net_info["edge_prefix"]52 to_edge = f"{prefix_to}{edge_prefix}{random.randrange(min_edge,max_edge + 1)}"53 if not (from_edge == "") and not (("gneJ" in from_edge) or ("-gneJ" in from_edge)):54 try:55 route_stage = traci.simulation.findRoute(from_edge,to_edge)56 traci.vehicle.setRoute(driver_id,route_stage.edges)57 except:58 pass59def remove_driver(driver_id):60 #print("remove_driver")61 for driver in drivers_list:62 if (driver["driver_id"] == driver_id):63 driver["state"] = "inactive"64 for area_id, area_data in areas.items():65 if (driver_id in area_data["drivers"]):66 area_data["drivers"].remove(driver_id)67 areas[area_id]["drivers_counter"] -= 168 try:69 traci.vehicle.remove(driver_id)70 except:71 pass72# Creating customer73def create_customer(timestamp,area_id):74 #print("create_customer")75 global customer_id_counter76 customer_id = f"customer_{customer_id_counter}"77 min_edge = areas[area_id]["edges"][0]78 max_edge = areas[area_id]["edges"][1]79 from_edge = random.randrange(min_edge,max_edge+1)80 to_edge = random.randrange(min_edge,max_edge+1)81 while from_edge == to_edge:82 to_edge = random.randrange(min_edge,max_edge+1)83 prefix_from = "" if random_choice(0.5) else "-"84 prefix_to = "" if random_choice(0.5) else "-"85 pos = random.randrange(int(traci.lane.getLength(f'gneE{from_edge}_0')))86 traci.person.add(customer_id, f'{prefix_from}gneE{from_edge}', pos, depart=timestamp)87 traci.person.appendDrivingStage(customer_id,f'{prefix_to}gneE{to_edge}','taxi')88 customer_id_counter += 189 areas[area_id]["customers"].append(customer_id)90 areas[area_id]["customers_counter"] += 191 customers_list.append({92 "customer_id": customer_id,93 "personality": assign_personality(areas[area_id]["customer_personality_probability_distribution"]),94 "area_id": area_id95 })96def assign_personality(distribution):97 value = random.random()98 for threshold, personality in distribution:99 if (value <= threshold):100 return personality101 return "normal"102# Dispatch pending rides103def dispatch_rides(timestamp):104 #print("dispatch_ride")105 #pending rides106 #for request in traci.person.getTaxiReservations(1):107 # pending_rides.append({108 # "id": request.id,109 # "customer_id": request.persons[0],110 # "reservation": request,111 # "status": "new"112 # })113 pending_rides.extend(list(traci.person.getTaxiReservations(1)))114 # filter idle drivers115 idle_drivers = list(traci.vehicle.getTaxiFleet(0))116 for ride in pending_rides:117 if (not ride.id in rides_stats):118 # retrieve first customer of the current ride119 customer_id = ride.persons[0]120 for customer in customers_list:121 if(customer["customer_id"] == customer_id):122 if (accept_ride_choice(customer["area_id"],"customer", customer["personality"])):123 # customer coordinates124 x_c,y_c = traci.person.getPosition(customer_id)125 driver_distances = []126 for driver_id in idle_drivers:127 # driver coordinates128 x_d,y_d = traci.vehicle.getPosition(driver_id)129 # compute distance of the driver from the customer130 #air_distance = abs(traci.simulation.getDistance2D(x_c,y_c,x_d,y_d,isDriving=False))131 #road_distance = abs(traci.simulation.getDistance2D(x_c,y_c,x_d,y_d,isDriving=True))132 # compute waiting time133 driver_edge = traci.vehicle.getRoadID(driver_id)134 customer_edge = traci.person.getRoadID(customer_id)135 if not (driver_edge == "") and not (("gneJ" in driver_edge) or ("-gneJ" in driver_edge)):136 if not (customer_edge == "") and not (("gneJ" in customer_edge) or ("-gneJ" in customer_edge)):137 try:138 waiting_route_stage = traci.simulation.findRoute(driver_edge,customer_edge)139 expected_waiting_time = waiting_route_stage.travelTime140 waiting_distance = waiting_route_stage.length141 driver_distances.append({142 "driver_id": driver_id,143 #"road_distance": road_distance,144 #"air_distance": air_distance,145 "expected_waiting_time": expected_waiting_time,146 "waiting_distance": waiting_distance147 })148 except:149 pass150 ride_route_stage = traci.simulation.findRoute(ride.fromEdge, ride.toEdge)151 ride_travel_time = ride_route_stage.travelTime152 ride_length = ride_route_stage.length153 ride_stats = rides_stats[ride.id] if ride.id in rides_stats else {154 "id": ride.id,155 "customer_id": customer_id,156 "found": False,157 "canceled": False,158 "expected_ride_length": ride_length,159 "expected_ride_time": ride_travel_time,160 "rejections": 0,161 "steps": 0,162 "timestamp_request": timestamp,163 "state": "pending",164 "from": ride.fromEdge,165 "to": ride.toEdge,166 "reservation": ride167 }168 from_edge_area_id = edge_area(ride.fromEdge)169 if (len(driver_distances) > 0):170 drivers_sorted = sorted(driver_distances, key=lambda x:x["expected_waiting_time"], reverse=False)171 # simulate send request to drivers172 for driver_ride_data in drivers_sorted:173 if (driver_ride_data["waiting_distance"] > request_driver_distance):174 break175 176 driver_accept_ride = False177 for d in drivers_list:178 if (d["state"] == "moving"):179 to_area = edge_area(traci.vehicle.getRoute(d["driver_id"])[-1])180 if (to_area != from_edge_area_id):181 continue182 if (d["driver_id"] == driver_ride_data['driver_id']):183 driver_accept_ride = accept_ride_choice(from_edge_area_id,"driver",d["personality"])184 if (driver_accept_ride):185 d["state"] = "occupied"186 d["ride_id"] = ride.id187 d["customer_id"] = customer_id188 break189 190 if (driver_accept_ride):191 #print("IDLE DRIVERS")192 #print(idle_drivers)193 idle_drivers.remove(driver_ride_data["driver_id"])194 #print(idle_drivers)195 pending_rides.remove(ride)196 onroad_rides.append(ride)197 ride_stats["found"] = True198 ride_stats["driver_id"] = driver_ride_data["driver_id"]199 ride_stats["expected_waiting_length"] = driver_ride_data["waiting_distance"]200 ride_stats["expected_total_length"] = ride_length + driver_ride_data["waiting_distance"]201 ride_stats["expected_waiting_time"] = driver_ride_data["expected_waiting_time"]202 ride_stats["expected_total_time"] = driver_ride_data["expected_waiting_time"] + ride_travel_time203 ride_stats["expected_price"] = compute_price(ride_travel_time,ride_length, areas[from_edge_area_id]["surge_multipliers"][-1])204 ride_stats["timestamp_accepted"] = timestamp205 ride_stats["surge_multiplier"] = areas[from_edge_area_id]["surge_multipliers"][-1]206 ride_stats["time_to_accept_request"] = timestamp - ride_stats["timestamp_request"]207 ride_stats["state"] = "waiting"208 rides_stats[ride.id] = ride_stats209 210 #print('***')211 #print(f"Driver {driver_ride_data['driver_id']} route before: {traci.vehicle.getRoute(driver_ride_data['driver_id'])}")212 #print(f"Customer {customer_id} road: {traci.person.getRoadID(customer_id)}") 213 traci.vehicle.dispatchTaxi(driver_ride_data["driver_id"], [ride.id])214 #print(f"Driver {driver_ride_data['driver_id']} route after: {traci.vehicle.getRoute(driver_ride_data['driver_id'])}")215 #print("***")216 matched_rides.append((customer_id,driver_ride_data['driver_id'],timestamp))217 areas[from_edge_area_id]["customers_counter"] -= 1218 break219 else:220 ride_stats["rejections"] = ride_stats["rejections"] + 1 221 if not (ride_stats["found"]):222 cancel_ride = random_choice(cancel_ride_p)223 if (cancel_ride) :224 #print(f"customer: {customer_id} - driver not found")225 traci.person.removeStages(customer_id)226 ride_stats["canceled"] = True227 ride_stats["state"] = "canceled"228 areas[from_edge_area_id]["canceled"] += 1229 pending_rides.remove(ride)230 areas[from_edge_area_id]["customers_counter"] -= 1231 areas[from_edge_area_id]["customers"].remove(customer_id)232 canceled_rides.append(customer_id)233 customers_list.remove(customer)234 #areas[from_edge_area_id]["surge_multipliers"].append(areas[from_edge_area_id]["surge_multipliers"][-1] + 0.1)235 else:236 #print(f"customer: {customer_id} - driver not found")237 #ride_stats["steps"] += 1238 #rides_stats[ride.id] = ride_stats239 traci.person.removeStages(customer_id)240 areas[from_edge_area_id]["customers_counter"] -= 1241 ride_stats["canceled"] = True242 ride_stats["state"] = "canceled"243 areas[from_edge_area_id]["canceled"] += 1244 pending_rides.remove(ride)245 canceled_rides.append(customer_id)246 areas[from_edge_area_id]["customers"].remove(customer_id)247 customers_list.remove(customer)248 #areas[from_edge_area_id]["surge_multipliers"].append(areas[from_edge_area_id]["surge_multipliers"][-1] + 0.1)249 # print statistics250 #print_dispatch_ride_stats(ride_stats)251 else:252 traci.person.removeStages(customer_id)253 areas[customer["area_id"]]["customers_counter"] -= 1254 customers_list.remove(customer)255def edge_area(edge_id):256 #print("edge_area")257 for area_id, area_data in areas.items():258 edges_names = []259 for i in range(area_data["edges"][0],area_data["edges"][1]+1):260 edges_names.append(f"gneE{i}")261 edges_names.append(f"-gneE{i}")262 if (edge_id in edges_names):263 return area_id264 return ""265# return a random choice with a certain probability266def random_choice(p=0.5):267 #print("random_choice")268 return random.random() < p269def expected_travel_time(edges):270 #print("expected_travel_time")271 travel_time = 0272 for edge_id in edges:273 travel_time += traci.edge.getTraveltime(edge_id)274 return travel_time275# computed expected price of the ride276def compute_price(travel_time, ride_length, surge_price_multiplier):277 #print("compute_price")278 price = (base_fare + (fee_per_minute * travel_time) + (fee_per_mile * ride_length/1000)) * surge_price_multiplier279 return price280def accept_ride_choice(area_id, agent, personality):281 surge_multiplier = areas[area_id]["surge_multipliers"][-1]282 policies = personality_driver_policy if agent == "driver" else personality_customer_policy283 choice_policy = policies[personality]284 for min_surge, max_surge, p in choice_policy:285 if (surge_multiplier >= min_surge and surge_multiplier < max_surge):286 return random_choice(p)287 return random_choice(0.5)288def update_surge_multiplier():289 #print("update_surge_multiplier")290 idle_drivers = traci.vehicle.getTaxiFleet(0)291 #print(f"IDLE DRIVERS: {idle_drivers}")292 for area_id, area_data in areas.items():293 customers_in_area = areas[area_id]["customers_counter"]294 #drivers_in_area = areas[area_id]["drivers_counter"]295 #idle_drivers_in_area = len(list(set(idle_drivers).intersection(areas[area_id]["drivers"])))296 idle_drivers_in_area = 0297 298 for driver in drivers_list:299 if (driver["state"] == "moving"):300 to_area = edge_area(traci.vehicle.getRoute(driver["driver_id"]))301 if (to_area == area_id):302 idle_drivers_in_area += 1303 elif (driver["area_id"] == area_id and driver["state"] == "active"):304 idle_drivers_in_area += 1305 306 #print(f"IDLE DRIVERS IN AREA {area_id}: {idle_drivers_in_area}")307 #print(f"CUSTOMERS IN AREA {area_id}: {customers_in_area}")308 if (customers_in_area > 0):309 #balance = (idle_drivers_in_area)/(customers_in_area)310 #print(f"balance: {balance}")311 if (idle_drivers_in_area == 0):312 balance = 1/(customers_in_area + 0.1)313 else:314 balance = (idle_drivers_in_area)/(customers_in_area)315 else:316 balance = idle_drivers_in_area317 print(f"customers_in_area: {customers_in_area}")318 print(f"drivers_in_area: {idle_drivers_in_area}")319 diff_balance = balance - area_data["balances"][-1]320 area_data["balances"].append(balance)321 surge_multiplier = area_data["surge_multipliers"][-1]322 for min_balance, max_balance, value in surge_multiplier_policy:323 if (balance >= min_balance and balance < max_balance):324 #print(f"Before: {surge_multiplier}")325 print(f"Add: {value}")326 surge_multiplier += value327 break328 #for min_diff, max_diff, value in surge_multiplier_policy:329 # if (abs(diff_balance) >= min_diff and abs(diff_balance) < max_diff):330 # if (diff_balance > 0):331 # surge_multiplier -= value332 # else:333 # if(surge_multiplier > 0):334 # surge_multiplier += value335 # break336 337 area_data["surge_multipliers"].append(max(0.7,min(surge_multiplier,3.5)))338 print(f"Surge: {area_data['surge_multipliers'][-1]}")339 #print(f"Before: {area_data['surge_multiplier']}")340 #print(f"Surge = {surge_multiplier}")341 #print(f"Min: {min(surge_multiplier,3.5)}")342 #print(f"After: {area_data['surge_multiplier']}")343 if(area_data["surge_multipliers"][-1] > 3.5):344 print("ERROR!!!")345 print("-"*10)346def update_drivers_area():347 #print("update_drivers_area")348 for driver in drivers_list:349 if not (driver["state"] == "inactive"):350 driver_id = driver["driver_id"]351 #print(8)352 #print(removed_drivers)353 #print(drivers_list)354 try:355 traci.vehicle.getRoadID(driver_id)356 except:357 continue358 pass359 driver_edge = traci.vehicle.getRoadID(driver_id)360 #print(driver_edge)361 area_id = edge_area(driver_edge)362 if not ((area_id == "") or (driver["area_id"] == area_id)):363 areas[driver["area_id"]]["drivers"].remove(driver_id)364 areas[area_id]["drivers"].append(driver_id)365 driver["area_id"] = area_id366 if (driver["state"] == "moving"):367 to_area = traci.vehicle.getRoute(driver_id)[-1]368 369 if not (to_area == "" and to_area == driver["area_id"]):370 driver["state"] == "active"371def update_rides_state(timestamp,step):372 #print("update_rides_state")373 idle_drivers = traci.vehicle.getTaxiFleet(0)374 pickup_drivers = traci.vehicle.getTaxiFleet(1)375 occupied_drivers = traci.vehicle.getTaxiFleet(2)376 for ride_id, ride in rides_stats.items():377 if not (ride["canceled"]):378 driver_id = ride["driver_id"]379 customer_id = ride["customer_id"]380 if ride["state"] == "waiting":381 driver_edge = traci.vehicle.getRoadID(driver_id)382 customer_edge = traci.person.getRoadID(customer_id)383 if driver_edge == customer_edge:384 ride["timestamp_pickup"] = timestamp385 ride["waiting_time"] = timestamp - ride["timestamp_accepted"]386 ride["state"] = "pickup"387 elif ((ride["state"] == "pickup") and (driver_id in idle_drivers)):388 for driver in drivers_list:389 if (driver["driver_id"] == ride["driver_id"]):390 if (driver["state"] == "occupied"):391 from_edge_area_id = edge_area(ride["from"])392 ride["end_step"] = step393 ride["timestamp_end"] = timestamp394 ride["ride_time"] = timestamp - ride["timestamp_pickup"]395 ride["ride_length"] = ride["expected_ride_length"]396 ride["waiting_length"] = ride["expected_waiting_length"]397 ride["total_time"] = timestamp - ride["timestamp_request"]398 ride["total_length"] = ride["waiting_length"] + ride["ride_length"]399 ride["price"] = compute_price(ride["ride_time"],ride["ride_length"],areas[from_edge_area_id]["surge_multipliers"][-1])400 ride["state"] = "end"401 areas[from_edge_area_id]["customers"].remove(ride["customer_id"])402 areas[from_edge_area_id]["completed"] +=1403 areas[from_edge_area_id]["waiting_times"].append(ride["waiting_time"])404 areas[from_edge_area_id]["ride_times"].append(ride["ride_time"])405 areas[from_edge_area_id]["total_times"].append(ride["total_time"])406 areas[from_edge_area_id]["waiting_lengths"].append(ride["waiting_length"])407 areas[from_edge_area_id]["ride_lengths"].append(ride["ride_length"])408 areas[from_edge_area_id]["total_lengths"].append(ride["total_time"])409 areas[from_edge_area_id]["expected_prices"].append(ride["expected_price"])410 areas[from_edge_area_id]["prices"].append(ride["price"])411 areas[from_edge_area_id]["diff_prices"].append(ride["price"] - ride["expected_price"])412 areas[from_edge_area_id]["diff_waiting_times"].append(ride["waiting_time"] - ride["expected_waiting_time"])413 areas[from_edge_area_id]["diff_ride_times"].append(ride["ride_time"] - ride["expected_ride_time"])414 areas[from_edge_area_id]["diff_total_times"].append(ride["total_time"] - ride["expected_total_time"])415 areas[from_edge_area_id]["diff_waiting_lengths"].append(ride["waiting_length"] - ride["expected_waiting_length"])416 areas[from_edge_area_id]["diff_ride_lengths"].append(ride["ride_length"] - ride["expected_ride_length"])417 areas[from_edge_area_id]["diff_total_lengths"].append(ride["total_length"] - ride["expected_total_length"])418 areas[from_edge_area_id]["rejections"].append(ride["rejections"])419 onroad_rides.remove(ride["reservation"])420 ended_rides.append(ride["reservation"])421 save_ride_stats(ride)422 save_global_statistics(step)423 424 driver["state"] = "active"425 driver["last_ride"] = timestamp426 driver["ride_id"] = None427 driver["customer_id"] = None428 if (timestamp - driver["start"] > 3600):429 stop_drive = random_choice(0.6)430 if (stop_drive or areas[from_edge_area_id]["surge_multipliers"][-1] <= 0.6):431 remove_driver(driver_id)432 removed_drivers.append((driver,timestamp))433 434 for customer in customers_list:435 if (customer["customer_id"] == customer_id):436 customers_list.remove(customer)437def update_drivers_movements(timestamp):438 #print("update_drivers_movements")439 for area_id, area_data in areas.items():440 move_probability = 0441 for other_area_id,other_area_data in areas.items(): 442 if not (other_area_id == area_id):443 for min_diff, max_diff, p in move_diff_probabilities:444 if (((area_data["surge_multipliers"][-1] - other_area_data["surge_multipliers"][-1]) > min_diff) and ((area_data["surge_multipliers"][-1] - other_area_data["surge_multipliers"][-1]) <= max_diff)):445 move_probability = p446 break447 for driver_id in other_area_data["drivers"]:448 for driver in drivers_list:449 if (driver["driver_id"] == driver_id and driver["state"] == "active"):450 if (random_choice(move_probability)):451 print(f"Move driver {driver_id} from area {other_area_id} to area {area_id}")452 move_driver_to_different_area(driver_id,area_id)453def update_drivers(timestamp):454 #print("update_drivers")455 for driver in drivers_list:456 surge_multiplier = areas[driver["area_id"]]["surge_multipliers"][-1]457 if (not((driver["state"] == "occupied") or (driver["state"] == "inactive") or (driver["state"] == "moving")) and (((timestamp - driver["last_ride"]) > timer_remove_driver_idle) or (driver["driver_id"] in list(traci.simulation.getArrivedIDList())))):458 #print(driver)459 remove_driver(driver["driver_id"])460 removed_drivers.append((driver,timestamp))461def print_dispatch_ride_stats(ride_stats):462 #print("print_dispatch_ride_stats")463 print(ride_stats)464def print_area_stats():465 #print("area_stats")466 print("*"*15)467 print("AREA STATISTICS")468 print("*"*15)469 for area_id, area_data in areas.items():470 print('-'*6)471 print(f"AREA {area_id}")472 print('-'*20)473 print("GENERATION PROBABILITY:")474 print('-'*20)475 print(f"Customer: {area_data['generation']['customer']}")476 print(f"Driver: {area_data['generation']['driver']}")477 print(f"Surge Multiplier: {area_data['surge_multiplier']}")478 print(f"ACTIVE USERS IN THE AREA")479 print(f"Customers: {area_data['customers']}")480 print(f"Drivers: {area_data['drivers']}")481 print(f"Canceled: {area_data['canceled']}")482 print(f"Customers counter: {area_data['customers_counter']}")483 print('-'*20)484 #print(f"Canceled rides: {canceled_rides}")485 #print(f"Pending rides: {pending_rides}")486 #print(f"On road rides: {onroad_rides}")487 #print(f"Ended rides: {ended_rides}")488 #print(f"Matched rides: {matched_rides}")489 #print(f"Removed drivers: {removed_drivers}")490 for m in matched_rides:491 c= m[0]492 d = m[1]493 #print(f"Customer {c} position: {traci.person.getRoadID(c)}")494 #print(f"Driver {d} Route: {traci.vehicle.getRoute(d)}")495def save_simulation_statistics():496 #print("save_simulation_statistics")497 with open(f"stats_file_{time_simulation}", 'a', newline='\n') as stats_writer:498 stats_writer.write("*"*21)499 stats_writer.write("\nSIMULATION PARAMETERS\n")500 stats_writer.write("*"*21)501 stats_writer.write('\n')502 stats_writer.write(f"Fault simulation: {'TRUE' if fault_simulation else 'FALSE'}\n")503 for min_surge, max_surge, p in move_probabilities:504 stats_writer.write(f"Move driver probability with surge multiplier between {min_surge} and {max_surge}: {p}\n")505 for min_diff, max_diff, value in surge_multiplier_policy:506 stats_writer.write(f"Surge multiplier policy with balance variations between {min_diff} and {max_diff}: {value}\n")507 508 stats_writer.write('\n')509 stats_writer.write("*"*30)510 stats_writer.write("\nPERSONALITY ACCEPT RIDE POLICY\n")511 stats_writer.write("*"*30)512 stats_writer.write("\nDriver Policy\n")513 stats_writer.write("-"*13)514 for personality, probabilities in personality_driver_policy.items():515 stats_writer.write(f"\nPersonality: {personality}\n")516 for min_p, max_p, accept_p in probabilities:517 stats_writer.write(f"Accept probability with surge multiplier between {min_p} and {max_p}: {accept_p}\n")518 stats_writer.write("-"*15)519 stats_writer.write("\nCustomer Policy\n")520 stats_writer.write("-"*15)521 for personality, probabilities in personality_customer_policy.items():522 stats_writer.write(f"\nPersonality: {personality}\n")523 for min_p, max_p, accept_p in probabilities:524 stats_writer.write(f"Accept probability with surge multiplier between {min_p} and {max_p}: {accept_p}\n")525 stats_writer.write("\n")526 stats_writer.write("*"*15)527 stats_writer.write("\nAREA STATISTICS\n")528 stats_writer.write("*"*15)529 for area_id, area_data in areas.items():530 stats_writer.write(f"\nAREA {area_id}\n")531 stats_writer.write('-'*22)532 stats_writer.write("\nGENERATION PROBABILITY\n")533 stats_writer.write('-'*22)534 stats_writer.write("\n")535 stats_writer.write(f"Customer: {area_data['generation']['customer']}\n")536 stats_writer.write(f"Driver: {area_data['generation']['driver']}\n")537 stats_writer.write('-'*9)538 stats_writer.write(f"\nSTATISTICS\n")539 stats_writer.write('-'*9)540 stats_writer.write('\n')541 stats_writer.write(f"Average Surge Multiplier: {average(area_data['surge_multipliers']):.2f}\n")542 stats_writer.write(f"Average Balance: {average(area_data['balances']):.2f} drivers/customer\n")543 stats_writer.write(f"Ride Canceled: {area_data['canceled']}\n")544 stats_writer.write(f"Rides completed: {area_data['completed']}\n")545 stats_writer.write(f"Average Rejections: {average(area_data['rejections']):.2f}\n")546 stats_writer.write(f"Average waiting time: {average(area_data['waiting_times']):.2f}\n")547 stats_writer.write(f"Average ride time (from meeting point to destination point): {average(area_data['ride_times']):.2f}\n")548 stats_writer.write(f"Average total ride time: {average(area_data['total_times']):.2f}\n")549 stats_writer.write(f"Average driver distance from the meeting point: {average(area_data['waiting_lengths']):.2f}\n")550 stats_writer.write(f"Average lengths from meeting point to destination point: {average(area_data['ride_lengths']):.2f}\n")551 stats_writer.write(f"Average total ride lenght: {average(area_data['total_lengths']):.2f}\n")552 stats_writer.write(f"Average expected price: {average(area_data['expected_prices']):.2f}\n")553 stats_writer.write(f"Average price: {average(area_data['prices']):.2f}\n")554 stats_writer.write(f"Average error on price prediction: {average(area_data['diff_prices']):.2f}\n")555 stats_writer.write(f"Average error on waiting time prediction: {average(area_data['diff_waiting_times']):.2f}\n")556 stats_writer.write(f"Average error on ride time prediction: {average(area_data['diff_ride_times']):.2f}\n")557 stats_writer.write(f"Average error on total time prediction: {average(area_data['diff_total_times']):.2f}\n")558 stats_writer.write(f"Average error on driver distance from the meeting point: {average(area_data['diff_waiting_lengths']):.2f}\n")559 stats_writer.write(f"Average error on lengths from meeting point to destination point prediction: {average(area_data['diff_ride_times']):.2f}\n")560 stats_writer.write(f"Average error on total ride lenght prediction: {average(area_data['diff_total_lengths']):.2f}\n")561 stats_writer.write('-'*9)562 stats_writer.write(f"\nCUSTOMER PROBABILITY DISTRIBUTION\n")563 stats_writer.write('-'*9)564 stats_writer.write('\n')565 customer_personality_counter = 0566 for threshold, personality in area_data["customer_personality_probability_distribution"]:567 if (customer_personality_counter == 0):568 stats_writer.write(f"Generation of {personality} customer, with probability {threshold:.2f}\n")569 else:570 previous_threshold = area_data["customer_personality_probability_distribution"][customer_personality_counter][0]571 stats_writer.write(f"Generation of {personality} customer, with probability {(threshold - previous_threshold):.2f}\n")572 customer_personality_counter += 1573 stats_writer.write('-'*9)574 stats_writer.write(f"\nDRIVER PROBABILITY DISTRIBUTION\n")575 stats_writer.write('-'*9)576 stats_writer.write('\n')577 driver_personality_counter = 0578 for threshold, personality in area_data["driver_personality_probability_distribution"]:579 if (driver_personality_counter == 0):580 stats_writer.write(f"Generation of {personality} driver, with probability {threshold:.2f}\n")581 else:582 previous_threshold = area_data["driver_personality_probability_distribution"][driver_personality_counter][0]583 stats_writer.write(f"Generation of {personality} driver, with probability {(threshold - previous_threshold):.2f}\n")584 driver_personality_counter += 1585 stats_writer.write('-'*9)586 stats_writer.write('\n')587 uber_stats["completed"].append(area_data['completed'])588 uber_stats["canceled"].append(area_data['canceled'])589 uber_stats["average_waiting_times"].append(average(area_data['waiting_times']))590 uber_stats["average_ride_times"].append(average(area_data['ride_times']))591 uber_stats["average_total_times"].append(average(area_data['total_times']))592 uber_stats["average_waiting_lengths"].append(average(area_data['waiting_lengths']))593 uber_stats["average_ride_lengths"].append(average(area_data['ride_lengths']))594 uber_stats["average_total_lengths"].append(average(area_data['total_lengths']))595 uber_stats["average_expected_prices"].append(average(area_data['expected_prices']))596 uber_stats["average_prices"].append(average(area_data['prices']))597 uber_stats["average_diff_prices"].append(average(area_data['diff_prices']))598 uber_stats["average_diff_waiting_times"].append(average(area_data['diff_waiting_times']))599 uber_stats["average_diff_ride_times"].append(average(area_data['diff_ride_times']))600 uber_stats["average_diff_total_times"].append(average(area_data['diff_total_times']))601 uber_stats["average_diff_waiting_lengths"].append(average(area_data['diff_waiting_lengths']))602 uber_stats["average_diff_ride_lengths"].append(average(area_data['diff_ride_times']))603 uber_stats["average_diff_total_lengths"].append(average(area_data['diff_total_lengths']))604 uber_stats["average_rejections"].append(average(area_data['rejections']))605 uber_stats["average_surge_multipliers"].append(average(area_data['surge_multipliers']))606 uber_stats["average_balances"].append(average(area_data['balances']))607 stats_writer.write("\n")608 stats_writer.write("*"*17)609 stats_writer.write("\nGLOBAL STATISTICS\n")610 stats_writer.write("*"*17)611 stats_writer.write('\n')612 stats_writer.write(f"Rides Canceled: {sum(uber_stats['canceled'])}\n")613 stats_writer.write(f"Rides completed: {sum(uber_stats['completed'])}\n")614 stats_writer.write(f"Average Rejections: {average(uber_stats['average_rejections']):.2f}\n")615 stats_writer.write(f"Average waiting time: {average(uber_stats['average_waiting_times']):.2f}\n")616 stats_writer.write(f"Average ride time (from meeting point to destination point): {average(uber_stats['average_ride_times']):.2f}\n")617 stats_writer.write(f"Average total ride time: {average(uber_stats['average_total_times']):.2f}\n")618 stats_writer.write(f"Average driver distance from the meeting point: {average(uber_stats['average_waiting_lengths']):.2f}\n")619 stats_writer.write(f"Average lengths from meeting point to destination point: {average(uber_stats['average_ride_lengths']):.2f}\n")620 stats_writer.write(f"Average total ride lenght: {average(uber_stats['average_total_lengths']):.2f}\n")621 stats_writer.write(f"Average expected price: {average(uber_stats['average_expected_prices']):.2f}\n")622 stats_writer.write(f"Average price: {average(uber_stats['average_prices']):.2f}\n")623 stats_writer.write(f"Average error on price prediction: {average(uber_stats['average_diff_prices']):.2f}\n")624 stats_writer.write(f"Average error on waiting time prediction: {average(uber_stats['average_diff_waiting_times']):.2f}\n")625 stats_writer.write(f"Average error on ride time prediction: {average(uber_stats['average_diff_ride_times']):.2f}\n")626 stats_writer.write(f"Average error on total time prediction: {average(uber_stats['average_diff_total_times']):.2f}\n")627 stats_writer.write(f"Average error on driver distance from the meeting point: {average(uber_stats['average_diff_waiting_lengths']):.2f}\n")628 stats_writer.write(f"Average error on lengths from meeting point to destination point prediction: {average(uber_stats['average_diff_ride_times']):.2f}\n")629 stats_writer.write(f"Average error on total ride lenght prediction: {average(uber_stats['average_diff_total_lengths']):.2f}\n")630 stats_writer.write(f"Average surge multiplier: {average(uber_stats['average_surge_multipliers']):.2f}\n")631 stats_writer.write(f"Average balance: {average(uber_stats['average_balances']):.2f}\n") 632def save_global_statistics(step,ride):633 #print("save_simulation_statistics")634 global uber_stats, row_index635 old_uber_stats = copy(uber_stats)636 uber_stats = init_uber_stats()637 with open(f"global_indicators_{time_simulation}", 'a', newline='\n') as out_file:638 stats_writer = csv.writer(out_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)639 for area_id, area_data in areas.items():640 uber_stats["completed"].append(area_data['completed'])641 uber_stats["canceled"].append(area_data['canceled'])642 uber_stats["average_waiting_times"].append(average(area_data['waiting_times']))643 uber_stats["average_ride_times"].append(average(area_data['ride_times']))644 uber_stats["average_total_times"].append(average(area_data['total_times']))645 uber_stats["average_waiting_lengths"].append(average(area_data['waiting_lengths']))646 uber_stats["average_ride_lengths"].append(average(area_data['ride_lengths']))647 uber_stats["average_total_lengths"].append(average(area_data['total_lengths']))648 uber_stats["average_expected_prices"].append(average(area_data['expected_prices']))649 uber_stats["average_prices"].append(average(area_data['prices']))650 uber_stats["average_diff_prices"].append(average(area_data['diff_prices']))651 uber_stats["average_diff_waiting_times"].append(average(area_data['diff_waiting_times']))652 uber_stats["average_diff_ride_times"].append(average(area_data['diff_ride_times']))653 uber_stats["average_diff_total_times"].append(average(area_data['diff_total_times']))654 uber_stats["average_diff_waiting_lengths"].append(average(area_data['diff_waiting_lengths']))655 uber_stats["average_diff_ride_lengths"].append(average(area_data['diff_ride_times']))656 uber_stats["average_diff_total_lengths"].append(average(area_data['diff_total_lengths']))657 uber_stats["average_rejections"].append(average(area_data['rejections']))658 uber_stats["average_surge_multipliers"].append(average(area_data['surge_multipliers']))659 uber_stats["average_balances"].append(average(area_data['balances']))660 stats_writer.writerow([661 f"{step}",662 f"{sum(uber_stats['canceled'])}",663 f"{sum(uber_stats['completed'])}",664 f"{(sum(uber_stats['completed'])/(sum(uber_stats['canceled']) + sum(uber_stats['completed']))):.2f}",665 f"{average(uber_stats['average_rejections']):.2f}",666 f"{average(uber_stats['average_waiting_times']):.2f}",667 f"{average(uber_stats['average_ride_times']):.2f}",668 f"{average(uber_stats['average_total_times']):.2f}",669 f"{average(uber_stats['average_waiting_lengths']):.2f}",670 f"{average(uber_stats['average_ride_lengths']):.2f}",671 f"{average(uber_stats['average_total_lengths']):.2f}",672 f"{average(uber_stats['average_expected_prices']):.2f}",673 f"{average(uber_stats['average_prices']):.2f}",674 f"{average(uber_stats['average_diff_prices']):.2f}",675 f"{average(uber_stats['average_diff_waiting_times']):.2f}",676 f"{average(uber_stats['average_diff_ride_times']):.2f}",677 f"{average(uber_stats['average_diff_total_times']):.2f}",678 f"{average(uber_stats['average_diff_waiting_lengths']):.2f}",679 f"{average(uber_stats['average_diff_ride_times']):.2f}",680 f"{average(uber_stats['average_diff_total_lengths']):.2f}",681 f"{average(uber_stats['average_surge_multipliers']):.2f}",682 f"{average(uber_stats['average_balances']):.2f}"683 ])684 with open(f"hardiness_indicators_{time_simulation}", 'a', newline='\n') as out_file:685 stats_writer = csv.writer(out_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)686 stats_writer.writerow([687 f"{step}",688 f"{sum(uber_stats['canceled'])}",689 f"{sum(uber_stats['completed'])}",690 f"{(sum(uber_stats['completed'])/(sum(uber_stats['canceled']) + sum(uber_stats['completed']))):.2f}",691 f"{average(uber_stats['average_rejections']):.2f}",692 f"{average(uber_stats['average_waiting_times']):.2f}",693 f"{average(uber_stats['average_ride_times']):.2f}",694 f"{average(uber_stats['average_total_times']):.2f}",695 f"{average(uber_stats['average_surge_multipliers']):.2f}",696 ])697 if (row_index > 0):698 with open(f"global_diff_indicators_{time_simulation}", 'a', newline='\n') as out_file:699 stats_writer = csv.writer(out_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)700 stats_writer.writerow([701 f"{step}",702 f"{sum(uber_stats['canceled']) - sum(old_uber_stats['canceled'])}",703 f"{sum(uber_stats['completed']) - sum(old_uber_stats['completed'])}",704 f"{(sum(uber_stats['completed'])/(sum(uber_stats['canceled']) + sum(uber_stats['completed'])) - sum(old_uber_stats['completed'])/(sum(old_uber_stats['canceled']) + sum(old_uber_stats['completed']))):.2f}",705 f"{(average(uber_stats['average_rejections']) - average(old_uber_stats['average_rejections'])):.2f}",706 f"{(average(uber_stats['average_waiting_times']) - average(old_uber_stats['average_waiting_times'])):.2f}",707 f"{(average(uber_stats['average_ride_times']) - average(old_uber_stats['average_ride_times'])):.2f}",708 f"{(average(uber_stats['average_total_times']) - average(old_uber_stats['average_total_times'])):.2f}",709 f"{(average(uber_stats['average_waiting_lengths']) - average(old_uber_stats['average_waiting_lengths'])):.2f}",710 f"{(average(uber_stats['average_ride_lengths']) - average(old_uber_stats['average_ride_lengths'])):.2f}",711 f"{(average(uber_stats['average_total_lengths']) - average(old_uber_stats['average_total_lengths'])):.2f}",712 f"{(average(uber_stats['average_expected_prices']) - average(old_uber_stats['average_expected_prices'])):.2f}",713 f"{(average(uber_stats['average_prices']) - average(old_uber_stats['average_prices'])):.2f}",714 f"{(average(uber_stats['average_diff_prices']) - average(old_uber_stats['average_diff_prices'])):.2f}",715 f"{(average(uber_stats['average_diff_waiting_times']) - average(old_uber_stats['average_diff_waiting_times'])):.2f}",716 f"{(average(uber_stats['average_diff_ride_times']) - average(old_uber_stats['average_diff_ride_times'])):.2f}",717 f"{(average(uber_stats['average_diff_total_times']) - average(old_uber_stats['average_diff_total_times'])):.2f}",718 f"{(average(uber_stats['average_diff_waiting_lengths']) - average(old_uber_stats['average_diff_waiting_lengths'])):.2f}",719 f"{(average(uber_stats['average_diff_ride_times']) - average(old_uber_stats['average_diff_ride_times'])):.2f}",720 f"{(average(uber_stats['average_diff_total_lengths']) - average(old_uber_stats['average_diff_total_lengths'])):.2f}",721 f"{(average(uber_stats['average_surge_multipliers']) - average(old_uber_stats['average_surge_multipliers'])):.2f}",722 f"{(average(uber_stats['average_balances']) - average(old_uber_stats['average_balances'])):.2f}"723 ])724 725 row_index += 1726def save_ride_stats(ride):727 #print("save_ride_stats")728 with open(f"output_file_{time_simulation}",'a', newline='') as out_file:729 out_writer = csv.writer(out_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)730 out_writer.writerow([731 ride["end_step"],732 "%.2f" % ride["ride_length"],733 "%.2f" % ride["waiting_length"],734 "%.2f" % ride["total_length"],735 "%.2f" % ride["expected_waiting_time"],736 "%.2f" % ride["expected_ride_time"],737 "%.2f" % ride["expected_total_time"],738 "%.2f" % ride["expected_price"],739 "%.2f" % ride["waiting_time"],740 "%.2f" % ride["ride_time"],741 "%.2f" % ride["total_time"],742 "%.2f" % ride["price"],743 "%.2f" % ride["surge_multiplier"],744 ride["rejections"]745 ])746def average(lst):747 if (len(lst)) > 0:748 return sum(lst) / len(lst)749 return 0750def init_uber_stats():751 return {752 "canceled": [],753 "completed": [],754 "average_prices": [],755 "average_expected_prices": [],756 "average_waiting_times": [],757 "average_ride_times": [],758 "average_total_times": [],759 "average_waiting_lengths": [],760 "average_ride_lengths": [],761 "average_total_lengths": [],762 "average_diff_prices": [],763 "average_diff_waiting_times": [],764 "average_diff_ride_times": [],765 "average_diff_total_times": [],766 "average_diff_waiting_lengths": [],767 "average_diff_ride_lengths": [],768 "average_diff_total_lengths": [],769 "average_surge_multipliers": [],770 "average_balances": [],771 "average_rejections": []772 }773def init_edges_speed():774 edge_prefix = net_info["edge_prefix"]775 for i in range(net_info["min_edge_id"],net_info["max_edge_id"] + 1):776 traci.edge.setMaxSpeed(f"{edge_prefix}{i}", random.randrange(9,21))777 traci.edge.setMaxSpeed(f"-{edge_prefix}{i}", random.randrange(9,21))778def init_random_routes(route_prefix,min_id, max_id,num_routes, factor=5):779 edge_prefix = net_info["edge_prefix"]780 for i in range(num_routes):781 route_id = f"{route_prefix}_route_{i}"782 from_edge = random.randrange(min_id,max_id + 1)783 to_edge = random.randrange(min_id,max_id + 1)784 prefix_from = "" if random_choice(0.5) else "-"785 prefix_to = "" if random_choice(0.5) else "-"786 while(abs(to_edge - from_edge) < factor):787 to_edge = random.randrange(min_id,max_id)788 route_stage = traci.simulation.findRoute(f"{prefix_from}{edge_prefix}{from_edge}",f"{prefix_to}{edge_prefix}{to_edge}")789 traci.route.add(route_id,route_stage.edges)790def run(simulator):791 print('RUN')792 step = 0793 while True:794 traci.simulationStep()795 timestamp = traci.simulation.getTime()796 step += 1797 if (step % time_update_surge == 0):798 update_surge_multiplier()799 update_drivers(timestamp)800 update_drivers_area()801 update_rides_state(timestamp, step)802 if (step % time_driver_generation) == 0:803 for area_id, area_data in areas.items():804 for i in range(area_data["generation"]["many"][1]):805 if (random_choice(area_data["generation"]["driver"])):806 create_driver(timestamp, area_id)807 808 if (step % time_customer_generation) == 0:809 for area_id, area_data in areas.items():810 for i in range(area_data["generation"]["many"][0]):811 if (random_choice(area_data["generation"]["customer"])):812 #simulator.create_customer(timestamp,customer_id_counter, area_data)813 create_customer(timestamp,area_id)814 815 816 if (step % time_dispatch) == 0:817 dispatch_rides(timestamp)818 819 if (step % time_move) == 0:820 update_drivers_movements(timestamp)821 822 if (fault_simulation and step == fault_simulation_step):823 areas["A"]["generation"]["customer"] = 0.8824 areas["A"]["generation"]["many"] = (8,3)825 print("FAULTY")826 if (peak):827 for start, end in peak_simulation:828 if (step == start):829 areas["A"]["generation"]["customer"] = 0.6830 areas["B"]["generation"]["customer"] = 0.6831 areas["C"]["generation"]["customer"] = 0.4832 areas["D"]["generation"]["customer"] = 0.4833 print("FAULTY")834 835 if (step == end):836 areas["A"]["generation"]["customer"] = 0.25837 areas["B"]["generation"]["customer"] = 0.25838 areas["C"]["generation"]["customer"] = 0.1839 areas["D"]["generation"]["customer"] = 0.1840 print("STOP FAULTY")841 842 if step == simulation_duration:843 save_simulation_statistics()844 break845 #print_area_stats()846 traci.close()847 sys.stdout.flush()848def get_options():849 optParser = optparse.OptionParser()850 optParser.add_option("--nogui", action="store_true",851 default=False, help="run the commandline version of sumo")852 options, args = optParser.parse_args()853 return options854if __name__ == "__main__":855 # first, generate the route file for this simulation856 options = get_options()857 # If you want to run this tutorial please uncomment following lines, that define the sumoBinary858 # and delete the line before traci.start, to use the gui859 if options.nogui:860 sumoBinary = checkBinary('sumo')861 else:862 sumoBinary = checkBinary('sumo-gui')863 traci.start([sumoBinary, "-c", "net_config/sumo.sumocfg",864 "--tripinfo-output", "net_config/tripinfo.xml"]) 865 print('INIT')866 init_edges_speed()867 num_random_routes = net_info["num_random_routes"]868 for area_id, area_data in areas.items():869 print(f"GENERATE ROUTES WITHIN AREA {area_id}")870 init_random_routes(f"area_{area_id}",area_data["edges"][0],area_data["edges"][1],num_random_routes)871 for i in range(10):872 if (random_choice(area_data["generation"]["driver"])):873 create_driver(0, area_id)874 for i in range(10):875 if (random_choice(area_data["generation"]["customer"])):876 create_customer(0, area_id)877 print(f"GENERATE ROUTES INTER-AREAS")878 init_random_routes(f"inter_areas", net_info["min_edge_id"],net_info["max_edge_id"],num_random_routes*5,factor=150)879 print(f"INIT SIMULATOR")880 simulator = Simulator(traci)...
archiver.py
Source:archiver.py
1"""2The Tornado Framework3By Ali Pesaranghader4University of Ottawa, Ontario, Canada5E-mail: apesaran -at- uottawa -dot- ca / alipsgh -at- gmail -dot- com6"""7import os8import zipfile9from os.path import basename10class Archiver:11 """12 This class stores results of experiments in .zip files for future reference!13 """14 @staticmethod15 def archive_single(label, stats, dir_path, name, sub_name):16 file_path = (dir_path + name + "_" + sub_name).lower()17 stats_writer = open(file_path + ".txt", 'w')18 stats_writer.write(label + "\n")19 stats_writer.write(str(stats) + "\n")20 stats_writer.close()21 zipper = zipfile.ZipFile(file_path + ".zip", 'w')22 zipper.write(file_path + ".txt", compress_type=zipfile.ZIP_DEFLATED, arcname=basename(file_path + ".txt"))23 zipper.close()24 os.remove(file_path + ".txt")25 @staticmethod26 def archive_multiple(labels, stats, dir_path, name, sub_name):27 file_path = (dir_path + name + "_" + sub_name).lower()28 stats_writer = open(file_path + ".txt", 'w')29 for i in range(0, len(labels)):30 stats_writer.write(labels[i] + "\n")31 stats_writer.write(str(stats[i]) + "\n")32 stats_writer.close()33 zipper = zipfile.ZipFile(file_path + ".zip", 'w')34 zipper.write(file_path + ".txt", compress_type=zipfile.ZIP_DEFLATED, arcname=basename(file_path + ".txt"))35 zipper.close()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!