当前位置: 首页>>代码示例>>Python>>正文


Python Brain.update方法代码示例

本文整理汇总了Python中brain.Brain.update方法的典型用法代码示例。如果您正苦于以下问题:Python Brain.update方法的具体用法?Python Brain.update怎么用?Python Brain.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在brain.Brain的用法示例。


在下文中一共展示了Brain.update方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Creature

# 需要导入模块: from brain import Brain [as 别名]
# 或者: from brain.Brain import update [as 别名]
class Creature(object):
    x = y = 0.0
    facing = 0.0 #In radians, 0 is facing east
    velocity = 0.0
    angularVelocity = 0.0 #positive is turning anticlockwise
    brain = None
    mouthPos = (0,0)
    age = 0
    EAT_RATE = 1
    MAX_ENERGY = 200
    energy = MAX_ENERGY
    ENERGY_USE_RATE = 0.07
    MAX_BOOST_FORCE = 5.0
    MAX_TURN_FORCE = 0.1
    MAX_ANTENNA_ANGLE = 90.0
    MAX_ANTENNA_LENGTH = 200

    ANTENNA_ANGLE = 40 #Degrees
    ANTENNA_LENGTH = 60

    def __init__(self, DNA=None, saveState=None):
        self.x = self.y = 0.0
        self.facing = 0.0 #In radians, 0 is facing east
        self.velocity = 0.0
        self.angularVelocity = 0.0 #positive is turning anticlockwise
        self.mouthPos = (0,0)
        self.energy = self.MAX_ENERGY

        self.pulser = Pulser()
        
        self.fAntenna = Antenna(self,0,10)
        self.lAntenna = Antenna(self,-5,7)
        self.rAntenna = Antenna(self,5,7)
        self.antennae = []
        
        self.lEye = Eye(self,10,8,500)
        self.rEye = Eye(self,-10,8,500)
        self.eyes = [self.lEye, self.rEye]

        self.fBooster = Booster(self)
        self.lTurn = AngularBooster(self, 0.05)
        self.rTurn = AngularBooster(self, -0.05)

        self.brain = Brain([self.pulser]+self.antennae+self.eyes,
                           5,
                           [self.lTurn, self.fBooster, self.rTurn])

        if saveState!=None:
            self.loadFromString(saveState)
        elif DNA!=None:
            self.loadFromDNA(DNA)

    def update(self):
        self.age += 1
        self.energy -= self.ENERGY_USE_RATE
        if self.energy < 0:
            self.energy = 0
            self.die()
            return 0
        
        self.brain.update()
        self.brain.update()
        
        cos = math.cos(self.facing)
        sin = math.sin(self.facing)

        dx = self.velocity * cos
        dy = - self.velocity * sin

        self.x += dx
        self.y += dy

        self.facing += self.angularVelocity
        if self.facing > math.pi:
            self.facing -= math.pi*2
        elif self.facing < -math.pi:
            self.facing += math.pi*2
        
        self.mouthPos = (self.x + 20*cos, self.y - 20*sin)

        l = self.ANTENNA_LENGTH
        a = math.radians(self.ANTENNA_ANGLE)
        
        self.fAntenna.x = self.x + l*cos
        self.fAntenna.y = self.y - l*sin

        cos2 = math.cos(self.facing+a)
        sin2 = math.sin(self.facing+a)
        self.lAntenna.x = self.x + l*cos2
        self.lAntenna.y = self.y - l*sin2

        cos2 = math.cos(self.facing-a)
        sin2 = math.sin(self.facing-a)
        self.rAntenna.x = self.x + l*cos2
        self.rAntenna.y = self.y - l*sin2

        self.velocity -= SPEED_DECAY
        if self.velocity < 0:
            self.velocity = 0

#.........这里部分代码省略.........
开发者ID:DiscoViking,项目名称:PyBrains,代码行数:103,代码来源:creature.py

示例2: __init__

