def run_model(f_grid, h_grid, i_threshold, w_direction, burn_seeds): '''Takes in arguemnts and returns final state of cells and total number of cells burnt''' pass length = len(f_grid) # Creates a initial b_grid which is all False b_grid = [] for i in range(length): grid = [] for j in range(length): grid.append(False) b_grid.append(grid) for b_cell in burn_seeds: if not b_cell: burning = False burning = True b_grid[b_cell[0]][b_cell[1]] = True # While loop keeps iterating until no cells are burning count = 0 while burning: # Creating a duplicate b_grid nb_grid = [] q = 0 for a in range(length): gri = [] for b in range(length): gri.append(False) nb_grid.append(gri) for i in range(length): for j in range(length): if b_grid[i][j] is False and f_grid[i][j] != 0: nb_grid[i][j] = check_ignition(b_grid, f_grid, h_grid, i_threshold, w_direction, i, j) elif b_grid[i][j] is True: if f_grid[i][j] != 0: f_grid[i][j] -= 1 nb_grid[i][j] = True else: count += 1 b_grid = nb_grid # Checking whether all values of b_grid are False or not for item in nb_grid: for thing in item: if thing is True: q += 1 if q == 0: burning = False return (f_grid, count)
def run_model(f_grid, h_grid, i_threshold, w_direction, burn_seeds): '''Takes in the current state of the landscape and returns a tuple containing (a) the final state of the landscape after the fire has stopped burning, and (b) the total number of cells that have been burnt.''' # Case where nothing is burning if not burn_seeds: return (f_grid, 0) # Keep track of how many cells have been burnt burnt_cells = set(burn_seeds) # Iterate until nothing is burning while burn_seeds: # Create b_grid from burn_seeds size = len(f_grid) b_grid = [] for x in range(size): row = [] for y in range(size): if (x, y) in burn_seeds: row.append(True) else: row.append(False) b_grid.append(row) # Update b_grid and f_grid for the next time step new_b_grid = [] for x in range(size): row = [] for y in range(size): # check ignition function for cells not currently burning if (x, y) not in burn_seeds: row.append(check_ignition( b_grid, f_grid, h_grid, i_threshold, w_direction, x, y)) # update fuel of burning cells and see if they continue burning else: f_grid[x][y] -= 1 if f_grid[x][y] == 0: row.append(False) else: row.append(True) new_b_grid.append(row) b_grid = new_b_grid # Update burn seeds for next time step and keep track of cells burnt burn_seeds = [] for x in range(size): for y in range(size): if b_grid[x][y]: burn_seeds.append((x, y)) burnt_cells.add((x, y)) return (f_grid, len(burnt_cells))
def next_time(f_grid, h_grid, i_threshold, w_direction, burn_seeds, burnt_seeds): ''' takes in arguments of grid of current fuel load, height grid, ignition threshold, wind direction, coordinates of all currently burning cells, and an empty list that will contain all burnt cells. This function returns the fuel load and all the cells that are burning at time t+1 ''' b_grid = create_bgrid(f_grid, burn_seeds) new_burn_seeds = [] # newly burning cells for i in range(len(f_grid)): for j in range(len(f_grid)): if (i, j) not in burn_seeds: if check_ignition(b_grid, f_grid, h_grid, i_threshold, w_direction, i, j) is True: new_burn_seeds.append((i, j)) for item in burn_seeds: # increment fuel load of burning cells f_grid[item[0]][item[1]] -= 1 if f_grid[item[0]][item[1]] == 0: burnt_seeds.add(item) burn_seeds = list(set(burn_seeds).difference(burnt_seeds)) burn_seeds += new_burn_seeds if len(burn_seeds) == 0: # an indication when no more cells are burning return None, None return f_grid, burn_seeds
def run_model_with_burn_count(f_grid, h_grid, i_threshold, w_direction, burn_seeds, burnt_cell_count): """ returns a tuple containing (a) the final state of the landscape once the fire has stopped burning, and (b) the total number of cells that have been burnt by the fire (including any initially burning cells in burn_seeds).""" m = len(f_grid) # creates b_grid: a list of lists representing what is currently burning b_grid = [] for i_num in range(m): i_row = [] for j_num in range(m): if (i_num, j_num) in burn_seeds: i_row.append(True) else: i_row.append(False) b_grid.append(i_row) # creates a list of cells that are adjacent to the burning seeds adjacent_cells = [] for seed in burn_seeds: for i_num in adjacency_range(m, seed[0]): for j_num in adjacency_range(m, seed[1]): adjacent_cells.append([i_num, j_num]) # reduces fuel load where there are burnt seeds for seed in burn_seeds: if f_grid[seed[0]][seed[1]] > 0: f_grid[seed[0]][seed[1]] -= 1 # creates a new list of burn_seeds consisting if adjacent cells that have # ignited. Cells are considered ignited if they have if they still have # fuel, are not currently in burn_seeds, and fulfils check_ignition # conditions new_burn_seeds = [] for cell in adjacent_cells: if (check_ignition(b_grid, f_grid, h_grid, i_threshold, w_direction, cell[0], cell[1]) and f_grid[cell[0]][cell[1]] > 0 and (cell[0], cell[1]) not in burn_seeds and (cell[0], cell[1]) not in new_burn_seeds): new_burn_seeds.append((cell[0], cell[1])) for seed in burn_seeds: if f_grid[seed[0]][seed[1]] > 0: new_burn_seeds.append((seed[0], seed[1])) else: burnt_cell_count += 1 if new_burn_seeds: return run_model_with_burn_count(f_grid, h_grid, i_threshold, w_direction, new_burn_seeds, burnt_cell_count) else: return (f_grid, burnt_cell_count)
def b_state_fuel(ij_list, cell_burnt_dict, cell_burnt, b_grid, f_grid, h_grid, i_threshold, w_direction, burn_seeds): """ Update all the burning states of the cell & fuel load for t + 1 and also check if it will ignite to prepare for updating the burning states of cell & fuel load of the cell for t + 2 onwards""" # Loop through all the coordinates and determine the new burning state # and fuel load for coordinates in ij_list: i = coordinates[0] j = coordinates[1] # Get the original f_grid before looping for comparison later # To compare whether the f_grid has changed value original_f_grid = f_grid[i][j] # Check for each coordinates whether it will ignite result = check_ignition(b_grid, f_grid, h_grid, i_threshold, w_direction, i, j) # For burning cells that are still burning and fuel load more than 0 # - 1 its value. AND if the burning cell is still burning and no more # fuel load then burning cell should stop burning if b_grid[i][j] is True and f_grid[i][j] > 0: f_grid[i][j] -= 1 elif b_grid[i][j] is True and f_grid[i][j] == 0: b_grid[i][j] = False # If the cell ignites and has a fuel load of more than 0 # then we will set the burning cell to burn for t + 1 # and if the cell ignites but no more fuel load, the burning cell # should not burn anymore # If the cell fails to ignite and the fuel_load values change which # means the burning cell is still burning if result is True: # if the cell has not be burned before then add it as burned cell if cell_burnt_dict[(i, j)] is False and b_grid[i][j] is False: cell_burnt += 1 cell_burnt_dict[(i, j)] = True if f_grid[i][j] > 0: b_grid[i][j] = True elif f_grid[i][j] == 0: b_grid[i][j] = False elif result is False: if original_f_grid != f_grid[i][j] and f_grid[i][j] > 0: b_grid[i][j] = True return f_grid, cell_burnt, b_grid
def run_model(f_grid, h_grid, i_threshold, w_direction, burn_seeds): ''' return final fuel state of landscape and total cells burned ''' m = len(f_grid) b = create_b(m, burn_seeds) f = f_grid coordinates = [(i, j) for i in range(m) for j in range(m)] count = set() count.update(burn_seeds) # run model until b_grid is all False (no cell is burning) while b != [[False] * m for dimension in range(m)]: catch_fire_cells = [] burning_cells = [] # classify each coordinate into a list from above for (i, j) in coordinates: if b[i][j]: if (i, j) not in burning_cells: burning_cells.append((i, j)) if not b[i][j]: if check_ignition(b, f, h_grid, i_threshold, w_direction, i, j): catch_fire_cells.append((i, j)) # burning cells fuel load -1 and evaluate to False if fuel = 0 after -1 for (i, j) in burning_cells: if f[i][j] - 1 > 0: b[i][j] = True if f[i][j] -1 == 0: b[i][j] = False f[i][j] -= 1 # cells that catch fire at t+1 evaluate to True # add to count of cells burned for (i, j) in catch_fire_cells: b[i][j] = True count.add((i, j)) return (f, len(count))
def run_model(f_grid, h_grid, i_threshold, w_direction, burn_seeds): size = len(f_grid) # Keep a set of all (i, j) pairs that have been burnt by fire. burnt = set() # Compute the initial burn grid. b_grid = [[False] * size for _ in range(size)] for i, j in burn_seeds: b_grid[i][j] = True burnt.add((i, j)) # Run the simulation while at least one cell is burning. while any(itertools.chain.from_iterable(b_grid)): new_b_grid = copy.deepcopy(b_grid) new_f_grid = copy.deepcopy(f_grid) # Work out what new cell should ignite. for i in range(size): for j in range(size): if check_ignition(b_grid, f_grid, h_grid, i_threshold, w_direction, i, j): new_b_grid[i][j] = True burnt.add((i, j)) # Decrease fuel for all currently burning cells. for i in range(size): for j in range(size): if b_grid[i][j]: new_f_grid[i][j] -= 1 if new_f_grid[i][j] == 0: new_b_grid[i][j] = False # Update our burn state for the next iteration. b_grid = new_b_grid f_grid = new_f_grid return (f_grid, len(burnt))
def update_state(b_grid, f_grid, h_grid, i_threshold, w_direction): # create matrices to store next state b_grid_next = [[x for x in y] for y in b_grid] f_grid_next = [[x for x in y] for y in f_grid] ignitions = 0 grid_size = len(b_grid) # update each cell in turn for i in range(grid_size): for j in range(grid_size): # handle fuel depletion if b_grid[i][j]: f_grid_next[i][j] -= 1 # extinguish fire at (i, j) if fuel depleted if f_grid_next[i][j] == 0: b_grid_next[i][j] = False # check for new ignitions else: new_ignition = check_ignition(b_grid, f_grid, h_grid, i_threshold, w_direction, i, j) if new_ignition: ignitions += 1 b_grid_next[i][j] = True return b_grid_next, f_grid_next, ignitions