Exemple #1
0
    def __init__(self,
                 data,
                 graph,
                 config,
                 attr_encoder,
                 is_uncertain=cmd_args.prob_dataset):

        self.data = deepcopy(data)
        self.graph = deepcopy(graph)
        self.obj_nums = len(graph.scene["objects"])
        self.clauses = []
        self.config = config
        self.attr_encoder = attr_encoder

        # self.obj_poss_left = [ self.obj_nums ** cmd_args.max_var_num ]
        self.obj_poss_left = [self.obj_nums]
        self.success = False
        self.possible = True

        if is_uncertain:
            self.interp = SceneInterp(graph.scene, config, is_uncertain=True)
        else:
            self.interp = SceneInterp(graph.scene["ground_truth"],
                                      config,
                                      is_uncertain=False)

        self.state = self.interp.get_init_state()
        self.is_uncertain = is_uncertain
    def get_option_num(self, clauses, scene, target):
        scene_interp = SceneInterp(scene, self.config)
        binding_dict = scene_interp.fast_query(clauses)

        success = check_success(binding_dict, target)
        possible = check_possible(binding_dict, target)
        option_num = len(binding_dict["var_0"]) if "var_0" in binding_dict.keys() else len(scene["objects"])
        return option_num, success, possible
Exemple #3
0
def eu_solve_prog(scene, config, target, max_depth):
    interpreter = SceneInterp(scene, config)
    clean_state = interpreter.get_init_state()
    prog = eu_synthesize(interpreter.interpreter,
                         target,
                         clean_state,
                         is_uncertain=False,
                         max_depth=max_depth)
    return prog
    def __init__(self,
                 data,
                 graph,
                 config,
                 attr_encoder,
                 state=None,
                 ref=False,
                 is_uncertain=False):

        self.data = deepcopy(data)
        self.graph = deepcopy(graph)
        self.obj_nums = len(graph.scene["objects"])
        self.clauses = []
        self.idx_selected = []

        self.config = config
        self.attr_encoder = attr_encoder
        self.actions = get_all_clauses(config)
        self.create_action_dict()
        self.ref_flag = ref
        if ref:
            self.ref = [0]
        else:
            self.ref = list(range(cmd_args.max_var_num))

        # self.obj_poss_left = [ self.obj_nums ** cmd_args.max_var_num ]
        self.obj_poss_left = [self.obj_nums]
        self.success = False
        self.possible = True
        self.unreachable = []
        self.reachable_dict, self.unreachable_dict = get_reachable_dict(
            self.actions)
        self.is_uncertain = is_uncertain
        if is_uncertain:
            self.interp = SceneInterp(graph.scene, config, is_uncertain=True)
        else:
            self.interp = SceneInterp(graph.scene["ground_truth"],
                                      config,
                                      is_uncertain=False)

        if type(state) == type(None):
            self.state = self.interp.get_init_state()
        else:
            self.state = state
Exemple #5
0
def shrink(prog, scene, config):
    scene_interp = SceneInterp(scene, config)
    last_prog = prog

    while (True):
        prog = remove_one(scene_interp, prog)

        # is minimal
        if type(prog) == type(None):
            return last_prog
        last_prog = prog
Exemple #6
0
    def __init__(self, data, graph, config, attr_encoder):

        self.data = deepcopy(data)
        self.graph = deepcopy(graph)
        self.obj_nums = len(graph.scene["objects"])
        self.clauses = []
        self.config = config
        self.attr_encoder = attr_encoder

        # self.obj_poss_left = [ self.obj_nums ** cmd_args.max_var_num ]
        self.obj_poss_left = [self.obj_nums]
        self.success = False
        self.possible = True
        self.state = None
        self.interp = SceneInterp(graph.scene, config)
        self.interp_state = self.interp.get_init_state()
Exemple #7
0
def add_attr(prog, scene):
    scene_interp = SceneInterp(scene, config)
    state = scene_interp.get_state(prog)

    bd_list = scene_interp.get_valid_binding(state)
    new_clauses = []
    ref = get_ref(prog)

    for var, obj in enumerate(bd_list):
        if var not in ref:
            continue
        color = scene["objects"][obj]["color"]
        shape = scene["objects"][obj]["shape"]
        new_clauses.append(["color", color, var])
        new_clauses.append(["shape", shape, var])

    prog += new_clauses
    return prog
