Skip to content

Commit

Permalink
Make MineRL evaluation more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
eErr0Re committed May 24, 2024
1 parent c53dc44 commit 1f0c97e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
26 changes: 14 additions & 12 deletions src/minecraft/getting_started_minerl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ minerl_grammar = @pcsgrammar begin
end

minerl_grammar_2 = @pcsgrammar begin
1:SEQ = [ACT]
8:DIR = 0b0001 | 0b0010 | 0b0100 | 0b1000 | 0b0101 | 0b1001 | 0b0110 | 0b1010 # forward | back | left | right | forward-left | forward-right | back-left | back-right
1:sequence_actions = [action]
1:action = (TIMES, DIR)
1:ACT = (TIMES, Dict("move" => DIR, "sprint" => 1, "jump" => 1))
6:TIMES = 5 | 10 | 25 | 50 | 75 | 100

Check warning on line 23 in src/minecraft/getting_started_minerl.jl

View check run for this annotation

Codecov / codecov/patch

src/minecraft/getting_started_minerl.jl#L20-L23

Added lines #L20 - L23 were not covered by tests
end

Expand All @@ -32,16 +32,18 @@ function evaluate_trace_minerl(prog, grammar, env, show_moves)
sum_of_rewards = 0
is_done = false

Check warning on line 33 in src/minecraft/getting_started_minerl.jl

View check run for this annotation

Codecov / codecov/patch

src/minecraft/getting_started_minerl.jl#L33

Added line #L33 was not covered by tests
obs = nothing
for saved_action sequence_of_actions
times, dir = saved_action

for (times, action) sequence_of_actions

Check warning on line 35 in src/minecraft/getting_started_minerl.jl

View check run for this annotation

Codecov / codecov/patch

src/minecraft/getting_started_minerl.jl#L35

Added line #L35 was not covered by tests
new_action = env.action_space.noop()
new_action["forward"] = dir & 1
new_action["back"] = dir >> 1 & 1
new_action["left"] = dir >> 2 & 1
new_action["right"] = dir >> 3
new_action["sprint"] = 1
new_action["jump"] = 1
for (key, val) in action
if key == "move"
new_action["forward"] = val & 1
new_action["back"] = val >> 1 & 1
new_action["left"] = val >> 2 & 1
new_action["right"] = val >> 3

Check warning on line 42 in src/minecraft/getting_started_minerl.jl

View check run for this annotation

Codecov / codecov/patch

src/minecraft/getting_started_minerl.jl#L37-L42

Added lines #L37 - L42 were not covered by tests
else
new_action[key] = val

Check warning on line 44 in src/minecraft/getting_started_minerl.jl

View check run for this annotation

Codecov / codecov/patch

src/minecraft/getting_started_minerl.jl#L44

Added line #L44 was not covered by tests
end
end

for i in 1:times
obs, reward, done, _ = env.step(new_action)
Expand Down Expand Up @@ -76,5 +78,5 @@ HerbSearch.evaluate_trace(prog::RuleNode, grammar::ContextSensitiveGrammar; show
HerbSearch.calculate_rule_cost(rule_index::Int, grammar::ContextSensitiveGrammar) = HerbSearch.calculate_rule_cost_prob(rule_index, grammar)

# resetEnv()
iter = HerbSearch.GuidedTraceSearchIterator(minerl_grammar_2, :sequence_actions)
iter = HerbSearch.GuidedTraceSearchIterator(minerl_grammar_2, :SEQ)
program = @time probe(Vector{Trace}(), iter, 3000000, 6)
6 changes: 3 additions & 3 deletions src/probe/update_grammar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ function update_grammar!(grammar::ContextSensitiveGrammar, PSols_with_eval_cache
# TODO: think about better thing here
fitness = min(best_reward / 100, 1)

Check warning on line 55 in src/probe/update_grammar.jl

View check run for this annotation

Codecov / codecov/patch

src/probe/update_grammar.jl#L55

Added line #L55 was not covered by tests

p_current = 2 ^ (grammar.log_probabilities[rule_index])
p_current = 2^(grammar.log_probabilities[rule_index])

Check warning on line 57 in src/probe/update_grammar.jl

View check run for this annotation

Codecov / codecov/patch

src/probe/update_grammar.jl#L57

Added line #L57 was not covered by tests

sum += p_current^(1 - fitness)
sum += p_current^(1 - fitness)
log_prob = ((1 - fitness) * log(2, p_current))
grammar.log_probabilities[rule_index] = log_prob
end
Expand All @@ -66,7 +66,7 @@ function update_grammar!(grammar::ContextSensitiveGrammar, PSols_with_eval_cache
total_sum += 2^(grammar.log_probabilities[rule_index])
end
expr = rulenode2expr(PSols_with_eval_cache[begin].program, grammar)
grammar.rules[9] = :([$expr; action])
grammar.rules[1] = :([$expr; ACT])
@assert abs(total_sum - 1) <= 1e-4 "Total sum is $(total_sum) "

Check warning on line 70 in src/probe/update_grammar.jl

View check run for this annotation

Codecov / codecov/patch

src/probe/update_grammar.jl#L59-L70

Added lines #L59 - L70 were not covered by tests
end

Expand Down

0 comments on commit 1f0c97e

Please sign in to comment.