def test_should_3_live(self): tensor = torch.tensor([[0, 0, 1], [0, 0, 1], [1, 0, 0]]) target = torch.tensor([[0, 0, 0], [0, 1, 0], [0, 0, 0]]) cells = Cells(tensor) cells.update() self.assertTrue(torch.equal(target, cells.matrix.type(torch.LongTensor)))
def test_should_more_die(self): """ 如果周围细胞数量大于3 细胞会死亡 """ tensor = torch.tensor([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) target = torch.tensor([[1, 0, 1], [0, 0, 0], [1, 0, 1]]) cells = Cells(tensor) cells.update() self.assertTrue(torch.equal(target, cells.matrix))
class MyDrawer(Drawer): def __init__(self, row_num, col_num, caption_text, width, height, data): super().__init__(row_num, col_num, caption_text, width, height) self.cells = Cells(data) def change_status(self): self.rects.load(self.cells.matrix) self.cells.update() def set_data(self, data): for i in range(len(data)): for j in range(len(data[0])): self.cells.matrix[i][j] = data[i][j]
class MyDrawer(Drawer): def __init__(self, caption_text, width, height, data): row_num, col_num = data.shape super().__init__(row_num, col_num, caption_text, width, height) self.cells = Cells(data) def change_status(self): self.rects.load(self.cells.matrix) self.cells.update() def set_data(self, data): for i in range(len(data)): for j in range(len(data[0])): self.cells.matrix[i][j] = data[i][j] def open_file(self): from tkinter import filedialog path = filedialog.askopenfilename() self.cells.load_from_file(path) return self.cells.shape
def test_load_from_file(self): # TODO: 需要重构 构造函数 cells = Cells(torch.tensor(0)) filename = '../patterns/test.rle' cells.load_from_file(filename) target = torch.zeros(3, 10) ones = [ [0, 7], [0, 8], [1, 3], [1, 5], [1, 8], [2, 0], [2, 1], [2, 5], [2, 6] ] for i, j in ones: target[i][j] = 1 self.assertTrue(torch.equal(target, cells.matrix))
def test_should_2_keep(self): tensor = torch.tensor([[0, 0, 0], [1, 0, 1], [0, 0, 0]]) target = torch.tensor([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) cells = Cells(tensor) cells.update() self.assertTrue(torch.equal(target, cells.matrix)) tensor = torch.tensor([[0, 1, 0], [1, 0, 1], [0, 0, 0]]) target = torch.tensor([[0, 1, 0], [0, 1, 0], [0, 0, 0]]) cells = Cells(tensor) cells.update() self.assertTrue(torch.equal(target, cells.matrix))
def __init__(self, caption_text, width, height, data): row_num, col_num = data.shape super().__init__(row_num, col_num, caption_text, width, height) self.cells = Cells(data)
from src.cells import Cells import torch data = torch.zeros(10, 10) data[0][2] = 1 data[1][2] = 1 data[2][2] = 1 data[2][1] = 1 data[1][0] = 1 cells = Cells(data) for _ in range(10): print(cells.matrix) cells.update()
from src.cells import Cells import time import torch cells = Cells(torch.tensor(1)) cells.load_from_file(r'..\..\patterns\gunstar.rle') start = time.time() for _ in range(1000): cells.update() print(time.time() - start)