Last active
December 20, 2021 04:19
-
-
Save fernandotenorio/f08648fce23b87b2ba9ded813cb25922 to your computer and use it in GitHub Desktop.
Creates a room for the CARoom simulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from CA import CARoom, CACell | |
from itertools import product | |
from random import random, Random | |
import multiprocessing as mp | |
class CustomRoom(object): | |
@staticmethod | |
def make_2_obstacle_room(full_factor=0.67, pos_seed=None, panic_prob=0.01, exit_size=2): | |
room_w = 20 | |
room_h = 20 | |
room = CARoom(room_w, room_h) | |
# set the external top/bottom walls | |
for row in [0, room.ny - 1]: | |
for col in range(room.nx): | |
room.setCell((row, col), CACell(CACell.OBSTACLE_STATE, {})) | |
# set the external left/right walls | |
for col in [0, room.nx - 1]: | |
for row in range(room.ny): | |
room.setCell((row, col), CACell(CACell.OBSTACLE_STATE, {})) | |
# Exits | |
half_rows = int(room.ny/2) | |
for i in range(exit_size): | |
room.setCell((half_rows + i, room.nx - 1), CACell(CACell.EXIT_STATE, {})) | |
# setup first obstacle block | |
obstacle_size = 8 | |
col = int(room.nx * 6/10) | |
row = 1 + int(room.ny/3) - obstacle_size | |
row_cols = product(range(row, row + obstacle_size), range(col, col + obstacle_size)) | |
for r_c in row_cols: | |
room.setCell(r_c, CACell(CACell.OBSTACLE_STATE, {})) | |
# setup second obstacle block | |
row = int(room.ny) - obstacle_size - row | |
row_cols = product(range(row, row + obstacle_size), range(col, col + obstacle_size)) | |
for r_c in row_cols: | |
room.setCell(r_c, CACell(CACell.OBSTACLE_STATE, {})) | |
# setup middle wall obstacle | |
for c in range(col, col + obstacle_size): | |
room.setCell((half_rows, c), CACell(CACell.OBSTACLE_STATE, {})) | |
# fill the remaing empty cells with a person, with a probability of capacity_factor | |
people = 0 | |
r = Random(pos_seed) | |
for i in range(room.ny): | |
for j in range(room.nx): | |
if room.getCell((i, j)).state == CACell.EMPTY_STATE and r.random() < full_factor: | |
fill = (r.randint(0, 255), r.randint(0, 255), r.randint(0, 255)) | |
room.setCell((i, j), CACell(CACell.PERSON_STATE, {'fill': fill, 'panic_prob': panic_prob})) | |
people+=1 | |
return room, people |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment