Exemplo n.º 1
0
 def _generate_episode_reinforce(self, sample, max_flips, walk_prob):
     f = sample.formula
     data = init_tensors(sample, self.device)
     true_lit_count = compute_true_lit_count(f.clauses, data.sol)
     log_probs = []
     flip = 0
     flipped = set()
     backflipped = 0
     unsat_clauses = []
     while flip < max_flips:
         unsat_clause_indices = [
             k for k in range(len(f.clauses)) if true_lit_count[k] == 0
         ]
         unsat_clauses.append(len(unsat_clause_indices))
         # print(len(unsat_clause_indices))
         sat = not unsat_clause_indices
         if sat:
             break
         if random.random() < walk_prob:
             unsat_clause = f.clauses[random.choice(unsat_clause_indices)]
             v, log_prob = abs(random.choice(unsat_clause)) - 1, None
         else:
             v, log_prob = self._select_variable_reinforce(data)
             if v.item() not in flipped:
                 flipped.add(v.item())
             else:
                 backflipped += 1
         flip_(data.x[0], data.sol, true_lit_count, v, f.occur_list)
         flip += 1
         log_probs.append(log_prob)
     # print(backflipped)
     return sat, (flip, backflipped, unsat_clauses), (log_probs, )
Exemplo n.º 2
0
    def _generate_episode_a2c(self, sample, max_flips):
        f = sample.formula
        data = init_tensors(sample, self.device)
        true_lit_count = compute_true_lit_count(f.clauses, data.sol)

        log_probs = []
        values = []
        entropies = []

        flip = 0
        while flip < max_flips:
            unsat_clause_indices = [
                k for k in range(len(f.clauses)) if true_lit_count[k] == 0
            ]
            sat = not unsat_clause_indices
            if sat:
                break
            v, log_prob, value, entropy = self._select_variable_a2c(data)
            flip_(data.x[0], data.sol, true_lit_count, v, f.occur_list)
            flip += 1

            log_probs.append(log_prob)
            values.append(value)
            entropies.append(entropy)

        return sat, flip, (log_probs, values, entropies)
Exemplo n.º 3
0
 def _eval_generate_episode_reinforce(self, sample, max_flips, walk_prob):
     f = sample.formula
     data = init_tensors(sample, self.device)
     true_lit_count = compute_true_lit_count(f.clauses, data.sol)
     flip = 0
     while flip < max_flips:
         unsat_clause_indices = [
             k for k in range(len(f.clauses)) if true_lit_count[k] == 0
         ]
         sat = not unsat_clause_indices
         if sat:
             break
         if random.random() < walk_prob:
             unsat_clause = f.clauses[random.choice(unsat_clause_indices)]
             v = abs(random.choice(unsat_clause)) - 1
         else:
             v = self._eval_select_variable_reinforce(data)
         flip_(data.x[0], data.sol, true_lit_count, v, f.occur_list)
         flip += 1
     return sat, flip, None