def setUp(self): self.config = { "max_population": 10, "tree_generation": { "method": "FULL_METHOD", "initial_max_depth": 4 }, "evaluator": { "use_cache": True }, "selection": { "method": "TOURNAMENT_SELECTION", "tournament_size": 2 }, "crossover": { "method": "POINT_CROSSOVER", "probability": 0.6 }, "mutation": { "methods": ["POINT_MUTATION"], "probability": 0.8 }, "function_nodes": [{ "type": "FUNCTION", "name": "ADD", "arity": 2 }, { "type": "FUNCTION", "name": "SUB", "arity": 2 }], "terminal_nodes": [ { "type": "CONSTANT", "value": 1.0 }, ], "input_variables": [{ "type": "INPUT", "name": "x" }], "data_file": "tests/data/sine.dat", "response_variables": [{ "name": "y" }], "recorder": { "store_file": "json_store_test.json", "compress": True } } config.load_data(self.config) self.functions = GPFunctionRegistry("SYMBOLIC_REGRESSION") self.generator = TreeGenerator(self.config) self.json_store = JSONStore(self.config) self.json_store.setup_store() self.population = self.generator.init() results = [] cache = {} evaluate(self.population.individuals, self.functions, self.config, results, cache, self.json_store) self.population.sort_individuals() self.selection = Selection(self.config, recorder=self.json_store) self.crossover = TreeCrossover(self.config, recorder=self.json_store) self.mutation = TreeMutation(self.config, recorder=self.json_store)
from playground.gp.tree.mutation import TreeMutation from playground.recorder.json_store import JSONStore # SETTINGS record_exception = False script_path = os.path.dirname(os.path.realpath(sys.argv[0])) # config_fp = os.path.join(script_path, "sine_config.json") # config_fp = os.path.join(script_path, "simple_test_func_5-config.json") config_fp = os.path.join(script_path, sys.argv[1]) if __name__ == "__main__": try: # setup random.seed(10) # seed random so results can be reproduced config = config.load_config(config_fp, script_path) json_store = JSONStore(config) functions = { "ADD": "+", "SUB": "-", "MUL": "*", "DIV": "/", "POW": "**", "SIN": "math.sin", "COS": "math.cos", "RAD": "math.radians", "LN": "math.ln", "EXP": "math.exp", "LOG": "math.log" } generator = TreeGenerator(config)
def gp_benchmark_loop(config): try: # setup random.seed(config["random_seed"]) # VERY IMPORTANT! load_data(config, config["call_path"]) json_store = JSONStore(config) # functions = GPFunctionRegistry("SYMBOLIC_REGRESSION") generator = TreeGenerator(config) # genetic operators selection = Selection(config, recorder=json_store) crossover = TreeCrossover(config, recorder=json_store) mutation = TreeMutation(config, recorder=json_store) # setup the initial random population population = generator.init() # create play details details = play.play_details( population=population, functions=config["functions"], evaluate=evaluate, selection=selection, crossover=crossover, mutation=mutation, editor=edit_trees, stop_func=default_stop_func, # print_func=print_func, config=config, recorder=json_store) # run symbolic regression start_time = time.time() play.play(details) end_time = time.time() time_taken = end_time - start_time # print msg print("DONE -> pop: {0} cross: {1} mut: {2} seed: {3} [{4}s]".format( config["max_population"], config["crossover"]["probability"], config["mutation"]["probability"], config["random_seed"], round(time_taken, 2))) # log on completion if config.get("log_path", False): config.pop("data") msg = { "timestamp": time.mktime(datetime.now().timetuple()), "status": "DONE", "config": config, "runtime": time_taken, "best_score": population.find_best_individuals()[0].score, "best": str(population.find_best_individuals()[0]) } log_path = os.path.expandvars(config["log_path"]) log_file = open(log_path, "a+") log_file.write(json.dumps(msg) + "\n") log_file.close() except Exception as err_msg: import traceback traceback.print_exc() # log exception if config.get("log_path", False): msg = { "timestamp": time.mktime(datetime.now().timetuple()), "status": "ERROR", "config": config, "error": err_msg } log_path = os.path.expandvars(config["log_path"]) log_file = open(log_path, "a+") log_file.write(json.dumps(msg) + "\n") log_file.close() raise # raise the exception return config