Skip to content

Instantly share code, notes, and snippets.

@austin362667
Created October 1, 2022 01:33
Show Gist options
  • Select an option

  • Save austin362667/2f0d05af4a7ad323d3bc3c4d483cc962 to your computer and use it in GitHub Desktop.

Select an option

Save austin362667/2f0d05af4a7ad323d3bc3c4d483cc962 to your computer and use it in GitHub Desktop.
ReactivePredictor predicts the case of a reactive user
func ReactivePredictor(memoryLength int, moves, wins types.Queue) float64 {
stateMachine := make(floats.Slice, int(math.Pow(2, 2.*float64(memoryLength)-1.)))
indMap := make(floats.Slice, 0)
for i := 2 * memoryLength; i >= 0; i-- {
indMap = append(indMap, math.Pow(2, float64(i)))
}
partOfMoves := moves.Array(moves.Length()-memoryLength, moves.Length()-1)
partOfWins := wins.Array(wins.Length()-memoryLength-1, wins.Length()-1)
lastState := append(partOfWins, partOfMoves...)
lastStateInd := 0.
for i := 0; i < len(lastState); i++ {
if lastState[i] == 1 {
lastStateInd += math.Pow(2., float64(i))
}
}
lastStateResult := moves.Index(moves.Length() - 1)
//update the state machine
if stateMachine[int(math.Mod(lastStateInd, float64(stateMachine.Length())))] == 0 { //no prior info
stateMachine[int(math.Mod(lastStateInd, float64(stateMachine.Length())))] = lastStateResult * 0.3
} else if stateMachine[int(math.Mod(lastStateInd, float64(stateMachine.Length())))] == lastStateResult*0.3 { //we've been here before so strengthen prediction
stateMachine[int(math.Mod(lastStateInd, float64(stateMachine.Length())))] = lastStateResult * 0.8
} else if stateMachine[int(math.Mod(lastStateInd, float64(stateMachine.Length())))] == lastStateResult*0.8 { //we've been here before so strengthen prediction
stateMachine[int(math.Mod(lastStateInd, float64(stateMachine.Length())))] = lastStateResult * 1
} else if stateMachine[int(math.Mod(lastStateInd, float64(stateMachine.Length())))] == lastStateResult*1 { //maximum confidence
stateMachine[int(math.Mod(lastStateInd, float64(stateMachine.Length())))] = lastStateResult * 1
} else { //changed his mind - so go back to 0
stateMachine[int(math.Mod(lastStateInd, float64(stateMachine.Length())))] = 0
}
// what is the current state
currentPartOfMoves := moves.Array(moves.Length()-memoryLength, moves.Length()-1)
currentPartOfWins := wins.Array(wins.Length()-memoryLength-1, wins.Length()-1)
currentState := append(currentPartOfWins, currentPartOfMoves...)
currentStateInd := 0.
for i := 0; i < len(currentState); i++ {
if currentState[i] == 1 {
currentStateInd += math.Pow(2., float64(i))
}
}
predictionAndScore := stateMachine[int(math.Mod(currentStateInd, float64(stateMachine.Length())))]
//log.Info('last state %v', lastState, ', last ind ', lastStateInd, ' current state ', currentState, ' current ind ', currentStateInd, ' state machine: ', stateMachine)
//log.Infof("ReactivePredictor PredictionAndScore: %f", predictionAndScore)
return predictionAndScore
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment