Example #1
0
def run(positions, plotEvery=1):
  encoder = OneDDepthEncoder(positions=positions,
                             radius=5,
                             wrapAround=True,
                             nPerPosition=28,
                             wPerPosition=3,
                             minVal=0,
                             maxVal=1)
  fetcher = Fetcher()
  plotter = Plotter(encoder)
  learner = QLearner(ACTIIONS, n=1008)

  lastState = None
  lastAction = None

  while True:
    outputData = fetcher.sync()

    if outputData is None:
      continue

    if fetcher.skippedTimesteps > 0:
      print ("Warning: skipped {0} timesteps, "
             "now at {1}").format(fetcher.skippedTimesteps, fetcher.timestep)

    if not ("reset" in outputData and
            "ForwardsSweepSensor" in outputData and
            "steer" in outputData):
      print ("Warning: Missing data on timestep {0}: {1}".format(
             fetcher.timestep, outputData))
      continue

    if outputData["reset"]:
      print "Reset."

    sensor = outputData["ForwardsSweepSensor"]
    steer = outputData["steer"]
    reward = outputData.get("reward") or 0

    encoding = encoder.encode(numpy.array(sensor))

    if lastState is not None:
      learner.update(lastState, str(lastAction), encoding, str(steer), reward)

    value = learner.value(encoding)

    qValues = {}
    for action in ACTIIONS:
      qValues[action] = learner.qValue(encoding, action)

    fetcher.inputData["qValues"] = qValues
    fetcher.inputData["bestAction"] = learner.bestAction(encoding)

    plotter.update(sensor, encoding, steer, reward, value, qValues)

    # if fetcher.timestep % plotEvery == 0:
    #   plotter.render()

    lastState = encoding
    lastAction = steer
Example #2
0
 def setUp(self):
     self.encoder = OneDDepthEncoder(name="one_d_depth",
                                     positions=range(36),
                                     radius=3,
                                     wrapAround=False,
                                     nPerPosition=57,
                                     wPerPosition=3,
                                     minVal=0,
                                     maxVal=1)
class OneDDepthEncoderTest(unittest.TestCase):
  """Unit tests for OneDDepthEncoder class"""

  def setUp(self):
    self.encoder = OneDDepthEncoder(name="one_d_depth",
                                    positions=range(36),
                                    radius=3,
                                    wrapAround=False,
                                    nPerPosition=57,
                                    wPerPosition=3,
                                    minVal=0,
                                    maxVal=1)


  def testEncodeUniform(self):
    inputData = numpy.array([0.5] * len(self.encoder.positions))
    outputData = self.encoder.encode(inputData)
    self._printInputOutput(inputData, outputData)


  def testEncodeSingleSpikeCenter(self):
    inputData = numpy.array([0.0] * len(self.encoder.positions))
    inputData[int(len(self.encoder.positions) / 2)] = 0.75
    outputData = self.encoder.encode(inputData)
    self._printInputOutput(inputData, outputData)


  def testEncodeSingleSpikeLeft(self):
    inputData = numpy.array([0.0] * len(self.encoder.positions))
    inputData[0] = 0.75
    outputData = self.encoder.encode(inputData)
    self._printInputOutput(inputData, outputData)


  def testEncodeSingleSpikeLeftWrapAround(self):
    self.encoder.wrapAround = True
    inputData = numpy.array([0.0] * len(self.encoder.positions))
    inputData[0] = 0.75
    outputData = self.encoder.encode(inputData)
    self._printInputOutput(inputData, outputData)


  def _printInputOutput(self, inputData, outputData):
    print self.id()
    print "=================================="
    print "Input data:"
    print inputData.tolist()
    print
    print "Output data:"
    self._printOutputData(outputData)
    print


  def _printOutputData(self, outputData):
    shape = len(self.encoder.positions), self.encoder.scalarEncoder.getWidth()
    for i in outputData.reshape(shape).tolist():
      print i
Example #4
0
    def __init__(self, position):
        self.encoder = OneDDepthEncoder(positions=positions,
                                        radius=5,
                                        wrapAround=True,
                                        nPerPosition=28,
                                        wPerPosition=3,
                                        minVal=0,
                                        maxVal=1)
        self.plotter = Plotter(self.encoder)
        self.learner = QLearner(ACTIIONS, n=1008)

        self.lastState = None
        self.lastAction = None
Example #5
0
class OneDDepthEncoderTest(unittest.TestCase):
    """Unit tests for OneDDepthEncoder class"""
    def setUp(self):
        self.encoder = OneDDepthEncoder(name="one_d_depth",
                                        positions=range(36),
                                        radius=3,
                                        wrapAround=False,
                                        nPerPosition=57,
                                        wPerPosition=3,
                                        minVal=0,
                                        maxVal=1)

    def testEncodeUniform(self):
        inputData = numpy.array([0.5] * len(self.encoder.positions))
        outputData = self.encoder.encode(inputData)
        self._printInputOutput(inputData, outputData)

    def testEncodeSingleSpikeCenter(self):
        inputData = numpy.array([0.0] * len(self.encoder.positions))
        inputData[int(len(self.encoder.positions) / 2)] = 0.75
        outputData = self.encoder.encode(inputData)
        self._printInputOutput(inputData, outputData)

    def testEncodeSingleSpikeLeft(self):
        inputData = numpy.array([0.0] * len(self.encoder.positions))
        inputData[0] = 0.75
        outputData = self.encoder.encode(inputData)
        self._printInputOutput(inputData, outputData)

    def testEncodeSingleSpikeLeftWrapAround(self):
        self.encoder.wrapAround = True
        inputData = numpy.array([0.0] * len(self.encoder.positions))
        inputData[0] = 0.75
        outputData = self.encoder.encode(inputData)
        self._printInputOutput(inputData, outputData)

    def _printInputOutput(self, inputData, outputData):
        print self.id()
        print "=================================="
        print "Input data:"
        print inputData.tolist()
        print
        print "Output data:"
        self._printOutputData(outputData)
        print

    def _printOutputData(self, outputData):
        shape = len(
            self.encoder.positions), self.encoder.scalarEncoder.getWidth()
        for i in outputData.reshape(shape).tolist():
            print i
