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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
|
import random
class Ant(object): """ 蚂蚁 """
UP = 0 LEFT = 1 DOWN = 2 RIGHT = 3
HOME_SMELL = 0 FOOD_SMELL = 1 MAX_SMELL = 15000 MAX_MEMORY = 100 VISION = 6 SMELL_LEAVE_RATE = 0.05
def __init__(self, map): """ :param map: 地图 :param dir: 方向 """ self.__map = map self.__map.add_ant(self) self.__dir = random.choice([Ant.UP, Ant.LEFT, Ant.DOWN, Ant.RIGHT]) self.__memory = [] self.__food = False self.x, self.y = self.__map.get_home_coor() self.__smell_home = Ant.MAX_SMELL self.__smell_food = 0 self.__speed = random.choice([1, 2, 3]) self.time = 0
def initial_home(self): self.__memory = [] self.__food = False self.__smell_home = Ant.MAX_SMELL self.__smell_food = 0 self.__dir = self.go_back()
def initial_food(self): self.__memory = [] self.__food = True self.__smell_home = 0 self.__smell_food = Ant.MAX_SMELL self.__dir = self.go_back()
def show_home_smell(self): """ 所剩的家信息素 :return: """ return self.__smell_home
def show_food_smell(self): """ 所剩的食物信息素 :return: """ return self.__smell_food
def move(self, dir): """ 移动到x, y :param x: :param y: :return: """ self.__dir = dir self.x, self.y = self.calcu_dire_coor(dir)
if len(self.__memory) >= self.MAX_MEMORY: self.__memory.pop(0) self.__memory.append((self.x, self.y))
def can_go_dires(self): """ 根据当前方向,计算出前,左,右,三个方向中可达的方向 :return: """ cango_dirs = [] dirs = None
assert self.__dir in [Ant.UP, Ant.DOWN, Ant.LEFT, Ant.RIGHT]
if self.__dir == Ant.UP: dirs = (Ant.UP, Ant.LEFT, Ant.RIGHT) elif self.__dir == Ant.LEFT: dirs = (Ant.LEFT, Ant.DOWN, Ant.UP) elif self.__dir == Ant.DOWN: dirs = (Ant.DOWN, Ant.RIGHT, Ant.LEFT) elif self.__dir == Ant.RIGHT: dirs = (Ant.RIGHT, Ant.UP, Ant.DOWN)
for dir in dirs: x, y = self.calcu_dire_coor(dir) if self.__map.can_go(x, y): cango_dirs.append(dir)
return cango_dirs
def calcu_vision(self, dir): """ 计算此方向的视野 :return: """ coors = [] if dir == Ant.UP: for i in range(self.x - Ant.VISION, self.x + Ant.VISION + 1): for j in range(self.y - Ant.VISION, self.y + 1): coors.append((i, j)) elif dir == Ant.LEFT: for i in range(self.x - Ant.VISION, self.x + 1): for j in range(self.y - Ant.VISION, self. y + 3 + 1): coors.append((i, j)) elif dir == Ant.DOWN: for i in range(self.x - Ant.VISION, self.x + Ant.VISION + 1): for j in range(self.y, self.y + Ant.VISION + 1): coors.append((i, j)) elif dir == Ant.RIGHT: for i in range(self.x, self.x + Ant.VISION + 1): for j in range(self.y - Ant.VISION, self.y + Ant.VISION + 1): coors.append((i, j))
return coors
def turn_left(self): """ 按照当前方向的逆时针转动 :return: """ if self.__dir == Ant.UP: return Ant.LEFT elif self.__dir == Ant.LEFT: return Ant.DOWN elif self.__dir == Ant.DOWN: return Ant.RIGHT else: return Ant.UP
def turn_right(self): """ 按照当前方向的顺时针转动 :return: """ if self.__dir == Ant.UP: return Ant.RIGHT elif self.__dir == Ant.RIGHT: return Ant.DOWN elif self.__dir == Ant.DOWN: return Ant.LEFT else: return Ant.UP
def go_back(self): """ 转向回头 :return: """ if self.__dir == Ant.UP: return Ant.DOWN elif self.__dir == Ant.DOWN: return Ant.UP elif self.__dir == Ant.LEFT: return Ant.RIGHT else: return Ant.LEFT
def has_food(self): return self.__food
def memory(self): """ 返回这只蚂蚁的记忆坐标 :return: """ return self.__memory
def calcu_dire_coor(self, dir): """ 计算如果按照此方向移动后,将会到达的坐标 :param dir: :return: """ if dir == Ant.UP: return self.x, self.y - 1 elif dir == Ant.DOWN: return self.x, self.y + 1 elif dir == Ant.LEFT: return self.x - 1, self.y else: return self.x + 1, self.y
def ramble(self, dirs): """ 漫游的选择一个方向 :return: """ if len(dirs) == 3: roll = random.randrange(1, 101) if 1 <= roll <= 90: return self.__dir elif 90 <= roll <= 95: return self.turn_left() else: return self.turn_right() elif len(dirs) == 2: if self.__dir in dirs: roll = random.randrange(1, 101) if 1 <= roll <= 90: return self.__dir else: for i in dirs: if i != self.__dir: return i else: roll = random.randrange(1, 101) if 1 <= roll <= 50: return dirs[0] else: return dirs[1] elif len(dirs) == 1: return dirs[0] else: return self.go_back()
def calcu_next_dire(self): """ 计算下一步移动方向 :return: """ cango_dirs = self.can_go_dires()
max_smell = {} for i in cango_dirs: max_smell[i] = 0
for dir in cango_dirs: visoins = self.calcu_vision(dir) if self.has_food(): smell_type = Ant.HOME_SMELL else: smell_type = Ant.FOOD_SMELL max_smell[dir] = self.__map.calcu_max_smell(visoins, self.memory(), smell_type)
will_dir = None max_s = 0 for dir, smell in max_smell.iteritems(): if smell > max_s: will_dir = dir max_s = smell
if will_dir is not None: return will_dir else: return self.ramble(cango_dirs)
def print_smell(self): """ 打印这只蚂蚁的信息素含量 :return: """
print "%s,家信息素%s, 食物信息素:%s" % (id(self) , self.__smell_home, self.__smell_food)
def one_step(self): """ 行动一步 :return: """ self.time += 1 if self.time < self.__speed: return self.time = 0
dire = self.calcu_next_dire() self.move(dire)
if self.has_food(): if self.__map.is_home(self.x, self.y): self.__map.put_food_to_home() self.initial_home() elif self.__map.is_food(self.x, self.y): self.__smell_food = Ant.MAX_SMELL else: if self.__map.is_food(self.x, self.y) and not self.__map.food_empty(): self.__map.take_a_food() self.initial_food() elif self.__map.is_home(self.x, self.y): self.__smell_home = Ant.MAX_SMELL
if self.has_food(): if self.__smell_food > 0 and not self.__map.is_food(self.x, self.y) and not self.__map.is_home(self.x, self.y): smell = self.__smell_food * Ant.SMELL_LEAVE_RATE self.__smell_food -= smell self.__map.leave_smell(self.x, self.y, Ant.FOOD_SMELL, smell) else: if self.__smell_home > 0 and not self.__map.is_home(self.x, self.y) and not self.__map.is_food(self.x, self.y): smell = self.__smell_home * Ant.SMELL_LEAVE_RATE self.__smell_home -= smell self.__map.leave_smell(self.x, self.y, Ant.HOME_SMELL, smell)
class Map(object): """ 地图 """ LAWN = 0 OBSTACLE = 1 HOME = 2 FOOD = 3 ANT = 4
SMELL_GONE_SPEED = 50 SMELL_GONE_RATE = 0.05
OBSTACLE_COUNT = 30
P_LAWN = " " P_OBSTACLE = "@" P_HOME = "#" P_FOOD = "$" P_ANT = "*"
def __init__(self, x, y, food_counts): """ :param x: :param y: """ self.__data = [] for i in range(x): temp = [] for j in range(y): temp.append(Map.LAWN) self.__data.append(temp)
self.__data[0][0] = Map.HOME self.__data[x-1][y-1] = Map.FOOD
self.__home_coor = (0, 0) self.__food_coor = (x-1, y-1)
for i in range(Map.OBSTACLE_COUNT): x1 = random.choice(range(x)) y1 = random.choice(range(y)) if self.is_home(x1, y1) or self.is_food(x1, y1): continue self.__data[x1][y1] = Map.OBSTACLE
self.__ants = [] self.__home_food = 0 self.__food_foods = food_counts
self.__smells = {} for x, i in enumerate(self.__data): for y, j in enumerate(i): if j == Map.LAWN: self.__smells[(x, y)] = {Ant.HOME_SMELL: 0, Ant.FOOD_SMELL: 0}
self.X = x self.Y = y
self.time = 0
def landform(self): return self.__data
def get_food_count(self): """ 获得地图上,家和食物所包含的食物个数 :return: """ return self.__home_food, self.__food_foods
def food_empty(self): return self.__food_foods <= 0
def take_a_food(self): """ 拿走一个食物 :return: """ self.__food_foods -= 1
def put_food_to_home(self): """ 向家中放一个食物 :return: """ self.__home_food += 1
def can_go(self, x, y): """ 判断这个坐标是否可达 :param x: :param y: :return: """ if 0 <= x <= self.X and 0 <= y <= self.Y and not self.is_obstacle(x, y): return True return False
def is_obstacle(self, x, y): """ 判断一个坐标是否为障碍物 :param x: :param y: :return: """ return self.__data[x][y] == Map.OBSTACLE
def is_home(self, x, y): """ 坐标是否为家 :param x: :param y: :return: """ return self.__data[x][y] == Map.HOME
def get_home_coor(self): """ 获得家的坐标 :return: """ return self.__home_coor
def get_food_coor(self): """ 获得食物的坐标 :return: """ return self.__food_coor
def is_food(self, x, y): """ 坐标是否为食物 :param x: :param y: :return: """ return self.__data[x][y] == Map.FOOD
def calcu_max_smell(self, coors, memory, smell_type): """ 计算一个坐标范围内的最大信息素 :return: """ smell = 0 for coor in coors: x, y = coor if not self.can_go(x, y): continue
if smell_type == Ant.FOOD_SMELL and self.is_food(x, y): smell = Ant.MAX_SMELL break
if smell_type == Ant.HOME_SMELL and self.is_home(x, y): smell = Ant.MAX_SMELL break
if (x, y) in memory: continue
if self.is_food(x, y) or self.is_home(x, y): continue
if smell_type == Ant.FOOD_SMELL: if self.__smells[(x, y)][Ant.FOOD_SMELL] > smell: smell = self.__smells[(x, y)][Ant.FOOD_SMELL]
if smell_type == Ant.HOME_SMELL: if self.__smells[(x, y)][Ant.HOME_SMELL] > smell: smell = self.__smells[(x, y)][Ant.HOME_SMELL] return smell
def leave_smell(self, x, y, smell_type, n): """ 在坐标x,y上留下信息素 :return: """ if self.__smells[(x, y)][smell_type] < n: self.__smells[(x, y)][smell_type] = n
def wastage(self): """ 信息素随着时间,不断的流逝 :return: """ self.time += 1
if self.time > Map.SMELL_GONE_SPEED: self.time = 0
for k, v in self.__smells.iteritems(): if v[Ant.HOME_SMELL] > 0: v[Ant.HOME_SMELL] -= 1 + (v[Ant.HOME_SMELL] * Map.SMELL_GONE_RATE) if v[Ant.HOME_SMELL] < 0: v[Ant.HOME_SMELL] = 0
if v[Ant.FOOD_SMELL] > 0: v[Ant.FOOD_SMELL] -= 1 + (v[Ant.FOOD_SMELL] * Map.SMELL_GONE_RATE) if v[Ant.FOOD_SMELL] < 0: v[Ant.FOOD_SMELL] = 0
def add_ant(self, ant): """ 在地图上添加一直蚂蚁 :param ant: :return: """ self.__ants.append(ant)
def get_ants(self): """ 获得所有蚂蚁 :return: """ return self.__ants
def get_ants_coors(self): """ 获得地图中每一只蚂蚁的坐标 :return: """ return [(ant.x, ant.y) for ant in self.__ants]
def show_ant(self): for r, item in enumerate(self.__data): row = [] for c, i in enumerate(item): if i == Map.LAWN: number = 0 for ant in self.__ants: if ant.x == r and ant.y == c: number += 1 if number == 0: row.append(Map.P_LAWN) else: row.append(str(number)) elif i == Map.OBSTACLE: row.append(Map.P_OBSTACLE) elif i == Map.HOME: row.append(str(self.__home_food)) elif i == Map.FOOD: row.append(str(self.__food_foods))
print "[%s]" % ("".join(row),)
def show_smell(self, smell_type): """ 展示地图上家的信息素分布 :return: """ for r, item in enumerate(self.__data): row = [] for c, i in enumerate(item): if i == Map.LAWN: number = 0 if (r, c) in self.__smells: number = self.__smells[(r, c)][smell_type] if number == 0: row.append(Map.P_LAWN) else: row.append(str(number)) elif i == Map.OBSTACLE: row.append(Map.P_OBSTACLE) elif i == Map.HOME: row.append(Map.P_HOME) elif i == Map.FOOD: row.append(Map.P_FOOD)
print "[%s]" % ("".join(row),)
def get_smells(self): """ 获得地图上家和食物的信息素分布 :return: """ return self.__smells
def print_smell(self, smell_type): """ 打印smell :return: """ for k, item in self.__smells.iteritems(): print k, item[smell_type]
def change(self): """ 世界发生一次变化 :param map: :param ants: :return: """ for ant in self.get_ants(): ant.one_step() self.wastage()
def snapshot(self): """ 快照 :return: """ return self.landform(), self.get_ants(), self.get_food_count(), self.get_smells()
class World(object):
def __init__(self, x, y , ant_counts, food_counts): self.__map = Map(x, y, food_counts) for i in range(ant_counts): ant = Ant(self.__map)
def run(self): while True: self.__map.change() yield self.__map.snapshot()
|