Skip to content

Commit

Permalink
reformat the codes
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanganVR committed Sep 28, 2018
1 parent 531bbfb commit f43ed82
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ venv*/
# data
data/
*.mp4
videos/
3 changes: 2 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[BASIC]

#variable-rgx=[a-z0-9_]{1,30}$
good-names=dx,dy,i,j,k,px,py,r,th,vx,vy,x,y,z
good-names=dx,dy,i,j,k,px,py,r,th,vx,vy,x,y,z,ob

[TYPECHECK]

Expand All @@ -12,3 +12,4 @@ generated-members=numpy.*,torch.*


disable=missing-docstring,useless-object-inheritance
max-line-length=120
2 changes: 1 addition & 1 deletion crowd_nav/configs/train.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ batch_size = 100


[imitation_learning]
il_episodes = 3000
il_episodes = 3
il_policy = orca
il_epochs = 50
il_learning_rate = 0.01
Expand Down
2 changes: 1 addition & 1 deletion crowd_nav/policy/multi_human_rl.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def build_occupancy_maps(self, human_states):
dm[2 * int(index) + 1].append(other_vx[i])
dm[2 * int(index) + 2].append(other_vy[i])
else:
raise NotImplemented
raise NotImplementedError
for i, cell in enumerate(dm):
dm[i] = sum(dm[i]) / len(dm[i]) if len(dm[i]) != 0 else 0
occupancy_maps.append([dm])
Expand Down
16 changes: 8 additions & 8 deletions crowd_nav/test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import torch
import logging
import argparse
import configparser
import os
import torch
import numpy as np
import gym
import os
from crowd_nav.utils.explorer import Explorer
from crowd_nav.policy.policy_factory import policy_factory
from crowd_sim.envs.utils.robot import Robot
Expand Down Expand Up @@ -46,7 +46,7 @@ def main():
logging.basicConfig(level=logging.INFO, format='%(asctime)s, %(levelname)s: %(message)s',
datefmt="%Y-%m-%d %H:%M:%S")
device = torch.device("cuda:0" if torch.cuda.is_available() and args.gpu else "cpu")
logging.info('Using device: {}'.format(device))
logging.info('Using device: %s', device)

# configure policy
policy = policy_factory[args.policy]()
Expand Down Expand Up @@ -80,7 +80,7 @@ def main():
robot.policy.safety_space = 0
else:
robot.policy.safety_space = 0
logging.info('ORCA agent buffer: {}'.format(robot.policy.safety_space))
logging.info('ORCA agent buffer: %f', robot.policy.safety_space)

policy.set_env(env)
robot.print_info()
Expand All @@ -90,19 +90,19 @@ def main():
last_pos = np.array(robot.get_position())
while not done:
action = robot.act(ob)
ob, reward, done, info = env.step(action)
ob, _, done, info = env.step(action)
current_pos = np.array(robot.get_position())
logging.debug('Speed: {:.2f}'.format(np.linalg.norm(current_pos - last_pos) / robot.time_step))
logging.debug('Speed: %.2f', np.linalg.norm(current_pos - last_pos) / robot.time_step)
last_pos = current_pos
if args.traj:
env.render('traj', args.video_file)
else:
env.render('video', args.video_file)

logging.info('It takes {:.2f} seconds to finish. Final status is {}'.format(env.global_time, info))
logging.info('It takes %.2f seconds to finish. Final status is %s', env.global_time, info)
if robot.visible and info == 'reach goal':
human_times = env.get_human_times()
logging.info('Average time for humans to reach goal: {:.2f}'.format(sum(human_times) / len(human_times)))
logging.info('Average time for humans to reach goal: %.2f', sum(human_times) / len(human_times))
else:
explorer.run_k_episodes(env.case_size[args.phase], args.phase, print_failure=True)

Expand Down
10 changes: 5 additions & 5 deletions crowd_nav/train.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import torch
import sys
import logging
import argparse
import configparser
import os
import shutil
import torch
import gym
import git
from crowd_sim.envs.utils.robot import Robot
Expand Down Expand Up @@ -55,9 +55,9 @@ def main():
logging.basicConfig(level=level, handlers=[stdout_handler, file_handler],
format='%(asctime)s, %(levelname)s: %(message)s', datefmt="%Y-%m-%d %H:%M:%S")
repo = git.Repo(search_parent_directories=True)
logging.info('Current git head hash code: {}'.format(repo.head.object.hexsha))
logging.info('Current git head hash code: %s'.format(repo.head.object.hexsha))
device = torch.device("cuda:0" if torch.cuda.is_available() and args.gpu else "cpu")
logging.info('Using device: {}'.format(device))
logging.info('Using device: %s', device)

# configure policy
policy = policy_factory[args.policy]()
Expand Down Expand Up @@ -130,7 +130,7 @@ def main():
trainer.optimize_epoch(il_epochs)
torch.save(model.state_dict(), il_weight_file)
logging.info('Finish imitation learning. Weights saved.')
logging.info('Experience set size: {}/{}'.format(len(memory), memory.capacity))
logging.info('Experience set size: %d/%d', len(memory), memory.capacity)
explorer.update_target_model(model)

