package main
import (
"fmt"
"math/rand"
)
func main() {
ta := TsetlinAutomation{
action: drop,
depth: 1,
maxDepth: 50,
}
stand := SimpleStand{
probability: 90,
}
wins := 0
fails := 0
for i := 0; i < 100; i++ {
if ta.tryStand(stand) {
wins++
} else {
fails++
}
}
fmt.Printf("Wins: %d, Fails: %d\n", wins, fails)
}
type Action int
const (
drop Action = iota
eat
numberOfActions = iota
)
type TsetlinAutomation struct {
action Action
depth int
maxDepth int
}
func (ta *TsetlinAutomation) penalty() {
ta.depth--
if ta.depth <= 0 {
ta.action = (ta.action + 1) % numberOfActions // меняем состояние на другое
ta.depth = 1
}
}
func (ta *TsetlinAutomation) reward() {
if ta.depth < ta.maxDepth {
ta.depth++
}
}
func (ta *TsetlinAutomation) tryStand(stand SimpleStand) bool {
if ta.action == stand.action() {
ta.reward()
return true
} else {
ta.penalty()
return false
}
}
type SimpleStand struct {
probability int // число между 0 и 100
}
// Функция возвращает с определённой вероятностью правильное действие
func (s *SimpleStand) action() Action {
num := rand.Int() % 100
if num > s.probability {
return eat
}
return drop
}