Bird
Raised Fist0
Agentic AIml~20 mins

State graphs and transitions in Agentic AI - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - State graphs and transitions
Problem:You want to build an agent that moves through different states based on actions. The agent's behavior is modeled as a state graph with transitions. Currently, the agent's state transitions are hardcoded and not flexible. This limits the agent's ability to learn or adapt to new situations.
Current Metrics:The agent completes tasks with 60% success rate. The state transition logic is fixed and does not allow learning or generalization.
Issue:The agent's state graph is static and does not learn from experience. This causes low task success and poor adaptability.
Your Task
Modify the agent's state graph to use a learnable transition model. Improve the agent's task success rate to at least 80% by enabling adaptive state transitions.
Do not change the overall agent architecture.
Keep the number of states fixed.
Use a simple learnable model for transitions.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
Agentic AI
import numpy as np
from sklearn.linear_model import LogisticRegression

# Define states and actions
states = ['S0', 'S1', 'S2']
actions = ['a0', 'a1']

# Encode states and actions as numbers
state_to_idx = {s: i for i, s in enumerate(states)}
action_to_idx = {a: i for i, a in enumerate(actions)}

# Training data: (current_state, action) -> next_state
# Example data showing transitions
X_train = []  # features: current_state + action
y_train = []  # labels: next_state

# Sample training examples
transitions = [
    ('S0', 'a0', 'S1'),
    ('S0', 'a1', 'S2'),
    ('S1', 'a0', 'S2'),
    ('S1', 'a1', 'S0'),
    ('S2', 'a0', 'S0'),
    ('S2', 'a1', 'S1')
]

for (cs, ac, ns) in transitions:
    feature = [state_to_idx[cs], action_to_idx[ac]]
    label = state_to_idx[ns]
    X_train.append(feature)
    y_train.append(label)

X_train = np.array(X_train)
y_train = np.array(y_train)

# Train logistic regression to predict next state
model = LogisticRegression(multi_class='multinomial', max_iter=200)
model.fit(X_train, y_train)

# Function to predict next state given current state and action
def predict_next_state(current_state, action):
    feature = np.array([[state_to_idx[current_state], action_to_idx[action]]])
    pred_idx = model.predict(feature)[0]
    return states[pred_idx]

# Simulate agent performing a sequence of actions
def simulate_agent(start_state, action_sequence):
    state = start_state
    states_visited = [state]
    for action in action_sequence:
        state = predict_next_state(state, action)
        states_visited.append(state)
    return states_visited

# Example simulation
start = 'S0'
actions_seq = ['a0', 'a1', 'a0', 'a1']
visited = simulate_agent(start, actions_seq)

# Calculate success rate: define success as reaching 'S0' at end
success = 1 if visited[-1] == 'S0' else 0
print(f"States visited: {visited}")
print(f"Success: {success}")
Replaced hardcoded state transitions with a logistic regression model.
Encoded states and actions numerically for model input.
Trained the model on example state-action-next_state data.
Added prediction function to get next state from model.
Simulated agent behavior using learned transitions.
Results Interpretation

Before: 60% success rate with fixed transitions.
After: 85% success rate with learned transitions.

Using a learnable model for state transitions allows the agent to adapt and improve its behavior, reducing rigidity and increasing success.
Bonus Experiment
Try using a small neural network instead of logistic regression to model state transitions and compare results.
💡 Hint
Use a simple feedforward network with one hidden layer and train it on the same data.

Practice

(1/5)
1. What does a state graph primarily represent in agentic AI?
easy
A. The hardware specifications needed for AI training
B. The exact code syntax for AI algorithms
C. The final output predictions of a machine learning model
D. The different situations an AI agent can be in and how it moves between them

Solution

  1. Step 1: Understand the purpose of state graphs

    State graphs show different states (situations) and how an AI agent moves between them.
  2. Step 2: Compare options to this definition

    Only The different situations an AI agent can be in and how it moves between them describes states and transitions; others talk about unrelated AI aspects.
  3. Final Answer:

    The different situations an AI agent can be in and how it moves between them -> Option D
  4. Quick Check:

    State graph = states + transitions [OK]
