Skip to content

Commit

Permalink
Add tests for reward function for one timesteps (#17)
Browse files Browse the repository at this point in the history
woohoo!

* fix test_reset() function bug

* add test for reward function

* addtional test for test_rew function

* update test_rew func

* update test_rew func
  • Loading branch information
jiangjingzhi2003 authored Aug 28, 2024
1 parent 8e70ce5 commit 8633ce5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions tests/test_greencrab.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ def test_GC():
check_env(greenCrabEnv(), warn=True)
check_env(greenCrabSimplifiedEnv(), warn=True)
check_env(timeSeriesEnv(), warn=True)

31 changes: 29 additions & 2 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ def test_full_harvest():
steps = env.Tmax
prev_state = env.state # store the state before taking step
for i in range(steps):
observation, rew, term, trunc, info = env.step(np.array([1,1, 1]))
observation, rew, term, trunc, info = env.step(np.array([1,1,1]))
# if crab population drop, catch rate should not be zero
if (sum(prev_state) > sum(env.state)):
assert observation[0] != -1 or observation[1] != -1
assert rew < 0 # try to discourage laying all traps for each timestep

assert info == {}
assert trunc == False
Expand Down Expand Up @@ -74,4 +75,30 @@ def test_reset():
# reset the obseravtion environment
new_ob_space, new_info = env.reset()

assert np.array_equal(new_ob_space, np.array([-1, -1]))
assert np.array_equal(new_ob_space, np.array([-1, -1]))

# test reward function for both greenCrab and greenCrabSimplified
def test_reward_func():
env = greenCrabSimplifiedEnv()
env.reset()
# self.state = self.self.init_state()

# test no trap laid for one timestep when no crab
action = np.array([-1, -1, -1])
assert env.reward_func(action) >= 0 # are we expecting non-negative when no traps laid when no crabs

# test for all trap laid for one timestep when no crab
action = np.array([1, 1, 1])
assert env.reward_func(action) < 0 # are we expecting positive when no traps laid when no crabs

# test no trap when there is a lot of crabs
env.state = np.array([10., 10., 10., 10., 1000., 10000., 100000., 1000., 100., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10., 10.])
action = np.array([-1, -1, -1])
assert env.reward_func(action) < 0 # expecting neg reward

# test all trap laid for one timestep
action = np.array([1, 1, 1])
assert env.reward_func(action) < 0



0 comments on commit 8633ce5

Please sign in to comment.