Пример #1
0
 def construct_var(self, var_num):
     rv = str(var_num)
     values = self.domains[var_num]
     self.pgm.add_var(
         Variable(rv,
                  values,
                  detect_boolean=self.detect_bool,
                  force_boolean=self.force_bool))
Пример #2
0
 def parse_node(self, s, l, t):
     # print(t)
     rv = t[0]
     for key, val in t[1]:
         if key == 'states':
             self.domains[rv] = val
             self.pgm.add_var(
                 Variable(rv,
                          val,
                          detect_boolean=self.detect_bool,
                          force_boolean=self.force_bool))
Пример #3
0
 def parse_domains(self, root):
     for cpt in root.find("nodes").findall('cpt'):
         rv = cpt.get('id')
         states = cpt.findall('state')
         values = [state.get('id') for state in states]
         self.domains[rv] = values
         self.pgm.add_var(
             Variable(rv,
                      values,
                      detect_boolean=self.detect_bool,
                      force_boolean=self.force_bool))
def parseNode(s, l, t):
    global domains
    # print(t)
    rv = t[0]
    for key, val in t[1]:
        if key == 'states':
            domains[rv] = val
            pgm.add_var(
                Variable(rv,
                         val,
                         detect_boolean=detect_bool,
                         force_boolean=force_bool))
Пример #5
0
 def parse_domains(self, root):
     for cpt in root.find("nodes").findall("cpt"):
         rv = cpt.get("id")
         states = cpt.findall("state")
         values = [state.get("id") for state in states]
         self.domains[rv] = values
         self.pgm.add_var(
             Variable(
                 rv,
                 values,
                 detect_boolean=self.detect_bool,
                 force_boolean=self.force_bool,
             ))
 def parse_domains(self, root):
     for cpt in root.findall('VARIABLE'):
         rv = cpt.find('NAME').text
         node_type = cpt.get("TYPE")
         if node_type != "nature":
             logger.error(
                 "Only probabilistic variables are supported. Found type {} for variable {}"
                 .format(node_type, name),
                 halt=True)
         states = cpt.findall('OUTCOME')
         values = [state.text for state in states]
         self.domains[rv] = values
         self.pgm.add_var(
             Variable(rv,
                      values,
                      detect_boolean=self.detect_bool,
                      force_boolean=self.force_bool))
Пример #7
0
def clause_to_cpt(clause, number, pgm):
    logger = logging.getLogger('problog')
    # print('Clause: {} -- {}'.format(clause, type(clause)))
    if isinstance(clause, Clause):
        if isinstance(clause.head, Or):
            heads = clause.head.to_list()
        elif isinstance(clause.head, Term):
            heads = [clause.head]
        else:
            heads = []
            logger.error('Unknown head type: {} ({})'.format(
                clause.head, type(clause.head)))
        # CPT for the choice node
        rv_cn = 'c{}'.format(number)
        parents = term_to_atoms(clause.body)
        parents_str = [str(t) for t in parents]
        probs_heads = []
        for head in heads:
            if head.probability is not None:
                probs_heads.append(head.probability.compute_value())
            else:
                probs_heads.append(1.0)
        probs = [1.0 - sum(probs_heads)] + probs_heads
        table_cn = dict()
        for keys in itertools.product([False, True], repeat=len(parents)):
            truth_values = dict(zip(parents, keys))
            truth_value = term_to_bool(clause.body, truth_values)
            if truth_value is None:
                logger.error('Expected a truth value. Instead god:\n'
                             ' {} -> {}'.format(truth_values, truth_value))
                table_cn[keys] = [1.0] + [0.0] * len(heads)
            elif truth_value:
                table_cn[keys] = probs
            else:
                table_cn[keys] = [1.0] + [0.0] * len(heads)
        pgm.add_var(Variable(rv_cn, list(range(len(heads) + 1))))
        cpd_cn = Factor(pgm, rv_cn, parents_str, table_cn)
        cpd_cn.latent = True
        pgm.add_factor(cpd_cn)
        # CPT for the head random variable
        for idx, head in enumerate(heads):
            rv = str(head.with_probability())
            pgm.add_var(Variable(rv, [0, 1]))
            pgm.add_factor(OrCPT(pgm, rv, [(rv_cn, idx + 1)]))
        return

    elif isinstance(clause, Or):
        heads = clause.to_list()
        # CPT for the choice node
        rv_cn = 'c{}'.format(number)
        parents = []
        probs_heads = [head.probability.compute_value() for head in heads]
        table_cn = [1.0 - sum(probs_heads)] + probs_heads
        pgm.add_var(Variable(rv_cn, list(range(len(heads) + 1))))
        cpd_cn = Factor(pgm, rv_cn, parents, table_cn)
        cpd_cn.latent = True
        pgm.add_factor(cpd_cn)
        # CPT for the head random variable
        for idx, head in enumerate(heads):
            rv = str(head.with_probability())
            pgm.add_var(Variable(rv, [0, 1]))
            pgm.add_factor(OrCPT(pgm, rv, [(rv_cn, idx + 1)]))
        return

    elif isinstance(clause, Term):
        # CPT for the choice node
        rv_cn = 'c{}'.format(number)
        parents = []
        prob = clause.probability.compute_value()
        table_cn = [1.0 - prob, prob]
        pgm.add_var(Variable(rv_cn, [0, 1]))
        cpd_cn = Factor(pgm, rv_cn, parents, table_cn)
        cpd_cn.latent = True
        pgm.add_factor(cpd_cn)
        # CPT  for the head random variable
        rv = str(clause.with_probability())
        pgm.add_var(Variable(rv, [0, 1]))
        pgm.add_factor(OrCPT(pgm, rv, [(rv_cn, 1)]))
        return

    return