def index(): """ main function - outputs in following format BEFORE consolidation (which is TODO) # filename, word, linenumbers # refAction.csv, ActionTypeName, 1 # refAction.csv, PhysicalType, 1 # goals.csv, Cleanliness, 11 """ lg = mod_log.Log(mod_cfg.fldrs['localPath']) lg.record_command('Starting indexing', 'index.py') # sys.modules[self.__module__].__file__) if silent == 'N': print('------------------') print('Rebuilding Indexes') print('------------------') ndxFile = ndxPath + '\\ndxFull.txt' opIndex = ndxPath + '\\ndxWordsToFiles.txt' with open(ndxFile, "w") as ndx: ndx.write('filename, word, linenumbers\n') files_to_index = mod_fl.FileList([mod_cfg.fldrs['public_data_path'] + os.sep + 'core'], ['*.csv'], ["__pycache__", ".git"], "files_to_index_filelist.csv") for f in files_to_index.get_list(): buildIndex(f, ndxFile, silent) # now build the one big index file consolidate(ndxFile, opIndex ) lg.record_command('Finished indexing', 'index.py') #, fle.GetModuleName()) if silent == 'N': print('Done')
def __init__(self, name, fldr): """ First setup aikif logging then initialise pyaixi """ mod_agt.Agent.__init__(self, name, fldr) self.lg = mod_log.Log(fldr) """ # options copied from - https://github.com/gkassel/pyaixi/blob/release-1.1.0/aixi.py self.default_options = {} self.default_options["agent"] = "mc_aixi_ctw" self.default_options["agent-horizon"] = 5 self.default_options["ct-depth"] = 30 self.default_options["compare"] = "" self.default_options["environment"] = "coin_flip" self.default_options["exploration"] = 0.0 # Do not explore. self.default_options["explore-decay"] = 1.0 # Exploration rate does not decay. self.default_options["learning-period"] = 0 # Learn forever. self.default_options["mc-simulations"] = 300 self.default_options["non-learning-only"] = False # Whether to record statistics gathered in the non-learning period only. self.default_options["profile"] = False # Whether to profile code. self.default_options["terminate-age"] = 0 # Never die. self.default_options["verbose"] = False """ self.start()
def __init__(self, nme, environ, target, start): self.nme = nme self.environ = environ self.start = start self.target = target self.method = 'No method defined' self.lg = mod_log.Log(mod_cfg.fldrs['log_folder']) #'T:\\user\\AIKIF') self.lg.record_command('CLS_PLAN_SEARCH - starting Plan', nme)
def __init__(self, fldr=None, lst=None): self.fldr = fldr self.lg = mod_log.Log(mod_cfg.fldrs['log_folder']) if lst is None: self.lstTools = [] else: self.lstTools = lst self.lg.record_command('Toolbox') self.lg.record_source(fldr)
def __init__(self, name, fldr): self.name = name self.fldr = fldr self.lstPrograms = [] self.log_folder = mod_cfg.fldrs['log_folder'] self.lg = mod_log.Log(self.log_folder) self.lg.record_command( 'program', 'generating program list in - ' + self.log_folder) self.list_all_python_programs()
def main(): """ Example to show AIKIF logging of results. Generates a sequence of random grids and runs the Game of Life, saving results """ iterations = 10 # how many simulations to run years = 100 # how many times to run each simulation width = 22 # grid height height = 78 # grid width time_delay = 0.03 # delay when printing on screen lg = mod_log.Log('test') lg.record_process('Game of Life', 'game_of_life_console.py') for i in range(iterations): s,e = run_game_of_life(years, width, height, time_delay, 'N') lg.log_result("Started with " + str(s) + " cells and ended with " + str(e) + " cells")
def run(scriptFile, logUsage='Y'): from subprocess import call print(' ... running ' + scriptFile) lg = mod_log.Log(os.getcwd()) lg.record_process(scriptFile) try: retcode = call(scriptFile + ' Q', shell=True) # Q tells program to run in silent mode if retcode < 0: if logUsage=='Y': lg.record_result(scriptFile + ' terminated by signal') else: if logUsage=='Y': lg.record_result(scriptFile + ' success') except OSError as e: #print("Execution failed:", e, file=sys.stderr) lg.record_result(scriptFile + ' error' + str(e))
def do_your_job(self, *arg): """ the goal of the email agent is to parse emails and index """ lg = mod_log.Log(self.log_folder) lg.record_command('email_collect.txt', 'email agent - checking folder - ' + self.root_folder)
# game_of_life_console.py import os import sys import time import random cur_folder = os.path.dirname(os.path.abspath(__file__)) lib_folder = os.path.abspath(cur_folder + os.sep + ".." + os.sep + "toolbox" ) aikif_folder = os.path.abspath(cur_folder + os.sep + ".." ) import aikif.toolbox.cls_grid_life as mod_grid import aikif.cls_log as mod_log lg = mod_log.Log('') lg.record_process("Running Game of Life Console...") os.system('cls' if os.name == 'nt' else 'clear') def main(): """ Example to show AIKIF logging of results. Generates a sequence of random grids and runs the Game of Life, saving results """ iterations = 10 # how many simulations to run years = 100 # how many times to run each simulation width = 22 # grid height height = 78 # grid width time_delay = 0.03 # delay when printing on screen lg = mod_log.Log('test') lg.record_process('Game of Life', 'game_of_life_console.py')
import os import sys import random """ Steps to run and Log an external AI program """ # 1. Setup AIKIF for logging root_folder = os.path.abspath( os.path.dirname(os.path.abspath(__file__)) + os.sep + ".." + os.sep + "..") #sys.path.append(root_folder) import aikif.agents.agent as mod_agt import aikif.cls_log as mod_log import aikif.config as mod_cfg lg = mod_log.Log(root_folder) # 2. Record details your_param1 = 999.999 your_param2 = 'test17' your_param3 = [12, 54, 23, 65, 987] lg.record_command('Initialise Dummy Learning algorithm', 'dummy_learn_1.py') lg.record_source('PARAM: your_param1 = ' + str(your_param1), 'dummy_learn_1.py') lg.record_source('PARAM: your_param2 = ' + your_param2, 'dummy_learn_1.py') lg.record_source('PARAM: your_param3 = ' + str(your_param3), 'dummy_learn_1.py') # 3. Import and run your algorithm import dummy_learn_1 as your_ai result = your_ai.main(your_param1, your_param2, your_param3)
def main(): """ Example of logging the success of a Brute force using AIKIF. This uses http://en.wikipedia.org/wiki/Fermat's_Last_Theorem which was proven in 1994-1995 so you will not get a success result with this program unless you set the power to 2 (the point of the program is not to solve it, but to show how to log a complex running algorithm) Steps are: 1. Define a name for the project (MATHS_FERMAT) which then becomes the log folder as subfolder of your private logs. 2. do the imports and instantiate the Log class 3. call record_command to show how often you run this example 4. call record_source to show the parameters chosen for each run 5. call record_result when you solve it (ha!), or it fails TODO - polish the log details to make easier aggregation - Add logSummary method to show percent success based on param ranges """ proj = 'MATHS_FERMAT' log_folder = mod_cfg.fldrs['log_folder'] + os.sep + proj mod_log.ensure_dir(log_folder) mylog = mod_log.Log(log_folder) mode = 'QUICK' mode = 'AVERAGE' #mode = 'HARD' # assumes you have infinite time and CPU resources #mode = 'TEST_FAILURE_NOT_FERMAT' start_x = randint(1, 9999999999999999999999999999999999) start_y = randint(1, 9999999999999999999999999999999999) start_z = randint(1, 9999999999999999999999999999999999) start_n = 3 end_n = 3 if mode == 'QUICK': steps_x = 50 steps_y = 30 steps_z = 10 start_n = 3 end_n = 15 elif mode == 'AVERAGE': steps_x = 100 steps_y = 100 steps_z = 20 start_n = 3 end_n = 22 elif mode == 'HARD': steps_x = 87999 steps_y = 999 steps_z = 37 start_n = 3 end_n = 959 elif mode == 'TEST_FAILURE_NOT_FERMAT': # test mode power 2 (to test 'True' case though not Fermat test) start_x = 2 start_y = 2 start_z = 2 start_n = 2 end_n = 3 steps_x = 10 steps_y = 10 steps_z = 10 mylog.record_command( proj, ' starting brute force program in mode ' + mode + ' - estim runtime = ' + mylog.estimate_complexity(steps_x, steps_y, steps_z, end_n - start_n)) mylog.record_source( proj, mode + ' Start Params. steps: x=' + str(steps_x) + 'y=' + str(steps_y) + ',z=' + str(steps_z) + '. Start vals =' + format_vars_as_string(start_x, start_y, start_z, start_n)) fame_and_fortune = False tot_calcs = 0 for n in range(start_n, end_n): print('n = ', n) for i in range(start_z, (start_z + steps_z)): print('testing n=', n, ' z=', i, ' (total calcs = ', tot_calcs, ')') for j in range(start_y, (start_y + steps_y)): for k in range(start_x, (start_x + steps_x)): tot_calcs += 1 if not fermat_test(k, j, i, n): fame_and_fortune = True mylog.record_result( proj, 'FOUND MATCH = ' + format_vars_detailed(k, j, i, n)) if fame_and_fortune: print('Found match!') else: print('No matches found - total calculations = ' + str(tot_calcs)) mylog.record_result( proj, 'FAILED with params: ' + format_vars_as_string(k, j, i, n) + ' after ' + str(tot_calcs) + ' calculations') sum = mod_log.LogSummary(mylog, log_folder) sum.summarise_events()
def setUp(self): """ Note, this gets called for EACH test """ unittest.TestCase.setUp(self) self.mylog = mod_log.Log(test_fldr)