Question: can you edit the python code below according to the question in the screenshot. import math from matplotlib import pyplot as plt , colors from

can you edit the python code below according to the question in the screenshot.
import math
from matplotlib import pyplot as plt, colors
from matplotlib.animation import FuncAnimation
import random
import numpy as np
from matplotlib.patches import Polygon
rand = random.Random()
LANE_WIDTH =30
NUM_LANES =2
HIGHWAY_LENGTH =150
HIGHWAY_WIDTH =40
INTERARRIVAL =6
CAR_SPEED =3
total_cars =0
new_cars =0
total_in_highway =0
total_cars_list =[]
new_cars_list =[]
colourmap = colors.ListedColormap(["lightgrey", "blue"])
normalizer = colors.Normalize(vmin=0.0, vmax=1.0)
class Car:
def __init__(self, id, highway):
self.id = id
self.active = False
self.highway = highway
self.lane = None
self.position = None
if rand.choice([True, False]):
self.start_lane =0
else:
self.start_lane =1
self.start_position =0
def enter_highway(self, lane, position):
if self.highway.enter_highway(self, lane, position):
self.active = True
def step(self):
if self.position HIGHWAY_LENGTH -1:
self.highway.move_car(self, self.lane, self.position +1)
else:
self.highway.leave_highway(self)
def __str__(self):
return "id: %d lane: %d position: %d"%(self.id, self.lane, self.position)
class Highway:
def __init__(self):
self.storage = HighwayGrid()
self.bitmap =[[0.0 for _ in range(HIGHWAY_LENGTH)] for _ in range(HIGHWAY_WIDTH)]
self.time_steps =[0,0]
def enter_highway(self, car, lane, position):
global total_cars
if not (0= lane NUM_LANES):
print("Invalid lane!")
return False
if self.storage.isoccupied(lane, position):
print("Move rejected: occupied")
return False
self.storage.add_car(lane, position, car)
car.lane = lane
car.position = position
total_cars +=1
return True
def leave_highway(self, car):
self.storage.remove_car(car)
def move_car(self, car, lane, position):
if position HIGHWAY_LENGTH:
if not self.storage.isoccupied(lane, position):
self.storage.move_car(lane, car.position, position, car)
car.position = position
def run_step(self, time_step):
for lane in range(NUM_LANES):
if rand.random()1/ INTERARRIVAL:
car = Car(total_cars +1, self)
car.enter_highway(lane, car.start_position)
self.time_steps[lane]=0
total_cars_list.append(total_cars)
new_cars_list.append(new_cars)
self.time_steps[lane]+=1
for car in self.storage.get_list():
if car.active:
if rand.random() CAR_SPEED:
car.step()
def refresh_image(self):
# Clear the bitmap
self.bitmap =[[0.0 for _ in range(HIGHWAY_LENGTH)] for _ in range(HIGHWAY_WIDTH)]
for position in range(HIGHWAY_LENGTH):
self.bitmap[15][position]=1.0
self.bitmap[25][position]=1.0
if position %5==0:
self.bitmap[20][position]=1.0
for car in self.storage.get_list():
lane = car.lane
position = car.position
self.bitmap[17+ lane *5][position]=1.0
def isoccupied(self, lane, position):
return self.storage.isoccupied(lane, position)
class HighwayGrid:
def __init__(self):
self.grid =[[None for _ in range(HIGHWAY_LENGTH)] for _ in range(NUM_LANES)]
def isoccupied(self, lane, position):
return self.grid[lane][position] is not None
def add_car(self, lane, position, car):
if self.grid[lane][position] is None:
self.grid[lane][position]= car
return True
return False
def move_car(self, lane, old_position, new_position, car):
self.grid[lane][old_position]= None
self.grid[lane][new_position]= car
def remove_car(self, car):
lane = car.lane
position = car.position
self.grid[lane][position]= None
def get_list(self):
cars =[]
for lane in self.grid:
for car in lane:
if car is not None:
cars.append(car)
return cars
hw = Highway()
display = plt.figure(figsize=(15,4))
image = plt.imshow(hw.bitmap, cmap=colourmap, norm=normalizer, animated=True)
t =0
def updatefigure(*args):
global t
t +=1
hw.run_step(t)
hw.refresh_image()
image.set_array(hw.bitmap)
return image,
anim = FuncAnimation(display, updatefigure, frames=2000, interval=50, blit=True, repeat=False)
plt.show()
can you edit the python code below according to

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!