def test_battles(self): map = Map() map.create(None, 3, 3) world = World(map) realm0 = world.create_realm('Realm0', 1.0, 1.0, 1.0) realm1 = world.create_realm('Realm1', 1.0, 1.0, 1.0) province = map.get_province(0, 0) army0 = Army(0, realm0) army0.size = 2 army1 = Army(0, realm1) army1.size = 1 province.add_army(army0) province.add_army(army1) result = world.tick() self.assertEqual(len(result.battles), 1) battle = result.battles[0] self.assertEqual(battle.army0, army0) self.assertEqual(battle.army1, army1) self.assertEqual(army0.size, 1) self.assertEqual(army1.size, 0) self.assertEqual(army0.province, province) self.assertTrue(army0 in province.armies) self.assertEqual(army1.province, None) self.assertFalse(army1 in province.armies)
def test_create_army(self): world = World() realm = world.create_realm('Realm0', 0.1, 0.2, 0.3) army0 = world.create_army(realm) self.assertNotEqual(army0, None) self.assertEqual(army0.id, 0) self.assertEqual(army0.realm, realm) self.assertEqual(army0.size, 0) self.assertEqual(army0.province, None) self.assertEqual(len(world.armies), 1) self.assertTrue(army0 in world.armies) self.assertEqual(len(realm.armies), 1) self.assertTrue(army0 in realm.armies) army1 = world.create_army(realm) self.assertNotEqual(army1, None) self.assertEqual(army1.id, 1) self.assertEqual(army1.realm, realm) self.assertEqual(army1.size, 0) self.assertEqual(army1.province, None) self.assertEqual(len(world.armies), 2) self.assertTrue(army1 in world.armies) self.assertEqual(len(realm.armies), 2) self.assertTrue(army1 in realm.armies)
def test_create_army(self): world = World() realm = world.create_realm('Test', 1.0, 1.0, 1.0) province = Province(None, 0,0) realm.add_province(province) create0 = CreateArmy(world, realm, province, 1) create0.execute() army0 = create0.army self.assertNotEqual(army0, None) self.assertEqual(army0.realm, realm) self.assertEqual(army0.size, 1) self.assertEqual(army0.province, province) self.assertEqual(len(province.armies), 1) self.assertTrue(army0 in province.armies) self.assertEqual(len(realm.armies), 1) self.assertTrue(army0 in realm.armies) create1 = CreateArmy(world, realm, province, 10) create1.execute() army1 = create1.army self.assertNotEqual(army1, None) self.assertEqual(army1.realm, realm) self.assertEqual(army1.size, 10) self.assertEqual(army1.province, province) self.assertEqual(len(province.armies), 2) self.assertTrue(army0 in province.armies) self.assertTrue(army1 in province.armies) self.assertEqual(len(realm.armies), 2) self.assertTrue(army0 in realm.armies) self.assertTrue(army1 in realm.armies)
def test_actions(self): world = World() realm = world.create_realm('Realm0', 1.0, 1.0, 1.0) province = Province(None, 0,0) action0 = Action() province.action = action0 realm.add_province(province) result = world.tick() self.assertEqual(world.turn, 1) self.assertEqual(action0.count, 1) self.assertEqual(province.action, None) self.assertEqual(len(result.battles), 0) army = world.create_army(realm) army.size = 1 province.add_army(army) action1 = Action() army.action = action1 result = world.tick() self.assertEqual(world.turn, 2) self.assertEqual(action0.count, 1) self.assertEqual(action1.count, 1) self.assertEqual(army.action, None) self.assertEqual(province.action, None) self.assertEqual(len(result.battles), 0)
def test_get_realm(self): world = World() realm0 = world.create_realm('Realm0', 0.1, 0.2, 0.3) realm1 = world.create_realm('Realm1', 0.1, 0.2, 0.3) self.assertEqual(realm0, world.get_realm(0)) self.assertEqual(realm1, world.get_realm(1))
def test_invalid_index(self): world = World() realm0 = world.create_realm('Realm0', 0.1, 0.2, 0.3) realm1 = world.create_realm('Realm1', 0.1, 0.2, 0.3) self.assertEqual(None, world.get_realm(-1)) self.assertEqual(None, world.get_realm(2)) self.assertEqual(None, world.get_realm(None))
def test_invalid_realm(self): world = World() self.assertRaises(AssertionError, world.create_army, None) realm = Realm() army = world.create_army(realm) self.assertEqual(army, None)
def test_create_realm(self): world = World() realm0 = world.create_realm('Realm0', 0.1, 0.2, 0.3) self.assertNotEqual(realm0, None) self.assertEqual(realm0.id, 0) self.assertEqual(realm0.name, 'Realm0') self.assertEqual(realm0.color.get_r(), 0.1) self.assertEqual(realm0.color.get_g(), 0.2) self.assertEqual(realm0.color.get_b(), 0.3) self.assertEqual(len(world.realms), 1) self.assertTrue(realm0 in world.realms) realm1 = world.create_realm('Realm1', 0.3, 0.2, 0.1) self.assertNotEqual(realm1, None) self.assertEqual(realm1.id, 1) self.assertEqual(realm1.name, 'Realm1') self.assertEqual(realm1.color.get_r(), 0.3) self.assertEqual(realm1.color.get_g(), 0.2) self.assertEqual(realm1.color.get_b(), 0.1) self.assertEqual(len(world.realms), 2) self.assertTrue(realm1 in world.realms)
def solve(self): nodes, edges = self.get_map() world = World(nodes, self.cost, edges=edges) ants = 20 solver = Solver(ant_count=ants) solution = solver.solve(world) print("Nodes: ", len(nodes)) print("Number of ants: ", ants) print("Best distance: ", solution.distance) print("Path: ") path = solution.tour for i in range(len(path)): print("\t" + str(i + 1) + ": ", path[i][3])
def start(window): global world, commands, scenes, current_scene # window & corner-case setup signal.signal(signal.SIGINT, lambda sig, frame: end(window)) # game setup world = World() commands = { "help": cmd_help, "exit": cmd_exit, "view": world.display_map, "scan": world.display_scanned_map, "stats": world.player.display_stats, "control mode": lambda: change_scene_to("control_mode"), "move north": partial(world.move_player, Direction.North), "move east": partial(world.move_player, Direction.East), "move south": partial(world.move_player, Direction.South), "move west": partial(world.move_player, Direction.West), "yell": cmd_yell } init_scenes("terminal_mode", window, world) # TODO: should start with "active_mode" or "menu"
def overseas(cls, date): if date is None: date = datetime.now().strftime('%Y%m%d') words = World.select().filter(World.date == date).all() return words
selected_army = None selected_province = None moved_army = None realm = None realm_index = 0 if __name__ == '__main__': window = pyglet.window.Window(800, 600, 'PyRealm 01') terrain0 = Terrain("Plain", Color(0.0, 1.0, 0.0)) gamemap = Map() gamemap.create(terrain0, 3, 3) world = World(gamemap) realm0 = world.create_realm('Askir', 1.0, 0.0, 0.0) realm0.add_province(gamemap.get_province(2, 2)) realm1 = world.create_realm('Thalak', 0.0, 0.0, 1.0) realm1.add_province(gamemap.get_province(0, 0)) realm = realm0 print('Realm={0:s}:'.format(realm.name)) view = MapView(gamemap, 50.0, 5.0, 5.0, 15.0) @window.event def on_draw(): window.clear()
import tkinter as tk from system.operation import Operation from model.world import World import settings root = tk.Tk() img = tk.PhotoImage(file='png/sports-car.png') root.tk.call('wm', 'iconphoto', root._w, img) menu = tk.Menu(root) root.config(menu=menu) root.protocol("WM_DELETE_WINDOW", lambda: op.terminate(root)) toolbar = tk.Frame(root) function = tk.Frame(toolbar) info = tk.Frame(toolbar) world = World() world.load() buttonGroup = tk.Frame(function) sliderGroup = tk.Frame(function) play = tk.Button(buttonGroup, text="Action") playPNG = tk.PhotoImage(file="png/play-button.png") pausePNG = tk.PhotoImage(file="png/pause.png") play.config(compound=tk.LEFT, image=playPNG, width="70", height="24", bg="#FFFFFF", command=lambda: op.runModel()) play.pack(side=tk.LEFT, padx=2, pady=2)
path = os.path.join(os.getcwd(), "data") START_DENSITY = 0.005 TERMINATE_DENSITY = 0.9 STEP = 0.005 ITERATION = 10 CAR_NUM = 15 START_COLLECT = 10000 checkLockTime = 10000 zero = [0 for i in range(10)] for car in range(15, 220, 5): print('Car number is {}'.format(car)) i = 0 while i < ITERATION: print('exp{}'.format(i + 1)) world = World(exp=True) world.load() world.carsNumber = car expList = [] time = 0 zeroList = [] while time < setDict["collecting_time"] + 1: world.onTick(0.2) if len(world.cars) == 0: print() break avgSpeed, avgDensity, flow = world.systemInfo() zeroList.append(0 if avgSpeed == 0.0 else 1) if time > 100 and zeroList[-10:] == zero: print() break
def sync(cls): now = datetime.now().strftime('%Y-%m-%d_%H') log.info(f'begin sync {now}') file_name = f"./data/{now}.json" if not os.path.exists(file_name): CrawlerService.crawler() raw_data = json.load(open(file_name, 'r')) data = raw_data['nCoV2019'] date = datetime.now().strftime('%Y%m%d') world = World(date=date, name='中国', confirmed=data['totalConNum'], suspected=data['totalSusNum'], cured=data['totalCureNum'], dead=data['totalDeathNum'], add_confirmed=data['addCon'], add_cured=data['addCure'], add_dead=data['addDeath'], add_suspected=data['addSus']) history_list = data['historyList'] histories = [] for history in history_list: histories.append( History(date=history['date'], confirmed=history['conNum'], cured=history['cureNum'], dead=history['deathNum'], suspected=history['susNum'])) overseas_list = data['overseasList'] worlds = [] for overseas in overseas_list: worlds.append( World(date=date, confirmed=overseas['conNum'], cured=overseas['cureNum'], suspected=overseas['susNum'], dead=overseas['deathNum'], name=overseas['name'])) domestic_list = data['domesticList'] provinces = [] cities = [] for domestic in domestic_list: provinces.append( Province(date=date, province_name=domestic['name'], short_name=domestic['name'], confirmed=domestic['conNum'], dead=domestic['deathNum'], suspected=domestic['susNum'], cured=domestic['cureNum'])) city_list = domestic['cities'] for city in city_list: cities.append( City(date=date, province_name=domestic['name'], city_name=city['name'], confirmed=city['conNum'], suspected=city['susNum'], cured=city['cureNum'], dead=city['deathNum'])) History.select().delete() History.bulk_insert(histories) World.select().filter(World.date == date).delete() world.insert() World.bulk_insert(worlds) Province.select().filter(Province.date == date).delete() Province.bulk_insert(provinces) City.select().filter(City.date == date).delete() City.bulk_insert(cities) log.info(f'sync {now} done')
def china_daily(cls, date): if date is None: date = datetime.now().strftime('%Y%m%d') world = World.select().filter(World.date == date, World.name == '中国').one() return world
import json from model.world import World from logic.simulator import Simulator from util import global_config from deepmerge import always_merger if __name__ == '__main__': config_file_path = 'conf/my_first_world.json' with open(config_file_path, 'r') as f: config = json.load(f) always_merger.merge(global_config.config, config) config = global_config.config world = World.from_config(config['world']) events = config["generational_events"] simulator = Simulator(world, generational_events=events) print("Running simulation with config {}:\n{}".format( config_file_path, json.dumps(global_config.get_vars(), indent=2))) print() print("Initial world: %s" % world) print() simulator.run() print("Final world: %s" % world)
from model.world import World from view import console import time if __name__ == "__main__": world = World() while True: world.tick() console.overview(world) time.sleep(0.05)