Skip to content

Commit

Permalink
update bots
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahBarrett98 committed Mar 7, 2021
1 parent d6e16c4 commit a67fa42
Show file tree
Hide file tree
Showing 18 changed files with 31 additions and 12 deletions.
Binary file modified __pycache__/settings.cpython-37.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import socket
import threading
from src.SocialStudy import questions
from src.Pi.PiBot.pibot2 import PiBot2
from src.Pi.PiBot.PiBot2 import PiBot2
"""
determine if main or pi
"""
Expand All @@ -29,7 +29,7 @@ def main_train_and_test():
threading.Thread(target=questions.get_user_input, args=()).start()
else:
pibot = PiBot2()
steps = 250
steps = 1
train.train_session(pibot, steps)
# publish_data(actions)

Expand Down
1 change: 1 addition & 0 deletions outputs/actions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"count": "0"}
1 change: 0 additions & 1 deletion outputs/actions.txt

This file was deleted.

2 changes: 1 addition & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
ACTION_FILE = r".\outputs\actions.txt"
RESPONSE_FILE = r".\outputs\responses.json"
else:
ACTION_FILE = "/home/pi/RealDilemma/outputs/actions.txt"
ACTION_FILE = "/home/pi/RealDilemma/outputs/actions.json"
RESPONSE_FILE = r"/home/pi/RealDilemma/outputs/responses.json"

QUESTIONS_FILE = "./outputs/Questions.json"
Expand Down
Binary file modified src/DRL/gym-pibot/gym_pibot/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion src/DRL/gym-pibot/gym_pibot/envs/pibot_env.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gym
from gym import error, spaces, utils
from gym.utils import seeding
from src.Pi.PiBot.pibot import PiBot
from src.Pi.PiBot.PiBot import PiBot
import numpy as np

ENERGY_THRES = 4000# thres for total amount of different motors used in robo
Expand Down
21 changes: 19 additions & 2 deletions src/DRL/gym-pibot/gym_pibot/envs/pibot_env2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import gym
from gym import error, spaces, utils
import numpy as np
from settings import ACTION_FILE
import json

ENERGY_THRES = 4000# thres for total amount of different motors used in robo
MAX_REWARD = 1000
Expand All @@ -13,7 +15,7 @@ class PiBotEnv2(gym.Env):
"""
metadata = {'render.modes': ['human']}

def __init__(self, PiBot, servo=False):
def __init__(self, PiBot, servo=True):
super(PiBotEnv2, self).__init__()
self.PiBot = PiBot
self.SERVO = servo
Expand All @@ -25,6 +27,9 @@ def __init__(self, PiBot, servo=False):
4: self.PiBot.stop,
5: self.PiBot._servo
}
# FOR RECORDING ACTIONS TAKEN #
self.ACTION = []
self._RECORD_ACTION = False
if self.SERVO:
#self.reward_range = (0, MAX_REWARD)
self.action_space = spaces.MultiDiscrete([6,# action
Expand Down Expand Up @@ -71,6 +76,8 @@ def do_action(self, action):
do = self.CONTROL_LOOKUP[action[0]]
duty = 100 - action[1]
time = action[2]
if self._RECORD_ACTION:
self.ACTION.append(str([int(x) for x in action]))
# perform action
do(duty, time)

Expand All @@ -81,7 +88,6 @@ def _get_reward(self):
"""
# maximize amount of movement, maximize distance in ultrasound sensor
state = self._get_state()
# diff(X) + score
reward = state[1] + state[2]
return reward

Expand All @@ -102,8 +108,19 @@ def _get_state(self):
:return:
"""
return self.PiBot.get_state()
def _record_actions(self):
with open(ACTION_FILE, "r") as f:
data = json.load(f)
count = int(data["count"])
data[count] = self.ACTION
data["count"] = count+1
with open(ACTION_FILE, "w") as f:
json.dump(data, f)

def close(self):
if self._RECORD_ACTION:
print("saving actions")
self._record_actions()
del self.PiBot


Expand Down
Binary file modified src/DRL/train/__pycache__/train.cpython-37.pyc
Binary file not shown.
10 changes: 6 additions & 4 deletions src/DRL/train/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ def train_session(pibot, steps, train_dict=TRAIN_DICT):
def train(steps, pibot, model, model_name) -> str:
# If the environment don't follow the interface, an error will be thrown
env = PiBotEnv2(pibot)
# record actions
env._RECORD_ACTION = True
check_env(env, warn=True)
# The algorithms require a vectorized environment to run
env = DummyVecEnv([partial(PiBotEnv2, PiBot=pibot)])
model = model(env=env)
d_env = DummyVecEnv([partial(PiBotEnv2, PiBot=pibot)])
model = model(env=d_env)
model.learn(total_timesteps=steps)
fpath = os.path.join(TRAIN_DIR, model_name)
model.save(fpath)
Expand All @@ -45,6 +47,6 @@ def train(steps, pibot, model, model_name) -> str:
policies[model_name] = fpath
with open(POLICYF, "w") as f:
json.dump(policies, f)
del pibot
del env
env._record_actions()

return fpath
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Pi/PiBot/PiBot2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import time
import numpy as np
from threading import Thread
from src.Pi.PiBot.pibot import PiBot
from src.Pi.PiBot.PiBot import PiBot

class PiBot2(PiBot):
"""
Expand Down
Binary file added src/Pi/PiBot/__pycache__/PiBot.cpython-37.pyc
Binary file not shown.
Binary file added src/Pi/PiBot/__pycache__/PiBot2.cpython-37.pyc
Binary file not shown.
Binary file modified src/SocialStudy/__pycache__/questions.cpython-37.pyc
Binary file not shown.

0 comments on commit a67fa42

Please sign in to comment.