Ejemplo n.º 1
0
 def randomizePositions(self, seed=None):
     # seed = 0 (reinit), seed = None (random), seed = num (seed it)
     if seed == 0: # Reinitialize to something random:
         seed = random.random() * 100000 + time.time()
     if seed != None: # use a specific seed:
         random.seed(seed)
     # Make the robots far from these positions:
     positions = [(2, 2), (7, 7)] # position of lights
     for n in range(len(engines)):
         engine = engines[n]
         # Put each robot in a random location:
         x, y, t = (1 + random.random() * 7, 
                    1 + random.random() * 7, 
                    random.random() * math.pi * 2)
         minDistance = min([distance(x, y, x2, y2) for (x2,y2) in positions])
         # make sure they are far enough apart:
         while minDistance < 2: # in meters
             x, y, t = (1 + random.random() * 7, 
                        1 + random.random() * 7, 
                        random.random() * math.pi * 2)
             minDistance = min([distance(x, y, x2, y2) 
                                for (x2,y2) in positions])
         positions.append( (x,y) )
         engine.robot.simulation[0].setPose(n, x, y, t)
     sim.redraw()
Ejemplo n.º 2
0
 def randomizePositions(self):
     positions = [(100, 100)]
     for n in range(len(engines)):
         engine = engines[n]
         # Put each robot in a random location:
         x, y, t = 1 + random.random() * 7, 1 + random.random() * 7, random.random() * math.pi * 2
         minDistance = min([distance(x, y, x2, y2) for (x2,y2) in positions])
         while minDistance < 1:
             x, y, t = 1 + random.random() * 7, 1 + random.random() * 7, random.random() * math.pi * 2
             minDistance = min([distance(x, y, x2, y2) for (x2,y2) in positions])
         positions.append( (x,y) )
         engine.robot.simulation[0].setPose(n, x, y, t)
Ejemplo n.º 3
0
def quadSound(myLoc, lastS, location):
    """
    Computes the sound heard for all quads.
    myLoc:    (x, y, t) of current robot; t where 0 is up
    lastS:    last sound made by robots
    location: (x, y, t) of robots; t where 0 is up
    """
    if not canHear:
        return [0.5 for x in range(robotCount)]
    # dist, freq for each robot; 0.5 is what is silence
    closest = [(10000, 0.5), (10000, 0.5), (10000, 0.5), (10000, 0.5)]
    for n in range(len(location)):
        loc = location[n]
        if loc != myLoc:
            # distance between robots:
            dist = distance(myLoc[0], myLoc[1], loc[0], loc[1])
            # global angle from one robot to another:
            # 0 to right, neg down (geometry-style)
            angle = Polar(loc[0] - myLoc[0], loc[1] - myLoc[1], bIsPolar=0)
            angle = angle.t  # get theta
            if angle < 0:
                angle = math.pi + (math.pi + angle)  # 0 to 2pi
            angle = (angle - math.pi / 2) % (math.pi * 2)
            q = quadNum(myLoc[2], angle)
            #print n, myLoc[2], angle, q
            # if shorter than previous, and less than N meters
            if dist < closest[q][0] and dist < 1.0 / 2.7 * 7.0:
                closest[q] = dist, lastS[n]  # new closest
    return [v[1] for v in closest]  # return the sounds
Ejemplo n.º 4
0
def quadSound(myLoc, lastS, location):
    """
    Computes the sound heard for all quads.
    myLoc:    (x, y, t) of current robot; t where 0 is up
    lastS:    last sound made by robots
    location: (x, y, t) of robots; t where 0 is up
    """
    if not canHear:
        return [0.5 for x in range(robotCount)]
    # dist, freq for each robot; 0.5 is what is silence
    closest = [(10000,0.5), (10000,0.5), (10000,0.5), (10000,0.5)] 
    for n in range(len(location)):
        loc = location[n]
        if loc != myLoc:
            # distance between robots:
            dist = distance(myLoc[0], myLoc[1], loc[0], loc[1])
            # global angle from one robot to another:
            # 0 to right, neg down (geometry-style)
            angle = Polar(loc[0] - myLoc[0], loc[1] - myLoc[1], bIsPolar=0) 
            angle = angle.t # get theta
            if angle < 0:
                angle = math.pi + (math.pi + angle) # 0 to 2pi
            angle = (angle - math.pi/2) % (math.pi * 2)
            q = quadNum(myLoc[2], angle) 
            #print n, myLoc[2], angle, q
            # if shorter than previous, and less than N meters
            if dist < closest[q][0] and dist < 1.0/2.7 * 7.0: 
                closest[q] = dist, lastS[n] # new closest
    return [v[1] for v in closest] # return the sounds
