Creating Play Time Maximization Model
For this tutorial, we need multiple things to build our model, this includes:
-
Neural Network Model
-
A Reinforcement Learning Model (Deep Q-Learning or Deep SARSA)
-
Categorical Policy Quick Setup
Designing Our Feature Vector And Classes List
Before we start creating our model, we first need to visualize on how we will design our data and what actions the model could take to extend our players’ play time.
FeatureVector
-- We have five features with one "bias".
local playerDataVector = {
{
1,
numberOfCurrencyAmount,
numberOfItemsAmount,
timePlayedInCurrentSession,
timePlayedInAllSessions,
healthAmount
}
}
ClassesList
local ClassesList = {
"NoEvent",
"FreeGiftEvent",
"ResourceMultiplierEvent",
"QuestEvent",
"ItemSpawnEvent",
"BossSpawnEvent",
"LimitedTimeQuestEvent",
"LimitedTimeItemSpawnEvent",
"LimitedTimeBossSpawnEvent",
}
Also, we would like you to be careful about limited time quest and item spawn events as the model will might learn to give it often. As such, it is important to give the model negative rewards inversely proportional to the duration between the two limited time events.
If you’re concerned about that the model may produce wrong result heavily upon first start up, then you can use a randomized dataset to heavily skew the prediction to the “NoEvent” class. Then use this randomized dataset to pretrain the Neural Network before doing any real-time training and prediction. Below, we will show you how it is done.
local numberOfData = 100
local randomPlayerDataMatrix = TensorL:createRandomUniformTensor({numberOfData, 6}, -100, 100) -- 100 random data with 6 features (including one "bias")
local labelDataMatrix = TensorL:createTensor({numberOfData, 1}, "NoEvent")
However, this require setting the Neural Network’s parameters to these settings temporarily so that it can be biased to “NoEvent” at start up as shown below.
NeuralNetwork.maximumNumberOfIterations = 1000
NeuralNetwork.learningRate = 0.3
Constructing Our Model
Before we start training our model, we first need to build our model. We have split this to multiple subsections to make it easy to follow through.
Constructing Our Neural Network
local NeuralNetwork = DataPredict.Model.NeuralNetwork.new({maximumNumberOfIterations = 1})
NeuralNetwork:setClassesList(ClassesList)
NeuralNetwork:addLayer(5, true) -- Five features and one bias.
NeuralNetwork:addLayer(#ClassesList, false) -- No bias.
Constructing Our Deep Reinforcement Learning Model
-- You can use deep Q-Learning here for faster learning. However, for more "safer" model, stick with deep SARSA.
local DeepReinforcementLearningModel = DataPredict.Model.DeepStateActionRewardStateAction.new()
-- Inserting our Neural Network here.
DeepReinforcementLearningModel:setModel(NeuralNetwork)
Constructing Our Categorical Policy Quick Setup Model
This part makes it easier for us to set up our model, but it is not strictly necessary. However, I do recommend you to use them as they contain built-in functions for handing training and predictions.
local PlayTimeMaximizationModel = DataPredict.QuickSetups.CategoricalPolicy.new()
-- Inserting our Deep Reinforcement Learning Model here.
PlayTimeMaximizationModel:setModel(DeepReinforcementLearningModel)
Training And Prediction
Because the way we have designed our Categorical Policy Quick Setup, you can immediately train while producing predictions for your player by calling reinforce() function.
This is because reinforce() function is responsible for producing prediction and perform pre-calculations at the same time as that is required to train our models.
-- Here, you notice that there is a reward value being inserted here. Generally, when you first call this, the reward value should be zero.
local eventName = PlayTimeMaximizationModel:reinforce(playerDataVector, rewardValue)
Rewarding Our Model
In order to assign the reward to that event is selected, we must first deploy the chosen event and observe if the player stayed for that event.
Below, it shows an example code for this.
local eventFunctionDictionary = {
["NoEvent"] = nil,
["ResourceMultiplierEvent"] = resourceMultiplierEvent,
["QuestEvent"] = questEvent,
["ItemSpawnEvent"] = itemSpawnEvent,
["BossSpawnEvent"] = bossSpawnEvent,
["LimitedTimeQuestEvent"] = limitedTimeQuestEvent,
["LimitedTimeItemSpawnEvent"] = limitedTimeItemSpawnEvent,
["LimitedTimeBossSpawnEvent"] = limitedTimeBossSpawnEvent,
}
local function run(Player)
local isPlayerInServer = true
local rewardValue = 0
local playerDataVector
local eventName
local eventFunction
while isPlayerInServer do
playerDataVector = getPlayerDataVector(Player)
eventName = PlayTimeMaximizationModel:reinforce(playerDataVector, rewardValue)
eventFunction = eventFunctionDictionary[eventName]
if (eventFunction) then eventFunction() end
task.wait(30)
-- Player leaving the game is more of a "rarer" and "extremely undesirable" event, therefore a very large negative value is used.
rewardValue = (isPlayerInServer and 20) or -100
isPlayerInServer = checkIfPlayerIsInServer(Player)
end
end
Model Parameters Loading
In here, we will use our model parameters so that it can be used to load out models. There are three cases in here:
-
The player is a first-time player.
-
The player is a returning player.
-
Every player uses the same global model.
Case 1: The Player Is A First-Time Player
Under this case, this is a new player that plays the game for the first time. In this case, we do not know how this player would act.
We have a multiple way to handle this issue:
-
We create a “global” model that trains from every player, and then make a deep copy of the model parameters and load it into our models.
-
We take from other players’ existing model parameters and load it into our models.
Case 2: The Player Is A Returning Player
Under this case, you can continue using the existing model parameters that was saved in Roblox’s Datastores.
--[[
We first need to get our Neural Network model. If you only kept the quick setup and discarded the rest, don't worry!
We can just do getModel() twice to get our Neural Network model.
--]]
local DeepReinforcementLearningModel = PlayTimeMaximizationModel:getModel()
local NeuralNetwork = DeepReinforcementLearningModel:getModel()
-- Notice that we must get it from the Neural Network model.
ModelParameters = NeuralNetwork:getModelParameters()
-- Notice that we must set it to the Neural Network model too.
NeuralNetwork:setModelParameters(ModelParameters)
Case 3: Every Player Uses The Same Global Model
Under this case, the procedure is the same to case 2 except that you need to:
-
Load model parameters upon server start.
-
Perform auto-save with the optional ability of merging with saved model parameters from other servers.
That’s all for today!