# reinforcement learning
Expand All @@ -142,7 +142,7 @@ def main():
if args.resume:
robot.policy.set_epsilon(epsilon_end)
explorer.run_k_episodes(100, 'train', update_memory=True, episode=0)
logging.info('Experience set size: {}/{}'.format(len(memory), memory.capacity))
logging.info('Experience set size: %d/%d', len(memory), memory.capacity)
episode = 0
while episode < train_episodes:
if args.resume:
Expand Down
19 changes: 9 additions & 10 deletions crowd_nav/utils/explorer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import torch
import copy
import torch
from crowd_sim.envs.utils.info import *


Expand Down Expand Up @@ -74,16 +74,16 @@ def run_k_episodes(self, k, phase, update_memory=False, imitation_learning=False
success_rate = success / k
collision_rate = collision / k
assert success + collision + timeout == k
avg_nav_time = sum(success_times) / len(success_times) if len(success_times) != 0 else self.env.time_limit
avg_nav_time = sum(success_times) / len(success_times) if success_times else self.env.time_limit

extra_info = '' if episode is None else 'in episode {} '.format(episode)
logging.info('{:<5} {}has success rate: {:.2f}, collision rate: {:.2f}, nav time: {:.2f}, total reward: {:.4f}'.
format(phase.upper(), extra_info, success_rate, collision_rate, avg_nav_time,
average(cumulative_rewards)))
if phase in ['val', 'test']:
total_time = sum(success_times + collision_times + timeout_times) * self.robot.time_step
logging.info('Frequency of being in danger: {:.2f} and average min separate distance in danger: {:.2f}'.
format(too_close / total_time, average(min_dist)))
logging.info('Frequency of being in danger: %.2f and average min separate distance in danger: %.2f',
too_close / total_time, average(min_dist))

if print_failure:
logging.info('Collision cases: ' + ' '.join([str(x) for x in collision_cases]))
Expand All @@ -93,8 +93,7 @@ def update_memory(self, states, actions, rewards, imitation_learning=False):
if self.memory is None or self.gamma is None:
raise ValueError('Memory or gamma value is not set!')

for i in range(len(states)):
state = states[i]
for i, state in enumerate(states):
reward = rewards[i]

# VALUE UPDATE
Expand Down Expand Up @@ -122,8 +121,8 @@ def update_memory(self, states, actions, rewards, imitation_learning=False):
self.memory.push((state, value))


def average(li):
if len(li) == 0:
return 0
def average(input_list):
if input_list:
return sum(input_list) / len(input_list)
else:
return sum(li) / len(li)
return 0
8 changes: 4 additions & 4 deletions crowd_nav/utils/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import numpy as np


def running_mean(x, N):
def running_mean(x, n):
cumsum = np.cumsum(np.insert(x, 0, 0))
return (cumsum[N:] - cumsum[:-N]) / float(N)
return (cumsum[n:] - cumsum[:-n]) / float(n)


def main():
Expand All @@ -31,8 +31,8 @@ def main():
ax4_legends = []

for i, log_file in enumerate(args.log_files):
with open(log_file, 'r') as fo:
log = fo.read()
with open(log_file, 'r') as file:
log = file.read()

val_pattern = r"VAL in episode (?P<episode>\d+) has success rate: (?P<sr>[0-1].\d+), " \
r"collision rate: (?P<cr>[0-1].\d+), nav time: (?P<time>\d+.\d+), " \
Expand Down
10 changes: 5 additions & 5 deletions crowd_nav/utils/trainer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
from torch.utils.data import DataLoader
import logging


class Trainer(object):
Expand All @@ -18,7 +18,7 @@ def __init__(self, model, memory, device, batch_size):
self.optimizer = None

def set_learning_rate(self, learning_rate):
logging.info('Current learning rate: {}'.format(learning_rate))
logging.info('Current learning rate: %f', learning_rate)
self.optimizer = optim.SGD(self.model.parameters(), lr=learning_rate, momentum=0.9)

def optimize_epoch(self, num_epochs):
Expand All @@ -40,15 +40,15 @@ def optimize_epoch(self, num_epochs):
epoch_loss += loss.data.item()

average_epoch_loss = epoch_loss / len(self.memory)
logging.debug('Average loss in epoch {}: {:.2E}'.format(epoch, average_epoch_loss))
logging.debug('Average loss in epoch %d: %.2E', epoch, average_epoch_loss)

return average_epoch_loss

def optimize_batch(self, num_batches):
if self.optimizer is None:
raise ValueError('Learning rate is not set!')
losses = 0
for batch in range(num_batches):
for _ in range(num_batches):
inputs, values = next(iter(self.data_loader))
inputs = Variable(inputs)
values = Variable(values)
Expand All @@ -61,6 +61,6 @@ def optimize_batch(self, num_batches):
losses += loss.data.item()

average_loss = losses / num_batches
logging.debug('Average loss : {:.2E}'.format(average_loss))
logging.debug('Average loss : %.2E', average_loss)

return average_loss
35 changes: 17 additions & 18 deletions crowd_sim/envs/crowd_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def configure(self, config):
self.circle_radius = config.getfloat('sim', 'circle_radius')
self.human_num = config.getint('sim', 'human_num')
else:
raise NotImplemented
raise NotImplementedError
self.case_counter = {'train': 0, 'test': 0, 'val': 0}

logging.info('human number: {}'.format(self.human_num))
Expand Down Expand Up @@ -133,8 +133,7 @@ def generate_random_human_position(self, human_num, rule):
py = (np.random.random() - 0.5) * height
collide = False
for agent in [self.robot] + self.humans:
if norm((
px - agent.px, py - agent.py)) < human.radius + agent.radius + self.discomfort_dist:
if norm((px - agent.px, py - agent.py)) < human.radius + agent.radius + self.discomfort_dist:
collide = True
break
if not collide:
Expand Down Expand Up @@ -268,7 +267,7 @@ def reset(self, phase='test', test_case=None):
self.train_val_sim = 'circle_crossing'

if self.config.get('humans', 'policy') == 'trajnet':
raise NotImplemented
raise NotImplementedError
else:
counter_offset = {'train': self.case_capacity['val'] + self.case_capacity['test'],
'val': 0, 'test': self.case_capacity['val']}
Expand All @@ -292,7 +291,7 @@ def reset(self, phase='test', test_case=None):
self.humans[1].set(-5, -5, -5, 5, 0, 0, np.pi / 2)
self.humans[2].set(5, -5, 5, 5, 0, 0, np.pi / 2)
else:
raise NotImplemented
raise NotImplementedError

for agent in [self.robot] + self.humans:
agent.time_step = self.time_step
Expand All @@ -306,11 +305,11 @@ def reset(self, phase='test', test_case=None):

# get current observation
if self.robot.sensor == 'coordinates':
obs = [human.get_observable_state() for human in self.humans]
ob = [human.get_observable_state() for human in self.humans]
elif self.robot.sensor == 'RGB':
raise NotImplemented
raise NotImplementedError

return obs
return ob

def onestep_lookahead(self, action):
return self.step(action, update=False)
Expand All @@ -323,10 +322,10 @@ def step(self, action, update=True):
human_actions = []
for human in self.humans:
# observation for humans is always coordinates
obs = [other_human.get_observable_state() for other_human in self.humans if other_human != human]
ob = [other_human.get_observable_state() for other_human in self.humans if other_human != human]
if self.robot.visible:
obs += [self.robot.get_observable_state()]
human_actions.append(human.act(obs))
ob += [self.robot.get_observable_state()]
human_actions.append(human.act(ob))

# collision detection
dmin = float('inf')
Expand Down Expand Up @@ -409,16 +408,16 @@ def step(self, action, update=True):

# compute the observation
if self.robot.sensor == 'coordinates':
obs = [human.get_observable_state() for human in self.humans]
ob = [human.get_observable_state() for human in self.humans]
elif self.robot.sensor == 'RGB':
raise NotImplemented
raise NotImplementedError
else:
if self.robot.sensor == 'coordinates':
obs = [human.get_next_observable_state(action) for human, action in zip(self.humans, human_actions)]
ob = [human.get_next_observable_state(action) for human, action in zip(self.humans, human_actions)]
elif self.robot.sensor == 'RGB':
raise NotImplemented
raise NotImplementedError

return obs, reward, done, info
return ob, reward, done, info

def render(self, mode='human', output_file=None):
from matplotlib import animation
Expand Down Expand Up @@ -537,7 +536,7 @@ def render(self, mode='human', output_file=None):
agent_state = state[1][i - 1]
theta = np.arctan2(agent_state.vy, agent_state.vx)
orientation.append(((agent_state.px, agent_state.py), (agent_state.px + radius * np.cos(theta),
agent_state.py + radius * np.sin(theta))))
agent_state.py + radius * np.sin(theta))))
orientations.append(orientation)
arrows = [patches.FancyArrowPatch(*orientation[0], color=arrow_color, arrowstyle=arrow_style)
for orientation in orientations]
Expand Down Expand Up @@ -608,4 +607,4 @@ def on_click(event):
else:
plt.show()
else:
raise NotImplemented
raise NotImplementedError
2 changes: 1 addition & 1 deletion crowd_sim/envs/policy/linear.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
from crowd_sim.envs.policy.policy import Policy
from crowd_sim.envs.utils.action import ActionXY
import numpy as np


class Linear(Policy):
Expand Down
2 changes: 1 addition & 1 deletion crowd_sim/envs/policy/policy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
import abc
import numpy as np


class Policy(object):
Expand Down

0 comments on commit f43ed82

Please sign in to comment.