コード例 #1
0
 def update(self, pos, angle, r=0.1):
     """Udpate casting ray."""
     for k in range(self.nir):
         v = vrotate((1, 0), angle + self.IRAngles[k])
         c = pos + [0.9 * r * v[0], 0.9 * r * v[1]]
         cdist = pos + [self.maxdist * v[0], self.maxdist * v[1]]
         self.callback.fixture = None
         world.RayCast(self.callback, c, cdist)
         if(self.callback.fixture is not None):
             self.IRValues[k] = dist(c, self.callback.point) / self.maxdist
         else:
             self.IRValues[k] = 1
コード例 #2
0
    def update(self, pos, angle, centers=[], extremes=0):
        """Update passing agnet pos, angle and list of positions of gradient emmiters."""
        maxd = 6.5
        sensors = range(self.ngrad)
        if(extremes):
            sensors = [0, self.ngrad - 1]

        for k in sensors:
            v = vrotate((1, 0), angle + self.GradAngles[k])
            vals = [0, 0]
            for i, c in enumerate(centers):
                vc = (c[0] - pos[0], c[1] - pos[1])
                d = dist(pos, c)
                if(d > maxd):
                    d = maxd
                vals[i] = ((maxd - d) / maxd) * (1 - abs(vangle(v, vc)) / np.pi)
            self.GradValues[k] = 1 - max(vals)
コード例 #3
0
 def updateHaptic(self):
     if(len(self.haptic) < len(self.salient)): self.haptic = [0]*len(self.salient)
     maxd = 0.5 
     dt = 1.3
     for i,s in enumerate(self.salient):
         sbox = (s[0],s[1])            
         mind = maxd
         if(self.haptic[i] > 0): self.haptic[i] /= dt
         else: self.haptic[i] = 0    
         for c in self.obj.contacts:
             cpos = c.contact.worldManifold.points[0]
             if(vnorm(cpos)<0.01): continue
             d = dist(cpos,sbox) 
             if(d < maxd and d < mind):
                 mind = d
                 h = 1 - d/maxd
                 self.haptic[i] = h
コード例 #4
0
 def update(self, pos, angle, r=0.1):
     """Udpate casting ray."""
     for k in range(self.retinaSize):
         v = vrotate((1, 0), angle + self.VSAngles[k])
         c = pos + [0.9 * r * v[0], 0.9 * r * v[1]]
         cdist = pos + [self.maxdist * v[0], self.maxdist * v[1]]
         self.callback.fixture = None
         world.RayCast(self.callback, c, cdist)
         if (self.callback.fixture is not None):
             if 'RGB' in self.callback.fixture.body.userData.keys():
                 self.RGB[k] = [
                     int(col -
                         100 * dist(c, self.callback.point) / self.maxdist)
                     for col in self.callback.fixture.body.userData['RGB']
                 ]
             else:
                 self.RGB[k] = [255, 255, 255]
         else:
             self.RGB[k] = [0, 0, 0]
コード例 #5
0
 def update(self, pos, angle, r=0.1):
     """Udpate casting ray."""
     for k in range(self.nir):
         v = vrotate((1, 0), angle + self.IRAngles[k])
         c = pos + [0.9 * r * v[0], 0.9 * r * v[1]]
         cdist = pos + [self.maxdist * v[0], self.maxdist * v[1]]
         self.callback.fixture = None
         world.RayCast(self.callback, c, cdist)
         if (self.callback.fixture is not None):
             if 'ignore' in self.callback.fixture.body.userData.keys():
                 self.IRValues[k] = 1
             elif any([
                     ig in self.callback.fixture.body.userData['name']
                     for ig in self.ignoreList
             ]):
                 self.IRValues[k] = 1
             else:
                 self.IRValues[k] = dist(c,
                                         self.callback.point) / self.maxdist
         else:
             self.IRValues[k] = 1
コード例 #6
0
 def updateHaptic(self):
     if (len(self.haptic) < len(self.salient)):
         self.haptic = [0] * len(self.salient)
     maxd = 0.5
     dt = 1.3
     for i, s in enumerate(self.salient):
         sbox = (s[0], s[1])
         mind = maxd
         if (self.haptic[i] > 0):
             self.haptic[i] /= dt
         else:
             self.haptic[i] = 0
         for c in self.obj.contacts:
             cpos = c.contact.worldManifold.points[0]
             if (vnorm(cpos) < 0.01):
                 continue
             d = dist(cpos, sbox)
             if (d < maxd and d < mind):
                 mind = d
                 h = 1 - d / maxd
                 self.haptic[i] = h
コード例 #7
0
 def getLinkDistance(self, i):
     return dist(self.getSalient()[i], self.getLinkExtreme(i))
