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


Python Simulator.insert_animal方法代码示例

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


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

示例1: main

# 需要导入模块: from simulator import Simulator [as 别名]
# 或者: from simulator.Simulator import insert_animal [as 别名]
def main():
    if len(sys.argv) < 2:
        genomes = [ random_genome(INIT_GENOME_LEN) for _ in range(INIT_GENOME_POOL) ]
        sim = Simulator(WIDTH, HEIGHT, ANIMAL_COUNT, PREDATOR_COUNT, genomes)
    else:
        sim = Simulator(WIDTH, HEIGHT, 0, PREDATOR_COUNT, [])
        with open(sys.argv[1]) as f:
            for line in f:
                sim.insert_animal(line.strip())

    next_output = 0
    time = 0
    deltatime = TIME_TICK

    while True:
        # output to file
        next_output -= deltatime
        if next_output <= 0:
            next_output = OUTPUT_PERIOD
            with open(OUTPUT_PREFIX + "{:.2f}.txt".format(time), 'w') as f:
                f.writelines(a.brain.genome + "\n" for a in sim.animals)
        # update state
        time += deltatime
        sim.update(deltatime)
        # debug
        if DEBUG_TIME:
            mini = min(a.energy for a in sim.animals)
            maxi = max(a.energy for a in sim.animals)
            print("{} ({:.2f} -- {:.2f}) alive at {:.2f}".format(len(sim.animals), mini, maxi, time))
开发者ID:vberger,项目名称:evolving-project,代码行数:31,代码来源:evolving-cli.py

示例2: main

# 需要导入模块: from simulator import Simulator [as 别名]
# 或者: from simulator.Simulator import insert_animal [as 别名]
def main():
    # init phase

    pygame.init()
    s = pygame.display.set_mode((WIDTH, HEIGHT))
    clock = pygame.time.Clock()
    s.fill((255,255,255))
    
    if len(sys.argv) < 2:
        genomes = [ random_genome(INIT_GENOME_LEN) for _ in range(INIT_GENOME_POOL) ]
        sim = Simulator(WIDTH, HEIGHT, ANIMAL_COUNT, PREDATOR_COUNT, genomes)
    else:
        sim = Simulator(WIDTH, HEIGHT, 0, PREDATOR_COUNT, [])
        with open(sys.argv[1]) as f:
            for line in f:
                sim.insert_animal(line.strip())

    while True:
        deltatime = float(clock.tick(50)) / 1000
        deltatime = TIME_TICK
        s.fill((255, 255, 255))
        for e in pygame.event.get():
            if e.type == QUIT:
                sys.exit(0)

        sim.update(deltatime)

        # display the pheromones
        for p in sim.pheromones:
            i = min(p.power(), 1.0)
            (r,g,b) = PHEROMONES_COLORS[p.sid]
            col = (int(255*(1.0-i) + r*i),int(255*(1.0-i) + g*i),int(255*(1.0-i) + b*i), 255)
            pygame.draw.circle(s, col, (int(p.x), int(p.y)), int(p.radius), 1)

        # display the objects
        for f in sim.objects:
            i = max(min(f.amount, 10),0)/10.0
            (r,g,b) = PHEROMONES_COLORS[f.kind]
            col = (int(255*(1.0-i) + r*i),int(255*(1.0-i) + g*i),int(255*(1.0-i) + b*i), 255)
            pygame.draw.circle(s, col, (f.x, f.y), 5)
            pygame.draw.circle(s, (0,0,0), (f.x, f.y), 5, 1)

        # display the animals
        for a in sim.animals:
            # choose a color:
            col = pygame.Color(int((100-max(0,a.energy))*255/100), int(max(0,a.energy)*255/100), 0, 255)
            # draw a head
            pygame.draw.circle(s, col, (int(a.x + math.cos(a.theta)*7), int(a.y + math.sin(a.theta)*7)), 5)
            pygame.draw.circle(s, (0,0,0), (int(a.x + math.cos(a.theta)*7), int(a.y + math.sin(a.theta)*7)), 5, 1)
            # draw a body
            pygame.draw.circle(s, col, (int(a.x), int(a.y)), 8)
            pygame.draw.circle(s, (0,0,0), (int(a.x), int(a.y)), 8, 1)
        # display the predators
        for p in sim.predators:
            # choose a color:
            col = pygame.Color(0,0,0,0)
            pygame.draw.circle(s, col, (int(p.x), int(p.y)), 12)
            pygame.draw.circle(s, (0,0,0), (int(p.x), int(p.y)), 12, 1)
        pygame.display.update()
开发者ID:vberger,项目名称:evolving-project,代码行数:61,代码来源:evolving.py


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