def prepareDepthData(inputDir, encoder_params):
	depthDir = os.path.join(os.path.dirname(__file__), inputDir)
	SDRs = []

	encoder = OneDDepthEncoder(**encoder_params)

	for f in os.listdir(depthDir):
		if f.endswith(".txt"):
			print "Loading ", f
			dataPath = os.path.join(depthDir, f)
			depthData = np.loadtxt(dataPath)
			SDR = encoder.encode(depthData)
			SDRs.append(SDR)

	return SDRs
Example #7
0
def prepareDepthData(inputDir, encoder_params):
    depthDir = os.path.join(os.path.dirname(__file__), inputDir)
    SDRs = []

    encoder = OneDDepthEncoder(**encoder_params)

    for f in os.listdir(depthDir):
        if f.endswith(".txt"):
            print "Loading ", f
            dataPath = os.path.join(depthDir, f)
            depthData = np.loadtxt(dataPath)
            SDR = encoder.encode(depthData)
            SDRs.append(SDR)

    return SDRs
 def setUp(self):
   self.encoder = OneDDepthEncoder(name="one_d_depth",
                                   positions=range(36),
                                   radius=3,
                                   wrapAround=False,
                                   nPerPosition=57,
                                   wPerPosition=3,
                                   minVal=0,
                                   maxVal=1)
Example #9
0
class Agent(object):

  def __init__(self, position):
    self.encoder = OneDDepthEncoder(positions=positions,
                                    radius=5,
                                    wrapAround=True,
                                    nPerPosition=28,
                                    wPerPosition=3,
                                    minVal=0,
                                    maxVal=1)
    self.plotter = Plotter(self.encoder)
    self.learner = QLearner(ACTIIONS, n=1008)

    self.lastState = None
    self.lastAction = None


  def sync(self, outputData):
    if not ("ForwardsSweepSensor" in outputData and
            "steer" in outputData):
      print "Warning: Missing data:", outputData
      return

    if outputData.get("reset"):
      print "Reset."

    sensor = outputData["ForwardsSweepSensor"]
    steer = outputData["steer"]
    reward = outputData.get("reward") or 0

    encoding = self.encoder.encode(numpy.array(sensor))

    if self.lastState is not None:
      self.learner.update(self.lastState, str(self.lastAction),
                          encoding, str(steer), reward)

    value = self.learner.value(encoding)

    qValues = {}
    for action in ACTIIONS:
      qValues[action] = self.learner.qValue(encoding, action)

    inputData = {}
    inputData["qValues"] = qValues
    inputData["bestAction"] = self.learner.bestAction(encoding)

    self.plotter.update(sensor, encoding, steer, reward, value, qValues)

    if outputData.get("reset"):
      self.plotter.render()

    self.lastState = encoding
    self.lastAction = steer

    return inputData
Example #10
0
class Agent(object):
    def __init__(self, position):
        self.encoder = OneDDepthEncoder(positions=positions,
                                        radius=5,
                                        wrapAround=True,
                                        nPerPosition=28,
                                        wPerPosition=3,
                                        minVal=0,
                                        maxVal=1)
        self.plotter = Plotter(self.encoder)
        self.learner = QLearner(ACTIIONS, n=1008)

        self.lastState = None
        self.lastAction = None

    def sync(self, outputData):
        if not ("ForwardsSweepSensor" in outputData and "steer" in outputData):
            print "Warning: Missing data:", outputData
            return

        if outputData.get("reset"):
            print "Reset."

        sensor = outputData["ForwardsSweepSensor"]
        steer = outputData["steer"]
        reward = outputData.get("reward") or 0

        encoding = self.encoder.encode(numpy.array(sensor))

        if self.lastState is not None:
            self.learner.update(self.lastState, str(self.lastAction), encoding,
                                str(steer), reward)

        value = self.learner.value(encoding)

        qValues = {}
        for action in ACTIIONS:
            qValues[action] = self.learner.qValue(encoding, action)

        inputData = {}
        inputData["qValues"] = qValues
        inputData["bestAction"] = self.learner.bestAction(encoding)

        self.plotter.update(sensor, encoding, steer, reward, value, qValues)

        if outputData.get("reset"):
            self.plotter.render()

        self.lastState = encoding
        self.lastAction = steer

        return inputData
Example #11
0
  def __init__(self, position):
    self.encoder = OneDDepthEncoder(positions=positions,
                                    radius=5,
                                    wrapAround=True,
                                    nPerPosition=28,
                                    wPerPosition=3,
                                    minVal=0,
                                    maxVal=1)
    self.plotter = Plotter(self.encoder)
    self.learner = QLearner(ACTIIONS, n=1008)

    self.lastState = None
    self.lastAction = None