UserPreferences

NNTestingProgram


  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 

# Load in saved weights from offline training
# Inputs are the two front sensor readings
# Output is a translate value, used to control the robot

from pyrobot.brain import Brain
from pyrobot.brain.conx import *
from time import *

class NNBrain(Brain):
   def setup(self):
      self.n = Network()
      self.n.addLayers(2,1,1)
      self.maxvalue = self.robot.range.getMaxvalue()
      self.doneLearning = 1
      self.n.loadWeightsFromFile("E05M01.wts")
      self.n.setLearning(0)
   def scale(self, val):
      x = val / self.maxvalue
      if x > 1:
         return 1.0
      else:
         return x
   def step(self):
      # Set inputs
      front = [s.distance() for s in self.robot.range["front"]]
      self.n['input'].copyActivations([self.scale(front[0]),
                                       self.scale(front[1])])
      self.n.propagate()
      translateActual = self.n['output'].activation[0]
      print "move", translateActual
      self.translate(translateActual)

def INIT(engine):
   return NNBrain('NNBrain', engine)