Skip to content
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

End the game early in case of a timeout #259

Merged
merged 3 commits into from
May 3, 2024
Merged
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
48 changes: 29 additions & 19 deletions kaggle_environments/envs/llm_20_questions/llm_20_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
DONE = "DONE"
INACTIVE = "INACTIVE"
ACTIVE = "ACTIVE"
TIMEOUT = "TIMEOUT"

GUESS = "guess"
ASK = "ask"
Expand Down Expand Up @@ -98,43 +99,40 @@ def guesser_action(active, inactive, step):
if active.action and keyword_guessed(active.action):
guessed = True
score = 20 - int(step / 3)
active.reward = score
inactive.reward = score
active.status = DONE
inactive.status = DONE
active.observation.keyword = keyword
active.observation.category = category
end_game(active, inactive, score, DONE, DONE)
return guessed

def end_game(active, inactive, reward, status, inactive_status):
active.observation.keyword = keyword
active.observation.category = category
inactive.observation.keyword = keyword
inactive.observation.category = category
return guessed
active.reward = reward
inactive.reward = reward
active.status = status
inactive.status = inactive_status


def answerer_action(active, inactive):
active.observation.keyword = keyword
active.observation.category = category
response = active.action
if not response:
response = "none"
active.status = ERROR
end_game(active, inactive, -1, ERROR, DONE)
elif "yes" in response.lower():
response = "yes"
elif "no" in response.lower():
response = "no"
else:
response = "maybe"
active.status = ERROR
end_game(active, inactive, -1, ERROR, DONE)
active.observation.answers.append(response)
inactive.observation.answers.append(response)

def increment_turn(active, inactive, step, guessed):
if step == 59 and not guessed:
active.observation.keyword = keyword
active.observation.category = category
inactive.observation.keyword = keyword
inactive.observation.category = category
active.reward = -1
inactive.reward = -1
active.status = DONE
inactive.status = DONE
end_game(active, inactive, -1, DONE, DONE)
elif active.observation.turnType == "guess":
active.observation.turnType = "ask"
elif active.observation.turnType == "ask":
Expand Down Expand Up @@ -166,21 +164,33 @@ def interpreter(state, env):

step = state[0].observation.step

end_early = (active1 and active1.status) in (TIMEOUT, ERROR) or (active2 and active2.status in (TIMEOUT, ERROR))

if active1 is not None:
guessed = False
if active1.observation.role == GUESSER:
guessed = guesser_action(active1, inactive1, step)
else:
answerer_action(active1, inactive1)
increment_turn(active1, inactive1, step, guessed)
if active1.status in (TIMEOUT, ERROR):
end_game(active1, inactive1, -1, active1.status, DONE)
elif end_early:
end_game(active1, inactive1, 0, DONE, DONE)
else:
increment_turn(active1, inactive1, step, guessed)

if active2 is not None:
guessed = False
if active2.observation.role == GUESSER:
guessed = guesser_action(active2, inactive2, step)
else:
answerer_action(active2, inactive2)
increment_turn(active2, inactive2, step, guessed)
if active2.status in (TIMEOUT, ERROR):
end_game(active2, inactive2, -1, active2.status, DONE)
elif end_early:
end_game(active2, inactive2, 0, DONE, DONE)
else:
increment_turn(active2, inactive2, step, guessed)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could use a helper to reduce duplication

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, merging now to get this deployed, but will follow up


return state

Expand Down
Loading