liveBook · Manning

State

As explained in Grokking AI Algorithms, Second Edition

A state is a configuration of the environment at a particular point in an agent's interaction with it.

A state is a data structure describing a possible solution or the environment’s configuration at a particular point in an agent’s interaction. Its meaning depends on the problem: it can encode selected items in a knapsack or record an agent’s current location in a simulator. section 4.4 section 10.5

States support two practical workflows:

  • Encode a candidate solution with a fixed representation. In the Knapsack Problem, each binary position marks an item as excluded (0) or included (1); a collection of states forms a population. section 4.4
  • Track interaction with an environment. A simulator resets the starting state, returns the current state, applies actions, calculates rewards, and checks whether the goal is achieved. section 10.5

A parking-lot state

A simple simulator stores the agent’s position as coordinates. Applying an action proposes a new position, checks whether it is within bounds, updates the coordinates when valid, and assigns a reward based on the destination cell. The state can then be encoded as a single value derived from the grid dimensions and the agent’s coordinates. section 10.5

Figure 4.11 Binary-encoding the Knapsack Problem
04_11

Visualizes how fixed binary positions represent included and excluded items in a Knapsack state. section 4.4

class Simulator:
    def __init__(self, road, road_size_x, road_size_y,
    ➥agent_start_x, agent_start_y):
        self.road = road
        self.road_size_x = road_size_x
        self.road_size_y = road_size_y
        self.agent_x = agent_start_x
        self.agent_y = agent_start_y #A
 
    def move_agent(self, action):
        next_x, next_y = self.agent_x, self.agent_y
 
        if action == COMMAND_NORTH: #B
            next_x -= 1
        elif action == COMMAND_SOUTH:
            next_x += 1
        elif action == COMMAND_EAST:
            next_y += 1
        elif action == COMMAND_WEST:
            next_y -= 1
 
        reward_update = 0
 
        if self.is_within_bounds(next_x, next_y): #C
            reward_update = self.cost_movement(next_x, next_y) #D
            self.agent_x, self.agent_y = next_x, next_y
        else:
            reward_update = ROAD_OUT_OF_BOUNDS_REWARD #E
 
        return reward_update

Shows state updates when a simulator applies movement actions and handles bounds and rewards. section 10.5

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Defines fixed representations and traces state updates through actions, rewards, bounds, and goal checks.
Examples
Uses binary-encoded knapsack selections and a parking-lot simulator with coordinates, collisions, rewards, and paths.
What you can do after
Build it from scratch, represent candidate solutions or implement simulator state access and updates.
How it connects
Connects states with chromosomes, genes, populations, actions, rewards, environments, and goal achievement.

Also explained in

Compare how 39 books explain state →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.