Hierarchical rule engine

From wenmin-wu/ds-skills · View interactive version

Hierarchical rule engine is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Two-level group-then-pattern dispatch for game AI agents where groups filter by game state and ordered patterns within a group fire the first matching action

How to use this skill

Skill file

Hierarchical Rule Engine

Overview

For heuristic game AI agents, a flat if-else chain becomes unmaintainable past ~20 rules. A two-level dispatch — groups that test broad game state (attacking, defending, set piece), then ordered patterns within the matching group — scales to hundreds of rules while staying readable. Each pattern is a dict with environment_fits (predicate) and get_action (callback), evaluated in priority order.

Quick Start

def make_pattern(predicate, action_fn):
    return lambda obs, x, y: {
        "environment_fits": predicate,
        "get_action": action_fn,
    }

def make_group(group_predicate, patterns_fn):
    return lambda obs, x, y: {
        "environment_fits": group_predicate,
        "get_memory_patterns": patterns_fn,
    }

groups = [attack_group, defend_group, set_piece_group, fallback_group]

def get_action(obs, x, y):
    for make_grp in groups:
        grp = make_grp(obs, x, y)
        if grp["environment_fits"](obs, x, y):
            for make_pat in grp["get_memory_patterns"](obs, x, y):
                pat = make_pat(obs, x, y)
                if pat["environment_fits"](obs, x, y):
                    return pat["get_action"](obs, x, y)
    return default_action

Workflow

  1. Define groups by broad game state (ball ownership, field zone, game mode)
  2. Within each group, define patterns ordered by priority (most specific first)
  3. Each pattern has a predicate (environment_fits) and action callback
  4. At each timestep, find the first matching group, then first matching pattern
  5. Always include a fallback group/pattern to avoid returning None

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.