Ejemplo n.º 5
0
 def randomizePositions(self):
     positions = [(2, 2), (7, 7)]  # position of lights
     for n in range(len(engines)):
         engine = engines[n]
         # Put each robot in a random location:
         x, y, t = (1 + random.random() * 7, 1 + random.random() * 7,
                    random.random() * math.pi * 2)
         minDistance = min(
             [distance(x, y, x2, y2) for (x2, y2) in positions])
         # make sure they are far enough apart:
         while minDistance < 1:
             x, y, t = (1 + random.random() * 7, 1 + random.random() * 7,
                        random.random() * math.pi * 2)
             minDistance = min(
                 [distance(x, y, x2, y2) for (x2, y2) in positions])
         positions.append((x, y))
         engine.robot.simulation[0].setPose(n, x, y, t)
     sim.redraw()
Ejemplo n.º 6
0
 def randomizePositions(self, seed=None):
     # seed = 0 (reinit), seed = None (random), seed = num (seed it)
     if seed == 0:  # Reinitialize to something random:
         seed = random.random() * 100000 + time.time()
     if seed != None:  # use a specific seed:
         random.seed(seed)
     # Make the robots far from these positions:
     positions = [(2, 2), (7, 7)]  # position of lights
     for n in range(len(engines)):
         engine = engines[n]
         # Put each robot in a random location:
         x, y, t = (1 + random.random() * 7, 1 + random.random() * 7,
                    random.random() * math.pi * 2)
         minDistance = min(
             [distance(x, y, x2, y2) for (x2, y2) in positions])
         # make sure they are far enough apart:
         while minDistance < 2:  # in meters
             x, y, t = (1 + random.random() * 7, 1 + random.random() * 7,
                        random.random() * math.pi * 2)
             minDistance = min(
                 [distance(x, y, x2, y2) for (x2, y2) in positions])
         positions.append((x, y))
         engine.robot.simulation[0].setPose(n, x, y, t)
     sim.redraw()
Ejemplo n.º 7
0
 def fitnessFunction(self, genePos, randomizeSeed=None):
     # seed = -1 (cont), seed = 0 (reinit), seed = None (random),
     # seed = num (seed it)
     if self.pre_init:  # initial generation fitness
         return 1.0
     fitness = 0.01
     print "-------------------------------"
     for count in range(numTrials):
         subfitness = 0.01
         if genePos >= 0:  # -1 is test of last one
             self.loadWeights(genePos)
         if randomizeSeed == -1:
             pass  # continue
         else:
             # seed = 0 (reinit), seed = None (random), seed = num (seed it)
             self.randomizePositions(randomizeSeed)
         sim.resetPaths()
         sim.redraw()
         s = [0] * robotCount  # each robot's sound
         lastS = [0] * robotCount  # previous sound
         location = [(0, 0, 0) for v in range(robotCount)]
         # Set the context values to zero:
         for n in range(robotCount):  # number of robots
             engine = engines[n]
             engine.brain.net.setContext(0.5)
             engine.brain.net["output"].setActivations(0.5)
             engine.brain.net["output"].resetActivationFlag()
         for i in range(self.seconds *
                        (1000 / sim.timeslice)):  # (10 per sec)
             # ------------------------------------------------
             # First, get the locations:
             # ------------------------------------------------
             for n in range(robotCount):  # number of robots
                 location[n] = engines[0].robot.simulation[0].getPose(n)
             # ------------------------------------------------
             # Next, compute the move for each robot
             # ------------------------------------------------
             for n in range(robotCount):  # number of robots
                 engine = engines[n]
                 engine.robot.update()
                 # compute quad for this robot
                 myLoc = location[n]
                 quad = quadSound(myLoc, lastS, location)
                 # print n, quad
                 # compute output for each robot
                 oTrans, oRotate, s[n] = engine.brain.propagate(quad)
                 # then set the move velocities:
                 engine.brain.step(oTrans, oRotate)
                 sim.robots[n].say(
                     "%.2f Heard: [%s]" %
                     (s[n], ",".join(map(lambda v: "%.2f" % v, quad))))
             # ------------------------------------------------
             # Save the sounds
             # ------------------------------------------------
             for n in range(robotCount):  # number of robots
                 lastS = [v for v in s]
             # ------------------------------------------------
             # Make the move:
             # ------------------------------------------------
             sim.step(run=0)
             # update tasks in GUI:
             if isinstance(sim, TkSimulator):
                 while sim.tk.dooneevent(2):
                     pass
             # Stop the robots from moving on other steps:
             for n in range(robotCount):  # number of robots
                 engine = engines[n]
                 engine.robot.stop()
             # play a sound, need to have a sound thread running
             for n in range(robotCount):  # number of robots
                 st = "%s robot audio" % colors[n]
                 if sim.display[st] and sd != None:
                     sd.playTone(
                         int(
                             round(
                                 engines[n].brain.net["output"].
                                 activation[-1], 1) * 2000) + 500,
                         .1)  # 500 - 2500
             # ------------------------------------------------
             # Compute fitness
             # ------------------------------------------------
             closeTo = [0, 0]  # number of lights
             # how many robots are close to which lights?
             for n in range(len(engines)):
                 engine = engines[n]
                 # get global coords
                 x, y, t = engine.robot.simulation[0].getPose(n)
                 # which light?
                 dists = [
                     distance(light.x, light.y, x, y)
                     for light in sim.lights
                 ]
                 if min(dists) <= 1.0:
                     if dists[0] < dists[1]:
                         closeTo[0] += 1
                     else:
                         closeTo[1] += 1
             # ------------------------------------------------
             # Finally, compute the fitness
             # ------------------------------------------------
             for total in closeTo:
                 subfitness += .25 * total
                 # only allow N per feeding area
                 if total > 2:
                     subfitness -= 1.0 * (total - 2)
                 subfitness = max(0.01, subfitness)
             #print "   ", closeTo, subfitness,
             #raw_input(" press [ENTER]")
         print "   subfitness: %d: %.5f" % (genePos, subfitness)
         fitness += subfitness
     print "Total Fitness %d: %.5f" % (genePos, fitness)
     return fitness
