def setUp(self): self.cell1 = Cell(1, 1, 1) self.cell2 = Cell(2, 2, 2) self.cell3 = Cell(3, 3, 3) self.cell4 = Cell(4, 4, 4) self.cell5 = Cell(5, 5, 5) self.cell6 = Cell(6, 6, 6) self.cell7 = Cell(7, 7, 7) self.cell8 = Cell(8, 8, 8) self.cell9 = Cell(9, 9, 9) pass
def check_neighbours_and_self(self, row_index, column_index): change_set = [] next_state = self.__cellular_table.next_state(row_index, column_index) if self.__cellular_table.is_alive(row_index, column_index) != next_state: change_set.append(Cell(row_index, column_index, next_state)) for neighbour in self.__cellular_table.neighbours( row_index, column_index): next_state = self.__cellular_table.next_state( neighbour[0], neighbour[1]) if self.__cellular_table.is_alive(neighbour[0], neighbour[1]) != next_state: change_set.append(Cell(neighbour[0], neighbour[1], next_state)) return change_set
def get_living_cells(self): cells = [] for row_index in range(self.row_count): for column_index in range(self.column_count): if self.is_alive(row_index, column_index): cells.append(Cell(row_index, column_index, True)) return cells
def test_getters(self): c1 = Cell(5, 5, 1, 0, 0) self.assertEqual(c1.getP1Strat(), 5) self.assertEqual(c1.getP2Strat(), 5) self.assertEqual(c1.getProb(), 1) self.assertEqual(c1.getMin(), 0) self.assertEqual(c1.getMax(), 0)
def test_place_avatar(self): m1 = Matrix("m1", 2, 2, 0, 0) m3 = Matrix("m3", 2, 2, 0, 0) c1 = Cell(0, 0, 0, 0, 0, 0) print(m1) cellLis = [c1, c1] finalLis = [cellLis, cellLis] m1.makeMatrix() m3.makeMatrix()
def import_excel(self, import_path: str): df = pandas.read_excel(import_path) cols = df["columns"].tolist() col = int(cols[0]) rows = df["rows"].tolist() row = int(rows[0]) epsilon = df["epsilon"].tolist() eps = float(epsilon[0]) delts = df["delta"].tolist() delt = float(delts[0]) its = df["iterations"].tolist() iters = int(its[0]) p1 = df["p1weights"].tolist() p2 = df["p2weights"].tolist() pays = df["payoffs"].tolist() #print(pays) x = 0 y = 0 l2 = [] while x < row: z = 0 rs = [] while z < col: strs = str(pays[y]).split(",") p1s = float(strs[0]) p2s = float(strs[1]) print(strs) print(p1s) print(p2s) rs.append(Cell(p1s, p2s, 0, 0, 0)) y += 1 z += 1 l2.append(rs) x += 1 m1 = Matrix("M1", col, row, eps, delt, l2, p1, p2) updater = MatrixUpdater(iters, []) updater.itter(m1) return updater
def __init__(self, nx, ny, goal): """ Initialize the maze grid. The maze consists of nx x ny cells and will be constructed starting at the cell indexed at (ix, iy). :param nx: number of cells in x :param ny: number of cells in y :param ix: starting point in x :param iy: starting point in y """ self.nx, self.ny = nx, ny self.ix, self.iy = 0, 0 self.maze_map = [[Cell(x, y) for y in range(ny)] for x in range(nx)] self.start = [0, 0] self.end = goal
def makeMatrix(self): #c = Cell(1, 1, 0, 0, 0) x = 0 while x < self.rows: y = 0 l = [] while y < self.columns: l.append(Cell(1, 1, 0, 0, 0)) y += 1 self.l2.append(l) x += 1
def test_setters(self): c1 = Cell(5, 5, 1, 0, 0) c1.setP1Strat(2) c1.setP2Strat(2) c1.setProb(0.5) c1.setMax(0.1) c1.setMin(0.01) self.assertEqual(c1.getP1Strat(), 2) self.assertEqual(c1.getP2Strat(), 2) self.assertEqual(c1.getProb(), 0.5) self.assertEqual(c1.getMax(), 0.1) self.assertEqual(c1.getMin(), 0.01)
def submitPayoffs(): global m4 global mUpdater delta = getDelta() epsilon = getError() rows = getUserRows() cols = getUserCols() iterations = getGamesPlayed() m4.setDelta(delta) m4.setEpsilon(epsilon) mUpdater.setIterations(iterations) i = 1 p2weightlist = [] p1weightlist = [] p1Payoff = [] p2Payoff = [] while i < 25: curText = labelArray[i].get("1.0", "end") if curText != '' and curText != '\n': if i == 1 or i == 2 or i == 3 or i == 4: curWeight = float(curText) p2weightlist.append(curWeight) elif i == 5 or i == 10 or i == 15 or i == 20: curWeight = float(curText) p1weightlist.append(curWeight) else: p1Payoff.append(float(curText[0])) p2Payoff.append(float(curText[2])) i += 1 l2 = [] x = 0 y = 0 while x < rows: z = 0 l3 = [] while z < cols: l3.append(Cell(p1Payoff[y], p2Payoff[y], 0, 0, 0)) y += 1 z += 1 l2.append(l3) x += 1 m4.setp1Weights(p1weightlist) m4.setp2Weights(p2weightlist) m4.setRows(rows) m4.setCols(cols) mUpdater.itter(m4) global slider # Creates a slider where max value is number of iterations in a game slider = tk.Scale(root, from_=1, to=mUpdater.getIterations(), length=500, orient=tk.HORIZONTAL, label="Number of Games", command=getIterationsFromSlider) slider.place(relx=0, rely=.9)
def reset(self): """ Reset the maze by creating again every cells. """ self.maze_map = [[Cell(x, y) for y in range(self.ny)] for x in range(self.nx)]
def read_layer_txt(name: str) -> Layer: example = None if name == "game of life": example = name name = "Examples/Game of Life/text/layer1.txt" elif name == "Langton ant": example = name name = "Examples/Langton Ant/text/layer1.txt" elif name == "wireworld": example = name name = "Examples/Wire World/text/layer1.txt" with open(name) as reader: name = reader.readline().strip() size = reader.readline().strip().split() height = int(size[1]) width = int(size[0]) cells = [[0 for _ in range(width)] for _ in range(height)] cells_states = [] neighbourhood = reader.readline().strip() # read grid reader.readline() for y in range(height): line = reader.readline().strip() for x in range(width): cells[y][x] = int(line[x]) # read cells states reader.readline() line = reader.readline() cells_states.append(("nothing", "white")) while line != "": line = line.strip().split() cells_states.append((line[0], line[1])) line = reader.readline() # read cells values line = reader.readline() values = list() while line != "": line = line.strip() values.append(float(line)) line = reader.readline() # Create layer with all parameters cells_class: List = list() i = 0 for y in range(height): cells_class.append(list()) for x in range(width): # Check if values have been declared if len(values) == 0: cells_class[y].append(Cell(cells[y][x], example=example)) else: # Check if current cell had value declared if cells[y][x] != 0: cells_class[y].append(Cell(cells[y][x], value=values[i], example=example)) i += 1 else: cells_class[y].append(Cell(cells[y][x], example=example)) layer = Layer(name, cells_class, neighbourhood, cells_states) return layer
def read_layer_excel(name: str) -> List[Layer]: example = None if name == "forest fire": example = name name = "Examples/Forest Fire/excel/example.xlsx" elif name == "game of life": example = name name = "Examples/Game of Life/excel/example.xlsx" elif name == "Langton ant": example = name name = "Examples/Langton Ant/excel/example.xlsx" elif name == "wireworld": example = name name = "Examples/Wire World/excel/example.xlsx" workbook = load_workbook(filename=name) sheets_names = workbook.get_sheet_names() layers: List[Layer] = list() for sheet_name in sheets_names: sheet = workbook.get_sheet_by_name(sheet_name) name = sheet_name height = int(sheet["B1"].value) width = int(sheet["A1"].value) neighbourhood = sheet["A2"].value cells_states = [] cells = [[tuple() for _ in range(width)] for _ in range(height)] # read cell types cells_states.append(("nothing", "white")) for row in range(height + 5, sheet.max_row + 1): if sheet.cell(row, 1).value is None: break type_name = str(sheet.cell(row, 1).value) color_hex = sheet.cell(row, 1).fill.start_color.value color_hex = "#" + color_hex[2:] cells_states.append((type_name, color_hex)) # read cell value and type for row in range(4, sheet.max_row): if sheet.cell(row, 1).value is None: break for col in range(1, sheet.max_column + 1): if sheet.cell(row, col).value is None: break cell_value = float(sheet.cell(row, col).value) color_hex = sheet.cell(row, col).fill.start_color.value color_hex = "#" + color_hex[2:] cells[row - 4][col - 1] = (cell_value, color_hex) # Create cell objects cells_class: List[List[Cell]] = list(list()) for y in range(height): cells_class.append(list()) for x in range(width): cells_class[y].append(Cell(0, value=cells[y][x][0], example=example)) for typee in range(len(cells_states)): if cells_states[typee][1] == cells[y][x][1]: cells_class[y][x].current_state = typee layer = Layer(name, cells_class, neighbourhood, cells_states) layers.append(layer) # Reading sliding window rules - optional rules = [] rule_size = int((sheet.max_row - (height + 3 + len(cells_states))) / 4) for i in range(rule_size): match, result = [], [] if neighbourhood == "von_neumann": match = read_von_neumann_excel(sheet, height + 7 + i * 4, 0, cells_states) result = read_von_neumann_excel(sheet, height + 7 + i * 4, 5, cells_states) elif neighbourhood == "moore": match = read_moore_excel(sheet, height + 7 + i * 4, 1, cells_states) result = read_moore_excel(sheet, height + 7 + i * 4, 5, cells_states) rule = (match, result) rules.append(rule) layer.rules = rules return layers
class CellTest(unittest.TestCase): def setUp(self): self.log = logging.getLogger(__name__) self.cell = Cell(1, 2, 3) def test_CellInit(self): self.log.debug("test_CellInit") wall_list = self.cell.wallList self.assertEqual(len(wall_list), 4, 'Did not find 4 walls.') for i in range(4): self.assertFalse(wall_list[i].is_removed()) self.assertEqual(self.cell.leftWall, wall_list[0], 'Left wall was not in first place in list.') self.assertEqual(self.cell.rightWall, wall_list[1], 'Right wall was not in second place in list.') self.assertEqual(self.cell.topWall, wall_list[2], 'Top wall was not in third place in list.') self.assertEqual(self.cell.bottomWall, wall_list[3], 'Bottom wall was not in fourth place in list.') self.assertEqual(self.cell.x, 1, 'X-coordinate is wrong.') self.assertEqual(self.cell.y, 2, 'y-coordinate is wrong.') self.assertEqual(self.cell.set, 3, 'Set is wrong.') def test_setSet(self): self.log.debug("test_setSet") self.cell.set_set(2) self.assertEqual(self.cell.set, 2, 'Cell has wrong Set.') def test_getSet(self): self.log.debug("test_getSet") self.assertEqual(self.cell.set, self.cell.get_set(), 'get_set returned wrong value.') def test_getX(self): self.log.debug("test_getX") self.assertEqual(self.cell.x, self.cell.get_x(), 'get_x returned wrong value.') def test_getY(self): self.log.debug("test_getY") self.assertEqual(self.cell.y, self.cell.get_y(), 'get_y returned wrong value.') def test_getLeft(self): self.log.debug("test_getLeft") self.assertEqual(self.cell.leftWall, self.cell.get_left(), 'get_left returned wrong value.') def test_setLeft(self): self.log.debug("test_setLeft") wall = Wall() self.cell.set_left(wall) self.assertEqual(self.cell.leftWall, wall, 'Left wall is set incorrectly.') def test_getRight(self): self.log.debug("test_getRight") self.assertEqual(self.cell.rightWall, self.cell.get_right(), 'get_right returned wrong value.') def test_setRight(self): self.log.debug("test_setRight") wall = Wall() self.cell.set_right(wall) self.assertEqual(self.cell.rightWall, wall, 'Right wall is set incorrectly.') def test_getTop(self): self.log.debug("test_getTop") self.assertEqual(self.cell.topWall, self.cell.get_top(), 'get_top returned wrong value.') def test_setTop(self): self.log.debug("test_setTop") wall = Wall() self.cell.set_top(wall) self.assertEqual(self.cell.topWall, wall, 'Top wall is set incorrectly.') def test_getBottom(self): self.log.debug("test_getBottom") self.assertEqual(self.cell.bottomWall, self.cell.get_bottom(), 'get_bottom returned wrong value.') def test_setBottom(self): self.log.debug("test_setBottom") wall = Wall() self.cell.set_bottom(wall) self.assertEqual(self.cell.bottomWall, wall, 'Bottom wall is set incorrectly.') def test_removeLeft(self): self.log.debug("test_removeLeft") self.cell.remove_left() self.assertTrue(self.cell.leftWall.is_removed(), 'Left wall is not removed.') def test_createLeft(self): self.log.debug("test_createLeft") self.cell.create_left() self.assertFalse(self.cell.leftWall.is_removed(), 'Left wall is removed.') def test_removeRight(self): self.log.debug("test_removeRight") self.cell.remove_right() self.assertTrue(self.cell.rightWall.is_removed(), 'Right wall is not removed.') def test_createRight(self): self.log.debug("test_createRight") self.cell.create_right() self.assertFalse(self.cell.rightWall.is_removed(), 'Right wall is removed.') def test_removeTop(self): self.log.debug("test_removeTop") self.cell.remove_top() self.assertTrue(self.cell.topWall.is_removed(), 'Top wall is not removed.') def test_createTop(self): self.log.debug("test_createTop") self.cell.create_top() self.assertFalse(self.cell.topWall.is_removed(), 'Top wall is removed.') def test_removeBottom(self): self.log.debug("test_removeBottom") self.cell.remove_bottom() self.assertTrue(self.cell.bottomWall.is_removed(), 'Bottom wall is not removed.') def test_createBottom(self): self.log.debug("test_createBottom") self.cell.create_bottom() self.assertFalse(self.cell.bottomWall.is_removed(), 'Bottom wall is removed.') def test_hasWall(self): self.log.debug("test_hasWall") self.assertTrue(self.cell.has_wall()) self.cell.remove_left() self.cell.remove_right() self.cell.remove_top() self.cell.remove_bottom() self.assertFalse(self.cell.has_wall(), 'Cell still has walls.') def test_wallCount(self): self.log.debug("test_wallCount") self.assertEqual(self.cell.wall_count(), 4, 'Cell does not have correct number of walls.') self.cell.remove_left() self.assertEqual(self.cell.wall_count(), 3, 'Cell does not have correct number of walls.') self.cell.remove_right() self.assertEqual(self.cell.wall_count(), 2, 'Cell does not have correct number of walls.') self.cell.remove_top() self.assertEqual(self.cell.wall_count(), 1, 'Cell does not have correct number of walls.') self.cell.remove_bottom() self.assertEqual(self.cell.wall_count(), 0, 'Cell does not have correct number of walls.') def test_chooseWallSuccess(self): self.log.debug("test_chooseWallSuccess") wall = self.cell.choose_wall() self.assertFalse(wall.is_removed(), 'Removed wall has been chosen.') def test_chooseWallFail(self): self.log.debug("test_chooseWallFail") self.cell.remove_left() self.cell.remove_right() self.cell.remove_top() self.cell.remove_bottom() wall = self.cell.choose_wall() self.assertIsNone(wall, 'A wall could be chosen.')
def setUp(self): self.log = logging.getLogger(__name__) self.cell = Cell(1, 2, 3)