Robot SimulationΒΆ
In [35]:
from jyro.simulator import (Robot, Pioneer, Pioneer16Sonars,
PioneerFrontLightSensors, Camera,
VSimulator)
import random
import numpy as np
In [36]:
robot = Pioneer("Pioneer", 3.5, 2, 0)
In [37]:
robot.addDevice(Pioneer16Sonars())
robot.addDevice(Camera())
light_sensors = PioneerFrontLightSensors(3.0)
light_sensors.lightMode = 'ambient'
robot.addDevice(light_sensors)
Out[37]:
In [38]:
def worldf(physics):
physics.addBox(0, 0, 4, 4, fill="backgroundgreen", wallcolor="gray")
physics.addLight(2, 0.75, 1.0) # increased brightness for new linear version of lights
In [39]:
sim = VSimulator(robot, worldf)
In [40]:
camera = robot.device["camera"]
In [41]:
image = camera.getImage()
image
Out[41]:

In [42]:
image.size
Out[42]:
(60, 40)
In [43]:
data = camera.getData()
data.shape
Out[43]:
(40, 60, 3)
In [44]:
robot.move(0.50, 0.35)
In [45]:
sim.step()
In [46]:
def random_action():
"""Generate a random action from a limited set of possible settings"""
possible = [-1.0, -0.5, 0.0, 0.5, 1.0]
return [random.choice(possible), random.choice(possible)]
def get_senses(robot):
light = robot["light"].getData()
sonar = [v/3.0 for v in robot["sonar"].getData()]
camera = robot["camera"].getData()
return [light, sonar, camera]
In [47]:
senses = get_senses(robot)
list(map(len, senses))
Out[47]:
[2, 16, 40]
In [48]:
def brain(robot):
senses = get_senses(robot)
net.propagate(senses)
translate, rotate = random_action()
#self.move(translate, rotate)
robot.move(0.50, 0.35)
In [49]:
robot.brain = brain
In [50]:
from conx import Network, Layer, FlattenLayer, SGD, ImageLayer, Conv2DLayer
import numpy as np
In [51]:
net = Network("Robot Prediction Network")
net.add(Layer("light", 2))
net.add(Layer("sonar", 16))
net.add(ImageLayer("camera", (40,60), 3))
net.add(FlattenLayer("flatten"))
net.add(Conv2DLayer("conv", 16, (3,3)))
net.add(Layer("hidden", 50, activation="relu"))
net.add(Layer("output1", 2, activation="sigmoid"))
net.add(Layer("hidden2", 5, activation="sigmoid"))
net.add(Layer("hidden3", 10, activation="sigmoid", dropout=0.25))
net.add(Layer("hidden4", 10, activation="sigmoid"))
net.add(Layer("output2", 5, activation="sigmoid"))
In [52]:
net.connect("sonar", "hidden2")
net.connect("light", "hidden")
net.connect("camera", "conv")
net.connect("conv", "flatten")
net.connect("flatten", "hidden2")
net.connect("hidden", "hidden2")
net.connect("hidden2", "hidden3")
##net.connect("hidden2", "output2")
net.connect("hidden3", "output2")
net.connect("hidden3", "hidden4")
net.connect("hidden4", "output1")
In [53]:
net.compile(optimizer="adam", error="mse")
#net.config["hspace"] = 200
In [54]:
net
Out[54]:
In [55]:
matrix = net.propagate_to("conv", get_senses(robot))
In [56]:
net["conv"].feature = 6
In [57]:
net.propagate_to_features("conv", get_senses(robot), scale=3)
Out[57]:
In [58]:
net.train_options
Out[58]:
{}
In [59]:
net.dashboard()
In [60]:
net
Out[60]:
In [61]:
net.test()
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-61-70ff5a56f9ac> in <module>()
----> 1 net.test()
/usr/local/lib/python3.5/dist-packages/conx/network.py in test(self, batch_size, tolerance, force, show_inputs, show_outputs, filter)
319 """
320 if len(self.dataset.inputs) == 0:
--> 321 raise Exception("nothing to test")
322 if self.dataset._split == len(self.dataset.inputs):
323 inputs = self.dataset._train_inputs
Exception: nothing to test