def __init__(self, recipes_path, seed=0): self.cookbook = Cookbook(recipes_path) self.n_features = \ 2 * WINDOW_WIDTH * WINDOW_HEIGHT * self.cookbook.n_kinds + \ self.cookbook.n_kinds + \ 4 + \ 1 self.n_actions = N_ACTIONS self.non_grabbable_indices = self.cookbook.environment self.grabbable_indices = [ i for i in range(self.cookbook.n_kinds) if i not in self.non_grabbable_indices ] self.workshop_indices = [ self.cookbook.index["workshop%d" % i] for i in range(N_WORKSHOPS) ] self.water_index = self.cookbook.index["water"] self.stone_index = self.cookbook.index["stone"] self.random = np.random.RandomState(seed)
def _loadCookbook(self, *args, **kwargs): for name in args: cookbook = None for origpath, path in reversed(self._cookbookPaths): fullpath = os.path.join(path, name) if not os.path.exists(fullpath): continue cookbook = Cookbook.loadFromPath(name, fullpath) break if not cookbook: raise ImportError("Cookbook '%s' not found" % name) self._registerCookbook(cookbook)
class CraftWorld(object): def __init__(self, recipes_path, seed=0): self.cookbook = Cookbook(recipes_path) self.n_features = \ 2 * WINDOW_WIDTH * WINDOW_HEIGHT * self.cookbook.n_kinds + \ self.cookbook.n_kinds + \ 4 + \ 1 self.n_actions = N_ACTIONS self.non_grabbable_indices = self.cookbook.environment self.grabbable_indices = [ i for i in range(self.cookbook.n_kinds) if i not in self.non_grabbable_indices ] self.workshop_indices = [ self.cookbook.index["workshop%d" % i] for i in range(N_WORKSHOPS) ] self.water_index = self.cookbook.index["water"] self.stone_index = self.cookbook.index["stone"] self.random = np.random.RandomState(seed) def sample_scenario_with_goal(self, goal): assert goal not in self.cookbook.environment ingredients = self.cookbook.primitives_for(goal) make_island = (goal == self.cookbook.index["gold"] or self.cookbook.index["gold"] in ingredients) make_cave = (goal == self.cookbook.index["gem"] or self.cookbook.index["gem"] in ingredients) if (goal not in self.cookbook.primitives and goal not in self.cookbook.recipes): raise ValueError("Don't know how to build a scenario for %s" % goal) return self.sample_scenario(make_island=make_island, make_cave=make_cave) def sample_scenario(self, make_island=False, make_cave=False): # generate grid grid = np.zeros((WIDTH, HEIGHT, self.cookbook.n_kinds)) i_bd = self.cookbook.index["boundary"] grid[0, :, i_bd] = 1 grid[WIDTH - 1:, :, i_bd] = 1 grid[:, 0, i_bd] = 1 grid[:, HEIGHT - 1:, i_bd] = 1 # treasure if make_island or make_cave: (gx, gy) = (1 + self.random.randint(WIDTH - 2), 1) treasure_index = \ self.cookbook.index["gold"] if make_island else self.cookbook.index["gem"] wall_index = \ self.water_index if make_island else self.stone_index grid[gx, gy, treasure_index] = 1 for i in range(-1, 2): for j in range(-1, 2): if not grid[gx + i, gy + j, :].any(): grid[gx + i, gy + j, wall_index] = 1 # ingredients for primitive in self.cookbook.primitives: if (primitive == self.cookbook.index["gold"] or primitive == self.cookbook.index["gem"]): continue for i in range(4): (x, y) = random_free(grid, self.random) grid[x, y, primitive] = 1 # generate crafting stations for i_ws in range(N_WORKSHOPS): ws_x, ws_y = random_free(grid, self.random) grid[ws_x, ws_y, self.cookbook.index["workshop%d" % i_ws]] = 1 # generate init pos init_pos = random_free(grid, self.random) return CraftScenario(grid, init_pos, self) def visualize(self, transitions): def _visualize(win): curses.start_color() for i in range(1, 8): curses.init_pair(i, i, curses.COLOR_BLACK) curses.init_pair(i + 10, curses.COLOR_BLACK, i) states = [transitions[0].s1] + [t.s2 for t in transitions] mstates = [transitions[0].m1] + [t.m2 for t in transitions] for state, mstate in zip(states, mstates): win.clear() for y in range(HEIGHT): for x in range(WIDTH): if not (state.grid[x, y, :].any() or (x, y) == state.pos): continue thing = state.grid[x, y, :].argmax() if (x, y) == state.pos: if state.dir == LEFT: ch1 = "<" ch2 = "@" elif state.dir == RIGHT: ch1 = "@" ch2 = ">" elif state.dir == UP: ch1 = "^" ch2 = "@" elif state.dir == DOWN: ch1 = "@" ch2 = "v" color = curses.color_pair(mstate.arg or 0) elif thing == self.cookbook.index["boundary"]: ch1 = ch2 = curses.ACS_BOARD color = curses.color_pair(10 + thing) else: name = self.cookbook.index.get(thing) ch1 = name[0] ch2 = name[-1] color = curses.color_pair(10 + thing) win.addch(HEIGHT - y, x * 2, ch1, color) win.addch(HEIGHT - y, x * 2 + 1, ch2, color) win.refresh() time.sleep(1) curses.wrapper(_visualize)
from cookbook import Cookbook print('Mary Beth Doyle/home/ubuntu/git/SensorDat.') mbd = Cookbook('/home/ubuntu/git/SensorDat/example/mary-beth-doyle.jsonld') mbd.run() del mbd print('Ellsworth South Inlet/home/ubuntu/git/SensorDat.') ells_s = Cookbook('/home/ubuntu/git/SensorDat/example/ellsworth_south_inlet.jsonld') ells_s.run() del ells_s print('Ellsworth North Inlet/home/ubuntu/git/SensorDat.') ells_n = Cookbook('/home/ubuntu/git/SensorDat/example/ellsworth_north_inlet.jsonld') ells_n.run() del ells_n print('Flagstar Bank/home/ubuntu/git/SensorDat.') flagstar = Cookbook('/home/ubuntu/git/SensorDat/example/flagstar-bank.jsonld') flagstar.run() del flagstar print('Hilton Garden Inn/home/ubuntu/git/SensorDat.') hilton = Cookbook('/home/ubuntu/git/SensorDat/example/hilton-garden-inn.jsonld') hilton.run() del hilton print('Kapnick Insurance Group/home/ubuntu/git/SensorDat.') kapnick = Cookbook('/home/ubuntu/git/SensorDat/example/kapnick-insurance-group.jsonld') kapnick.run() del kapnick
def __init__(self, config): self.n_features = SIZE + 1 self.n_actions = 2 self.cookbook = Cookbook(config.recipes)
def __init__(self, config): self.n_actions = 5 self.n_features = 12 self.cookbook = Cookbook(config.recipes) self.random = np.random.RandomState(0)