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

Apple tracking #11

Open
wants to merge 1 commit into
base: main
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
3 changes: 3 additions & 0 deletions meltingpot/configs/substrates/commons_harvest__farmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@

# Warning: setting `_ENABLE_DEBUG_OBSERVATIONS = True` may cause slowdown.
_ENABLE_DEBUG_OBSERVATIONS = False
# Prints out when an avatar eats an apple.
_ENABLE_EAT_PRINTING = False

APPLE_RESPAWN_RADIUS = 2.0
REGROWTH_PROBABILITIES = [0.0, 0.0025, 0.005, 0.025]
Expand Down Expand Up @@ -369,6 +371,7 @@ def create_apple_prefab(regrowth_radius=-1.0, # pylint: disable=dangerous-defau
"liveState": "apple",
"waitState": "appleWait",
"rewardForEating": 1.0,
"printEat": _ENABLE_EAT_PRINTING,
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@

# Warning: setting `_ENABLE_DEBUG_OBSERVATIONS = True` may cause slowdown.
_ENABLE_DEBUG_OBSERVATIONS = False
# Prints out when an avatar eats an apple.
_ENABLE_EAT_PRINTING = False

APPLE_RESPAWN_RADIUS = 2.0
REGROWTH_PROBABILITIES = [0.0, 0.0025, 0.005, 0.025]
Expand Down Expand Up @@ -376,6 +378,7 @@ def create_apple_prefab(regrowth_radius=-1.0, # pylint: disable=dangerous-defau
"liveState": "apple",
"waitState": "appleWait",
"rewardForEating": 1.0,
"printEat": _ENABLE_EAT_PRINTING,
}
},
{
Expand Down
5 changes: 5 additions & 0 deletions meltingpot/lua/modules/component_library.lua
Original file line number Diff line number Diff line change
Expand Up @@ -958,12 +958,14 @@ function Edible:__init__(kwargs)
{'liveState', args.stringType},
{'waitState', args.stringType},
{'rewardForEating', args.numberType},
{'printEat', args.booleanType},
})
Edible.Base.__init__(self, kwargs)

self._config.liveState = kwargs.liveState
self._config.waitState = kwargs.waitState
self._config.rewardForEating = kwargs.rewardForEating
self._config.printEat = kwargs.printEat
end

function Edible:reset()
Expand Down Expand Up @@ -995,6 +997,9 @@ function Edible:onEnter(enteringGameObject, contactName)
avatarComponent:addReward(self._config.rewardForEating)
events:add('edible_consumed', 'dict',
'player_index', avatarComponent:getIndex()) -- int
if self._printEat then
print("Avatar " .. avatarComponent:getIndex() .. " consumed apple!")
end
-- Change the edible to its wait (disabled) state.
self.gameObject:setState(self._waitState)
end
Expand Down
39 changes: 39 additions & 0 deletions parse_edible_output.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

# Check if at least the input source (program command or input file) is provided
if [ -z "$1" ]; then
echo "Usage: $0 <evaluation command | input_file> [output_file]"
exit 1
fi

INPUT_SOURCE="$1"
OUTPUT_FILE="${2:-avatar_apple_consumption.csv}"

# associative array
declare -A avatar_counts

process_input() {
# internal field seperator, line by line
while IFS= read -r line; do
if [[ $line =~ Avatar\ ([0-9]+)\ consumed\ apple! ]]; then
avatar_index="${BASH_REMATCH[1]}"
((avatar_counts[$avatar_index]++))
fi
done
}

# file or a command
if [[ -f "$INPUT_SOURCE" ]]; then
# from file
process_input < "$INPUT_SOURCE"
else
# from STDOUT
$INPUT_SOURCE | process_input
fi

echo "Avatar,Count" > "$OUTPUT_FILE"
for avatar in "${!avatar_counts[@]}"; do
echo "$avatar,${avatar_counts[$avatar]}" >> "$OUTPUT_FILE"
done

echo "Results have been written to $OUTPUT_FILE"