Skip to content

ForagerRL_step5

RoiArthurB edited this page Jul 28, 2026 · 4 revisions

5. Q-Learning Algorithm

By Killian Trouillet


Step 5: Q-Learning Algorithm

Content

This is the key step. We replace random movement with the Q-Learning algorithm. The agent now makes decisions based on its Q-Table and improves its policy after every step using the Bellman equation.

Formulation

  • Definition of RL hyperparameters: learning rate (α), discount factor (γ), epsilon (ε)
  • Implementation of epsilon-greedy action selection
  • Implementation of the Q-Learning update rule (Bellman equation)
  • Decay of epsilon over episodes (exploration → exploitation)

Model Definition

Hyperparameters

We define the RL parameters as global variables so they can be adjusted via the experiment interface:

global {
    // ... (previous variables)
    float alpha <- 0.1;         // Learning rate: how fast the agent learns
    float gamma_rl <- 0.95;     // Discount factor: importance of future rewards
    float epsilon <- 1.0;       // Exploration rate: probability of random action
    float epsilon_min <- 0.01;  // Minimum exploration rate
    float epsilon_decay <- 0.995; // Multiply epsilon by this after each episode
  • α (alpha): Controls how much new information overrides old information. Higher = faster learning but less stable.
  • γ (gamma_rl): How much the agent values future rewards vs. immediate ones. 0.95 means future rewards are almost as important as immediate ones.
  • ε (epsilon): The probability of taking a random action instead of the best known action. Starts at 1.0 (100% random) and decays over time.

Epsilon-greedy action selection

This is the core decision-making function. With probability ε, the agent explores (picks a random action). Otherwise, it exploits (picks the action with the highest Q-value):

    string choose_action {
        string s <- get_state();
        // Explore: random action
        if (flip(epsilon)) {
            return action_list[rnd(3)];
        }
        // Exploit: best known action
        string best_action <- action_list[0];
        float best_q <- get_q(s, action_list[0]);
        loop a over: action_list {
            float q_val <- get_q(s, a);
            if (q_val > best_q) {
                best_q <- q_val;
                best_action <- a;
            }
        }
        return best_action;
    }
  • flip(epsilon): Returns true with probability epsilon.

Q-Learning update (Bellman equation)

After every action, we update the Q-value for the (state, action) pair we just used. The formula is:

Q(s,a) ← Q(s,a) + α × [r + γ × max Q(s', a') − Q(s,a)]

In GAML:

    action update_q_value (string s, string a, float r, string s_next) {
        float old_q <- get_q(s, a);
        // Find the max Q-value achievable from the new state
        float max_next_q <- get_q(s_next, action_list[0]);
        loop act over: action_list {
            float q_val <- get_q(s_next, act);
            if (q_val > max_next_q) {
                max_next_q <- q_val;
            }
        }
        // Apply the Bellman equation
        float new_q <- old_q + alpha * (r + gamma_rl * max_next_q - old_q);
        q_table[s + "::" + a] <- new_q;
    }

Modified movement reflex

The random_move reflex becomes act, using the Q-Learning pipeline:

    reflex act {
        string current_state <- get_state();
        string action_taken <- choose_action();

        // ... (movement and reward logic as before)

        string new_state <- get_state();
        do update_q_value(current_state, action_taken, step_reward, new_state);
        episode_reward <- episode_reward + step_reward;
    }

Epsilon decay

At the end of each episode, we reduce epsilon to make the agent explore less and exploit more:

    // In end_episode action:
    if (epsilon > epsilon_min) {
        epsilon <- epsilon * epsilon_decay;
    }

Experiment parameters

We expose the hyperparameters in the GUI so users can experiment:

experiment smart_forager type: gui {
    parameter "Learning Rate (α)" var: alpha min: 0.01 max: 1.0 category: "RL";
    parameter "Discount Factor (γ)" var: gamma_rl min: 0.0 max: 1.0 category: "RL";
    parameter "Initial Epsilon (ε)" var: epsilon min: 0.0 max: 1.0 category: "RL";
    parameter "Epsilon Decay" var: epsilon_decay min: 0.9 max: 1.0 category: "RL";
    // ...

Complete Model

/**
* Name: SmartForager - Step 5: Q-Learning Algorithm
* Author: Killian Trouillet
* Description: This fifth step replaces random movement with the Q-Learning algorithm.
*              The forager now uses epsilon-greedy action selection and updates its Q-Table
*              after each step using the Bellman equation.
* Tags: reinforcement-learning, q-learning, epsilon-greedy, tutorial
*/

model SmartForager

global {
	int grid_size <- 10;
	int food_x <- 9;
	int food_y <- 9;
	list<point> obstacle_positions <- [{2,2}, {3,2}, {2,3}, {6,4}, {7,4}, {7,5}];
	
	int max_steps_per_episode <- 200;
	int step_count <- 0;
	int episode <- 0;
	float episode_reward <- 0.0;
	float last_episode_reward <- 0.0;
	bool food_found <- false;
	
	// === NEW: RL Hyperparameters ===
	float alpha <- 0.1;        // Learning rate
	float gamma_rl <- 0.95;    // Discount factor
	float epsilon <- 1.0;      // Exploration rate (starts high)
	float epsilon_min <- 0.01; // Minimum exploration rate
	float epsilon_decay <- 0.995; // Decay factor per episode
	
	init {
		ask world_cell grid_at {food_x, food_y} {
			is_food <- true;
		}
		loop pos over: obstacle_positions {
			ask world_cell grid_at pos {
				is_obstacle <- true;
			}
		}
		create forager number: 1 {
			my_cell <- world_cell grid_at {0, 0};
			location <- my_cell.location;
		}
	}
	
	reflex manage_episode {
		step_count <- step_count + 1;
		if (food_found or step_count >= max_steps_per_episode) {
			do end_episode();
		}
	}
	
	action end_episode() {
		episode <- episode + 1;
		last_episode_reward <- episode_reward;
		write "Ep " + episode + " | Steps: " + step_count 
		      + " | Reward: " + round(episode_reward) 
		      + " | Eps: " + (epsilon with_precision 3)
		      + " | Q-size: " + length(forager[0].q_table);
		
		episode_reward <- 0.0;
		step_count <- 0;
		food_found <- false;
		
		// Decay epsilon
		if (epsilon > epsilon_min) {
			epsilon <- epsilon * epsilon_decay;
		}
		
		ask world_cell grid_at {food_x, food_y} {
			is_food <- true;
		}
		ask forager[0] {
			my_cell <- world_cell grid_at {0, 0};
			location <- my_cell.location;
		}
	}
}

grid world_cell width: 10 height: 10 neighbors: 4 {
	bool is_food <- false;
	bool is_obstacle <- false;
	rgb color <- #white update: is_obstacle ? rgb(60, 60, 60) : #white;
}

species forager {
	world_cell my_cell;
	
	map<string, float> q_table;
	list<string> action_list <- ["up", "right", "down", "left"];
	
	string get_state() {
		return string(my_cell.grid_x) + "_" + string(my_cell.grid_y);
	}
	
	float get_q (string s, string a) {
		string key <- s + "::" + a;
		if (q_table contains_key key) {
			return float(q_table[key]);
		}
		return 0.0;
	}
	
	// === NEW: Epsilon-greedy action selection ===
	string choose_action() {
		string s <- get_state();
		// With probability epsilon, explore (random action)
		if (flip(epsilon)) {
			return action_list[rnd(3)];
		}
		// Otherwise, exploit (pick best known action)
		string best_action <- action_list[0];
		float best_q <- get_q(s, action_list[0]);
		loop a over: action_list {
			float q_val <- get_q(s, a);
			if (q_val > best_q) {
				best_q <- q_val;
				best_action <- a;
			}
		}
		return best_action;
	}
	
	// === NEW: Q-value update using Bellman equation ===
	action update_q_value (string s, string a, float r, string s_next) {
		float old_q <- get_q(s, a);
		// Find max Q-value for the next state
		float max_next_q <- get_q(s_next, action_list[0]);
		loop act over: action_list {
			float q_val <- get_q(s_next, act);
			if (q_val > max_next_q) {
				max_next_q <- q_val;
			}
		}
		// Bellman equation: Q(s,a) = Q(s,a) + α * [r + γ * max Q(s',a') - Q(s,a)]
		float new_q <- old_q + alpha * (r + gamma_rl * max_next_q - old_q);
		q_table[s + "::" + a] <- new_q;
	}
	
	// === MODIFIED: Use Q-Learning instead of random walk ===
	reflex act {
		string current_state <- get_state();
		string action_taken <- choose_action();
		
		// Translate action string to grid movement
		int new_x <- my_cell.grid_x;
		int new_y <- my_cell.grid_y;
		
		switch action_taken {
			match "up"    { new_y <- new_y - 1; }
			match "right" { new_x <- new_x + 1; }
			match "down"  { new_y <- new_y + 1; }
			match "left"  { new_x <- new_x - 1; }
		}
		
		float step_reward <- -1.0;
		
		if (new_x >= 0 and new_x < grid_size and new_y >= 0 and new_y < grid_size) {
			world_cell target <- world_cell grid_at {new_x, new_y};
			if (not target.is_obstacle) {
				my_cell <- target;
				location <- my_cell.location;
				if (my_cell.is_food) {
					my_cell.is_food <- false;
					step_reward <- 100.0;
					food_found <- true;
				}
			} else {
				step_reward <- -5.0;
			}
		} else {
			step_reward <- -5.0;
		}
		
		// Q-Learning update
		string new_state <- get_state();
		do update_q_value(current_state, action_taken, step_reward, new_state);
		
		episode_reward <- episode_reward + step_reward;
	}
	
	aspect default {
		draw circle(0.8) color: #blue;
	}
}

experiment smart_forager type: gui {
	parameter "Learning Rate (α)" var: alpha min: 0.01 max: 1.0 category: "RL";
	parameter "Discount Factor (γ)" var: gamma_rl min: 0.0 max: 1.0 category: "RL";
	parameter "Initial Epsilon (ε)" var: epsilon min: 0.0 max: 1.0 category: "RL";
	parameter "Epsilon Decay" var: epsilon_decay min: 0.9 max: 1.0 category: "RL";
	parameter "Max steps per episode" var: max_steps_per_episode min: 50 max: 1000 category: "Simulation";
	
	output {
		display Grid_World title: "Grid World" {
			grid world_cell border: #lightgray;
			species forager;
			graphics "food" {
				ask world_cell where each.is_food {
					draw circle(5) color: rgb(50, 180, 50);
				}
			}
		}
		monitor "Episode" value: episode;
		monitor "Step" value: step_count;
		monitor "Current Reward" value: episode_reward;
		monitor "Last Episode Reward" value: last_episode_reward;
		monitor "Epsilon" value: epsilon with_precision 4;
		monitor "Q-Table Size" value: length(forager[0].q_table);
	}
}
  1. What's new (Changelog)
  2. Migration Guide (2025-06 → 2026-06)
  1. Installation and Launching
    1. Installation
    2. Launching GAMA
    3. Updating GAMA
    4. Installing Plugins
  2. Workspace, Projects and Models
    1. Navigating in the Workspace
    2. Changing Workspace
    3. Importing Models
  3. Editing Models
    1. GAML Editor (Generalities)
    2. GAML Editor Tools
    3. Validation of Models
  4. Running Experiments
    1. Launching Experiments
    2. Experiments User interface
    3. Controls of experiments
    4. Parameters view
    5. Inspectors and monitors
    6. Displays
    7. Batch Specific UI
    8. Errors View
  5. Running Headless
    1. Getting Started
    2. Headless Legacy
    3. Headless Batch
  6. Preferences
  7. Troubleshooting
  1. Introduction
    1. Start with GAML
    2. Organization of a Model
    3. Basic programming concepts in GAML
  2. Manipulate basic Species
  3. Global Species
    1. Regular Species
    2. Defining Actions and Behaviors
    3. Interaction between Agents
    4. Attaching Skills
    5. Inheritance
  4. Defining Advanced Species
    1. Grid Species
    2. Graph Species
    3. Mirror Species
    4. Multi-Level Architecture
  5. Defining GUI Experiment
    1. Defining Parameters
    2. Defining Displays Generalities
    3. Defining 3D Displays
    4. Defining Charts
    5. Defining Monitors and Inspectors
    6. Defining Export files
    7. Defining User Interaction
  6. Classes and Objects
    1. Class Definition and Instantiation
    2. Attributes and Actions
    3. Inheritance
    4. Virtual Classes
    5. Advanced Features
  7. Exploring Models
    1. Run Several Simulations
    2. Batch Experiments
    3. Exploration Methods
  8. Optimizing Models
    1. Runtime Concepts
    2. Analyzing code performance
    3. Optimizing Models
  9. Multi-Paradigm Modeling
    1. Control Architecture
    2. Defining Differential Equations
  1. Manipulate OSM Data
  2. Cleaning OSM Data
  3. Diffusion
  4. Using Database
  5. Using Dataframes
  6. Using FIPA ACL
  7. Using BDI with BEN
  8. Using Driving Skill
  9. Manipulate dates
  10. Manipulate lights
  11. Using comodel
  12. Save and restore Simulations
  13. Using network
  14. Writing Unit Tests
  15. Ensure model's reproducibility
  16. Going further with extensions
    1. Calling R
    2. Using Graphical Editor
    3. Using Git from GAMA
  1. Built-in Species
  2. Built-in Skills
  3. Built-in Architecture
  4. Statements
  5. Data Type
  6. File Type
  7. Stats Extension
  8. Sound Extension
  9. BDI Extension
  10. FIPA Skill
  11. Expressions
    1. Literals
    2. Units and Constants
    3. Pseudo Variables
    4. Variables And Attributes
    5. Operators (Definition)
    6. Operators [A-A]
    7. Operators [B-C]
    8. Operators [D-H]
    9. Operators [I-M]
    10. Operators [N-R]
    11. Operators [S-Z]
  12. Exhaustive list of GAMA Keywords
  1. Installing the development version
    1. Install GAMA source code with Eclipse plugin
  2. Developing Extensions
    1. Developing Plugins
    2. Developing Skills
    3. Developing Statements
    4. Developing Operators
    5. Developing Types
    6. Developing Species
    7. Developing Control Architectures
    8. Index of annotations
  3. Introduction to GAMA Java API
    1. Architecture of GAMA
    2. IScope
  4. Using GAMA flags
  5. Creating a release of GAMA
  6. Documentation generation

  1. Predator Prey
  2. Road Traffic
  3. Incremental Model
  4. Luneray's flu
  5. BDI Agents
  6. Forager RL
  7. SIR Analysis
  8. 3D Model

  1. Team
  2. Projects using GAMA
  3. Scientific References
  4. Training Sessions

Resources

  1. Videos
  2. Conferences
  3. Pedagogical materials

Clone this wiki locally