コード例 #8
0
    def checkPositions(self):
        """Get the positions of both epucks and rewards"""
        self.player1_pos = self.epucks[0].getPosition()
        self.player2_pos = self.epucks[1].getPosition()
        """Calculate distance between both epucks"""
        #epuck_dist = dist(self.player1_pos, self.player2_pos)
        #epuck_dist = self.constrain(epuck_dist, 0, 2)
        # print "epuck distance:", epuck_dist

        #for e in self.epucks:
        #    e.avoid_epuck_w = 2.5 - epuck_dist

        # convert positions and angles
        #self.p1_absposx = round(self.player1_pos[0], 3)
        #self.p1_absposy = round(self.player1_pos[1], 3)
        self.p1_absposx = ((self.player1_pos[0] + 4) * (540 - 180) / 8) + 180
        self.p1_absposy = ((self.player1_pos[1] - 2.7) *
                           (240 - 360) / 2.5) + 240
        self.p1_absposx = round(self.p1_absposx, 3)
        self.p1_absposy = round(self.p1_absposy, 3)

        #self.p2_absposx = round(self.player2_pos[0], 3)
        #self.p2_absposy = round(self.player2_pos[1], 3)
        self.p2_absposx = ((self.player2_pos[0] - 4) * (540 - 180) / 8) + 540
        self.p2_absposy = ((self.player2_pos[1] - 2.7) *
                           (240 - 360) / 2.5) + 240
        self.p2_absposx = round(self.p2_absposx, 3)
        self.p2_absposy = round(self.p2_absposy, 3)

        self.player1_ang = int(np.rad2deg(self.epucks[0].getAngle())) + 90
        self.player2_ang = int(np.rad2deg(self.epucks[1].getAngle())) + 90
        self.player1_ang = self.player1_ang % 360
        self.player2_ang = self.player2_ang % 360
        # print "Player 1 angle: ", self.player1_ang
        # print "Player 2 angle: ", self.player2_ang

        hreward_pos = self.objs[0].position
        lreward_pos = self.objs[1].position
        # print "High and Low Reward pos", hreward_pos, lreward_pos
        # hreward_abs_posx = (hreward_pos[0] * (540-360)/4) + 360
        # hreward_abs_posy = (round(hreward_pos[1]-5.2, 3) * (120-360)/5) + 120
        # lreward_abs_posx = (lreward_pos[0] * (540-360)/4) + 360
        # lreward_abs_posy = (round(lreward_pos[1]-0.2, 3) * (120-360)/5) + 360
        # print "High Reward positions", hreward_abs_posx, hreward_abs_posy
        # print "Low Reward positions", lreward_abs_posx, lreward_abs_posy
        """Calculate distance between both epucks and rewards"""
        p1hr_dist = dist(self.player1_pos, hreward_pos)
        p1lr_dist = dist(self.player1_pos, lreward_pos)
        p2hr_dist = dist(self.player2_pos, hreward_pos)
        p2lr_dist = dist(self.player2_pos, lreward_pos)

        if (p1hr_dist <
                config_data['reward_area']):  # REWARD AT 0.5, CIRCLE AT 1.25
            if (p2hr_dist < 1.5):
                print "IT'S A TIE !! Both players get 0 points"
                self.ties_n += 1
                if (self.contextual is True):
                    self.epucks[0].update_weights(2,
                                                  0)  #STATE 2:Tie ; REWARD: 0
                #self.epucks[0].contextual_layer.update(2, 0, self.epucks[0].action)
                #self.epucks[0].state = 2
                if (self.contextual is True):
                    self.epucks[1].update_weights(2,
                                                  0)  #STATE 2:Tie ; REWARD: 0
                #self.epucks[1].contextual_layer.update(2, 0, self.epucks[1].action)
                #self.epucks[1].state = 2
                self.savedata()
                self.restart()
            else:
                print "Player 1 obtained the HIGH Reward !!"
                self.player1_wins += 1
                if (self.payoff_structure == "high"):
                    self.player1_score += 4
                    if (self.contextual is True):
                        self.epucks[0].update_weights(
                            1, 4)  #STATE 1:High ; REWARD: 4
                    #self.epucks[0].contextual_layer.update(1, 4, self.epucks[0].action)
                    #self.epucks[0].state = 1
                if (self.payoff_structure == "low"):
                    self.player1_score += 2
                    if (self.contextual is True):
                        self.epucks[0].update_weights(
                            1, 2)  #STATE 1:High ; REWARD: 2
                    #self.epucks[0].contextual_layer.update(1, 2, self.epucks[0].action)
                    #self.epucks[0].state = 1
                self.player2_score += 1
                if (self.contextual is True):
                    self.epucks[1].update_weights(0,
                                                  1)  #STATE 0:Low ; REWARD: 1
                #self.epucks[1].contextual_layer.update(0, 1, self.epucks[1].action)
                #self.epucks[1].state = 0
                self.savedata()
                self.restart()

        elif (p2hr_dist < config_data['reward_area']):
            if (p1hr_dist < 1.5):
                print "IT'S A TIE !! Both players get 0 points"
                self.ties_n += 1
                if (self.contextual is True):
                    self.epucks[0].update_weights(2,
                                                  0)  #STATE 2:Tie ; REWARD: 0
                #self.epucks[0].contextual_layer.update(2, 0, self.epucks[0].action)
                #self.epucks[0].state = 2
                if (self.contextual is True):
                    self.epucks[1].update_weights(2,
                                                  0)  #STATE 2:Tie ; REWARD: 0
                #self.epucks[1].contextual_layer.update(2, 0, self.epucks[1].action)
                #self.epucks[1].state = 2
                self.savedata()
                self.restart()
            else:
                print "Player 2 obtained the HIGH Reward !!"
                self.player2_wins += 1
                if (self.payoff_structure == "high"):
                    self.player2_score += 4
                    if (self.contextual is True):
                        self.epucks[1].update_weights(
                            1, 4)  #STATE 1:High ; REWARD: 4
                    #self.epucks[1].contextual_layer.update(1, 4, self.epucks[1].action)
                    #self.epucks[1].state = 1
                if (self.payoff_structure == "low"):
                    self.player2_score += 2
                    if (self.contextual is True):
                        self.epucks[1].update_weights(
                            1, 2)  #STATE 1:High ; REWARD: 2
                    #self.epucks[1].contextual_layer.update(1, 2, self.epucks[1].action)
                    #self.epucks[1].state = 1
                self.player1_score += 1
                if (self.contextual is True):
                    self.epucks[0].update_weights(0,
                                                  1)  #STATE 0:Low ; REWARD: 1
                #self.epucks[0].contextual_layer.update(0, 1, self.epucks[0].action)
                #self.epucks[0].state = 0
                self.savedata()
                self.restart()

        if (p1lr_dist < config_data['reward_area']):
            if (p2lr_dist < 1.5):
                print "IT'S A TIE !! Both players get 0 points"
                if (self.contextual is True):
                    self.epucks[0].update_weights(2,
                                                  0)  #STATE 2:Tie ; REWARD: 0
                #self.epucks[0].contextual_layer.update(2, 0, self.epucks[0].action)
                #self.epucks[0].state = 2
                if (self.contextual is True):
                    self.epucks[1].update_weights(2,
                                                  0)  #STATE 2:Tie ; REWARD: 0
                #self.epucks[1].contextual_layer.update(2, 0, self.epucks[1].action)
                #self.epucks[1].state = 2
                self.ties_n += 1
                self.savedata()
                self.restart()
            else:
                print "Player 1 obtained the LOW Reward !!"
                self.player2_wins += 1
                self.player1_score += 1
                if (self.contextual is True):
                    self.epucks[0].update_weights(0,
                                                  1)  #STATE 0:Low ; REWARD: 1
                #self.epucks[0].contextual_layer.update(0, 1, self.epucks[0].action)
                #self.epucks[0].state = 0
                if (self.payoff_structure == "high"):
                    self.player2_score += 4
                    if (self.contextual is True):
                        self.epucks[1].update_weights(
                            1, 4)  #STATE 1:High ; REWARD: 4
                    #self.epucks[1].contextual_layer.update(1, 4, self.epucks[1].action)
                    #self.epucks[1].state = 1
                if (self.payoff_structure == "low"):
                    self.player2_score += 2
                    if (self.contextual is True):
                        self.epucks[1].update_weights(
                            1, 2)  #STATE 1:High ; REWARD: 2
                    #self.epucks[1].contextual_layer.update(1, 2, self.epucks[1].action)
                    #self.epucks[1].state = 1
                self.savedata()
                self.restart()

        elif (p2lr_dist < config_data['reward_area']):
            if (p1lr_dist < 1.5):
                print "IT'S A TIE !! Both players get 0 points"
                if (self.contextual is True):
                    self.epucks[0].update_weights(2,
                                                  0)  #STATE 2:Tie ; REWARD: 0
                #self.epucks[0].contextual_layer.update(2, 0, self.epucks[0].action)
                #self.epucks[0].state = 2
                if (self.contextual is True):
                    self.epucks[1].update_weights(2,
                                                  0)  #STATE 2:Tie ; REWARD: 0
                #self.epucks[1].contextual_layer.update(2, 0, self.epucks[1].action)
                #self.epucks[1].state = 2
                self.ties_n += 1
                self.savedata()
                self.restart()
            else:
                print "Player 2 obtained the LOW Reward !!"
                self.player1_wins += 1
                self.player2_score += 1
                if (self.contextual is True):
                    self.epucks[1].update_weights(0,
                                                  1)  #STATE 0:Low ; REWARD: 1
                #self.epucks[1].contextual_layer.update(0, 1, self.epucks[1].action)
                #self.epucks[1].state = 0
                if (self.payoff_structure == "high"):
                    self.player1_score += 4
                    if (self.contextual is True):
                        self.epucks[0].update_weights(
                            1, 4)  #STATE 1:High ; REWARD: 4
                    #self.epucks[0].contextual_layer.update(1, 4, self.epucks[0].action)
                    #self.epucks[0].state = 1
                if (self.payoff_structure == "low"):
                    self.player1_score += 2
                    if (self.contextual is True):
                        self.epucks[0].update_weights(
                            1, 2)  #STATE 1:High ; REWARD: 2
                    #self.epucks[0].contextual_layer.update(1, 2, self.epucks[0].action)
                    #self.epucks[0].state = 1
                self.savedata()
                self.restart()
コード例 #9
0
 def getLinkDistance(self,i):
     return dist(self.getSalient()[i],self.getLinkExtreme(i))