Machine Learning Modeling Pipeline
Machine Learning Pipeline Overview
*** UPCOMING : The modeling pipeline will be part of a future release ***
OpenGameData supports training and exporting machine learning models through the built-in modeling pipeline. This chapter describes the archtecture and usage of this pipeline.
TODO : some of this chapter’s contents belongs in the user guide
Process for Adding and Configuring Modeling Modules
This document outlines the step-by-step process for adding a new modeling module into the codebase and configuring it in a game’s JSON schema
Adding a New Modeling Module
Implementing the Model Class
When creating a new model:
Inherit from
PopulationModel(fromogd.core.generators.models.PopulationModel).Review existing examples like:
KMeansModelLogisticRegressionModelNeuralNetModelRandomForestModel
Steps:
Create the model file in:
ogd/games/<GAME>/models/<MyModel>.pyImplement the following methods in the model class:
__init__: Set up internal lists, parameters, and objects (e.g., scalers, classifiers)._featureFilter(cls, mode): Return the exact list of feature names needed (must match schema file features)._updateFromFeatureData(self, feature): Handle and store incoming feature values._train(self): Train the model using accumulated data._apply(self, apply_to): Apply the model to new feature data for predictions._render(self, save_path=None): Output charts, plots, or other reports._modelInfo(self): Log metrics, statistics, feature importances, cluster summaries, etc.
Registering the Model
To enable the system to load the model at runtime from the JSON config:
Do not hard-code into the loader, rely on
ModelRegistryandloader._loadConfiguredModels.Ensure the name in the JSON matches the class name exactly.
Process:
Import the new model into the game’s code if required.
ModelRegistry:Calls
_loadFromSchema()to read models from the config.Registers the model with the feature listener system.
Loader instantiates the model based on JSON config.
Integration Points
Ensure
_featureFiltermatches schema-defined features exactly.Verify model handles correct data types (numeric, time durations, etc.).
Output model results using the same conventions as other models for consistency.
Configuring the Model in the JSON Schema File
Location: ogd/games/<GAME>/schemas/<GAME>.json
Example for BLOOM: ogd/games/BLOOM/schemas/BLOOM.json
Adding My Model to the Config
Example: KMeansModel for BLOOM
"models": [
{
"name": "KMeansModel",
"params": {
"n_clusters": 6,
"features": [
"BuildCount",
"CityInspectionCount",
"DairyInspectionCount",
"GrainInspectionCount",
"AveragePhosphorusViewTime",
"AverageEconomyViewTime"
]
}
}
]
Key Fields:
name— Exact Python class name of the model.params— Optional parameters passed toGeneratorParametersin the model.features— Exact list from_featureFilter.
How the Loader Uses It
When running an export:
GeneratorLoaderreads “models” from the schema.Instantiates each model with provided parameters.
ModelRegistryregisters them so they receive matching features.ModelManagercalls:TrainModels()→_trainGetModelInfo()→_modelInfoRenderModels()→_render
Special Configuration Rules
All features in “
features” must already exist in the “features” section of the schema.If the model needs new features:
Add them to the schema first, including type and description.
Special hyperparameters (e.g.,
hidden_layer_sizesfor neural nets) go under “params”.
Another Example
Neural Network for BLOOM:
"models": [
{
"name": "NeuralNetModel",
"params": {
"hidden_layer_sizes": [8],
"activation": "relu"
},
"features": [
"EconomyViewCount",
"AlertReviewCount",
"TotalPolicyChangeCount",
"GameCompletionStatus"
]
}
]
End-to-End Workflow
Order of Steps:
Implement the model — subclass
PopulationModel.Test locally — verify:
_updateFromFeatureData_train_apply
Add config — insert model into “
models” in the game’s JSON schema.Run export — check:
Features are collected correctly.
Training logs appear.
Model metrics and outputs are generated.
(Optional) Visualize results in
_render.
Summary: To add a new machine learning module to the OGD pipeline, you only need to:
Create one Python file for the model.
Update the game’s JSON schema.