# 需要导入模块: from brain import Brain [as 别名]
# 或者: from brain.Brain import update [as 别名]
class Body:
    """This class represents the mouse body and its current behaviour in the control process"""

    def __init__(self, scene_, config_):
        """Class initialization"""

        self.n_iter = 0
        self.scene = scene_
        self.config = config_
        self.logger = config_.logger
        self.muscle_type = self.config.muscle_type + "(self.scene, muscle_config)"
        self.name = self.config.body["name"]

        # Get body object
        if not self.config.body["obj"] in self.scene.objects:
            self.logger.error("Body " + self.name + " doesn't exit. Check your configuration file!")
            self.active = False
        else:
            self.body_obj = self.scene.objects[self.config.body["obj"]]

        # Create and init variables for loss function
        self.origin = self.body_obj.worldTransform * vec((0, 0, 0))
        self.position = self.origin
        self.dist = vec(self.position - self.origin).length
        self.powers = []
        self.av_power = 0.0
        self.loss_fct = 0.0

        # Create 4 legs
        self.l_fo_leg = Foreleg(scene_, config_, "L")
        self.r_fo_leg = Foreleg(scene_, config_, "R")
        self.l_ba_leg = Backleg(scene_, config_, "L")
        self.r_ba_leg = Backleg(scene_, config_, "R")

        # Create the brain object
        self.brain = Brain(scene_, config_)

        # Create the muscles objects following config
        self.muscles = []
        for muscle_config in self.config.body["muscles"]:
            self.muscles.append(eval(self.muscle_type))

    def compute_traveled_dist(self):
        """Return a float representing the distance between origin and the current position"""

        # Get current position
        self.position = self.body_obj.worldTransform * vec((0, 0, 0))

        # Get distance
        self.dist = vec(self.position - self.origin).length

        return 

    def compute_power(self):
        """Compute time-step power at each iteration"""

        power = 0.0

        # Get power from legs
        power += self.l_ba_leg.get_power()
        power += self.r_ba_leg.get_power()
        power += self.l_fo_leg.get_power()
        power += self.r_fo_leg.get_power()

        # Get power from body muscles
        for m in self.muscles:
            power += m.get_power()

        # Append to powers list
        self.powers.append(power)

        return

    def get_loss_fct(self):
        """Compute the body loss function. This should be called only at the end of the simulation
        in order to avoid useless computation at each iteration"""

        self.compute_traveled_dist()
        self.av_power = sum(self.powers) / float(len(self.powers))

        self.loss_fct = math.tanh(self.dist / self.config.dist_ref) * \
            math.tanh(self.config.power_ref / self.av_power)

        return self.loss_fct

    def update(self):
        """Update control signals and forces"""

        # Update brain
        self.brain.update()
        ctrl_sig = [float(self.brain.state[0]), float(self.brain.state[1]), float(self.brain.state[2]),
                    float(self.brain.state[3])]

        # Update the four legs
        self.l_ba_leg.update(ctrl_sig)
        self.r_ba_leg.update(ctrl_sig)
        self.l_fo_leg.update(ctrl_sig)
        self.r_fo_leg.update(ctrl_sig)

        # Update other muscles
#.........这里部分代码省略.........
开发者ID:Gabs48,项目名称:mouse_locomotion,代码行数:103,代码来源:body.py

示例3: test_can_update_event

# 需要导入模块: from brain import Brain [as 别名]
# 或者: from brain.Brain import update [as 别名]
 def test_can_update_event(self):
     Brain.create(self.event)
     self.event.title = "Updated event"
     Brain.update(self.event)
     assert Brain.read("223-unique-id").title == "Updated event"
开发者ID:michaelmcmillan,项目名称:BedpresBro,代码行数:7,代码来源:brain_test.py

示例4: push_events_to_channels

# 需要导入模块: from brain import Brain [as 别名]
# 或者: from brain.Brain import update [as 别名]
 def push_events_to_channels(self):
     enrolling_events = self.whats_enrolling()
     for enrolling_event in enrolling_events:
         for channel in self.channels:
             channel.notify(enrolling_event)
             Brain.update(enrolling_event)
开发者ID:michaelmcmillan,项目名称:BedpresBro,代码行数:8,代码来源:bro.py


注:本文中的brain.Brain.update方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。