def p_prob(self, p): """ prob : FLOAT | INT """ prob = p[1] if not 0 <= prob <= 1: raise ParserError(f'Probability value ({prob}) out of bounds.') p[0] = prob
def p_start_dist(self, p): """ start : START COLON pmatrix """ pm = np.array(p[3]) if not np.isclose(pm.sum(), 1.): raise ParserError('Start distribution is not normalized (sums to ' f'{pm.sum()}).') self.start = pm
def p_structure_t_a_dist(self, p): """ structure_item : T COLON action pmatrix """ a, pm = p[3], p[4] pm = np.reshape(pm, (self.nstates, self.nstates)) if not np.isclose(pm.sum(axis=1), 1.0).all(): raise ParserError(f'Transition state distribution (action={a}) is ' 'not normalized;') self.T[a] = pm
def p_structure_t_o_dist(self, p): """ structure_item : T COLON observation pmatrix """ o, pm = p[3], p[4] pm = np.reshape(pm, (self.nnodes, self.nnodes)) if not np.isclose(pm.sum(axis=1), 1.).all(): raise ParserError(f'Transition node distribution (observation={o})' ' is not normalized;') self.T[o] = pm
def p_structure_a_n_dist(self, p): """ structure_item : A COLON node pmatrix """ n0, pm = p[3], p[4] pm = np.array(pm) if not np.isclose(pm.sum(), 1.): raise ParserError(f'Action distribution (node={n0}) is not ' f'normalized (sums to {pm.sum()}).') self.A[n0] = pm
def p_confounding_dist(self, p): """ confounding : U COLON pmatrix """ pm = np.array(p[3]) pmsum = pm.sum() if not np.isclose(pmsum, 1.0): raise ParserError( f'Confounding distribution is not normalized (sums to {pmsum}).' ) self.U = pm
def p_structure_t_os_dist(self, p): """ structure_item : T COLON observation COLON node pmatrix """ o, n0, pm = p[3], p[5], p[6] pm = np.array(pm) if not np.isclose(pm.sum(), 1.): raise ParserError( f'Transition distribution (observation={o}, ode={n0}) is not ' f'normalized (sums to {pm.sum()}).') self.T[o, n0] = pm
def p_structure_t_as_dist(self, p): """ structure_item : T COLON action COLON state pmatrix """ a, s0, pm = p[3], p[5], p[6] pm = np.array(pm) if not np.isclose(pm.sum(), 1.): raise ParserError(f'Transition distribution (action={a}, ' f'state={s0}) is not normalized (sums to ' f'{pm.sum()}).') self.T[a, s0] = pm