import binary_encoding import std/math import std/random const N_IN* = TOTAL_BITS # 870 N_OUT* = OUTPUT_BITS # 7 TRACE_MAX_AGE* = 40 LEARNING_RATE* = 0.1 MISS_PENALTY* = -0.03 HIT_REWARD* = 0.1 W_CLAMP* = 5.0 type HebbianNet* = object W*: array[N_IN * N_OUT, float] EligibilityTrace* = object input*: BinaryVector output*: OutputVector age*: int alive*: bool VirtualBullet* = object trace*: EligibilityTrace fireX*: float fireY*: float aimAngleDeg*: float fireDist*: float bulletSpeed*: float active*: bool proc initHebbianNet*(): HebbianNet = for w in result.W.mitems: w = rand(0.2) - 0.1 proc forward*(net: HebbianNet, input: BinaryVector): OutputVector = for j in 0.. 0.0: 1'u8 else: 0'u8 proc learn*(net: var HebbianNet, trace: EligibilityTrace, reward: float) = let decayedReward = reward * pow(0.95, float(trace.age)) for i in 0..