Exemple #8
0
def cont_single(file_name,
                datapoint,
                model,
                graphs,
                save_dir,
                attr_encoder,
                config,
                save=True,
                n=10):

    file_path = os.path.join(save_dir, str(file_name))
    exist = os.path.exists(file_path)
    logging.info(f"task: {file_name}, {exist}")
    if exist:
        logging.info("skip")
        return

    total_ct = 0
    memory = ReplayMemory(10000)
    method = None

    policy = copy.deepcopy(model.policy)
    target = copy.deepcopy(policy)
    target.eval()

    optimizer = optim.RMSprop(policy.parameters(), lr=cmd_args.lr)

    success_progs = []
    all_progs = []

    eps = cmd_args.eps
    graph = graphs[datapoint.graph_id]

    for it in range(cmd_args.test_iter):

        suc, prog, obj_left = episode(policy, target, datapoint, graph, config,
                                      attr_encoder, memory, total_ct,
                                      optimizer)
        total_ct += 1

        if suc:
            success_progs.append(prog)
            method = "model"
            break

        if not suc:
            all_progs.append((prog, obj_left))

    logging.info(f"success: {success_progs}")

    # if not success, explot the best selections.
    scene_interpreter = SceneInterp(graph.ground_truth_scene, config)

    if len(success_progs) == 0:
        logging.info("start exploit")

        # take second element for sort
        candidates = sorted(all_progs, key=lambda x: x[1])
        for prog, obj_left in candidates:
            # no progress
            if len(prog) == 0:
                logging.info("empty prog to exploit, stop")
                break

            logging.info(f"prog: {prog}")
            logging.info(f"obj left: {obj_left}")
            prog_left = scene_interpreter.interp_enum(prog,
                                                      datapoint.y,
                                                      max_depth=2)

            if not type(prog_left) == type(None):
                print(prog_left)
                prog.append(prog_left)
                success_progs.append(prog)
                method = f"exploit: {len(prog_left)}"
                logging.info(
                    f"We found: {success_progs} at depth {len(prog_left)}")
                break

    if save:
        file_path = os.path.join(save_dir, str(file_name))
        with open(file_path, 'w') as suc_prog_file:
            res = {}
            res['prog'] = success_progs
            res['method'] = method
            json.dump(res, suc_prog_file)

    return success_progs
def cont_single(file_name,
                datapoint,
                policy,
                graphs,
                save_dir,
                attr_encoder,
                config,
                save=True,
                n=10):

    policy = deepcopy(policy)
    policy.train()
    optimizer = torch.optim.Adam(policy.parameters(), lr=cmd_args.lr)

    success_progs = []
    all_progs = []

    global sub_ct
    eps = cmd_args.eps
    graph = graphs[datapoint.graph_id]

    for it in range(cmd_args.episode_iter):
        sub_ct = 0
        env, loss = fit_one(policy, datapoint, graph, eps, attr_encoder,
                            config)
        if env.success:
            success_progs.append(env.clauses)

        if not env.possible:
            all_progs.append((env.clauses[:-1], env.obj_poss_left[-2]))

        if it % cmd_args.batch_size == 0:
            optimizer.zero_grad()

        loss.backward()

        if it % cmd_args.batch_size == 0:
            optimizer.step()

        if len(success_progs) > 0:
            break

    if save:
        file_path = os.path.join(save_dir, str(file_name))
        with open(file_path, 'w') as suc_prog_file:
            json.dump(success_progs, suc_prog_file)

    logging.info(f"success: {success_progs}")
    # print(f"all: {all_progs}")
    # if not success, explot the best selections.
    scene_interpreter = SceneInterp(graph.scene, config)

    if len(success_progs) == 0:
        logging.info("start exploit")
        # take second element for sort
        candidates = sorted(all_progs, key=lambda x: x[1])
        for prog, obj_left in candidates:
            # no progress
            if len(prog) == 0:
                logging.info("empty prog to exploit, stop")
                break

            logging.info(f"prog: {prog}")
            logging.info(f"obj left: {obj_left}")
            prog_left = scene_interpreter.interp_enum(prog, datapoint.y)
            if not type(prog_left) == type(None):
                print(prog_left)
                prog.append(prog_left)
                success_progs.append(prog)
                logging.info(f"We found: {success_progs}")
                break

    return success_progs
def check_prog_correct(scene, prog, target, config):
    interp = SceneInterp(scene["ground_truth"], config, is_uncertain=False)
    binding_dict = interp.fast_query(prog)
    res = check_success(binding_dict, target)
    return res