Skip to content

Commit 65a1b2c

Browse files
committed
Make random agent and generate data for plotting
1 parent 1b421f6 commit 65a1b2c

7 files changed

+371
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
0.000000000000000000e+00,1.950000000000000000e+02,0.000000000000000000e+00
2+
1.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
3+
2.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
4+
3.000000000000000000e+00,6.880000000000000000e+02,0.000000000000000000e+00
5+
4.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
6+
5.000000000000000000e+00,4.460000000000000000e+02,0.000000000000000000e+00
7+
6.000000000000000000e+00,6.890000000000000000e+02,0.000000000000000000e+00
8+
7.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
9+
8.000000000000000000e+00,7.080000000000000000e+02,0.000000000000000000e+00
10+
9.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
11+
1.000000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
12+
1.100000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
13+
1.200000000000000000e+01,6.540000000000000000e+02,0.000000000000000000e+00
14+
1.300000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
15+
1.400000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
16+
1.500000000000000000e+01,1.910000000000000000e+02,0.000000000000000000e+00
17+
1.600000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
18+
1.700000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
19+
1.800000000000000000e+01,9.520000000000000000e+02,0.000000000000000000e+00
20+
1.900000000000000000e+01,2.640000000000000000e+02,0.000000000000000000e+00
21+
2.000000000000000000e+01,4.260000000000000000e+02,0.000000000000000000e+00
22+
2.100000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
23+
2.200000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
24+
2.300000000000000000e+01,9.990000000000000000e+02,0.000000000000000000e+00
25+
2.400000000000000000e+01,6.390000000000000000e+02,0.000000000000000000e+00
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
0.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
2+
1.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
3+
2.000000000000000000e+00,6.320000000000000000e+02,0.000000000000000000e+00
4+
3.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
5+
4.000000000000000000e+00,6.410000000000000000e+02,0.000000000000000000e+00
6+
5.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
7+
6.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
8+
7.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
9+
8.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
10+
9.000000000000000000e+00,4.960000000000000000e+02,0.000000000000000000e+00
11+
1.000000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
12+
1.100000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
13+
1.200000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
14+
1.300000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
15+
1.400000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
16+
1.500000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
17+
1.600000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
18+
1.700000000000000000e+01,6.730000000000000000e+02,0.000000000000000000e+00
19+
1.800000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
20+
1.900000000000000000e+01,2.680000000000000000e+02,0.000000000000000000e+00
21+
2.000000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
22+
2.100000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
23+
2.200000000000000000e+01,2.750000000000000000e+02,0.000000000000000000e+00
24+
2.300000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
25+
2.400000000000000000e+01,8.010000000000000000e+02,0.000000000000000000e+00
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
0.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
2+
1.000000000000000000e+00,4.740000000000000000e+02,0.000000000000000000e+00
3+
2.000000000000000000e+00,2.820000000000000000e+02,0.000000000000000000e+00
4+
3.000000000000000000e+00,2.960000000000000000e+02,0.000000000000000000e+00
5+
4.000000000000000000e+00,5.460000000000000000e+02,0.000000000000000000e+00
6+
5.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
7+
6.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
8+
7.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
9+
8.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
10+
9.000000000000000000e+00,5.690000000000000000e+02,0.000000000000000000e+00
11+
1.000000000000000000e+01,6.830000000000000000e+02,0.000000000000000000e+00
12+
1.100000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
13+
1.200000000000000000e+01,6.550000000000000000e+02,0.000000000000000000e+00
14+
1.300000000000000000e+01,7.650000000000000000e+02,0.000000000000000000e+00
15+
1.400000000000000000e+01,2.080000000000000000e+02,0.000000000000000000e+00
16+
1.500000000000000000e+01,1.710000000000000000e+02,0.000000000000000000e+00
17+
1.600000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
18+
1.700000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
19+
1.800000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
20+
1.900000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
21+
2.000000000000000000e+01,6.990000000000000000e+02,0.000000000000000000e+00
22+
2.100000000000000000e+01,4.220000000000000000e+02,0.000000000000000000e+00
23+
2.200000000000000000e+01,5.720000000000000000e+02,0.000000000000000000e+00
24+
2.300000000000000000e+01,6.130000000000000000e+02,0.000000000000000000e+00
25+
2.400000000000000000e+01,6.500000000000000000e+02,0.000000000000000000e+00
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
0.000000000000000000e+00,2.890000000000000000e+02,0.000000000000000000e+00
2+
1.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
3+
2.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
4+
3.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
5+
4.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
6+
5.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
7+
6.000000000000000000e+00,2.450000000000000000e+02,0.000000000000000000e+00
8+
7.000000000000000000e+00,5.690000000000000000e+02,0.000000000000000000e+00
9+
8.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
10+
9.000000000000000000e+00,4.900000000000000000e+02,0.000000000000000000e+00
11+
1.000000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
12+
1.100000000000000000e+01,4.150000000000000000e+02,0.000000000000000000e+00
13+
1.200000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
14+
1.300000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
15+
1.400000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
16+
1.500000000000000000e+01,1.350000000000000000e+02,0.000000000000000000e+00
17+
1.600000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
18+
1.700000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
19+
1.800000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
20+
1.900000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
21+
2.000000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
22+
2.100000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
23+
2.200000000000000000e+01,6.730000000000000000e+02,0.000000000000000000e+00
24+
2.300000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
25+
2.400000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
0.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
2+
1.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
3+
2.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
4+
3.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
5+
4.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
6+
5.000000000000000000e+00,3.830000000000000000e+02,0.000000000000000000e+00
7+
6.000000000000000000e+00,6.860000000000000000e+02,0.000000000000000000e+00
8+
7.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
9+
8.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
10+
9.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
11+
1.000000000000000000e+01,6.310000000000000000e+02,0.000000000000000000e+00
12+
1.100000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
13+
1.200000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
14+
1.300000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
15+
1.400000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
16+
1.500000000000000000e+01,5.690000000000000000e+02,0.000000000000000000e+00
17+
1.600000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
18+
1.700000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
19+
1.800000000000000000e+01,3.620000000000000000e+02,0.000000000000000000e+00
20+
1.900000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
21+
2.000000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
22+
2.100000000000000000e+01,9.180000000000000000e+02,0.000000000000000000e+00
23+
2.200000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
24+
2.300000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
25+
2.400000000000000000e+01,5.970000000000000000e+02,0.000000000000000000e+00
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
0.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
2+
1.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
3+
2.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
4+
3.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
5+
4.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
6+
5.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
7+
6.000000000000000000e+00,5.400000000000000000e+02,0.000000000000000000e+00
8+
7.000000000000000000e+00,1.720000000000000000e+02,0.000000000000000000e+00
9+
8.000000000000000000e+00,1.000000000000000000e+03,0.000000000000000000e+00
10+
9.000000000000000000e+00,4.400000000000000000e+02,0.000000000000000000e+00
11+
1.000000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
12+
1.100000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
13+
1.200000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
14+
1.300000000000000000e+01,2.580000000000000000e+02,0.000000000000000000e+00
15+
1.400000000000000000e+01,1.650000000000000000e+02,0.000000000000000000e+00
16+
1.500000000000000000e+01,6.000000000000000000e+02,0.000000000000000000e+00
17+
1.600000000000000000e+01,7.180000000000000000e+02,0.000000000000000000e+00
18+
1.700000000000000000e+01,6.200000000000000000e+02,0.000000000000000000e+00
19+
1.800000000000000000e+01,7.170000000000000000e+02,0.000000000000000000e+00
20+
1.900000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
21+
2.000000000000000000e+01,9.810000000000000000e+02,0.000000000000000000e+00
22+
2.100000000000000000e+01,4.930000000000000000e+02,0.000000000000000000e+00
23+
2.200000000000000000e+01,1.000000000000000000e+03,0.000000000000000000e+00
24+
2.300000000000000000e+01,2.430000000000000000e+02,0.000000000000000000e+00
25+
2.400000000000000000e+01,6.730000000000000000e+02,0.000000000000000000e+00
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
#!/usr/bin/env python
2+
3+
#import rospy
4+
from random import randint
5+
#import time
6+
from geometry_msgs.msg import Twist, Vector3, Point, Quaternion, Pose2D
7+
from sensor_msgs.msg import LaserScan
8+
from std_msgs.msg import String, Int32MultiArray, Bool
9+
from nav_msgs.msg import OccupancyGrid
10+
from std_srvs.srv import Empty
11+
from scipy.spatial import distance
12+
import copy
13+
import pickle
14+
from getpass import getuser
15+
from nav_msgs.msg import Odometry
16+
import sys
17+
import numpy as np
18+
#from os import getcwd
19+
from datetime import datetime
20+
#import pandas as pd
21+
import os
22+
import math
23+
24+
## Global Variables
25+
# mobile_base velocity publisher
26+
command_pub = None
27+
# String command that will be sent to the robot
28+
cmd_msg = String()
29+
30+
# current time
31+
#cur_time = 0
32+
# current state. can be either "init", "drive, "halt", "turn_r", "turn_l"
33+
cur_state = "init"
34+
35+
# Are we training or executing?
36+
train = None
37+
map_name = ""
38+
39+
vel = Twist()
40+
current_position = Pose2D(-2,-2,0)
41+
42+
goal_point = Pose2D(4,4,0) # Weird coord transform error
43+
path = []
44+
global_map =np.genfromtxt("/home/"+getuser()+"/turtlepath/rl-ws/map.csv", delimiter=',')
45+
map = {}
46+
47+
finished = False
48+
49+
50+
def reset_stage():
51+
global current_position
52+
current_position = Pose2D(-2,-2,0)
53+
54+
def master_train(map_name,goal_point,train):
55+
global map
56+
# train several times in real life to build a map
57+
count = 0
58+
episode_num = 25
59+
dt = datetime.now()
60+
training_data = [[i,0, True] for i in range(episode_num)]
61+
satisfied = False
62+
s_count = 0
63+
while not satisfied and s_count < 10:
64+
s_count +=1
65+
count = 0
66+
for i in range(0,episode_num):
67+
path, crashed = train_q_real_life(map_name,goal_point,train, count,episode_num)
68+
print(len(path))
69+
reset_stage()
70+
71+
training_data[int(i)][1] = len(path)
72+
training_data[int(i)][2] = crashed
73+
74+
count += 1
75+
if(len(path) > 19):
76+
satisfied = False
77+
else:
78+
print("Took: " + str(s_count*episode_num))
79+
satisfied = True
80+
81+
# save data each count from training to use for plots and analysis
82+
filepath = "/home/"+getuser()+"/turtlepath/rl-ws/data/"
83+
filename = "random_" + map_name + "_" + dt.strftime("%Y-%m-%d-%H-%M-%S") + "_c" + str(count)
84+
np.savetxt(filepath + filename + ".csv", training_data, delimiter=",")
85+
86+
87+
return path
88+
89+
def train_q_real_life(map_name, goal_point, train, count, max_count):
90+
global current_position
91+
92+
if(count < 2):
93+
eps = 1 # totally random walk to build a better model
94+
if(count == max_count -1):
95+
eps = .05
96+
97+
timeout = 0
98+
path = []
99+
# a is action
100+
# 0 is North
101+
# 1 is East
102+
# 2 is South
103+
# 3 is West
104+
# s is state and equals a point in the map
105+
s = to_str(current_position)
106+
# Choose A from S at random
107+
a = e_greedy()
108+
# Loop through episode
109+
crashed = False
110+
while timeout < 1000 and s != to_str(goal_point) and not crashed:
111+
path.append(a)
112+
113+
# Take action A, observe R,S'
114+
r, s_prime, crashed = execute_rl(a,s)
115+
# Choose A' from S' at random
116+
a_prime = e_greedy()
117+
118+
# S<- S'; A<-A';
119+
s = s_prime
120+
a = a_prime
121+
timeout+=1
122+
123+
print(path)
124+
print("Crashed: " + str(crashed))
125+
return path, crashed
126+
127+
def to_str(s):
128+
return("X: "+ str(s.x) + " "+"Y: "+ str(s.y))
129+
130+
def decode_action(a):
131+
# a is action
132+
# 0 is North
133+
# 1 is East
134+
# 2 is South
135+
# 3 is West
136+
137+
if a == 0:
138+
action = "north"
139+
elif a == 1:
140+
action = "east"
141+
elif a == 2:
142+
action = "south"
143+
else:
144+
action = "west"
145+
return action
146+
147+
def e_greedy():
148+
# return a random action, a
149+
return np.random.randint(0,4)
150+
151+
def decode_string(s):
152+
if(s[8] == '-'):
153+
y = -1 * int(s[9])
154+
elif(len(s) >= 10 and s[9] == '-'):
155+
y = -1 * int(s[10])
156+
elif(s[3] == '-'):
157+
y = int(s[9])
158+
else:
159+
y = int(s[8])
160+
161+
if(s[3] == '-'):
162+
x = -1 * int(s[4])
163+
else:
164+
x = int(s[3])
165+
166+
return x,y
167+
168+
def execute_rl(a,s):
169+
global map
170+
global vel
171+
global current_position
172+
global finished
173+
prev = copy.deepcopy(current_position)
174+
#send_command(decode_action(a))
175+
x,y = decode_string(s)
176+
177+
if(a == 0):
178+
x +=1
179+
elif(a == 1):
180+
y -= 1
181+
elif(a == 2):
182+
x -=1
183+
elif(a ==3):
184+
y +=1
185+
186+
new_position = Pose2D(x,y,0)
187+
if not (check_global(new_position)):
188+
current_position = new_position
189+
190+
crashed = False
191+
#print("Next")
192+
# return it's new location
193+
reward = -1
194+
s_prime = to_str(current_position)
195+
if(s == s_prime):
196+
reward = -5
197+
if(crashed):
198+
reward = -10
199+
# give a big reward for reaching the goal
200+
if current_position == goal_point:
201+
reward = 100
202+
#print("Start: "+s+ " Finish: " + s_prime)
203+
#print(reward)
204+
if not (s in map): # If Q is not initalized for our action set it to 0
205+
d={}
206+
d[a] = {"state":s_prime,"reward":reward}
207+
map[s] = d
208+
elif not (a in map[s]):
209+
map[s][a] = {"state":s_prime,"reward":reward}
210+
else:
211+
map[s][a] = {"state":s_prime,"reward":reward}
212+
return reward, s_prime, crashed
213+
214+
def check_global(point):
215+
global global_map
216+
x = point.x + 5
217+
y = (point.y*-1 + 5)
218+
return bool(global_map[y][x])
219+
220+
master_train("test1", goal_point, True)
221+
print("done")

0 commit comments

Comments
 (0)