Skip to the content.

Q-learning: learning which tier should run each task

What it shows

Placement here is not hand-written, it is discovered. QLearningOrchestrator turns the tier choice into a reinforcement problem: a state is the application type, a coarse bucket of the task length (LENGTH_BUCKETS = 1000, 5000, 20000 MI) and a bucket of the pending load of the tier, and an action is one of the tiers the ALL architecture allows, in the order cloud, edge, mist. Open choose_action for the epsilon-greedy rule (one decision in ten explores, EXPLORATION_RATE = 0.1, otherwise the tier with the largest value wins) and observe, called from results_returned, which books the reward and applies the textbook update Q += 0.2 * (reward + 0.9 * max Q(next) - Q). The reward is the negated total delay, or a penalty of -1000 when the task fails. This module is the shared home of the agent: the mist-only and edge-orchestrated examples import this class and only change the scenario, and the softmax example derives its Boltzmann policy from it.

flowchart TD
    task["Task"] --> state["State<br/>type, len bucket, load bucket"]
    state --> eps{"random < 0.1?"}
    eps -->|yes| explore["Random tier"]
    eps -->|no| greedy["Tier with max Q"]
    explore --> node["Least loaded node<br/>of the tier"]
    greedy --> node
    node --> exec["Run on cloud, edge or mist"]
    exec --> reward["Reward<br/>-delay, or -1000 if failed"]
    reward --> update["Q += 0.2 (r + 0.9 maxQ - Q)"]
    update --> state
    classDef learn fill:#cfe8ff,stroke:#333;
    class reward,update learn;

The scenario

settings/ runs one scenario, twice, on the same seed, so the only difference is the policy:

The first run uses DefaultOrchestrator with TRADE_OFF, the second the learned policy.

Run it

From the repository root, after pip install -e .:

python examples/q_learning/q_learning.py

No-install variant:

PYTHONPATH=src python examples/q_learning/q_learning.py

What to look for

orchestration strategy, same seed and same workload:
TRADE_OFF                tasks=  5550 ok=  4173 avg_delay=0.1997   cloud=  467 edge= 2656 mist= 2427 failed(delay)= 1377 failed(mobility)=   0 energy=23.3791
Q_LEARNING               tasks=  5550 ok=  4128 avg_delay=0.1742   cloud=  859 edge= 3263 mist= 1428 failed(delay)= 1422 failed(mobility)=   0 energy=24.0632

Raise EXPLORATION_RATE or lower LEARNING_RATE in the class to watch the table converge differently.

Files

Where to go next