AgentsHelper Module¶
Helper functions for agent utilities and orchestration.
- class HMB.AgentsHelper.RandomAgent(ActionSpaceSampleFunc)[source]¶
Bases:
objectRandom policy baseline that always samples from a provided sampler. Useful as a non-learning baseline or to perform pure exploration.
Initialize the RandomAgent with a callable that samples random actions.
- Parameters:
ActionSpaceSampleFunc (callable) – Callable returning a random action index when called.
- class HMB.AgentsHelper.QAgent(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, epsilon=0.1)[source]¶
Bases:
objectBase tabular Q-agent with epsilon-greedy action selection.
This base class stores a Q-table and implements a simple epsilon-greedy action selection policy. Subclasses should implement UpdateParameters to perform learning updates (Q-learning, SARSA, Expected SARSA, …).
- Parameters:
ActionSpaceSampleFunc (callable) – Callable returning a sampled action index from the environment’s action space; used for exploration.
alpha (float) – Learning rate (0 < alpha <= 1).
gamma (float) – Discount factor for future rewards (0 <= gamma <= 1).
noOfStates (int) – Number of discrete states (size of first Q dimension).
noOfActions (int) – Number of discrete actions (size of second Q dimension).
epsilon (float, optional) – Exploration probability for epsilon-greedy policy (default 0.1).
- Variables:
qTable (numpy.ndarray) – Array of shape (noOfStates, noOfActions) storing Q-values.
epsilon (alpha, gamma,) – Learning and policy hyperparameters.
ActionSpaceSampleFunc (callable) – Stored reference to the sampler.
Initialize the QAgent and allocate the Q-table.
- Parameters:
ActionSpaceSampleFunc (callable) – Function to sample random actions.
alpha (float) – Learning rate.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
epsilon (float, optional) – Exploration probability for epsilon-greedy policy.
- __init__(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, epsilon=0.1)[source]¶
Initialize the QAgent and allocate the Q-table.
- Parameters:
ActionSpaceSampleFunc (callable) – Function to sample random actions.
alpha (float) – Learning rate.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
epsilon (float, optional) – Exploration probability for epsilon-greedy policy.
- class HMB.AgentsHelper.QLearningAgent(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, epsilon=0.1)[source]¶
Bases:
QAgentTabular Q-learning agent implementing the standard off-policy update.
Implements the classical Q-learning update which bootstraps using the maximum action-value in the next state.
Initialize the QAgent and allocate the Q-table.
- Parameters:
ActionSpaceSampleFunc (callable) – Function to sample random actions.
alpha (float) – Learning rate.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
epsilon (float, optional) – Exploration probability for epsilon-greedy policy.
- class HMB.AgentsHelper.SARSAAgent(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, epsilon=0.1)[source]¶
Bases:
QAgentOn-policy SARSA agent that updates using the Q-value of the next taken action.
Uses the (state, action, reward, nextState, nextAction) tuple to form the TD target for on-policy updates.
Initialize the QAgent and allocate the Q-table.
- Parameters:
ActionSpaceSampleFunc (callable) – Function to sample random actions.
alpha (float) – Learning rate.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
epsilon (float, optional) – Exploration probability for epsilon-greedy policy.
- class HMB.AgentsHelper.ExpectedSARSAAgent(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, epsilon=0.1)[source]¶
Bases:
QAgentExpected SARSA agent that uses the expectation under the epsilon-greedy policy as the bootstrap target.
The expected value is computed exactly by accounting for the epsilon mass that is shared between greedy and non-greedy actions and handling ties.
Initialize the QAgent and allocate the Q-table.
- Parameters:
ActionSpaceSampleFunc (callable) – Function to sample random actions.
alpha (float) – Learning rate.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
epsilon (float, optional) – Exploration probability for epsilon-greedy policy.
- class HMB.AgentsHelper.GreedyAgent(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions)[source]¶
Bases:
QAgentDeterministic greedy policy that always picks the current best action. This convenience subclass of QAgent enforces epsilon=0 for deterministic action selection.
Initialize GreedyAgent by calling QAgent with epsilon forced to 0.
- Parameters:
0. (See QAgent.__init__ for parameter descriptions; epsilon is forced to)
- class HMB.AgentsHelper.SoftmaxPolicyAgent(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, temperature=1.0, epsilon=0.0)[source]¶
Bases:
QAgentSoftmax (Boltzmann) policy over Q-values for stochastic action selection.
The temperature parameter controls exploration: lower values concentrate probability mass on higher-valued actions, higher values approach a uniform distribution. An optional small epsilon mixes the softmax probabilities with a uniform distribution.
Initialize the SoftmaxPolicyAgent.
- Parameters:
- class HMB.AgentsHelper.DoubleQLearningAgent(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, epsilon=0.1)[source]¶
Bases:
QAgentDouble Q-learning agent implementing two Q-tables to reduce overestimation.
On each update a random coin flip decides which table is updated; the other table is used to evaluate the chosen action (the Double Q-learning scheme described by Van Hasselt et al.).
Initialize double-Q tables and parameters.
- Parameters:
internally. (See QAgent.__init__ for parameter descriptions; an additional second Q-table is allocated)
- __init__(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, epsilon=0.1)[source]¶
Initialize double-Q tables and parameters.
- Parameters:
internally. (See QAgent.__init__ for parameter descriptions; an additional second Q-table is allocated)
- UpdateParameters(state, nextState, reward, action, nextAction=None)[source]¶
Perform Double Q-learning update by randomly choosing which table to update.
If the chosen table is A then the argmax is computed on A and evaluated using B (and vice-versa). This reduces the maximization bias present in standard Q-learning.
- class HMB.AgentsHelper.QLambdaAgent(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, lambd=0.9, epsilon=0.1)[source]¶
Bases:
QAgentQ(u03BB) agent with accumulating eligibility traces (tabular).
Implements off-policy Q(u03BB) with accumulating traces. The agent maintains an eligibility matrix of the same shape as the Q-table and updates all Q-values proportionally to their eligibility on each step.
Initialize QLambdaAgent with eligibility traces.
- Parameters:
lambd (float) – Eligibility trace decay parameter (u03BB), typically in [0, 1].
- __init__(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, lambd=0.9, epsilon=0.1)[source]¶
Initialize QLambdaAgent with eligibility traces.
- Parameters:
lambd (float) – Eligibility trace decay parameter (u03BB), typically in [0, 1].
- ResetTraces()[source]¶
Reset eligibility traces to zero.
Call this at the start of each episode if episodic resets are used.
- class HMB.AgentsHelper.SARSALambdaAgent(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, lambd=0.9, epsilon=0.1, traceType='accumulating')[source]¶
Bases:
QAgentOn-policy SARSA(u03BB) agent with eligibility traces.
This agent implements accumulating or replacing eligibility traces for SARSA-style on-policy learning. Call ResetTraces() at episode start.
- Parameters:
ActionSpaceSampleFunc (callable) – Action sampler used for exploration.
alpha (float) – Learning rate.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
lambd (float, optional) – Trace-decay parameter (default 0.9).
epsilon (float, optional) – Epsilon for epsilon-greedy policy (default 0.1).
trace_type (str, optional) – “accumulating” or “replacing” (default “accumulating”).
Initialize the SARSALambdaAgent.
- Parameters:
ActionSpaceSampleFunc (callable) – Function to sample random actions.
alpha (float) – Learning rate.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
lambd (float, optional) – Trace-decay parameter for eligibility traces.
epsilon (float, optional) – Exploration probability for epsilon-greedy policy.
traceType (str, optional) – Type of eligibility traces (“accumulating” or “replacing”).
- __init__(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, lambd=0.9, epsilon=0.1, traceType='accumulating')[source]¶
Initialize the SARSALambdaAgent.
- Parameters:
ActionSpaceSampleFunc (callable) – Function to sample random actions.
alpha (float) – Learning rate.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
lambd (float, optional) – Trace-decay parameter for eligibility traces.
epsilon (float, optional) – Exploration probability for epsilon-greedy policy.
traceType (str, optional) – Type of eligibility traces (“accumulating” or “replacing”).
- class HMB.AgentsHelper.MonteCarloAgent(ActionSpaceSampleFunc, gamma, noOfStates, noOfActions, epsilon=0.1, useFirstVisit=True)[source]¶
Bases:
objectFirst-visit Monte Carlo control (episodic) with incremental averaging.
Stores an episode buffer and updates Q at episode end using returns.
- Parameters:
ActionSpaceSampleFunc (callable) – Action sampler for exploration.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
epsilon (float, optional) – Epsilon for epsilon-greedy policy (default 0.1).
useFirstVisit (bool, optional) – If True use first-visit MC, otherwise every-visit.
Initialize the MonteCarloAgent.
- Parameters:
ActionSpaceSampleFunc (callable) – Function to sample random actions.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
epsilon (float, optional) – Exploration probability for epsilon-greedy policy.
useFirstVisit (bool, optional) – If True use first-visit MC, otherwise every-visit.
- __init__(ActionSpaceSampleFunc, gamma, noOfStates, noOfActions, epsilon=0.1, useFirstVisit=True)[source]¶
Initialize the MonteCarloAgent.
- Parameters:
ActionSpaceSampleFunc (callable) – Function to sample random actions.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
epsilon (float, optional) – Exploration probability for epsilon-greedy policy.
useFirstVisit (bool, optional) – If True use first-visit MC, otherwise every-visit.
- class HMB.AgentsHelper.DynaQAgent(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, epsilon=0.1, planningSteps=5)[source]¶
Bases:
QAgentDyna-Q agent: model-based planning with a simple tabular model.
Learns a one-step model (last observed next state and reward per (s,a)) and performs planning updates by sampling previously observed pairs.
- Parameters:
ActionSpaceSampleFunc (callable) – Action sampler for exploration.
alpha (float) – Learning rate.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
epsilon (float, optional) – Epsilon for epsilon-greedy policy (default 0.1).
planningSteps (int, optional) – Number of model-based planning updates per real step.
Initialize the DynaQAgent.
- Parameters:
ActionSpaceSampleFunc (callable) – Function to sample random actions.
alpha (float) – Learning rate.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
epsilon (float, optional) – Exploration probability for epsilon-greedy policy.
planningSteps (int, optional) – Number of planning steps to perform.
- __init__(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, epsilon=0.1, planningSteps=5)[source]¶
Initialize the DynaQAgent.
- Parameters:
ActionSpaceSampleFunc (callable) – Function to sample random actions.
alpha (float) – Learning rate.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
epsilon (float, optional) – Exploration probability for epsilon-greedy policy.
planningSteps (int, optional) – Number of planning steps to perform.
- class HMB.AgentsHelper.UCB1Agent(ActionSpaceSampleFunc, noOfActions, c=1.0)[source]¶
Bases:
objectUCB1 multi-armed bandit agent (no states).
Uses the UCB1 formula to select actions in pure bandit problems.
Initialize the UCB1Agent.
- Parameters:
- class HMB.AgentsHelper.CountBonusQLAgent(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, epsilon=0.1, bonus_coef=1.0, countType='state')[source]¶
Bases:
QAgentQ-learning augmented with a count-based intrinsic bonus to encourage exploration.
The bonus can be computed per-state or per state-action pair.
Initialize the CountBonusQLAgent.
- Parameters:
ActionSpaceSampleFunc (callable) – Function to sample random actions.
alpha (float) – Learning rate.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
epsilon (float, optional) – Exploration probability for epsilon-greedy policy.
bonus_coef (float, optional) – Coefficient for the exploration bonus.
countType (str, optional) – Type of count-based bonus (“state” or “state_action”).
- __init__(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, epsilon=0.1, bonus_coef=1.0, countType='state')[source]¶
Initialize the CountBonusQLAgent.
- Parameters:
ActionSpaceSampleFunc (callable) – Function to sample random actions.
alpha (float) – Learning rate.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
epsilon (float, optional) – Exploration probability for epsilon-greedy policy.
bonus_coef (float, optional) – Coefficient for the exploration bonus.
countType (str, optional) – Type of count-based bonus (“state” or “state_action”).
- class HMB.AgentsHelper.NStepTDAgent(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, n=3, epsilon=0.1)[source]¶
Bases:
objectn-step on-policy TD agent (n-step SARSA style).
Maintains a buffer of recent transitions and performs n-step updates.
Initialize the NStepTDAgent.
- Parameters:
ActionSpaceSampleFunc (callable) – Function to sample random actions.
alpha (float) – Learning rate.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
n (int, optional) – Number of steps for n-step TD updates (default 3).
epsilon (float, optional) – Exploration probability for epsilon-greedy policy.
- __init__(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, n=3, epsilon=0.1)[source]¶
Initialize the NStepTDAgent.
- Parameters:
ActionSpaceSampleFunc (callable) – Function to sample random actions.
alpha (float) – Learning rate.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
n (int, optional) – Number of steps for n-step TD updates (default 3).
epsilon (float, optional) – Exploration probability for epsilon-greedy policy.
- class HMB.AgentsHelper.PrioritizedSweepingAgent(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, epsilon=0.1, planningSteps=5, theta=0.0001)[source]¶
Bases:
QAgentPrioritized Sweeping agent (model-based planning with prioritized updates).
This agent maintains a one-step model (reward and next-state for each observed (s,a)) and a predecessor map for states. When a real transition is observed it computes a priority for the state and uses a priority queue to selectively perform planning updates on predecessor state-action pairs with largest expected TD error first. This is useful for sample-efficient planning in small tabular MDPs.
- Parameters:
ActionSpaceSampleFunc (callable) – Action sampler used for exploration.
alpha (float) – Learning rate for Q-value updates.
gamma (float) – Discount factor.
noOfStates (int) – Number of discrete states.
noOfActions (int) – Number of discrete actions.
epsilon (float, optional) – Epsilon for epsilon-greedy action selection (default 0.1).
planningSteps (int, optional) – Number of prioritized-planning iterations to perform per real update (default 5).
theta (float, optional) – Priority threshold; only priorities greater than theta are pushed (default 1e-4).
Initialize PrioritizedSweepingAgent and allocate model/priorities.
- __init__(ActionSpaceSampleFunc, alpha, gamma, noOfStates, noOfActions, epsilon=0.1, planningSteps=5, theta=0.0001)[source]¶
Initialize PrioritizedSweepingAgent and allocate model/priorities.
- UpdateParameters(state, nextState, reward, action, nextAction=None)[source]¶
- Process a real experience (state, action, reward, nextState):
Update Q with a standard Q-learning step (real experience).
Update internal one-step model and predecessors.
Compute priority for the experienced state and push it if large.
Run up to planningSteps prioritized planning updates.