def wrapped(*args, **kwargs): statprof.start() try: return main(*args, **kwargs) finally: statprof.stop() statprof.display(OUT=file(output_file, 'wb'))
def stat(): if options.profile: statprof.stop() output = StringIO.StringIO() output.write("-" * 80) output.write("\nWorker with PID %s processed %d requests\n" % (workerPid, site.cnt)) if options.profile: output.write("\n") #format = statprof.DisplayFormats.ByLine #format = statprof.DisplayFormats.ByMethod #statprof.display(output, format = format) statprof.display(output) output.write("-" * 80) output.write("\n") output.write("\n") sys.stdout.write(output.getvalue()) if options.profile: statprof.reset() statprof.start() reactor.callLater(options.interval, stat)
def stat(): if options.profile: statprof.stop() output = StringIO.StringIO() output.write("-" * 80 + "\n") output.write("Worker Statistics (PID %s)\n\n%s" % (workerPid, factory.stats.stats())) if options.profile: output.write("\n") # format = statprof.DisplayFormats.ByLine # format = statprof.DisplayFormats.ByMethod # statprof.display(output, format = format) statprof.display(output) output.write("-" * 80 + "\n\n") sys.stdout.write(output.getvalue()) if options.profile: statprof.reset(PROFILER_FREQ) statprof.start() reactor.callLater(options.interval, stat)
def wrapped(*args, **kwargs): statprof.start() try: return fn(*args, **kwargs) finally: statprof.stop() statprof.display()
def inner(*args, **kwargs): statprof.reset(frequency=1000) statprof.start() try: return fn(*args, **kwargs) finally: statprof.stop() statprof.display()
def catching(proc, *args, **kwargs): import statprof statprof.start() try: return proc(*args, **kwargs) finally: statprof.stop() statprof.display()
def main(): import argparse parser = argparse.ArgumentParser(description='volta console worker') parser.add_argument('-d', '--debug', '-v', '--verbose', dest='verbose', action='store_true', default=False) parser.add_argument('-t', '--trace', dest='trace', action='store_true', default=False) parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', default=False) parser.add_argument('-l', '--log', dest='log', default='volta.log') parser.add_argument('-c', '--config', dest='config') parser.add_argument( '-p', '--patch-cfg', action='append', help= 'Patch config with yaml snippet (similar to -o, but has full compatibility to\ and the exact scheme of yaml format config)', dest='patches', default=[]) parser.add_argument('--defaults', dest='defaults', default='/etc/volta.yaml') args = parser.parse_args() if not args.config: raise RuntimeError('Empty config') init_logging(args.log, args.verbose, args.quiet) configs = [] try: configs = configs + [load_cfg(args.defaults)] except Exception: logger.info('Unable to load default configs... %s', args.defaults) configs = configs + [load_cfg(args.config)] if args.trace: import statprof statprof.start() patched_configs = configs + parse_and_check_patches(args.patches) perform_test(patched_configs, args.log) if args.trace: statprof.stop() statprof.display()
def pilot_detail(request, pilot_id): return pilot_detail_view.pilot_detail(request, pilot_id) statprof.start() try: return pilot_detail_view.pilot_detail(request, pilot_id) finally: statprof.stop() statprof.display() return pilot_detail_view.pilot_detail(request, pilot_id) return render(request, "analizer/index.html", {"error_message": "Unsupported: "})
def profile(fun, skip=False): """ Statistical profiling of a given function. :param fun: :param skip: :return: """ import statprof if skip: return fun() statprof.start() try: result = fun() finally: statprof.stop() statprof.display() return result
def inner(*args, **kwargs): if deco_kwargs.get('traceback'): traceback.print_stack() print('starting %s' % fn.__name__) start = time.time() stat_profile = deco_kwargs.get('stat_profile') if stat_profile: import statprof statprof.reset(frequency=10000) statprof.start() fn(*args, **kwargs) fn_time = time.time() - start print('finished %s in %s s' % (fn.__name__, fn_time)) if stat_profile: statprof.stop() statprof.display() return fn_time
def statprofile(ui, func, fp): try: import statprof except ImportError: raise util.Abort(_('statprof not available - install using "easy_install statprof"')) freq = ui.configint("profiling", "freq", default=1000) if freq > 0: statprof.reset(freq) else: ui.warn(_("invalid sampling frequency '%s' - ignoring\n") % freq) statprof.start() try: return func() finally: statprof.stop() statprof.display(fp)
def inner(*args, **kwargs): if deco_kwargs.get('traceback'): traceback.print_stack() print('starting %s' % fn.__name__) start = time.time() stat_profile = deco_kwargs.get('stat_profile') if stat_profile: import statprof statprof.reset(frequency=1000) statprof.start() try: return fn(*args, **kwargs) finally: fn_name = fn.__name__ print('finished %s in %.3f s' % (fn_name, time.time() - start)) if stat_profile: statprof.stop() statprof.display()
def statprofile(ui, func, fp): try: import statprof except ImportError: raise util.Abort(_( 'statprof not available - install using "easy_install statprof"')) freq = ui.configint('profiling', 'freq', default=1000) if freq > 0: statprof.reset(freq) else: ui.warn(_("invalid sampling frequency '%s' - ignoring\n") % freq) statprof.start() try: return func() finally: statprof.stop() statprof.display(fp)
def run(main, profile=None): if not profile: main() return if profile == 'hotshot': import hotshot, hotshot.stats p = hotshot.Profile("/tmp/pro") p.runcall(main) p.close() hotshot.stats.load("/tmp/pro").sort_stats('cumulative').print_stats() elif profile == 'stat': import statprof statprof.start() try: main() finally: statprof.stop() statprof.display()
def inner(*args, **kwargs): log_fn = logger.debug if deco_kwargs.get('traceback'): traceback.print_stack() if deco_kwargs.get('traceback'): traceback.print_stack() log_fn('starting %s', fn.__name__) start = time.time() stat_profile = deco_kwargs.get('stat_profile') if stat_profile: import statprof statprof.reset(frequency=10000) statprof.start() try: return fn(*args, **kwargs) finally: log_fn('finished %s in %s s', fn.__name__, time.time() - start) if stat_profile: statprof.stop() statprof.display()
def statprofile(ui, fp): try: import statprof except ImportError: raise error.Abort(_( 'statprof not available - install using "easy_install statprof"')) freq = ui.configint('profiling', 'freq', default=1000) if freq > 0: # Cannot reset when profiler is already active. So silently no-op. if statprof.state.profile_level == 0: statprof.reset(freq) else: ui.warn(_("invalid sampling frequency '%s' - ignoring\n") % freq) statprof.start() try: yield finally: statprof.stop() statprof.display(fp)
def stat(): if options.profile: statprof.stop() output = StringIO.StringIO() output.write("-" * 80 + "\n") output.write("Worker Statistics (PID %s)\n\n%s" % (workerPid, factory.stats.stats())) if options.profile: output.write("\n") #format = statprof.DisplayFormats.ByLine #format = statprof.DisplayFormats.ByMethod #statprof.display(output, format = format) statprof.display(output) output.write("-" * 80 + "\n\n") sys.stdout.write(output.getvalue()) if options.profile: statprof.reset(PROFILER_FREQ) statprof.start() reactor.callLater(options.interval, stat)
def generateOrders(): # pylint: disable=invalid-name """Called once per turn to tell the Python AI to generate and issue orders to control its empire. at end of this function, fo.doneTurn() should be called to indicate to the client that orders are finished and can be sent to the server for processing.""" rules = fo.getGameRules() print "Defined game rules:" for rule in rules.getRulesAsStrings: print "Name: " + rule.name + " value: " + str(rule.value) print "Rule RULE_NUM_COMBAT_ROUNDS value: " + str( rules.getInt("RULE_NUM_COMBAT_ROUNDS")) empire = fo.getEmpire() if empire is None: print "This client has no empire. Doing nothing to generate orders." try: # early abort if no empire. no need to do meter calculations # on last-seen gamestate if nothing can be ordered anyway... # # note that doneTurn() is issued on behalf of the client network # id, not the empire id, so not having a correct empire id does # not invalidate doneTurn() fo.doneTurn() except Exception as e: print_error(e) return if empire.eliminated: print "This empire has been eliminated. Aborting order generation" try: # early abort if already eliminated. no need to do meter calculations # on last-seen gamestate if nothing can be ordered anyway... fo.doneTurn() except Exception as e: print_error(e) return # This code block is required for correct AI work. print "Meter / Resource Pool updating..." fo.initMeterEstimatesDiscrepancies() fo.updateMeterEstimates(False) fo.updateResourcePools() turn = fo.currentTurn() turn_uid = foAIstate.set_turn_uid() print "\n\n\n", "=" * 20, print "Starting turn %s (%s) of game: %s" % (turn, turn_uid, foAIstate.uid), print "=" * 20, "\n" turn_timer.start("AI planning") # set the random seed (based on galaxy seed, empire name and current turn) # for game-reload consistency. random_seed = str( fo.getGalaxySetupData().seed) + "%05d%s" % (turn, fo.getEmpire().name) random.seed(random_seed) universe = fo.getUniverse() empire = fo.getEmpire() planet_id = PlanetUtilsAI.get_capital() planet = None if planet_id is not None: planet = universe.getPlanet(planet_id) aggression_name = get_trait_name_aggression(foAIstate.character) print "***************************************************************************" print "******* Log info for AI progress chart script. Do not modify. **********" print("Generating Orders") print( "EmpireID: {empire.empireID}" " Name: {empire.name}_{empire.empireID}_pid:{p_id}_{p_name}RIdx_{res_idx}_{aggression}" " Turn: {turn}").format(empire=empire, p_id=fo.playerID(), p_name=fo.playerName(), res_idx=ResearchAI.get_research_index(), turn=turn, aggression=aggression_name.capitalize()) print "EmpireColors: {0.colour.r} {0.colour.g} {0.colour.b} {0.colour.a}".format( empire) if planet: print "CapitalID: " + str( planet_id ) + " Name: " + planet.name + " Species: " + planet.speciesName else: print "CapitalID: None Currently Name: None Species: None " print "***************************************************************************" print "***************************************************************************" if turn == 1: declare_war_on_all() human_player = fo.empirePlayerID(1) greet = diplomatic_corp.get_first_turn_greet_message() fo.sendChatMessage( human_player, '%s (%s): [[%s]]' % (empire.name, get_trait_name_aggression( foAIstate.character), greet)) # turn cleanup !!! this was formerly done at start of every turn -- not sure why foAIstate.split_new_fleets() foAIstate.refresh( ) # checks exploration border & clears roles/missions of missing fleets & updates fleet locs & threats foAIstate.report_system_threats() print("Calling AI Modules") # call AI modules action_list = [ ColonisationAI.survey_universe, ProductionAI.find_best_designs_this_turn, PriorityAI.calculate_priorities, ExplorationAI.assign_scouts_to_explore_systems, ColonisationAI.assign_colony_fleets_to_colonise, InvasionAI.assign_invasion_fleets_to_invade, MilitaryAI.assign_military_fleets_to_systems, FleetUtilsAI.generate_fleet_orders_for_fleet_missions, FleetUtilsAI.issue_fleet_orders_for_fleet_missions, ResearchAI.generate_research_orders, ProductionAI.generate_production_orders, ResourcesAI.generate_resources_orders, ] for action in action_list: try: main_timer.start(action.__name__) action() main_timer.stop() except Exception as e: print_error(e, location=action.__name__) main_timer.stop_print_and_clear() turn_timer.stop_print_and_clear() turn_timer.start("Server_Processing") try: fo.doneTurn() except Exception as e: print_error(e) # TODO move it to cycle above if using_statprof: try: statprof.stop() statprof.display() statprof.start() except: pass
def generateOrders(): # pylint: disable=invalid-name "called by client to get the AI's orders for the turn" global _lastTurnTimestamp universe = fo.getUniverse() turnStartTime = time() #starting AI timer here, to be sure AI doesn't get blame for any lags in server being able to provide the Universe object empire = fo.getEmpire() planetID = PlanetUtilsAI.getCapital() planet = None if planetID is not None: planet = universe.getPlanet(planetID) print "***************************************************************************" print "***************************************************************************" print ("Generating Orders") print "EmpireID: " + str(empire.empireID) + " Name: " + empire.name+ "_"+str(empire.empireID) +"_pid:"+str(fo.playerID())+"_"+fo.playerName()+"_"+_aggressions.get(foAIstate.aggression, "?") + " Turn: " + str(fo.currentTurn()) empireColor = empire.colour print "EmpireColors: %d %d %d %d"% (empireColor.r, empireColor.g, empireColor.b, empireColor.a) if planet: print "CapitalID: " + str(planetID) + " Name: " + planet.name + " Species: " + planet.speciesName else: print "CapitalID: None Currently Name: None Species: None " print "***************************************************************************" print "***************************************************************************" if fo.currentTurn() == 1: declareWarOnAll() # turn cleanup !!! this was formerly done at start of every turn -- not sure why splitNewFleets() #updateShipDesigns() #should not be needed anymore; #updateFleetsRoles() foAIstate.clean() #checks exploration border & clears roles/missions of missing fleets & updates fleet locs foAIstate.reportSystemThreats() # ...missions # ...demands/priorities print("Calling AI Modules") # call AI modules timer = [time()] try: PriorityAI.calculatePriorities() except: print "Error: exception triggered and caught: ", traceback.format_exc() # try traceback.print_exc() timer.append( time() ) try: ExplorationAI.assignScoutsToExploreSystems() except: print "Error: exception triggered and caught: ", traceback.format_exc() timer.append( time() ) try: ColonisationAI.assignColonyFleetsToColonise() except: print "Error: exception triggered and caught: ", traceback.format_exc() timer.append( time() ) try: InvasionAI.assignInvasionFleetsToInvade() except: print "Error: exception triggered and caught: ", traceback.format_exc() timer.append( time() ) try: MilitaryAI.assignMilitaryFleetsToSystems() except: print "Error: exception triggered and caught: ", traceback.format_exc() timer.append( time() ) try: FleetUtilsAI.generateAIFleetOrdersForAIFleetMissions() except: print "Error: exception triggered and caught: ", traceback.format_exc() timer.append( time() ) try: FleetUtilsAI.issueAIFleetOrdersForAIFleetMissions() except: print "Error: exception triggered and caught: ", traceback.format_exc() timer.append( time() ) try: ResearchAI.generateResearchOrders() except: print "Error: exception triggered and caught: ", traceback.format_exc() timer.append( time() ) try: ProductionAI.generateProductionOrders() except: print "Error: exception triggered and caught: ", traceback.format_exc() timer.append( time() ) try: ResourcesAI.generateResourcesOrders() except: print "Error: exception triggered and caught: ", traceback.format_exc() timer.append( time() ) try: foAIstate.afterTurnCleanup() except: print "Error: exception triggered and caught: ", traceback.format_exc() timer.append( time() ) times = [timer[i] - timer[i-1] for i in range(1, len(timer) ) ] turnEndTime = time() timeFmt = "%30s: %8d msec " print "AI Module Time Requirements:" for mod, modTime in zip(timerEntries , times): print timeFmt % ((30*' '+mod)[-30:], int(1000*modTime)) if _timerFile: _timerFile.write( _timerFileFmt% tuple( [ fo.currentTurn() ]+map(lambda x: int(1000*x), times )) +'\n') _timerFile.flush() if _timerBucketFile: _timerBucketFile.write( _timerBucketFileFmt% tuple( [ fo.currentTurn(), (turnStartTime-_lastTurnTimestamp)*1000, (turnEndTime-turnStartTime)*1000 ]) +'\n') _timerBucketFile.flush() _lastTurnTimestamp = time() try: fo.doneTurn() except: print "Error: exception triggered and caught: ", traceback.format_exc() if using_statprof: try: statprof.stop() statprof.display() statprof.start() except: pass
def generateOrders(): # pylint: disable=invalid-name """Called once per turn to tell the Python AI to generate and issue orders to control its empire. at end of this function, fo.doneTurn() should be called to indicate to the client that orders are finished and can be sent to the server for processing.""" turn_timer.start("AI planning") universe = fo.getUniverse() empire = fo.getEmpire() planet_id = PlanetUtilsAI.get_capital() # set the random seed (based on galaxy seed, empire ID and current turn) # for game-reload consistency random_seed = str(fo.getGalaxySetupData().seed) + "%03d%05d" % ( fo.empireID(), fo.currentTurn()) random.seed(random_seed) planet = None if planet_id is not None: planet = universe.getPlanet(planet_id) aggression_name = fo.aggression.values[foAIstate.aggression].name print "***************************************************************************" print "********** String for chart. Do not modify. ***************************" print("Generating Orders") print( "EmpireID: {empire.empireID}" " Name: {empire.name}_{empire.empireID}_pid:{p_id}_{p_name}RIdx_{res_idx}_{aggression}" " Turn: {turn}").format(empire=empire, p_id=fo.playerID(), p_name=fo.playerName(), res_idx=ResearchAI.get_research_index(), turn=fo.currentTurn(), aggression=aggression_name.capitalize()) print "EmpireColors: {0.colour.r} {0.colour.g} {0.colour.b} {0.colour.a}".format( empire) if planet: print "CapitalID: " + str( planet_id ) + " Name: " + planet.name + " Species: " + planet.speciesName else: print "CapitalID: None Currently Name: None Species: None " print "***************************************************************************" print "***************************************************************************" if fo.currentTurn() == 1: declare_war_on_all() human_player = fo.empirePlayerID(1) fo.sendChatMessage( human_player, '%s Empire (%s):\n"Ave, Human, morituri te salutant!"' % (empire.name, aggression_name)) # turn cleanup !!! this was formerly done at start of every turn -- not sure why foAIstate.split_new_fleets() foAIstate.refresh( ) # checks exploration border & clears roles/missions of missing fleets & updates fleet locs & threats foAIstate.report_system_threats() # ...missions # ...demands/priorities print("Calling AI Modules") # call AI modules action_list = [ PriorityAI.calculate_priorities, ExplorationAI.assign_scouts_to_explore_systems, ColonisationAI.assign_colony_fleets_to_colonise, InvasionAI.assign_invasion_fleets_to_invade, MilitaryAI.assign_military_fleets_to_systems, FleetUtilsAI.generate_fleet_orders_for_fleet_missions, FleetUtilsAI.issue_fleet_orders_for_fleet_missions, ResearchAI.generate_research_orders, ProductionAI.generateProductionOrders, ResourcesAI.generate_resources_orders, foAIstate.after_turn_cleanup, ] for action in action_list: try: main_timer.start(action.__name__) action() main_timer.stop() except Exception as e: print_error(e, location=action.__name__) main_timer.end() turn_timer.end() turn_timer.start("Server_Processing") try: fo.doneTurn() except Exception as e: print_error(e) # TODO move it to cycle above if using_statprof: try: statprof.stop() statprof.display() statprof.start() except: pass
def get_output(**kwargs): buffer = StringIO() display(buffer, **kwargs) buffer.seek(0) return buffer.read()
def generateOrders(): # pylint: disable=invalid-name """Called once per turn to tell the Python AI to generate and issue orders to control its empire. at end of this function, fo.doneTurn() should be called to indicate to the client that orders are finished and can be sent to the server for processing.""" empire = fo.getEmpire() if empire is None: print "This client has no empire. Doing nothing to generate orders." try: # early abort if no empire. no need to do meter calculations # on last-seen gamestate if nothing can be ordered anyway... # # note that doneTurn() is issued on behalf of the client network # id, not the empire id, so not having a correct empire id does # not invalidate doneTurn() fo.doneTurn() except Exception as e: print_error(e) return if empire.eliminated: print "This empire has been eliminated. Aborting order generation" try: # early abort if already eliminated. no need to do meter calculations # on last-seen gamestate if nothing can be ordered anyway... fo.doneTurn() except Exception as e: print_error(e) return # This code block is required for correct AI work. print "Meter / Resource Pool updating..." fo.initMeterEstimatesDiscrepancies() fo.updateMeterEstimates(False) fo.updateResourcePools() turn = fo.currentTurn() turn_uid = foAIstate.set_turn_uid() print "\n\n\n", "=" * 20, print "Starting turn %s (%s) of game: %s" % (turn, turn_uid, foAIstate.uid), print "=" * 20, "\n" turn_timer.start("AI planning") # set the random seed (based on galaxy seed, empire name and current turn) # for game-reload consistency. random_seed = str(fo.getGalaxySetupData().seed) + "%05d%s" % (turn, fo.getEmpire().name) random.seed(random_seed) universe = fo.getUniverse() empire = fo.getEmpire() planet_id = PlanetUtilsAI.get_capital() planet = None if planet_id is not None: planet = universe.getPlanet(planet_id) aggression_name = get_trait_name_aggression(foAIstate.character) print "***************************************************************************" print "******* Log info for AI progress chart script. Do not modify. **********" print ("Generating Orders") print ("EmpireID: {empire.empireID}" " Name: {empire.name}_{empire.empireID}_pid:{p_id}_{p_name}RIdx_{res_idx}_{aggression}" " Turn: {turn}").format(empire=empire, p_id=fo.playerID(), p_name=fo.playerName(), res_idx=ResearchAI.get_research_index(), turn=turn, aggression=aggression_name.capitalize()) print "EmpireColors: {0.colour.r} {0.colour.g} {0.colour.b} {0.colour.a}".format(empire) if planet: print "CapitalID: " + str(planet_id) + " Name: " + planet.name + " Species: " + planet.speciesName else: print "CapitalID: None Currently Name: None Species: None " print "***************************************************************************" print "***************************************************************************" if turn == 1: declare_war_on_all() human_player = fo.empirePlayerID(1) greet = diplomatic_corp.get_first_turn_greet_message() fo.sendChatMessage(human_player, '%s (%s): [[%s]]' % (empire.name, get_trait_name_aggression(foAIstate.character), greet)) # turn cleanup !!! this was formerly done at start of every turn -- not sure why foAIstate.split_new_fleets() foAIstate.refresh() # checks exploration border & clears roles/missions of missing fleets & updates fleet locs & threats foAIstate.report_system_threats() print("Calling AI Modules") # call AI modules action_list = [ColonisationAI.survey_universe, ProductionAI.find_best_designs_this_turn, PriorityAI.calculate_priorities, ExplorationAI.assign_scouts_to_explore_systems, ColonisationAI.assign_colony_fleets_to_colonise, InvasionAI.assign_invasion_fleets_to_invade, MilitaryAI.assign_military_fleets_to_systems, FleetUtilsAI.generate_fleet_orders_for_fleet_missions, FleetUtilsAI.issue_fleet_orders_for_fleet_missions, ResearchAI.generate_research_orders, ProductionAI.generate_production_orders, ResourcesAI.generate_resources_orders, ] for action in action_list: try: main_timer.start(action.__name__) action() main_timer.stop() except Exception as e: print_error(e, location=action.__name__) main_timer.stop_print_and_clear() turn_timer.stop_print_and_clear() turn_timer.start("Server_Processing") try: fo.doneTurn() except Exception as e: print_error(e) # TODO move it to cycle above if using_statprof: try: statprof.stop() statprof.display() statprof.start() except: pass
import pstats import sys def parse_1kg(): for line in vcf.Reader(filename='vcf/test/1kg.vcf.gz'): pass if len(sys.argv) == 1: sys.argv.append(None) if sys.argv[1] == 'profile': cProfile.run('parse_1kg()', '1kg.prof') p = pstats.Stats('1kg.prof') p.strip_dirs().sort_stats('time').print_stats() elif sys.argv[1] == 'time': n = 1 t = timeit.timeit('parse_1kg()', "from __main__ import parse_1kg", number=n) print t/n elif sys.argv[1] == 'stat': import statprof statprof.start() try: parse_1kg() finally: statprof.stop() statprof.display() else: print 'prof.py profile/time'
def stat_profiler(): statprof.start() yield statprof statprof.stop() statprof.display()
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, description= """Peppercorn: Domain-level nucleic acid reaction enumerator.""") add_peppercorn_args(parser) args = parser.parse_args() # ~~~~~~~~~~~~~ # Logging Setup # ~~~~~~~~~~~~~ title = "Peppercorn Domain-level Reaction Enumerator" logger = logging.getLogger('peppercornenumerator') logger.setLevel(logging.DEBUG) if args.logfile: banner = "{} {}".format(title, __version__) fh = logging.FileHandler(args.logfile) formatter = logging.Formatter('%(levelname)s - %(message)s') set_handle_verbosity(fh, args.verbose) fh.setFormatter(formatter) logger.addHandler(fh) else: banner = "{} {}".format(colors.BOLD + title + colors.ENDC, colors.GREEN + __version__ + colors.ENDC) ch = logging.StreamHandler() formatter = ColorFormatter('%(levelname)s %(message)s', use_color=True) set_handle_verbosity(ch, args.verbose) ch.setFormatter(formatter) logger.addHandler(ch) logger.info(banner) assertions = False try: assert False except AssertionError: assertions = True logger.debug(f'Using assert statements: {assertions}.') systeminput = args.input_filename if not systeminput: if sys.stdout.isatty(): logger.info("Reading file from STDIN, Ctrl-D to stop") systeminput = '' for l in sys.stdin: systeminput += l if args.interactive: loger.error( "Interactive mode needs to read input from file, not STDIN.") raise SystemExit # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # Input parsing to set initial complexes for enumeration # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # try: complexes, reactions = read_pil(systeminput, args.input_filename is not None) except ParseException as ex_pil: try: complexes, reactions = read_seesaw(systeminput, args.input_filename is not None, conc=args.seesaw_conc, explicit=args.seesaw_explicit, reactions=args.seesaw_reactions) except ParseException as ex_ssw: logger.error('Pil-format parsing error:') logger.error('Cannot parse line {:5d}: "{}"'.format( ex_pil.lineno, ex_pil.line)) logger.error( ' {} '.format(' ' * (ex_pil.col - 1) + '^')) logger.error('SeeSaw-format parsing error:') logger.error('Cannot parse line {:5d}: "{}"'.format( ex_ssw.lineno, ex_ssw.line)) logger.error( ' {} '.format(' ' * (ex_ssw.col - 1) + '^')) raise SystemExit init_cplxs = [ x for x in complexes.values() if x.concentration is None or x.concentration[1] != 0 ] name_cplxs = list(complexes.values()) enum = Enumerator(init_cplxs, reactions, named_complexes=name_cplxs) # Log initial complexes logger.info("") logger.info("Initial complexes:") for c in enum.initial_complexes: logger.info("{}: {}".format(c, c.kernel_string)) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # Transfer options to enumerator object # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # logger.info("") logger.info("Enumeration settings:") enum.max_complex_size = args.max_complex_size logger.info("Max complex size = {}".format(enum.max_complex_size)) enum.max_complex_count = max(args.max_complex_count, len(complexes)) logger.info("Max complex count = {}".format(enum.max_complex_count)) enum.max_reaction_count = max(args.max_reaction_count, len(reactions)) logger.info("Max reaction count = {}".format(enum.max_reaction_count)) enum.max_helix = not args.no_max_helix logger.info('Max-helix semantics = {}'.format(enum.max_helix)) enum.reject_remote = args.reject_remote logger.info('Reject-remote semantics = {}'.format(enum.reject_remote)) enum.dG_bp = args.dG_bp logger.info('Average strength of a toehold base-pair dG_bp = {}'.format( enum.dG_bp)) if args.ignore_branch_3way: if branch_3way in UNI_REACTIONS: UNI_REACTIONS.remove(branch_3way) logger.info('No 3-way branch migration.') if args.ignore_branch_4way: if branch_4way in UNI_REACTIONS: UNI_REACTIONS.remove(branch_4way) logger.info('No 4-way branch migration.') PepperComplex.PREFIX = args.complex_prefix logger.info("Prefix for new complexes = {PepperComplex.PREFIX}") # Set either k-slow or release cutoff. if args.k_slow: if args.release_cutoff is not None: args.release_cutoff = None logger.warning('Release-cutoff overwritten by k-slow!') if args.release_cutoff_1_1 != args.release_cutoff_1_2: logger.warning('Release-cutoff (1,1) overwritten by k-slow!') logger.warning('Release-cutoff (1,2) overwritten by k-slow!') rc, k_rc = 0, None while True: rc += 1 k_rc = opening_rate(rc) if k_rc < args.k_slow: break enum.release_cutoff = rc enum.k_slow = args.k_slow logger.info('Rate-dependent enumeration: k-slow = {}'.format( enum.k_slow)) logger.info(' - corresponding release-cutoff: {} < L < {}'.format( rc - 1, rc)) else: if args.release_cutoff is not None: enum.release_cutoff = args.release_cutoff logger.info( 'Rate-independent enumeration: release cutoff L = {}'.format( enum.release_cutoff)) logger.info(' - corresponding k-slow: {}'.format( opening_rate(enum.release_cutoff))) else: logger.info("Rate-independent enumeration:") enum.release_cutoff_1_1 = args.release_cutoff_1_1 logger.info( " - release cutoff for reaction arity (1,1) = {}".format( enum.release_cutoff_1_1)) enum.release_cutoff_1_2 = args.release_cutoff_1_2 logger.info( " - release cutoff for reaction arity (1,2) = {}".format( enum.release_cutoff_1_2)) if args.k_fast: enum.k_fast = args.k_fast logger.info('Rate-dependent enumeration: k-fast = {}'.format( enum.k_fast)) # DEBUGGING enum.DFS = not args.bfs enum.interactive = args.interactive enum.interruptible = args.interruptible # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # Run reaction enumeration (or not) # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # logger.info("") if args.profile: try: import statprof except ImportError as err: logger.warning( "Python-module statprof not found (pip install statprof-smarkets). Peppercorn profiling disabled." ) args.profile = False if args.dry_run: logger.info("Dry run (not enumerating any reactions)... ") enum.dry_run() logger.info("Done.") else: logger.info("Enumerating reactions...") if args.interactive: logger.info("Interactive mode enabled: Fast and slow reactions " + \ "will be printed for each complex as enumerated." + \ "Press ^C at any time to terminate and write accumulated" + \ "complexes to output.") if args.profile: statprof.start() try: enum.enumerate() finally: statprof.stop() statprof.display() else: enum.enumerate() logger.info("Done.") # ~~~~~~~~~~~~~~~~~~~ # # Handle condensation # # ~~~~~~~~~~~~~~~~~~~ # condensed = args.condensed detailed = (not args.condensed or args.detailed) if condensed: logger.info("Output will be condensed to remove transient complexes.") if args.profile: statprof.start() try: enum.condense() finally: statprof.stop() statprof.display() else: enum.condense() if args.sbml: if detailed and condensed: logger.error("SBML output can be detailed OR condensed, not both.") enum.to_sbml(args.sbml, condensed=condensed) output = enum.to_pil(args.output_filename, detailed=detailed, condensed=condensed, molarity=args.concentration_unit, time=args.time_unit) if output is not None: print(output, end='')
def stop_profiling(): statprof.stop() statprof.display()
def generateOrders(): # pylint: disable=invalid-name """Called once per turn to tell the Python AI to generate and issue orders to control its empire. at end of this function, fo.doneTurn() should be called to indicate to the client that orders are finished and can be sent to the server for processing.""" try: rules = fo.getGameRules() debug("Defined game rules:") for rule_name, rule_value in rules.getRulesAsStrings().items(): debug("%s: %s", rule_name, rule_value) debug("Rule RULE_NUM_COMBAT_ROUNDS value: " + str(rules.getInt("RULE_NUM_COMBAT_ROUNDS"))) except Exception as e: error("Exception %s when trying to get game rules" % e, exc_info=True) empire = fo.getEmpire() if empire is None: fatal("This client has no empire. Doing nothing to generate orders.") try: # early abort if no empire. no need to do meter calculations # on last-seen gamestate if nothing can be ordered anyway... # # note that doneTurn() is issued on behalf of the client network # id, not the empire id, so not having a correct empire id does # not invalidate doneTurn() fo.doneTurn() except Exception as e: error("Exception %s in doneTurn() on non-existent empire" % e, exc_info=True) return if empire.eliminated: debug("This empire has been eliminated. Aborting order generation") try: # early abort if already eliminated. no need to do meter calculations # on last-seen gamestate if nothing can be ordered anyway... fo.doneTurn() except Exception as e: error("Exception %s while trying doneTurn() on eliminated empire" % e, exc_info=True) return # This code block is required for correct AI work. info("Meter / Resource Pool updating...") fo.initMeterEstimatesDiscrepancies() fo.updateMeterEstimates(False) fo.updateResourcePools() turn = fo.currentTurn() aistate = get_aistate() turn_uid = aistate.set_turn_uid() debug("\n\n\n" + "=" * 20) debug("Starting turn %s (%s) of game: %s" % (turn, turn_uid, aistate.uid)) debug("=" * 20 + "\n") turn_timer.start("AI planning") # set the random seed (based on galaxy seed, empire name and current turn) # for game-reload consistency. random_seed = str(fo.getGalaxySetupData().seed) + "%05d%s" % (turn, fo.getEmpire().name) random.seed(random_seed) universe = fo.getUniverse() empire = fo.getEmpire() planet_id = PlanetUtilsAI.get_capital() planet = None if planet_id is not None: planet = universe.getPlanet(planet_id) aggression_name = get_trait_name_aggression(aistate.character) debug("***************************************************************************") debug("******* Log info for AI progress chart script. Do not modify. **********") debug("Generating Orders") debug("EmpireID: {empire.empireID}" " Name: {empire.name}_{empire.empireID}_pid:{p_id}_{p_name}RIdx_{res_idx}_{aggression}" " Turn: {turn}".format(empire=empire, p_id=fo.playerID(), p_name=fo.playerName(), res_idx=ResearchAI.get_research_index(), turn=turn, aggression=aggression_name.capitalize())) debug("EmpireColors: {0.colour.r} {0.colour.g} {0.colour.b} {0.colour.a}".format(empire)) if planet: debug("CapitalID: " + str(planet_id) + " Name: " + planet.name + " Species: " + planet.speciesName) else: debug("CapitalID: None Currently Name: None Species: None ") debug("***************************************************************************") debug("***************************************************************************") # When loading a savegame, the AI will already have issued orders for this turn. # To avoid duplicate orders, generally try not to replay turns. However, for debugging # purposes it is often useful to replay the turn and observe varying results after # code changes. Set the replay_after_load flag in the AI config to let the AI issue # new orders after a game load. Note that the orders from the original savegame are # still being issued and the AIstate was saved after those orders were issued. # TODO: Consider adding an option to clear AI orders after load (must save AIstate at turn start then) if fo.currentTurn() == aistate.last_turn_played: info("The AIstate indicates that this turn was already played.") if not check_bool(get_option_dict().get('replay_turn_after_load', 'False')): info("Aborting new order generation. Orders from savegame will still be issued.") try: fo.doneTurn() except Exception as e: error("Exception %s while trying doneTurn()" % e, exc_info=True) return else: info("Issuing new orders anyway.") if turn == 1: human_player = fo.empirePlayerID(1) greet = diplomatic_corp.get_first_turn_greet_message() fo.sendChatMessage(human_player, '%s (%s): [[%s]]' % (empire.name, get_trait_name_aggression(aistate.character), greet)) aistate.prepare_for_new_turn() turn_state.state.update() debug("Calling AI Modules") # call AI modules action_list = [ColonisationAI.survey_universe, ProductionAI.find_best_designs_this_turn, PriorityAI.calculate_priorities, ExplorationAI.assign_scouts_to_explore_systems, ColonisationAI.assign_colony_fleets_to_colonise, InvasionAI.assign_invasion_fleets_to_invade, MilitaryAI.assign_military_fleets_to_systems, FleetUtilsAI.generate_fleet_orders_for_fleet_missions, FleetUtilsAI.issue_fleet_orders_for_fleet_missions, ResearchAI.generate_research_orders, ProductionAI.generate_production_orders, ResourcesAI.generate_resources_orders, ] for action in action_list: try: main_timer.start(action.__name__) action() main_timer.stop() except Exception as e: error("Exception %s while trying to %s" % (e, action.__name__), exc_info=True) main_timer.stop_print_and_clear() turn_timer.stop_print_and_clear() debug('Size of issued orders: ' + str(fo.getOrders().size)) turn_timer.start("Server_Processing") try: fo.doneTurn() except Exception as e: error("Exception %s while trying doneTurn()" % e, exc_info=True) # TODO move it to cycle above finally: aistate.last_turn_played = fo.currentTurn() if using_statprof: try: statprof.stop() statprof.display() statprof.start() except: pass
def train(config, level_name=None, timestamp=None, time_budget=None, verbose_logging=None, debug=None, profile=None): """ Trains a given YAML file. Parameters ---------- config : str A YAML configuration file specifying the training procedure. level_name : bool, optional Display the log level (e.g. DEBUG, INFO) for each logged message. timestamp : bool, optional Display human-readable timestamps for each logged message. time_budget : int, optional Time budget in seconds. Stop training at the end of an epoch if more than this number of seconds has elapsed. verbose_logging : bool, optional Display timestamp, log level and source logger for every logged message (implies timestamp and level_name are True). debug : bool, optional Display any DEBUG-level log messages, False by default. """ train_obj = serial.load_train_file(config) try: iter(train_obj) iterable = True except TypeError: iterable = False # Undo our custom logging setup. restore_defaults() # Set up the root logger with a custom handler that logs stdout for INFO # and DEBUG and stderr for WARNING, ERROR, CRITICAL. root_logger = logging.getLogger() if verbose_logging: formatter = logging.Formatter(fmt="%(asctime)s %(name)s %(levelname)s " "%(message)s") handler = CustomStreamHandler(formatter=formatter) else: if timestamp: prefix = '%(asctime)s ' else: prefix = '' formatter = CustomFormatter(prefix=prefix, only_from='pylearn2') handler = CustomStreamHandler(formatter=formatter) root_logger.addHandler(handler) # Set the root logger level. if debug: root_logger.setLevel(logging.DEBUG) else: root_logger.setLevel(logging.INFO) if profile: import statprof statprof.start() if iterable: for number, subobj in enumerate(iter(train_obj)): # Publish a variable indicating the training phase. phase_variable = 'PYLEARN2_TRAIN_PHASE' phase_value = 'phase%d' % (number + 1) os.environ[phase_variable] = phase_value # Execute this training phase. subobj.main_loop(time_budget=time_budget) # Clean up, in case there's a lot of memory used that's # necessary for the next phase. del subobj gc.collect() else: train_obj.main_loop(time_budget=time_budget) if profile: statprof.stop() with open('prof.out','w') as f: statprof.display(f) statprof.display() print 'note: profile results were also saved to prof.out'
def filter_statprof(statprof): # only report the profiling for the Root node.. that's almost always what is needed. if mpi_info.is_I_root == False: return if platform.system().lower() != 'windows': if (statprof == None): PRINT( "Note: Profiling disabled. Unable to load the 'statprof' Python module. Install 'statprof'." ) return else: PRINT( "Note: Profiling disabled when running under Windows because 'statprof' does not work." ) return pass PRINT("Final Profiling Info") PRINT("-----------------------------------") PRINT("\t Include filters:") filtersInclude = None if filtersForFiltersInclude != None and len(filtersForFiltersInclude) > 0: filtersInclude = [] for filter in filtersForFiltersInclude: PRINT("\t\t %s" % (str(filter), )) filtersInclude.append(str(filter)) else: PRINT("\t\t None") PRINT("\t Exclude filters:") filtersExclude = None if filtersForFiltersExclude != None and len(filtersForFiltersExclude) > 0: filtersExclude = [] for filter in filtersForFiltersExclude: PRINT("\t\t %s" % (str(filter), )) filtersExclude.append(str(filter)) else: PRINT("\t\t None") PRINT("") PRINT("") outp = open('profile_data.txt', 'w') statprof.display(fp=outp) outp.close() inp = open('profile_data.txt', 'r') lines = inp.readlines() inp.close() error_lines = [] profile_info = [] line_number = -1 for line in lines: line_number += 1 # filter (include filter) if filtersInclude != None: found = False for name in filtersInclude: if line.find(name) > -1: found = True break if found == False: continue pass # filter (exclude filter) if filtersExclude != None: found = False for name in filtersExclude: if line.find(name) != -1: found = True if found == True: continue pass if line_number > 1: try: numbers = re.findall( r"([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)", line) percent_time = float(numbers[0][0]) cum_seconds = float(numbers[1][0]) self_seconds = float(numbers[2][0]) text = re.search(r"([a-zA-Z][\.\w\:]+)", line) info = text.group() #sort(key=lambda tup: tup[1]) profile_info.append( (percent_time, cum_seconds, self_seconds, info)) except: error_lines.append(line) profile_info.sort(key=lambda sme: sme[0]) for line in profile_info: PRINT("%f \t %f \t %f \t %s" % (line[0], line[1], line[2], line[3])) if len(error_lines) > 0: PRINT("---------------------") PRINT("Error lines!") PRINT("") for line in error_lines: PRINT("\t %s" % line) PRINT("") PRINT( "Raw output in file 'profile_data.txt' and CSV data in 'profile.csv'") PRINT("") outp = open('profile_data.csv', 'w') for line in profile_info: outp.write("%f,%f,%f,%s\n" % (line[0], line[1], line[2], line[3])) outp.close()
def generateOrders(): # pylint: disable=invalid-name """Called once per turn to tell the Python AI to generate and issue orders to control its empire. at end of this function, fo.doneTurn() should be called to indicate to the client that orders are finished and can be sent to the server for processing.""" empire = fo.getEmpire() if empire.eliminated: print "This empire has been eliminated. Aborting order generation" try: # early abort if already eliminated. no need to do meter calculations # on last-seen gamestate if nothing can be ordered anyway... fo.doneTurn() except Exception as e: print_error(e) return # This code block is required for correct AI work. print "Meter / Resource Pool updating..." fo.initMeterEstimatesDiscrepancies() fo.updateMeterEstimates(0) fo.updateResourcePools() turn = fo.currentTurn() turn_uid = foAIstate.set_turn_uid() print "Start turn %s (%s) of game: %s" % (turn, turn_uid, foAIstate.uid) turn_timer.start("AI planning") # set the random seed (based on galaxy seed, empire name and current turn) # for game-reload consistency. random_seed = str(fo.getGalaxySetupData().seed) + "%05d%s" % (turn, fo.getEmpire().name) random.seed(random_seed) aggression_name = fo.aggression.values[foAIstate.aggression].name if turn == 1: declare_war_on_all() human_player = fo.empirePlayerID(1) fo.sendChatMessage(human_player, '%s Empire (%s):\n"Ave, Human, morituri te salutant!"' % (empire.name, aggression_name)) # turn cleanup !!! this was formerly done at start of every turn -- not sure why foAIstate.split_new_fleets() foAIstate.refresh() # checks exploration border & clears roles/missions of missing fleets & updates fleet locs & threats foAIstate.report_system_threats() print("Calling AI Modules") # call AI modules action_list = [ColonisationAI.survey_universe, ProductionAI.find_best_designs_this_turn, PriorityAI.calculate_priorities, ExplorationAI.assign_scouts_to_explore_systems, ColonisationAI.assign_colony_fleets_to_colonise, InvasionAI.assign_invasion_fleets_to_invade, MilitaryAI.assign_military_fleets_to_systems, FleetUtilsAI.generate_fleet_orders_for_fleet_missions, FleetUtilsAI.issue_fleet_orders_for_fleet_missions, ResearchAI.generate_research_orders, ProductionAI.generateProductionOrders, ResourcesAI.generate_resources_orders, foAIstate.after_turn_cleanup, ] for action in action_list: try: main_timer.start(action.__name__) action() main_timer.stop() except Exception as e: print_error(e, location=action.__name__) main_timer.end() turn_timer.end() turn_timer.start("Server_Processing") try: fo.doneTurn() except Exception as e: print_error(e) # TODO move it to cycle above if using_statprof: try: statprof.stop() statprof.display() statprof.start() except: pass
def main(): """ Test if an implementation CRN is a correct implementation of a formal CRN. An example from Robert's CRN bisimulation code: fCRN = "a -> b" icrn = "a1 -> b1; x -> a1; x -> b1; y -> b1; y -> a1; x -> a0; a0 -> a1" """ parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) add_commandline_arguments(parser) args = parser.parse_args() logger = logging.getLogger('crnverifier') logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() formatter = logging.Formatter('%(levelname)s %(filename)s: %(message)s') ch.setFormatter(formatter) if args.verbose == 0: ch.setLevel(logging.WARNING) elif args.verbose == 1: ch.setLevel(logging.INFO) elif args.verbose == 2: ch.setLevel(logging.DEBUG) elif args.verbose >= 3: ch.setLevel(logging.NOTSET) logger.addHandler(ch) if args.profile: try: import statprof statprof.start() except ImportError as err: log.warning( 'Cannot import statprof module. (pip install statprof-smarkets.)' ) args.profile = False if args.method in ('crn-bisimulation', 'integrated-hybrid', 'compositional-hybrid'): # TODO: Interactive mode asks for input if fcrn isn't given, etc. assert args.formal_crn is not None assert args.implementation_crn is not None fcrn, icrn, fs, inter = get_bisimulation_inputs( args.formal_crn, args.implementation_crn, args.interpretation, args.constant_species) if args.method == 'crn-bisimulation': v = limit_runtime(args.verify_timeout, crn_bisimulation_test, fcrn, icrn, fs, interpretation=inter, permissive=args.permissive_check) elif args.method == 'compositional-hybrid': v = limit_runtime(args.verify_timeout, compositional_hybrid_test, fcrn, icrn, fs, inter, not args.non_modular) else: assert args.method == 'integrated-hybrid' v = limit_runtime(args.verify_timeout, integrated_hybrid_test, fcrn, icrn, fs, inter, not args.non_modular) # Unpack the verification results if it wasn't a timeout. (v, i) = (None, None) if v is None else v if v: log.info('Returned interpretation:\n ' + \ '\n '.join(f"{k} -> {' + '.join(v)}" for k, v in natural_sort(i.items()))) log.info('Interpreted CRN:\n {}'.format('\n '.join( pretty_crn(clean_crn(icrn, inter=i))))) print_bisimulation_outputs(v, i, args.method, args.verify_timeout) elif args.method in ('modular-crn-bisimulation'): # TODO: Interactive mode asks for input if fcrn isn't given, etc. assert args.formal_crn is not None assert args.implementation_crn is not None fcrns, icrns, fs, inter = get_modular_bisimulation_inputs( args.formal_crn, args.implementation_crn, args.interpretation, args.constant_species) v = limit_runtime(args.verify_timeout, modular_crn_bisimulation_test, fcrns, icrns, fs, interpretation=inter, permissive=args.permissive_check) # Unpack the verification results if it wasn't a timeout. (v, i) = (None, None) if v is None else v if v: log.info("Returned interpretation:\n " + \ '\n '.join(f"{k} -> {' + '.join(v)}" for k, v in natural_sort(i.items()))) for e, icrn in enumerate(icrns, 1): log.info('Interpreted CRN module {}:\n {}'.format( e, '\n '.join(pretty_crn(clean_crn(icrn, inter=i))))) print_bisimulation_outputs(v, i, args.method, args.verify_timeout) elif args.method in ('formal-basis', 'pathway-decomposition'): # TODO: Interactive mode asks for input if fcrn isn't given, etc. if args.method == 'pathway-decomposition': assert len(args.crn_files) > 1 assert len(args.formal_species) crns, fs = get_pathway_decomposition_inputs(args.crn_files, args.formal_species, args.constant_species) if args.method == 'pathway-decomposition': v = limit_runtime(args.verify_timeout, pathway_decomposition_eq, crns, fs, not args.non_modular) if v is True: print( f"Verification result for {args.method} = {v}. The CRNs are equivalent." ) elif v is False: print( f"Verification result for {args.method} = {v}. The CRNs are not equivalent." ) elif v is None: print( f"No verification result for {args.method}.", f"Verification did not terminate within {args.verify_timeout} seconds." ) elif args.method == 'formal-basis': for e, crn in enumerate(crns, 1): try: v = limit_runtime(args.verify_timeout, get_formal_basis, crn, fs, not args.non_modular) if v is None: print('Timeout, no formal basis found.') else: fbasis, _ = v print('Formal basis {}:\n {}'.format( e, "\n ".join(pretty_crn(fbasis)))) except NoFormalBasisError as err: print("Could not find formal basis {}: {}".format(e, err)) else: raise NotImplementedError(args.method) if args.profile: statprof.stop() statprof.display()
def generateOrders(): # pylint: disable=invalid-name """Called once per turn to tell the Python AI to generate and issue orders to control its empire. at end of this function, fo.doneTurn() should be called to indicate to the client that orders are finished and can be sent to the server for processing.""" turn = fo.currentTurn() turn_uid = foAIstate.set_turn_uid() print "Start turn %s (%s) of game: %s" % (turn, turn_uid, foAIstate.uid) turn_timer.start("AI planning") universe = fo.getUniverse() empire = fo.getEmpire() planet_id = PlanetUtilsAI.get_capital() # set the random seed (based on galaxy seed, empire ID and current turn) # for game-reload consistency random_seed = str(fo.getGalaxySetupData().seed) + "%03d%05d" % (fo.empireID(), turn) random.seed(random_seed) planet = None if planet_id is not None: planet = universe.getPlanet(planet_id) aggression_name = fo.aggression.values[foAIstate.aggression].name print "***************************************************************************" print "********** String for chart. Do not modify. ***************************" print ("Generating Orders") print ("EmpireID: {empire.empireID}" " Name: {empire.name}_{empire.empireID}_pid:{p_id}_{p_name}RIdx_{res_idx}_{aggression}" " Turn: {turn}").format(empire=empire, p_id=fo.playerID(), p_name=fo.playerName(), res_idx=ResearchAI.get_research_index(), turn=turn, aggression=aggression_name.capitalize()) print "EmpireColors: {0.colour.r} {0.colour.g} {0.colour.b} {0.colour.a}".format(empire) if planet: print "CapitalID: " + str(planet_id) + " Name: " + planet.name + " Species: " + planet.speciesName else: print "CapitalID: None Currently Name: None Species: None " print "***************************************************************************" print "***************************************************************************" if turn == 1: declare_war_on_all() human_player = fo.empirePlayerID(1) fo.sendChatMessage(human_player, '%s Empire (%s):\n"Ave, Human, morituri te salutant!"' % (empire.name, aggression_name)) # turn cleanup !!! this was formerly done at start of every turn -- not sure why foAIstate.split_new_fleets() foAIstate.refresh() # checks exploration border & clears roles/missions of missing fleets & updates fleet locs & threats foAIstate.report_system_threats() # ...missions # ...demands/priorities print("Calling AI Modules") # call AI modules action_list = [ColonisationAI.survey_universe, ProductionAI.find_best_designs_this_turn, PriorityAI.calculate_priorities, ExplorationAI.assign_scouts_to_explore_systems, ColonisationAI.assign_colony_fleets_to_colonise, InvasionAI.assign_invasion_fleets_to_invade, MilitaryAI.assign_military_fleets_to_systems, FleetUtilsAI.generate_fleet_orders_for_fleet_missions, FleetUtilsAI.issue_fleet_orders_for_fleet_missions, ResearchAI.generate_research_orders, ProductionAI.generateProductionOrders, ResourcesAI.generate_resources_orders, foAIstate.after_turn_cleanup, ] for action in action_list: try: main_timer.start(action.__name__) action() main_timer.stop() except Exception as e: print_error(e, location=action.__name__) main_timer.end() turn_timer.end() turn_timer.start("Server_Processing") try: fo.doneTurn() except Exception as e: print_error(e) # TODO move it to cycle above if using_statprof: try: statprof.stop() statprof.display() statprof.start() except: pass