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

delete actions on writing history #46

Merged
merged 1 commit into from
Dec 9, 2022
Merged
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 physbo/search/discrete/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ def write(
time_run_simulator=time_run_simulator,
)
self.training.add(X=X, t=t, Z=Z)
local_index = np.searchsorted(self.actions, action)
local_index = local_index[np.take(self.actions, local_index, mode='clip') == action]
self.actions = self._delete_actions(local_index)
if self.new_data is None:
self.new_data = variable(X=X, t=t, Z=Z)
else:
Expand Down
3 changes: 3 additions & 0 deletions physbo/search/discrete_multi/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ def write(
else:
self.new_data_list[i].add(X=X, t=t[:, i], Z=Z)
self.training_list[i].add(X=X, t=t[:, i], Z=Z)
local_index = np.searchsorted(self.actions, action)
local_index = local_index[np.take(self.actions, local_index, mode='clip') == action]
self.actions = self._delete_actions(local_index)

def _model(self, i):
training = self.training_list[i]
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/test_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ def policy():
return physbo.search.discrete.policy(test_X=X)


def test_write(policy, X):
simulator = lambda x: 1.0
ACTIONS = np.array([0, 1], np.int32)

policy.write(ACTIONS, np.apply_along_axis(simulator, 1, X[ACTIONS]))
numpy.testing.assert_array_equal(ACTIONS, policy.history.chosen_actions[:len(ACTIONS)])
assert len(X) - len(ACTIONS) == len(policy.actions)


def test_randomsearch(policy, mocker):
simulator = mocker.MagicMock(return_value=1.0)
write_spy = mocker.spy(physbo.search.discrete.policy, "write")
Expand Down