def _compile_state_cpfs( self, scope: Dict[str, TensorFluent], **kwargs ) -> List[CPFPair]: """Compiles the next state fluent CPFs given the current `state` and `action` scope. Args: scope (Dict[str, :obj:`rddl2tf.core.fluent.TensorFluent`]): The fluent scope for CPF evaluation. batch_size (Optional[int]): The batch size. Returns: A list of state fluent CPFs compiled to :obj:`rddl2tf.core.fluent.TensorFluent`. """ noise = kwargs["noise"] next_state_fluents = [] with self.graph.as_default(): with tf.name_scope("state_cpfs"): for cpf in self.rddl.domain.state_cpfs: cpf_noise = noise.get(cpf.name) name_scope = utils.identifier(cpf.name) with tf.name_scope(name_scope): t = self._compile_expression(cpf.expr, scope, noise=cpf_noise) next_state_fluents.append((cpf.name, t)) key = lambda f: self.rddl.domain.next_state_fluent_ordering.index(f[0]) next_state_fluents = sorted(next_state_fluents, key=key) return next_state_fluents
def _compile_intermediate_cpfs( self, scope: Dict[str, TensorFluent], **kwargs ) -> List[CPFPair]: """Compiles the intermediate fluent CPFs given the current `state` and `action` scope. Args: scope (Dict[str, :obj:`rddl2tf.core.fluent.TensorFluent`]): The fluent scope for CPF evaluation. batch_size (Optional[int]): The batch size. Returns: A list of intermediate fluent CPFs compiled to :obj:`rddl2tf.core.fluent.TensorFluent`. """ noise = kwargs["noise"] interm_fluents = [] with self.graph.as_default(): with tf.name_scope("intermediate_cpfs"): for cpf in self.rddl.domain.intermediate_cpfs: cpf_noise = noise.get(cpf.name) name_scope = utils.identifier(cpf.name) with tf.name_scope(name_scope): t = self._compile_expression(cpf.expr, scope, noise=cpf_noise) interm_fluents.append((cpf.name, t)) scope[cpf.name] = t return interm_fluents
def _compile_pvariables(self, pvariables: List[Tuple[str, str, np.array]]) -> List[Tuple[str, TensorFluent]]: fluents = [] with self.graph.as_default(): for name, pvar_range, fluent in pvariables: dtype = utils.range_type_to_dtype(pvar_range) t = tf.constant(fluent, dtype=dtype, name=utils.identifier(name)) scope = [None] * len(t.shape) fluent = TensorFluent(t, scope, batch=False) fluents.append((name, fluent)) return fluents
def _compile_batch_fluents(self, fluents: List[Tuple[str, TensorFluent]]) -> Sequence[tf.Tensor]: '''Compiles `fluents` into tensors with given `batch_size`. Returns: Sequence[tf.Tensor]: A tuple of tensors with first dimension corresponding to the batch size. ''' batch_fluents = [] with self.graph.as_default(): for name, fluent in fluents: name_scope = utils.identifier(name) with tf.compat.v1.name_scope(name_scope): t = tf.stack([fluent.tensor] * self.batch_size) batch_fluents.append(t) return tuple(batch_fluents)