world.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import random
  2. import time
  3. import sys, os
  4. from termcolor import colored, cprint
  5. from agent import Agent
  6. from resource import *
  7. from collections import Counter
  8. # main world contructor fn
  9. class World():
  10. def __init__(self, sim_params):
  11. # call square constructor with sim params
  12. self.squares = build_squares(sim_params.world_size, sim_params.resources)
  13. # set reso tallies to 0 at round start
  14. self.raw_resource_count = 0
  15. self.initial_raw_resource_count = 0
  16. self.harvested_resource_count = 0
  17. # tally resources at round start
  18. for s in self.squares:
  19. if s.square_resource:
  20. self.raw_resource_count += 1
  21. self.initial_raw_resource_count += 1
  22. # setup all square obects, put them in array
  23. class WorldSquare():
  24. def __init__(self, x, y, square_resource):
  25. self.x = x
  26. self.y = y
  27. self.square_resource = square_resource
  28. occupied = False
  29. occupant = None
  30. square_resource = None
  31. # setup grid of squares
  32. def build_squares(world_size, resources):
  33. squares = [ WorldSquare(x=x, y=y,
  34. square_resource=select_resource(resources)) for x in
  35. range(world_size) for y in range(world_size) ]
  36. return squares
  37. # setup agents
  38. def setup_agents(sim_params, world):
  39. # TODO - stop agents from being on same square!
  40. # TODO - if two spawn on same square, can cause "over harvesting"
  41. world.agent_list=[]
  42. for i in range(sim_params.num_agents):
  43. unplaced = True
  44. # randomly choose squares (coords) until an empty one picked
  45. while unplaced:
  46. x_pos = random.choice(range(sim_params.world_size))
  47. y_pos = random.choice(range(sim_params.world_size))
  48. target = next((square for square in world.squares \
  49. if square.x == x_pos and square.y == y_pos), None)
  50. if not target.occupied:
  51. target.occupied = True
  52. unplaced = False
  53. world.agent_list.append(Agent(
  54. x=x_pos,
  55. y=y_pos,
  56. id=i,
  57. # TODO - implement action durations
  58. harvest_duration = 2,
  59. refine_duration = 5,
  60. learning_rate = 1,
  61. world = world
  62. ))
  63. target.occupant = world.agent_list[-1]
  64. def draw_world(sim_params, world, round_num):
  65. # this fn will draw the map each round
  66. # relies on python termcolor module for coloring
  67. os.system("clear")
  68. print("starting round ", round_num)
  69. print("\t\t\t initial resource count: ", + world.initial_raw_resource_count)
  70. print("\t\t\t current resource count: ", + world.raw_resource_count)
  71. map_string = ""
  72. # horizontal divider added between every row
  73. map_string_horizontal_divider = "\t" + sim_params.world_size * (5 * "-" + "\t")
  74. # map_string += map_string_horizontal_divider + "\n"
  75. for y in reversed(range(sim_params.world_size)):
  76. row = [square for square in world.squares if square.y == y]
  77. map_string += map_string_horizontal_divider + "\n"
  78. map_string += "y:" + str(y)+ "\t"
  79. for sq in row:
  80. if sq.occupied:
  81. occupant = sq.occupant.id
  82. else:
  83. occupant = " "
  84. if sq.square_resource:
  85. reso = sq.square_resource["name"][-1]
  86. else:
  87. reso = " "
  88. map_string += "|{}|{}|\t".format(colored(occupant, "blue"),
  89. colored(reso, "red"))
  90. map_string += "\n" + map_string_horizontal_divider + "\n\n"
  91. bottom_string = "\t "
  92. for col in range(sim_params.world_size):
  93. piece = "x:" + str(col) + "\t"
  94. bottom_string += piece
  95. map_string += bottom_string
  96. print(map_string)
  97. print("\n" + 80 * "*")
  98. for agent in world.agent_list:
  99. agent.counts = {}
  100. for item in agent.inventory:
  101. if item["name"] in agent.counts:
  102. agent.counts[item["name"]] += 1
  103. else:
  104. agent.counts[item["name"]] = 1
  105. print("Agent", agent.id, "inventory: ", agent.counts)
  106. time.sleep(0.1)