except: pass result[index] = ret variables = [] for var_index in xrange(2): v = Variable("V.{}".format(var_index), BITSIZE) variables.append(v) constants = OrderedSet(["1"]) grammar = Grammar(variables, constants=constants) game = Game(grammar, variables, bitsize=BITSIZE) task_groups = [] workers = [] commands = [] for index in xrange(1): task_group = "TG" task_groups.append(task_group) # this may take some time synthesis_inputs = get_random_inputs(len(variables), 20) command = [max_iter, uct_scalar, game, oracle, synthesis_inputs] workers.append(synthesise)
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