Skip to content

Commit

Permalink
add test_cpue_2 and test_reset()
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangjingzhi2003 committed Jul 13, 2024
1 parent 4692f49 commit 1ee12f8
Showing 1 changed file with 50 additions and 9 deletions.
59 changes: 50 additions & 9 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,75 @@
def test_action_units():
env = greenCrabSimplifiedEnv()
env.reset()
action = np.array([-1,-1,-1])
action = np.array([-1,-1,-1]) # no traps
natural_units = np.maximum( env.max_action * (1 + action)/2 , 0.)
assert np.array_equal(natural_units, np.array([0,0,0]))
assert np.array_equal(natural_units, np.array([0,0,0])) # check if no crab exist and no change in population when do nothing

def test_no_harvest():
env = greenCrabSimplifiedEnv()
env.reset()

steps = 3
steps = 10
for i in range(steps):
observation, rew, term, trunc, info = env.step(np.array([-1,-1, -1]))

assert info == {}
assert trunc == False
assert term == False
assert rew < 0
assert sum(env.state) > 0
assert info == {}
assert trunc == False
assert term == False
assert rew < 0
assert sum(env.state) > 0
assert observation[0] == -1 and observation[1] == -1 #check if catch rate is zero in observation


def test_full_harvest():
env = greenCrabSimplifiedEnv()
env.reset()

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]))
# 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 info == {}
assert trunc == False
assert term == False
assert sum(env.state) < 100000

def test_cpue_2():
env = greenCrabSimplifiedEnv()
env.reset()
max_action = 2000

# check if return 0 when sum(action_natural_units) = 0
action = np.array([-1,-1, -1])
action_natural_units = np.maximum( max_action * (1 + action)/2 , 0.) # action_natural_units = 0
observation = env.observations
assert all((env.cpue_2(observation, action_natural_units))== np.float32([0,0]))

# when sum(action_natural_units) > 0
action = np.array([1,1, 1])
action_natural_units = np.maximum( max_action * (1 + action)/2 , 0.)
observation = env.observations
cpue_2_value = env.cpue_2(observation, action_natural_units)
assert -1<cpue_2_value[0]<1
assert -1<cpue_2_value[1]<1

def test_reset():
env = greenCrabSimplifiedEnv()
steps = env.Tmax
# run the simulation util the
for i in range(steps):
env.step(np.array([0,0, 0])) # set constant amount of trap every year

curr_ob_space = env.observation_space

# if final observation space is [-1, -1], set it to other obseravtion space for test
if curr_ob_space == np.array([-1,-1]):
curr_ob_space = np.array([1,1])

# reset the obseravtion environment
new_ob_space, new_info = env.reset()

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

0 comments on commit 1ee12f8

Please sign in to comment.