Skip to content

fix(Nexto_EZ): buffer randomness value consistency #335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion RLBotPack/Necto/Nexto_EZ/bot.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import numpy as np
from config import *

from collections import deque
from logging import DEBUG, StreamHandler, Formatter
from pathlib import Path
from rlbot.botmanager.helper_process_request import HelperProcessRequest
from rlbot.utils.logging_utils import get_logger, FORMAT
from rlbot.utils.structures.game_data_struct import GameTickPacket

from multiprocessing import shared_memory
Expand All @@ -21,20 +24,40 @@
class Nexto_EZ(Nexto):
def __init__(self, name, team, index):
super().__init__(name, team, index)
self.logger = get_logger("nexto-ez")
if DEBUG_RANDOM_TICKS:
ch = StreamHandler()
ch.setFormatter(Formatter(FORMAT))
ch.setLevel(DEBUG)
self.logger.addHandler(ch)

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

def update_controls(self, packet: GameTickPacket):
self.random_debug_tick_count += 1
if self.random_debug_tick_count >= self.random_debug_log_rate:
self.random_debug_tick_count = 0
sum_, len_ = sum(self.random_debug_tick_history), len(self.random_debug_tick_history)
self.logger.debug(
"bot#:%d %d of the last %d (%.2f%%) ticks were random compared to configured value of %d%%",
self.index, sum_, len_, sum_/len_ * 100, self.shared_memory.buf[0]/2.55
)
if np.random.random() * 255 > self.shared_memory.buf[0]:
super().update_controls(packet)
self.random_debug_tick_history.append(0) # not random this tick
else:
# Randomize inputs. Don't influence long term inputs such as jump.
self.controls.throttle = np.random.random()
self.controls.steer = np.random.random() * 2 - 1
self.controls.pitch = np.random.random() * 2 - 1
self.controls.yaw = np.random.random() * 2 - 1
self.controls.roll = np.random.random() * 2 - 1
self.random_debug_tick_history.append(1) # is random this tick

def get_helper_process_request(self):
if SHOW_GUI:
Expand Down
12 changes: 11 additions & 1 deletion RLBotPack/Necto/Nexto_EZ/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@
SHOW_GUI = True

# in Percent. 100% = Full Nexto, 0% = random agent
STRENGTH = 80
STRENGTH = 80

# enable logging for randomness check
DEBUG_RANDOM_TICKS = False

# frequency of logging, to avoid spamming
# every # of ticks, log the percent of random ticks
STRENGTH_LOG_RATE = 240

# maximum window of ticks to store for computing percent of random
TICK_HISTORY_LENGTH = 240
4 changes: 2 additions & 2 deletions RLBotPack/Necto/Nexto_EZ/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def format_value(val):
# percentage label
value_label = ttk.Label(
root,
text=format_value(memory.buf[0] / 255),
text=format_value((255 - memory.buf[0]) / 255),
)
value_label.grid(
column=1,
Expand All @@ -128,7 +128,7 @@ def update_slider(val):
root,
from_=0,
to=1,
value=memory.buf[0] / 255,
value=(255 - memory.buf[0]) / 255,
orient='horizontal',
command=lambda val: update_slider(val),
).grid(
Expand Down