def plan(model, obs, init, rng, noise=c, num_cand=num_cand, scale=c):
"""Random shooting around a plan for each of B states: obs (B, 3), init
(B, n, 1). Each plan is perturbed into num_cand candidates (the plan
itself is one), all are rolled out in the model, and the candidate with
the highest predicted return is returned with that return."""
B = len(obs)
A = init[:, None] + noise * rng.standard_normal(
(B, num_cand) + init.shape[1:], dtype=np.float32)
A[:, 0] = init
A = np.clip(A, -scale, scale)
s = np.repeat(obs[:, None], num_cand, axis=1) # (B, num_cand, 3)
ret = rollout_model(model, s, A)[1].sum(-1) # (B, num_cand)
best = ret.argmax(1)
return A[np.arange(B), best], ret[np.arange(B), best]