MoonRLLab is a discrete reinforcement-learning lab for MoonBit.
import { "liuzhiyug/moonrllab" }
fn main {
let report = @moonrllab.train_with_memory_logger(8, 40, 20260815)
println(report.compact_line())
}moon run examples/basicpub(open) trait Agent {
fn reset_episode(Self) -> Unit
fn choose_action(Self, Int) -> Int
fn learn(Self, Transition, Int?) -> Unit
fn epsilon(Self) -> Double
fn q_report(Self, Int) -> String
}pub(open) trait Environment {
fn reset(Self) -> Int
fn actions(Self) -> Array[Int]
fn state_space(Self) -> Array[Int]
fn step(Self, Int) -> Transition
fn render(Self) -> String
}pub(open) trait Logger {
fn start_episode(Self, Int) -> Unit
fn step(Self, Int, Int, Int, Double, Int, Bool, Int) -> Unit
fn finish_episode(Self, EpisodeRecord) -> Unit
fn finish(Self, TrainingReport) -> Unit
}impl Environment for BanditEnvpub struct BanditPolicy {
values : Array[Double]
counts : Array[Int]
schedule : EpsilonSchedule
rng : LcgRng
} derive(Debug)fn BenchmarkResult::record(self : BenchmarkResult, episode : Int, reward : Double, steps : Int, solved : Bool) -> Unitpub struct CliffWalkingEnv {
width : Int
height : Int
start_x : Int
start_y : Int
goal_x : Int
goal_y : Int
x : Int
y : Int
_last_stage : EpisodeStage
} derive(Debug)impl Logger for ConsoleLoggerfn step(self : ConsoleLogger, episode : Int, state : Int, action : Int, reward : Double, next_state : Int, done : Bool, step : Int) -> Unitpub struct DoubleQLearningAgent {
left : QTable
right : QTable
policy : EpsilonGreedyPolicy
alpha : Double
gamma : Double
updates : Int
} derive(Debug)fn DoubleQLearningAgent::learn(self : DoubleQLearningAgent, transition : Transition, update_left : Bool) -> Unitfn DoubleQLearningAgent::new(states : Array[Int], actions : Array[Int], policy : EpsilonGreedyPolicy, alpha : Double, gamma : Double) -> DoubleQLearningAgentpub struct EpisodeAccumulator {
reward : Double
steps : Int
last_state : Int
solved : Bool
} derive(Debug)impl Policy for EpsilonGreedyPolicyfn choose_action(self : EpsilonGreedyPolicy, state : Int, actions : Array[Int], q_values : Array[Double]) -> Intfn EpsilonGreedyPolicy::choose_action(self : EpsilonGreedyPolicy, _state : Int, actions : Array[Int], q_values : Array[Double]) -> Intpub struct ExpectedSARSAAgent {
table : QTable
policy : EpsilonGreedyPolicy
alpha : Double
gamma : Double
} derive(Debug)impl Agent for ExpectedSARSAAgentfn ExpectedSARSAAgent::learn(self : ExpectedSARSAAgent, transition : Transition, _next_action : Int?) -> Unitfn ExpectedSARSAAgent::new(states : Array[Int], actions : Array[Int], policy : EpsilonGreedyPolicy, alpha : Double, gamma : Double) -> ExpectedSARSAAgentpub struct GridWorldEnv {
width : Int
height : Int
start_x : Int
start_y : Int
goal_x : Int
goal_y : Int
x : Int
y : Int
_last_stage : EpisodeStage
} derive(Debug)impl Environment for GridWorldEnvfn LearningRateSchedule::new(initial : Double, minimum : Double, decay : Double) -> LearningRateSchedulepub struct MemoryLogger {
started : Int
transitions : Int
finished : Int
last_report : TrainingReport?
} derive(Debug)impl Logger for MemoryLoggerfn step(self : MemoryLogger, _episode : Int, _state : Int, _action : Int, _reward : Double, _next_state : Int, _done : Bool, _step : Int) -> Unitpub struct MonteCarloAgent {
table : QTable
policy : EpsilonGreedyPolicy
alpha : Double
gamma : Double
visits : Array[Int]
} derive(Debug)fn MonteCarloAgent::new(states : Array[Int], actions : Array[Int], policy : EpsilonGreedyPolicy, alpha : Double, gamma : Double) -> MonteCarloAgentpub struct QLearningAgent {
table : QTable
policy : EpsilonGreedyPolicy
alpha : Double
gamma : Double
} derive(Debug)impl Agent for QLearningAgentfn QLearningAgent::learn(self : QLearningAgent, transition : Transition, _next_action : Int?) -> Unitfn QLearningAgent::new(states : Array[Int], actions : Array[Int], policy : EpsilonGreedyPolicy, alpha : Double, gamma : Double) -> QLearningAgentfn QLearningAgent::set_q_value(self : QLearningAgent, state : Int, action : Int, value : Double) -> Unitimpl Environment for RandomWalkEnvpub struct RunningStats {
count : Int
mean : Double
m2 : Double
minimum : Double
maximum : Double
} derive(Debug)pub struct SARSAAgent {
table : QTable
policy : EpsilonGreedyPolicy
alpha : Double
gamma : Double
} derive(Debug)impl Agent for SARSAAgentfn SARSAAgent::new(states : Array[Int], actions : Array[Int], policy : EpsilonGreedyPolicy, alpha : Double, gamma : Double) -> SARSAAgentfn[Env : Environment, Ag : Agent, Log : Logger] Trainer::train(self : Trainer, env : Env, agent : Ag, logger : Log) -> TrainingReportfn Trainer::train_q_learning(self : Trainer, env : GridWorldEnv, agent : QLearningAgent, logger : ConsoleLogger) -> TrainingReportfn Trainer::train_sarsa(self : Trainer, env : GridWorldEnv, agent : SARSAAgent, logger : ConsoleLogger) -> TrainingReportfn Transition::new(state : Int, action : Int, reward : Double, next_state : Int, done : Bool, step : Int) -> Transitionpub struct ValidationFinding {
rule : String
severity : ValidationSeverity
message : String
} derive(Eq, Debug)pub struct ValidationReport {
findings : Array[ValidationFinding]
passed : Int
notices : Int
failures : Int
} derive(Debug)fn ValueIterationResult::greedy_path(self : ValueIterationResult, start : Int, goal : Int, limit : Int) -> Array[Int]fn audit_score(audit : EnvironmentAudit, expected_states : Int, expected_actions : Int) -> ScorecardSetfn evaluate_grid_policy(policy : Array[Int], goal : Int, gamma : Double, limit : Int) -> PolicyEvaluationfn gridworld_value_iteration(width : Int, height : Int, goal : Int, gamma : Double, tolerance : Double, limit : Int) -> ValueIterationResultfn linear_interpolate(left : Double, right : Double, ratio : Double) -> Doublefn project_name() -> Stringfn report_footer() -> Stringfn train_gridworld_expected_sarsa_generic(episodes : Int, max_steps : Int, seed : Int) -> TrainingReportMoonRLLab is a discrete reinforcement-learning lab for MoonBit.