示例#1
0
def gen_program_worker(input_types):
    """
    Generate programs with the given input types.
    Statements are generated by choosing a function randomly, and then sampling parameters so that
    unused variables take precedence. Programs that has unused variables are discarded.
    """
    def helper(functions, program, programs):
        random.shuffle(functions)
        if progress_counter.value >= num_programs:
            return True

        if len(program) >= program_len:
            if get_unused_indices(program) or program in programs:
                return False
            else:
                programs.add(program)
                progress_counter.value += 1
                print("\rGenerating programs... %d\\%d" %
                      (progress_counter.value, num_programs),
                      end="")
                return True

        type_to_vars = collections.defaultdict(list)
        for i, typ in enumerate(program.var_types):
            type_to_vars[typ].insert(0, i)

        # Move free indices to the front
        free_indxs = get_free_indices(program, program_len)
        for typ in program.var_types:
            for var in type_to_vars[typ]:
                if var in free_indxs:
                    type_to_vars[typ].remove(var)
                    type_to_vars[typ].insert(0, var)

        for func in LAMBDAS:
            type_to_vars[func.type].append(func)

        used = set(program.statements)
        for function in functions:
            for args in iterate_inputs(function, type_to_vars):
                if len([arg for arg in args if arg in free_indxs]) == 0:
                    continue
                statement = Statement(function, args)
                if statement in used:
                    continue

                next_program = Program(program.input_types,
                                       program.statements + [statement])
                if helper(functions, next_program, programs):
                    return True

    program_base = Program(input_types, [])
    res = set()
    while progress_counter.value < num_programs:
        helper(ALL_FUNCTIONS, program_base, res)
    return res
示例#2
0
    def helper(functions, program, programs):
        random.shuffle(functions)
        if progress_counter.value >= num_programs:
            return True

        if len(program) >= program_len:
            if get_unused_indices(program) or program in programs:
                return False
            else:
                programs.add(program)
                progress_counter.value += 1
                print("\rGenerating programs... %d\\%d" %
                      (progress_counter.value, num_programs),
                      end="")
                return True

        type_to_vars = collections.defaultdict(list)
        for i, typ in enumerate(program.var_types):
            type_to_vars[typ].insert(0, i)

        # Move free indices to the front
        free_indxs = get_free_indices(program, program_len)
        for typ in program.var_types:
            for var in type_to_vars[typ]:
                if var in free_indxs:
                    type_to_vars[typ].remove(var)
                    type_to_vars[typ].insert(0, var)

        for func in LAMBDAS:
            type_to_vars[func.type].append(func)

        used = set(program.statements)
        for function in functions:
            for args in iterate_inputs(function, type_to_vars):
                if len([arg for arg in args if arg in free_indxs]) == 0:
                    continue
                statement = Statement(function, args)
                if statement in used:
                    continue

                next_program = Program(program.input_types,
                                       program.statements + [statement])
                if helper(functions, next_program, programs):
                    return True
示例#3
0
def tokens_to_program(seq, input_types):
    tokens = [reverse_program_vocab[token] for token in seq]
    if tokens[0] == START_PROGRAM_TOKEN:
        tokens = tokens[1:]

    indx = 0
    statements = []
    while indx < len(tokens) and tokens[indx] != END_PROGRAM_TOKEN:
        token = tokens[indx]
        if not token in ALL_FUNCTIONS:
            return None

        if isinstance(token.input_type, tuple):
            num_args = len(token.input_type)
        else:
            num_args = 1
        args = tokens[indx + 1:indx + 1 + num_args]
        statements.append(Statement(token, args))
        indx = indx + 1 + num_args

    return Program(input_types, statements)
示例#4
0
def solve_problem_worker(data):
    examples = Example.from_line(data)
    env = ProgramEnv(examples)

    if method == 'beam':
        solution = cab(env, max_program_len, model, params.cab_beam_size, params.cab_width,
                       params.cab_width_growth, timeout, max_beam_size=max_beam_size)
    elif method == 'dfs':
        solution = dfs(env, max_program_len, model, params.dfs_max_width, timeout)

    counter.value += 1
    print("\rSolving problems... %d (failed: %d)" % (counter.value, fail_counter.value), end="")

    if solution['result'] is False:
        solution['result'] = "Failed"
        fail_counter.value += 1
    else:
        values = [Value.construct(x) for x in data['examples'][0]['inputs']]
        value_types = [x.type for x in values]
        solution['result'] = Program(value_types, solution['result']).encode()
    return solution
示例#5
0
def solve_problem_worker(args):
    line, input, input_lens, output, output_lens, input_masks, output_masks = args
    examples = Example.from_line(line)
    sol = Program.parse(line['program'])
    env = ProgramEnv(examples)
    res = robustfill_cab(env, program_len, model, 100, 48, timeout, input,
                         input_lens, output, output_lens, input_masks,
                         output_masks)

    counter.value += 1
    print("\rSolving problems... %d (failed: %d)" %
          (counter.value, fail_counter.value),
          end="")

    if res['result'] is None:
        res['result'] = "Failed"
        fail_counter.value += 1
        return res
    else:
        res['result'] = str(Program(sol.input_types, res['result']))
        return res