pyRDDLGym-jax 2.4__py3-none-any.whl → 2.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyRDDLGym_jax/__init__.py CHANGED
@@ -1 +1 @@
1
- __version__ = '2.4'
1
+ __version__ = '2.6'
@@ -237,7 +237,8 @@ class JaxRDDLCompiler:
237
237
 
238
238
  def compile_transition(self, check_constraints: bool=False,
239
239
  constraint_func: bool=False,
240
- init_params_constr: Dict[str, Any]={}) -> Callable:
240
+ init_params_constr: Dict[str, Any]={},
241
+ cache_path_info: bool=False) -> Callable:
241
242
  '''Compiles the current RDDL model into a JAX transition function that
242
243
  samples the next state.
243
244
 
@@ -274,6 +275,7 @@ class JaxRDDLCompiler:
274
275
  returned log and does not raise an exception
275
276
  :param constraint_func: produces the h(s, a) function described above
276
277
  in addition to the usual outputs
278
+ :param cache_path_info: whether to save full path traces as part of the log
277
279
  '''
278
280
  NORMAL = JaxRDDLCompiler.ERROR_CODES['NORMAL']
279
281
  rddl = self.rddl
@@ -322,8 +324,11 @@ class JaxRDDLCompiler:
322
324
  errors |= err
323
325
 
324
326
  # calculate fluent values
325
- fluents = {name: values for (name, values) in subs.items()
326
- if name not in rddl.non_fluents}
327
+ if cache_path_info:
328
+ fluents = {name: values for (name, values) in subs.items()
329
+ if name not in rddl.non_fluents}
330
+ else:
331
+ fluents = {}
327
332
 
328
333
  # set the next state to the current state
329
334
  for (state, next_state) in rddl.next_state.items():
@@ -367,7 +372,9 @@ class JaxRDDLCompiler:
367
372
  n_batch: int,
368
373
  check_constraints: bool=False,
369
374
  constraint_func: bool=False,
370
- init_params_constr: Dict[str, Any]={}) -> Callable:
375
+ init_params_constr: Dict[str, Any]={},
376
+ model_params_reduction: Callable=lambda x: x[0],
377
+ cache_path_info: bool=False) -> Callable:
371
378
  '''Compiles the current RDDL model into a JAX transition function that
372
379
  samples trajectories with a fixed horizon from a policy.
373
380
 
@@ -399,10 +406,13 @@ class JaxRDDLCompiler:
399
406
  returned log and does not raise an exception
400
407
  :param constraint_func: produces the h(s, a) constraint function
401
408
  in addition to the usual outputs
409
+ :param model_params_reduction: how to aggregate updated model_params across runs
410
+ in the batch (defaults to selecting the first element's parameters in the batch)
411
+ :param cache_path_info: whether to save full path traces as part of the log
402
412
  '''
403
413
  rddl = self.rddl
404
414
  jax_step_fn = self.compile_transition(
405
- check_constraints, constraint_func, init_params_constr)
415
+ check_constraints, constraint_func, init_params_constr, cache_path_info)
406
416
 
407
417
  # for POMDP only observ-fluents are assumed visible to the policy
408
418
  if rddl.observ_fluents:
@@ -421,7 +431,6 @@ class JaxRDDLCompiler:
421
431
  return jax_step_fn(subkey, actions, subs, model_params)
422
432
 
423
433
  # do a batched step update from the policy
424
- # TODO: come up with a better way to reduce the model_param batch dim
425
434
  def _jax_wrapped_batched_step_policy(carry, step):
426
435
  key, policy_params, hyperparams, subs, model_params = carry
427
436
  key, *subkeys = random.split(key, num=1 + n_batch)
@@ -430,7 +439,7 @@ class JaxRDDLCompiler:
430
439
  _jax_wrapped_single_step_policy,
431
440
  in_axes=(0, None, None, None, 0, None)
432
441
  )(keys, policy_params, hyperparams, step, subs, model_params)
433
- model_params = jax.tree_map(partial(jnp.mean, axis=0), model_params)
442
+ model_params = jax.tree_util.tree_map(model_params_reduction, model_params)
434
443
  carry = (key, policy_params, hyperparams, subs, model_params)
435
444
  return carry, log
436
445
 
@@ -440,7 +449,7 @@ class JaxRDDLCompiler:
440
449
  start = (key, policy_params, hyperparams, subs, model_params)
441
450
  steps = jnp.arange(n_steps)
442
451
  end, log = jax.lax.scan(_jax_wrapped_batched_step_policy, start, steps)
443
- log = jax.tree_map(partial(jnp.swapaxes, axis1=0, axis2=1), log)
452
+ log = jax.tree_util.tree_map(partial(jnp.swapaxes, axis1=0, axis2=1), log)
444
453
  model_params = end[-1]
445
454
  return log, model_params
446
455
 
@@ -707,7 +716,10 @@ class JaxRDDLCompiler:
707
716
  sample = jnp.asarray(value, dtype=self._fix_dtype(value))
708
717
  new_slices = [None] * len(jax_nested_expr)
709
718
  for (i, jax_expr) in enumerate(jax_nested_expr):
710
- new_slices[i], key, err, params = jax_expr(x, params, key)
719
+ new_slice, key, err, params = jax_expr(x, params, key)
720
+ if not jnp.issubdtype(jnp.result_type(new_slice), jnp.integer):
721
+ new_slice = jnp.asarray(new_slice, dtype=self.INT)
722
+ new_slices[i] = new_slice
711
723
  error |= err
712
724
  new_slices = tuple(new_slices)
713
725
  sample = sample[new_slices]
@@ -986,7 +998,8 @@ class JaxRDDLCompiler:
986
998
  sample_cases = [None] * len(jax_cases)
987
999
  for (i, jax_case) in enumerate(jax_cases):
988
1000
  sample_cases[i], key, err_case, params = jax_case(x, params, key)
989
- err |= err_case
1001
+ err |= err_case
1002
+ sample_cases = jnp.asarray(sample_cases)
990
1003
  sample_cases = jnp.asarray(sample_cases, dtype=self._fix_dtype(sample_cases))
991
1004
 
992
1005
  # predicate (enum) is an integer - use it to extract from case array
@@ -1056,15 +1056,13 @@ class ExactLogic(Logic):
1056
1056
  def control_if(self, id, init_params):
1057
1057
  return self._jax_wrapped_calc_if_then_else_exact
1058
1058
 
1059
- @staticmethod
1060
- def _jax_wrapped_calc_switch_exact(pred, cases, params):
1061
- pred = pred[jnp.newaxis, ...]
1062
- sample = jnp.take_along_axis(cases, pred, axis=0)
1063
- assert sample.shape[0] == 1
1064
- return sample[0, ...], params
1065
-
1066
1059
  def control_switch(self, id, init_params):
1067
- return self._jax_wrapped_calc_switch_exact
1060
+ def _jax_wrapped_calc_switch_exact(pred, cases, params):
1061
+ pred = jnp.asarray(pred[jnp.newaxis, ...], dtype=self.INT)
1062
+ sample = jnp.take_along_axis(cases, pred, axis=0)
1063
+ assert sample.shape[0] == 1
1064
+ return sample[0, ...], params
1065
+ return _jax_wrapped_calc_switch_exact
1068
1066
 
1069
1067
  # ===========================================================================
1070
1068
  # random variables