Skip to content

Commit 961f75a

Browse files
authored
fix(Nexto_EZ): buffer randomness value consistency (#335)
Updates the Nexto_EZ bot to match user expectations when the GUI slider is unmodified. * when initialized the `Nexto_EZ` class loads the shared memory buffer with a value from 0-255 where 0 represents a fully random bot and 255 is fully nexto. * when initialized the gui expects the value to be in the 0=fully-random format and displays correctly, but does not update the value until the slider is adjusted. * when the slider is adjusted it writes a 0-255 value where 255 is random and 0 is fully nexto. (Note that this is inverted from the initialization step.) * when deciding whether the tick should be random or not the `Nexto_EZ` class expects the buffer to contain a 0-255 value where 255 is fully random. (see line 50) It seems to match expectations if the user modifies the slider before playing. However if the slider is not changed, or when `SHOW_GUI` is set to False the bot's behavior will be the inverse of expectation. * update the initialization of the class and gui to set and expect the 0=fully-nexto 255=fully-random format, respectively. * add debug logging to elevate confidence the bot is behaving as expected. defaults to False/Off
1 parent d7cc043 commit 961f75a

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

RLBotPack/Necto/Nexto_EZ/bot.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import numpy as np
22
from config import *
33

4+
from collections import deque
5+
from logging import DEBUG, StreamHandler, Formatter
46
from pathlib import Path
57
from rlbot.botmanager.helper_process_request import HelperProcessRequest
8+
from rlbot.utils.logging_utils import get_logger, FORMAT
69
from rlbot.utils.structures.game_data_struct import GameTickPacket
710

811
from multiprocessing import shared_memory
@@ -21,20 +24,40 @@
2124
class Nexto_EZ(Nexto):
2225
def __init__(self, name, team, index):
2326
super().__init__(name, team, index)
27+
self.logger = get_logger("nexto-ez")
28+
if DEBUG_RANDOM_TICKS:
29+
ch = StreamHandler()
30+
ch.setFormatter(Formatter(FORMAT))
31+
ch.setLevel(DEBUG)
32+
self.logger.addHandler(ch)
2433

2534
self.shared_memory = shared_memory.SharedMemory(create=True, size=1)
26-
self.shared_memory.buf[0] = int(round(STRENGTH * 2.55))
35+
# buf[0] is a value from 0 - 255 (max random=255, max nexto=0)
36+
self.shared_memory.buf[0] = int(round((100-STRENGTH) * 2.55))
37+
self.random_debug_log_rate = STRENGTH_LOG_RATE
38+
self.random_debug_tick_count = 0
39+
self.random_debug_tick_history = deque(maxlen=TICK_HISTORY_LENGTH)
2740

2841
def update_controls(self, packet: GameTickPacket):
42+
self.random_debug_tick_count += 1
43+
if self.random_debug_tick_count >= self.random_debug_log_rate:
44+
self.random_debug_tick_count = 0
45+
sum_, len_ = sum(self.random_debug_tick_history), len(self.random_debug_tick_history)
46+
self.logger.debug(
47+
"bot#:%d %d of the last %d (%.2f%%) ticks were random compared to configured value of %d%%",
48+
self.index, sum_, len_, sum_/len_ * 100, self.shared_memory.buf[0]/2.55
49+
)
2950
if np.random.random() * 255 > self.shared_memory.buf[0]:
3051
super().update_controls(packet)
52+
self.random_debug_tick_history.append(0) # not random this tick
3153
else:
3254
# Randomize inputs. Don't influence long term inputs such as jump.
3355
self.controls.throttle = np.random.random()
3456
self.controls.steer = np.random.random() * 2 - 1
3557
self.controls.pitch = np.random.random() * 2 - 1
3658
self.controls.yaw = np.random.random() * 2 - 1
3759
self.controls.roll = np.random.random() * 2 - 1
60+
self.random_debug_tick_history.append(1) # is random this tick
3861

3962
def get_helper_process_request(self):
4063
if SHOW_GUI:

RLBotPack/Necto/Nexto_EZ/config.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,14 @@
22
SHOW_GUI = True
33

44
# in Percent. 100% = Full Nexto, 0% = random agent
5-
STRENGTH = 80
5+
STRENGTH = 80
6+
7+
# enable logging for randomness check
8+
DEBUG_RANDOM_TICKS = False
9+
10+
# frequency of logging, to avoid spamming
11+
# every # of ticks, log the percent of random ticks
12+
STRENGTH_LOG_RATE = 240
13+
14+
# maximum window of ticks to store for computing percent of random
15+
TICK_HISTORY_LENGTH = 240

RLBotPack/Necto/Nexto_EZ/gui.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def format_value(val):
109109
# percentage label
110110
value_label = ttk.Label(
111111
root,
112-
text=format_value(memory.buf[0] / 255),
112+
text=format_value((255 - memory.buf[0]) / 255),
113113
)
114114
value_label.grid(
115115
column=1,
@@ -128,7 +128,7 @@ def update_slider(val):
128128
root,
129129
from_=0,
130130
to=1,
131-
value=memory.buf[0] / 255,
131+
value=(255 - memory.buf[0]) / 255,
132132
orient='horizontal',
133133
command=lambda val: update_slider(val),
134134
).grid(

0 commit comments

Comments
 (0)