Beispiel #1
0
 def __init__(self):
     self.vocab = Index()
     for word in [
             'a', 'find', 'add', 'remove', 'clone', 'grass', 'red', 'wood',
             'blue', 'clear', 'tree', 'house', 'wall', 'door', 'window',
             'course', 'block', 'in', 'with', 'here', 'tall', 'short',
             ling.START, ling.STOP, ling.UNK
     ]:
         self.vocab.index(word)
Beispiel #2
0
class Cookbook(object):
    """Holds the components of a world, and rules on how to create stuff."""
    def __init__(self, recipes_path):
        with open(recipes_path) as recipes_f:
            recipes = yaml.load(recipes_f)
        self.index = Index()
        self.environment = set(
            self.index.index(e) for e in recipes["environment"])
        self.primitives = set(
            self.index.index(p) for p in recipes["primitives"])
        self.recipes = {}
        for output, inputs in recipes["recipes"].items():
            d = {}
            for inp, count in inputs.items():
                # special keys
                if "_" in inp:
                    d[inp] = count
                else:
                    d[self.index.index(inp)] = count
            self.recipes[self.index.index(output)] = d
        self.kinds = self.environment | self.primitives | set(
            self.recipes.keys())
        self.n_kinds = len(self.index)

    def primitives_for(self, goal):
        out = {}

        if goal not in self.recipes:
            return out

        def insert(kind, count):
            assert kind in self.primitives
            if kind not in out:
                out[kind] = count
            else:
                out[kind] += count

        for ingredient, count in self.recipes[goal].items():
            if not isinstance(ingredient, int):
                assert ingredient[0] == "_"
                continue
            elif ingredient in self.primitives:
                insert(ingredient, count)
            else:
                sub_recipe = self.recipes[ingredient]
                n_produce = sub_recipe[
                    "_yield"] if "_yield" in sub_recipe else 1
                n_needed = int(np.ceil(1. * count / n_produce))
                expanded = self.primitives_for(ingredient)
                for k, v in expanded.items():
                    insert(k, v * n_needed)

        return out
Beispiel #3
0
class CraftEnv(object):
    GO = 0
    ADD = 1
    REMOVE = 2
    CLONE = 3
    STOP = 4
    SAY = 5
    n_actions = 6

    world_shape = Scene._size
    n_block_types = 1 + len(BlockType.enumerate())
    n_world_obs = n_block_types + 1
    n_state_obs = n_block_types + (5 * 5 * 5 * n_block_types)

    _action_names = {
        GO: 'g',
        ADD: 'A',
        REMOVE: 'R',
        CLONE: 'C',
        STOP: '.',
    }

    def __init__(self):
        self.vocab = Index()
        for word in [
                'a', 'find', 'add', 'remove', 'clone', 'grass', 'red', 'wood',
                'blue', 'clear', 'tree', 'house', 'wall', 'door', 'window',
                'course', 'block', 'in', 'with', 'here', 'tall', 'short',
                ling.START, ling.STOP, ling.UNK
        ]:
            self.vocab.index(word)

    ALLOWED = set(['add a wood course', 'clone a wood block', 'add a course'])

    #@classmethod
    def sample_task(self, task_id, interesting=False):
        while True:
            try:
                task = Task.sample(task_id, self)
                if FLAGS.debug and task._nl_desc not in self.ALLOWED:
                    continue
                #if interesting and task.action not in (Task.CLONE, Task.ADD, Task.REMOVE):
                #    continue
                break
            except BuilderException as e:
                print(e)
                pass
        return task

    #@classmethod
    def action_name(cls, action):
        return cls._action_names[action]
Beispiel #4
0
class Cookbook(object):
    def __init__(self):
        dir_path = os.path.dirname(os.path.realpath(__file__))
        with open(os.path.join(dir_path, "recipes.yaml")) as recipes_f:
            recipes = yaml.load(recipes_f)
        # self.environment = set(recipes["environment"])
        self.index = Index()
        self.environment = set(self.index.index(e) for e in recipes["environment"])
        self.primitives = set(self.index.index(p) for p in recipes["primitives"])
        self.recipes = {}
        for output, inputs in recipes["recipes"].items():
            d = {}
            for inp, count in inputs.items():
                # special keys
                if "_" in inp:
                    d[inp] = count
                else:
                    d[self.index.index(inp)] = count
            self.recipes[self.index.index(output)] = d
        kinds = self.environment | self.primitives | set(self.recipes.keys())
        self.n_kinds = len(self.index)

    def primitives_for(self, goal):
        out = {}

        def insert(kind, count):
            assert kind in self.primitives
            if kind not in out:
                out[kind] = count
            else:
                out[kind] += count

        for ingredient, count in self.recipes[goal].items():
            if not isinstance(ingredient, int):
                assert ingredient[0] == "_"
                continue
            elif ingredient in self.primitives:
                insert(ingredient, count)
            else:
                sub_recipe = self.recipes[ingredient]
                n_produce = sub_recipe["_yield"] if "_yield" in sub_recipe else 1
                n_needed = int(np.ceil(1. * count / n_produce))
                expanded = self.primitives_for(ingredient)
                for k, v in expanded.items():
                    insert(k, v * n_needed)

        return out
Beispiel #5
0
 def __init__(self):
     dir_path = os.path.dirname(os.path.realpath(__file__))
     with open(os.path.join(dir_path, "recipes.yaml")) as recipes_f:
         recipes = yaml.load(recipes_f)
     # self.environment = set(recipes["environment"])
     self.index = Index()
     self.environment = set(self.index.index(e) for e in recipes["environment"])
     self.primitives = set(self.index.index(p) for p in recipes["primitives"])
     self.recipes = {}
     for output, inputs in recipes["recipes"].items():
         d = {}
         for inp, count in inputs.items():
             # special keys
             if "_" in inp:
                 d[inp] = count
             else:
                 d[self.index.index(inp)] = count
         self.recipes[self.index.index(output)] = d
     kinds = self.environment | self.primitives | set(self.recipes.keys())
     self.n_kinds = len(self.index)
Beispiel #6
0
 def __init__(self, recipes_path):
     with open(recipes_path) as recipes_f:
         recipes = yaml.load(recipes_f)
     self.index = Index()
     self.environment = set(
         self.index.index(e) for e in recipes["environment"])
     self.primitives = set(
         self.index.index(p) for p in recipes["primitives"])
     self.recipes = {}
     for output, inputs in recipes["recipes"].items():
         d = {}
         for inp, count in inputs.items():
             # special keys
             if "_" in inp:
                 d[inp] = count
             else:
                 d[self.index.index(inp)] = count
         self.recipes[self.index.index(output)] = d
     self.kinds = self.environment | self.primitives | set(
         self.recipes.keys())
     self.n_kinds = len(self.index)