コード例 #1
0
def oracle(args):
    """
    Synthesis oracle
    :param args: list of inputs
    :return: output
    """
    # initialise
    global in_out_map

    # calc hash
    args_sha1 = to_sha1(str(args).replace("L", ""))

    # return output
    return in_out_map[args_sha1]
コード例 #2
0
    def query_oracle(self, args):
        """
        Queries the blackbox oracle
        :param args: list of integers
        :return: oracle results
        """
        # calculate key
        key = to_sha1(str(args))
        # check if key ins known
        if key not in self.oracle_queries:
            # query oracle
            self.oracle_queries[key] = self.oracle(args)
        # oracle query
        result = self.oracle_queries[key]

        return result
コード例 #3
0
def worker_synthesize_from_assembly_oracle(commands, result, worker_index):
    # initialise
    global in_out_map

    # parse synthesis parameters
    variables = commands[0]
    grammar = commands[1]
    utc_scalar = commands[2]
    max_mcts_rounds = commands[3]
    playout_depth = commands[4]
    output_name = commands[5]
    output_index = commands[6]
    output_bitsize = commands[7]
    synthesis_inputs = commands[8]
    synthesis_outputs = commands[9]

    # fill in/out map
    in_out_map = dict()
    for index in xrange(len(synthesis_inputs)):
        current_inputs_sha1 = to_sha1(
            str(synthesis_inputs[index]).replace("L", ""))
        in_out_map[current_inputs_sha1] = synthesis_outputs[index]

    # init mcts
    game = Game(grammar, variables, bitsize=output_bitsize * 8)
    s = State(game, output_bitsize * 8)

    mc = MCTS(game, oracle, synthesis_inputs, uct_scalar=utc_scalar)
    mc.playout_nesting = playout_depth
    mc.verbosity_level = 0

    # start synthesis
    mc.search(s, max_mcts_rounds)

    # prepare output json
    ret = OrderedDict()

    ret["output"] = OrderedDict()
    ret["output"]["name"] = output_name
    ret["output"]["number"] = output_index
    ret["output"]["size"] = output_bitsize * 8

    # top non-terminal
    ret["top_non_terminal"] = OrderedDict()
    ret["top_non_terminal"]["expression"] = OrderedDict()
    ret["top_non_terminal"]["expression"]["infix"] = rpn_to_infix(
        mc.top_non_terminal_node.state.expr)
    ret["top_non_terminal"]["reward"] = mc.top_non_terminal_reward

    # top terminal
    ret["top_terminal"] = OrderedDict()
    ret["top_terminal"]["expression"] = OrderedDict()
    expr = mc.top_terminal_node.state.expr if not mc.final_expression else mc.final_expression
    reward = mc.top_terminal_reward if not mc.final_expression else 1.0
    ret["top_terminal"]["expression"]["infix"] = rpn_to_infix(expr)
    ret["top_terminal"]["reward"] = reward

    # synthesis results
    ret["successful"] = "yes" if mc.final_expression else "no"
    if mc.final_expression:
        ret["result"] = OrderedDict()
        ret["result"]["final_expression"] = OrderedDict()
        ret["result"]["final_expression"]["infix"] = rpn_to_infix(
            mc.final_expression)
        try:
            ret["result"]["final_expression"]["simplified"] = str(
                simplify(game.to_z3(mc.final_expression)))
        except:
            ret["result"]["final_expression"]["simplified"] = rpn_to_infix(
                mc.final_expression)

    # store return value
    result[worker_index] = ret