Ejemplo n.º 8
0
 def fitnessFunction(self, genePos):
     self.seconds = self.generation
     if genePos >= 0:
         self.loadWeights(genePos)
         self.randomizePositions()
     sim.resetPaths()
     sim.redraw()
     fitness = [0.0] * 4
     s = [0] * 4 # each robot's sound
     lastS = [0] * 4 # previous sound
     location = [(0, 0, 0) for v in range(4)] # each robot's location
     stallCount = 0
     for i in range(self.seconds * (1000/sim.timeslice)): # simulated seconds (10/sec)
         # get the locations
         for n in range(4): # number of robots
             location[n] = engines[0].robot.simulation[0].getPose(n)
         # compute the move for each robot
         for n in range(4): # number of robots
             engine = engines[n]
             engine.robot.update()
             # compute quad for this robot
             myLoc = location[n]
             quad = quadSound(myLoc, lastS, location)
             # compute output for each robot
             oTrans, oRotate, s[n] = engine.brain.propagate(quad)
             # then set the move velocities:
             engine.brain.step(oTrans, oRotate)
         # save the sounds
         for n in range(4): # number of robots
             lastS = [v for v in s]
         # make the move:
         sim.step(run=0)
         sim.update_idletasks()
         # play a sound, need to have a thread running
         if self.playSound:
             sd.playTone(int(round(engines[0].brain.net["output"].activation[-1], 1) * 2000) + 500, .1) # 500 - 2500
             # real time?
             time.sleep(.1)
         # compute fitness
         closeTo = [0, 0] # how many robots are close to which lights?
         for n in range(len(engines)):
             engine = engines[n]
             # only allow two per feeding area
             reading = max(engine.robot.light[0].values())
             if reading >= 1.0:
                 # get global coords
                 x, y, t = engine.robot.simulation[0].getPose(n)
                 # which light?
                 dists = [distance(light.x, light.y, x, y) for light in sim.lights]
                 if dists[0] < dists[1]:
                     closeTo[0] += 1
                 else:
                     closeTo[1] += 1
         for n in range(len(engines)):
             if engines[n].robot.stall: continue
             for total in closeTo:
                 if total <= 2:
                     fitness[n] += .25 * total
                 else:
                     fitness[n] -= 1.0
         # ==================================================
         # Check for all stalled:
         stalled = 0
         for n in range(4): # number of robots
             engine = engines[n]
             if engine.robot.stall: stalled += 1
         if stalled == 4:
             stallCount += 1
         else:
             stallCount = 0
         if stallCount == 10:
             break
     fit = reduce(operator.add, fitness)
     fit = max(0.01, fit)
     print "Fitness %d: %.5f" % (genePos, fit)
     return fit
