TensorTonic
Study Plans
Study Plan
Initialize Legal MCTS Edge Statistics
Reinforcement Learning
Easy

A newly expanded search node needs one edge per action. Only legal actions receive prior probability. For legal policy logit lal_ala​ and largest legal logit mmm, compute

Pa=exp⁡(la−m)∑b∈Lexp⁡(lb−m).P_a=\frac{\exp(l_a-m)}{\sum_{b\in\mathcal{L}}\exp(l_b-m)}.Pa​=∑b∈L​exp(lb​−m)exp(la​−m)​.

Here L\mathcal{L}L is the set of legal actions and PaP_aPa​ is the prior for action aaa. Illegal actions receive prior zero. Every edge starts with visit count NNN, total value WWW, and mean value QQQ equal to zero. Return a dictionary with keys N, W, Q, and P. N is a NumPy int64 array; W, Q, and P are NumPy float64 arrays. All four are one-dimensional and aligned with the input actions.

Loading visualization...

Examples

Input: policy_logits = [0,0,0], legal_mask = [true,false,true]

Output: {"N":[0,0,0],"W":[0,0,0],"Q":[0,0,0],"P":[0.5,0,0.5]}

Explanation: The illegal middle action is excluded before the two equal legal logits are normalized.

Input: policy_logits = [1000,-1000,999], legal_mask = [true,false,true]

Output: {"N":[0,0,0],"W":[0,0,0],"Q":[0,0,0],"P":[0.731059,0,0.268941]}

Input: policy_logits = [5,2,-3], legal_mask = [false,false,true]

Output: {"N":[0,0,0],"W":[0,0,0],"Q":[0,0,0],"P":[0,0,1]}

Hint 1

np.flatnonzero finds the legal action indices.

Hint 2

np.zeros supports an explicit dtype for each statistic.

Hint 3

np.exp can operate on just the selected legal logits.

Requirements

  • Use NumPy float64 for probability computation.
  • Preserve both input arrays.

Constraints

  • Policy logits and legal mask are aligned, nonempty one-dimensional NumPy arrays. Logits are numeric and the mask has Boolean dtype.
  • The legal mask contains Boolean values and at least one legal action.
  • Every policy logit is finite.
Try Similar Problems
Select an Action with PUCTEasyBack Up an MCTS Simulation PathMediumFind a Go Group and Its LibertiesEasyMix Dirichlet Noise into Root PriorsEasySample a Recent Self-Play Replay BatchEasyTurn Root Visits into a Search PolicyMediumCompute the Policy and Value HeadsMediumApply an AlphaGo Residual BlockMediumEpsilon-Greedy Action SelectionEasyDouble DQN TargetEasySoftmax (Boltzmann) ExplorationEasyQ-Learning UpdateEasy

Accepts: array

Accepts: array

You must run your code first.

Initialize Legal MCTS Edge Statistics

Reinforcement Learning
Easy

A newly expanded search node needs one edge per action. Only legal actions receive prior probability. For legal policy logit lal_ala​ and largest legal logit mmm, compute

Pa=exp⁡(la−m)∑b∈Lexp⁡(lb−m).P_a=\frac{\exp(l_a-m)}{\sum_{b\in\mathcal{L}}\exp(l_b-m)}.Pa​=∑b∈L​exp(lb​−m)exp(la​−m)​.

Here L\mathcal{L}L is the set of legal actions and PaP_aPa​ is the prior for action aaa. Illegal actions receive prior zero. Every edge starts with visit count NNN, total value WWW, and mean value QQQ equal to zero. Return a dictionary with keys N, W, Q, and P. N is a NumPy int64 array; W, Q, and P are NumPy float64 arrays. All four are one-dimensional and aligned with the input actions.

Loading visualization...

Examples

Input: policy_logits = [0,0,0], legal_mask = [true,false,true]

Output: {"N":[0,0,0],"W":[0,0,0],"Q":[0,0,0],"P":[0.5,0,0.5]}

Explanation: The illegal middle action is excluded before the two equal legal logits are normalized.

Input: policy_logits = [1000,-1000,999], legal_mask = [true,false,true]

Output: {"N":[0,0,0],"W":[0,0,0],"Q":[0,0,0],"P":[0.731059,0,0.268941]}

Input: policy_logits = [5,2,-3], legal_mask = [false,false,true]

Output: {"N":[0,0,0],"W":[0,0,0],"Q":[0,0,0],"P":[0,0,1]}

Hint 1

np.flatnonzero finds the legal action indices.

Hint 2

np.zeros supports an explicit dtype for each statistic.

Hint 3

np.exp can operate on just the selected legal logits.

Requirements

  • Use NumPy float64 for probability computation.
  • Preserve both input arrays.

Constraints

  • Policy logits and legal mask are aligned, nonempty one-dimensional NumPy arrays. Logits are numeric and the mask has Boolean dtype.
  • The legal mask contains Boolean values and at least one legal action.
  • Every policy logit is finite.
Try Similar Problems
Select an Action with PUCTEasyBack Up an MCTS Simulation PathMediumFind a Go Group and Its LibertiesEasyMix Dirichlet Noise into Root PriorsEasySample a Recent Self-Play Replay BatchEasyTurn Root Visits into a Search PolicyMediumCompute the Policy and Value HeadsMediumApply an AlphaGo Residual BlockMediumEpsilon-Greedy Action SelectionEasyDouble DQN TargetEasySoftmax (Boltzmann) ExplorationEasyQ-Learning UpdateEasy

Accepts: array

Accepts: array

You must run your code first.