From 7b26290393cc2ca99a33de76ef08c1f66bf0bcd6 Mon Sep 17 00:00:00 2001 From: Charlie Date: Mon, 22 Jul 2019 15:21:16 -0700 Subject: [PATCH 1/2] Getting it to work --- .gitignore | 4 ++- example/configuration.py | 13 ++++---- example/run_example.py | 6 +++- example/test.html | 68 ---------------------------------------- package/driver.py | 7 +++-- package/environment.py | 4 +-- requirements.txt | 6 ++-- srcdir.py | 2 +- 8 files changed, 24 insertions(+), 86 deletions(-) diff --git a/.gitignore b/.gitignore index 07fe0a3..8fee2af 100644 --- a/.gitignore +++ b/.gitignore @@ -93,4 +93,6 @@ ENV/ .ropeproject .idea/ -.pypirc \ No newline at end of file +.pypirc +example/dqn_selenium_ai_weights.h5f + diff --git a/example/configuration.py b/example/configuration.py index e8edc6f..9c2770a 100644 --- a/example/configuration.py +++ b/example/configuration.py @@ -1,7 +1,10 @@ import os -import keras.backend as K import numpy as np +from package.environment import SeleniumEnvironment +from package.processor import SeleniumObservationProcessor +from srcdir import srcdir +import keras.backend as K from keras.layers import Dense, Activation, Flatten, Convolution2D, Permute from keras.layers.convolutional import Conv2D from keras.models import Sequential @@ -9,11 +12,6 @@ from rl.memory import SequentialMemory from rl.policy import LinearAnnealedPolicy, EpsGreedyQPolicy -from package.environment import SeleniumEnvironment -from package.processor import SeleniumObservationProcessor -from srcdir import srcdir - - class ExampleConfiguration(AbstractConfiguration): mode = KickoffModes.train # type: KickoffModes @@ -103,6 +101,7 @@ def on_step_reset(self, driver): def on_environment_creation(self): # Create file within the docker container to use as the test web page filepath = srcdir + '/example/test.html' + print(filepath) self.environment.selenium_docker_wrapper.upload_file_to_container(filepath) self.starting_url = 'file://' + '/' + os.path.basename(filepath) @@ -156,4 +155,4 @@ def determine_reward(self, driver, action_index): reward = 0 done = False - return done, reward \ No newline at end of file + return done, reward diff --git a/example/run_example.py b/example/run_example.py index 403027d..6c3afd6 100644 --- a/example/run_example.py +++ b/example/run_example.py @@ -1,7 +1,11 @@ +import os.path +import sys +sys.path.append(os.path.join(os.path.dirname(__file__), '..')) +sys.path.append(os.path.join(os.path.dirname(__file__), '../package')) from example.configuration import ExampleConfiguration from package.kickoff import kickoff if __name__ == '__main__': configuration = ExampleConfiguration() - kickoff(configuration=configuration) \ No newline at end of file + kickoff(configuration=configuration) diff --git a/example/test.html b/example/test.html index b2d5ad5..e84fe56 100644 --- a/example/test.html +++ b/example/test.html @@ -1,71 +1,3 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/package/driver.py b/package/driver.py index 156464a..48a5d67 100644 --- a/package/driver.py +++ b/package/driver.py @@ -65,15 +65,16 @@ def _connect_to_remote_instance(self, chrome_options, selenium_url): desired_capabilities=chrome_options.to_capabilities()) def clean(self): - print 'Cleaning up Selenium driver and docker container' + print('Cleaning up Selenium driver and docker container') self.driver and self.driver.quit() self.container and self.docker_client.remove_container(self.container_id, force=True) def create_archive(self, filepath): + print(filepath) pw_tarstream = BytesIO() pw_tar = tarfile.TarFile(fileobj=pw_tarstream, mode='w') - file_data = open(filepath, 'r').read() + file_data = open(filepath, 'rb').read() tarinfo = tarfile.TarInfo(name=os.path.basename(filepath)) tarinfo.size = len(file_data) tarinfo.mtime = time.time() @@ -84,4 +85,4 @@ def create_archive(self, filepath): def upload_file_to_container(self, filepath): with self.create_archive(filepath) as archive: - return self.docker_client.put_archive(container=self.container_id, path='/', data=archive) \ No newline at end of file + return self.docker_client.put_archive(container=self.container_id, path='/', data=archive) diff --git a/package/environment.py b/package/environment.py index 7727667..d88c759 100644 --- a/package/environment.py +++ b/package/environment.py @@ -30,7 +30,7 @@ def make(self): vnc_address = re.sub(str(container_port), str(container_vnc_port), container_url) vnc_address = re.sub('http', 'vnc', vnc_address) - print 'You can view the VNC instance at ' + vnc_address, 'Password "secret"' + print('You can view the VNC instance at ' + vnc_address, 'Password "secret"') self.vnc_env = VNCEnv() self.vnc_env._configure(remotes=str(vnc_address), vnc_kwargs={'password': 'secret'}) @@ -70,4 +70,4 @@ def step(self, action_index): done, reward = self.configuration.determine_reward(self.driver, action_index) observation_array = self.vnc_env._step(action) - return observation_array, reward, done, {} \ No newline at end of file + return observation_array, reward, done, {} diff --git a/requirements.txt b/requirements.txt index 4b3b8e1..0a4b699 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ +numpy +go-vncdriver attrs==17.2.0 autobahn==17.5.1 Automat==0.6.0 @@ -10,7 +12,6 @@ docker-py==1.10.3 docker-pycreds==0.2.1 fastzbarlight==0.0.14 funcsigs==1.0.2 -go-vncdriver==0.4.19 gym==0.7.4 h5py==2.7.0 idna==2.5 @@ -19,7 +20,6 @@ ipaddress==1.0.18 Keras==2.0.4 keras-rl==0.3.0 mock==2.0.0 -numpy==1.13.0rc2 olefile==0.44 pbr==3.0.1 Pillow==4.1.1 @@ -39,4 +39,4 @@ universe==0.21.3 urllib3==1.21.1 websocket-client==0.40.0 Werkzeug==0.12.2 -zope.interface==4.4.1 \ No newline at end of file +zope.interface==4.4.1 diff --git a/srcdir.py b/srcdir.py index 6256c2e..d6d86e6 100644 --- a/srcdir.py +++ b/srcdir.py @@ -1,2 +1,2 @@ import os -srcdir = os.path.dirname(__file__) \ No newline at end of file +srcdir = os.path.dirname(os.path.abspath(__file__)) From 014e7deea4fa885fb474cd094b7d22465a17d110 Mon Sep 17 00:00:00 2001 From: Charlie Date: Mon, 22 Jul 2019 17:46:56 -0700 Subject: [PATCH 2/2] adding a fix --- example/configuration.py | 61 ++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/example/configuration.py b/example/configuration.py index 9c2770a..d7189fc 100644 --- a/example/configuration.py +++ b/example/configuration.py @@ -13,7 +13,7 @@ from rl.policy import LinearAnnealedPolicy, EpsGreedyQPolicy class ExampleConfiguration(AbstractConfiguration): - mode = KickoffModes.train # type: KickoffModes + mode = KickoffModes.test # type: KickoffModes use_preset_training = True render = True # Default true when testing @@ -110,36 +110,37 @@ def get_preset_training_step(self): return 0 targeted_element_xpath = '//input[contains(@id, "add-to-cart-button")]' - target_element = self.environment.driver.find_element_by_xpath(targeted_element_xpath) - - target_element_top = target_element.location["y"] - target_element_bottom = target_element_top + target_element.size["height"] - - scroll_amount = self.environment.driver.execute_script("return window.pageYOffset;") - window_size = self.environment.driver.get_window_size() - inner_height = self.environment.driver.execute_script("return window.innerHeight;") - - if scroll_amount > target_element_top: - action = self.environment.action_space.mouse_scroll_up - elif scroll_amount + inner_height < target_element_bottom: - action = self.environment.action_space.mouse_scroll_down - else: - target_element_left = target_element.location["x"] - target_element_right = target_element_left + target_element.size["width"] - - if self.environment.action_space.mouse_position_x > target_element_right: - action = self.environment.action_space.move_mouse_left - elif self.environment.action_space.mouse_position_x < target_element_left: - action = self.environment.action_space.move_mouse_right - elif self.environment.action_space.mouse_position_y - (window_size["height"] - inner_height) + scroll_amount \ - > target_element_bottom: - action = self.environment.action_space.move_mouse_up - elif self.environment.action_space.mouse_position_y - (window_size["height"] - inner_height) + scroll_amount \ - < target_element_top: - action = self.environment.action_space.move_mouse_down + try: + target_element = self.environment.driver.find_element_by_xpath(targeted_element_xpath) + target_element_top = target_element.location["y"] + target_element_bottom = target_element_top + target_element.size["height"] + + scroll_amount = self.environment.driver.execute_script("return window.pageYOffset;") + window_size = self.environment.driver.get_window_size() + inner_height = self.environment.driver.execute_script("return window.innerHeight;") + + if scroll_amount > target_element_top: + action = self.environment.action_space.mouse_scroll_up + elif scroll_amount + inner_height < target_element_bottom: + action = self.environment.action_space.mouse_scroll_down else: - action = self.environment.action_space.mouse_press - + target_element_left = target_element.location["x"] + target_element_right = target_element_left + target_element.size["width"] + + if self.environment.action_space.mouse_position_x > target_element_right: + action = self.environment.action_space.move_mouse_left + elif self.environment.action_space.mouse_position_x < target_element_left: + action = self.environment.action_space.move_mouse_right + elif self.environment.action_space.mouse_position_y - (window_size["height"] - inner_height) + scroll_amount \ + > target_element_bottom: + action = self.environment.action_space.move_mouse_up + elif self.environment.action_space.mouse_position_y - (window_size["height"] - inner_height) + scroll_amount \ + < target_element_top: + action = self.environment.action_space.move_mouse_down + else: + action = self.environment.action_space.mouse_press + except: + return 0 return self.environment.action_space.available_actions.index(action) def determine_reward(self, driver, action_index):