Ejemplo n.º 9
0
    def fitnessFunction(self, genePos, randomizeSeed=None):
        # seed = -1 (cont), seed = 0 (reinit), seed = None (random), 
        # seed = num (seed it)
        if self.pre_init: # initial generation fitness
            return 1.0
        fitness = 0.01
	print "-------------------------------"
        for count in range(numTrials):
            subfitness = 0.01
            if genePos >= 0: # -1 is test of last one
                self.loadWeights(genePos)
            if randomizeSeed == -1:
                pass # continue
            else:
                # seed = 0 (reinit), seed = None (random), seed = num (seed it)
                self.randomizePositions(randomizeSeed)
            sim.resetPaths()
            sim.redraw()
            s = [0] * robotCount # each robot's sound
            lastS = [0] * robotCount # previous sound
            location = [(0, 0, 0) for v in range(robotCount)] 
            # Set the context values to zero:
            for n in range(robotCount): # number of robots
                engine = engines[n]
                engine.brain.net.setContext(0.5)
                engine.brain.net["output"].setActivations(0.5)
                engine.brain.net["output"].resetActivationFlag()
            for i in range(self.seconds * (1000/sim.timeslice)): # (10 per sec)
                # ------------------------------------------------
                # First, get the locations:
                # ------------------------------------------------
                for n in range(robotCount): # number of robots
                    location[n] = engines[0].robot.simulation[0].getPose(n)
                # ------------------------------------------------
                # Next, compute the move for each robot
                # ------------------------------------------------
                for n in range(robotCount): # number of robots
                    engine = engines[n]
                    engine.robot.update()
                    # compute quad for this robot
                    myLoc = location[n]
                    quad = quadSound(myLoc, lastS, location)
                    # print n, quad
                    # compute output for each robot
                    oTrans, oRotate, s[n] = engine.brain.propagate(quad)
                    # then set the move velocities:
                    engine.brain.step(oTrans, oRotate)
                    sim.robots[n].say("%.2f Heard: [%s]" % 
                                      (s[n], 
                                       ",".join(map(lambda v: "%.2f" % v, quad))))
                # ------------------------------------------------
                # Save the sounds
                # ------------------------------------------------
                for n in range(robotCount): # number of robots
                    lastS = [v for v in s]
                # ------------------------------------------------
                # Make the move:
                # ------------------------------------------------
                sim.step(run=0)
                # update tasks in GUI:
                if isinstance(sim, TkSimulator):
                    while sim.tk.dooneevent(2): pass
                # Stop the robots from moving on other steps:
                for n in range(robotCount): # number of robots
                    engine = engines[n]
                    engine.robot.stop()
                # play a sound, need to have a sound thread running
                for n in range(robotCount): # number of robots
                    st = "%s robot audio" % colors[n]
                    if sim.display[st] and sd != None:
                        sd.playTone(int(round(engines[n].brain.net["output"].activation[-1], 1) * 2000) + 500, .1) # 500 - 2500
                # ------------------------------------------------
                # Compute fitness
                # ------------------------------------------------
                closeTo = [0, 0] # number of lights
                # how many robots are close to which lights?
                for n in range(len(engines)):
                    engine = engines[n]
                    # get global coords
                    x, y, t = engine.robot.simulation[0].getPose(n)
                    # which light?
                    dists = [distance(light.x, light.y, x, y) 
                             for light in sim.lights]
                    if min(dists) <= 1.0:
                        if dists[0] < dists[1]:
                            closeTo[0] += 1
                        else:
                            closeTo[1] += 1
                # ------------------------------------------------
                # Finally, compute the fitness
                # ------------------------------------------------
                for total in closeTo:
                    subfitness += .25 * total
                    # only allow N per feeding area
                    if total > 2:
                        subfitness -= 1.0 * (total - 2)
                    subfitness = max(0.01, subfitness)
                #print "   ", closeTo, subfitness,
                #raw_input(" press [ENTER]")
            print "   subfitness: %d: %.5f" % (genePos,subfitness)
            fitness += subfitness
        print "Total Fitness %d: %.5f" % (genePos, fitness)
        return fitness
Ejemplo n.º 10
0
 def update(self):
     x = self.robot.x
     y = self.robot.y
     dist = distance( self.startX, self.startY, x, y) 
     if dist > 8.0:
         self.goto('A1')