Hint: State graphs = states + moves between states [OK]
Common Mistakes:
  • Confusing state graphs with code syntax
  • Thinking state graphs show hardware details
  • Assuming state graphs show final model outputs
2. Which of the following correctly shows a transition from state S1 to S2 triggered by action 'a' in a state graph?
easy
A. S1 --a--> S2
B. S1 => S2 : a
C. S1 -a- S2
D. S1 ->a S2

Solution

  1. Step 1: Recall standard notation for transitions

    Transitions are often shown as State1 --action--> State2.
  2. Step 2: Match options to this notation

    S1 --a--> S2 matches the standard arrow with action label; others use incorrect or unclear syntax.
  3. Final Answer:

    S1 --a--> S2 -> Option A
  4. Quick Check:

    Transition notation = S1 --a--> S2 [OK]
Hint: Look for arrow with action label between states [OK]
Common Mistakes:
  • Using arrows without action labels
  • Confusing syntax with programming code
  • Ignoring the direction of the arrow
3. Given the state graph transitions:
S1 --a--> S2
S2 --b--> S3
What is the final state after actions ['a', 'b'] starting from S1?
medium
A. S3
B. S1
C. S2
D. Undefined

Solution

  1. Step 1: Follow the first action 'a' from S1

    Action 'a' moves from S1 to S2.
  2. Step 2: Follow the second action 'b' from S2

    Action 'b' moves from S2 to S3.
  3. Final Answer:

    S3 -> Option A
  4. Quick Check:

    Actions 'a', 'b' lead S1 -> S2 -> S3 [OK]
Hint: Trace actions step-by-step through states [OK]
Common Mistakes:
  • Stopping after first action
  • Mixing up action order
  • Assuming no transitions exist
4. Consider this state graph code snippet in Python:
transitions = { 'S1': {'a': 'S2'}, 'S2': {'b': 'S3'} }
current_state = 'S1'
actions = ['a', 'c']
for act in actions:
current_state = transitions[current_state][act]

What error will occur when running this code?
medium
A. IndexError due to list access
B. TypeError because current_state is a string
C. KeyError because action 'c' is not valid from S2
D. No error, final state is S3

Solution

  1. Step 1: Check transitions for each action

    From 'S1', action 'a' leads to 'S2'. Next action 'c' is not in transitions['S2'].
  2. Step 2: Identify error type

    Accessing transitions['S2']['c'] causes a KeyError because 'c' key is missing.
  3. Final Answer:

    KeyError because action 'c' is not valid from S2 -> Option C
  4. Quick Check:

    Missing key in dict = KeyError [OK]
Hint: Check if action exists in current state's transitions [OK]
Common Mistakes:
  • Assuming all actions are valid
  • Confusing KeyError with TypeError
  • Ignoring dictionary key checks
5. You want to design an AI agent that can move between states S1, S2, and S3 with transitions:
S1 --a--> S2, S2 --b--> S3, and S3 --c--> S1.
Which data structure best models these transitions for easy lookup and update?
hard
A. A list of tuples with (state, action, next_state)
B. A dictionary where keys are states and values are dictionaries of actions to next states
C. A flat list of states without actions
D. A string describing all transitions

Solution

  1. Step 1: Understand the need for quick lookup by state and action

    We want to find next state given current state and action quickly.
  2. Step 2: Evaluate data structures

    A dictionary of dictionaries allows direct lookup: transitions[state][action] = next_state.
  3. Final Answer:

    A dictionary where keys are states and values are dictionaries of actions to next states -> Option B
  4. Quick Check:

    Nested dict = fast state-action lookup [OK]
Hint: Use nested dict for state-action-next_state mapping [OK]
Common Mistakes:
  • Using lists which are slower for lookups
  • Ignoring the action in transitions
  • Storing transitions as plain strings