Пример #1
0
    def setUp(self):
        self.config = {
            "max_population": 50,

            "tree_generation": {
                "method": "FULL_METHOD",
                "initial_max_depth": 4
            },

            "evaluator": {
                "use_cache": True
            },

            "function_nodes": [
                {"type": "FUNCTION", "name": "ADD", "arity": 2},
                {"type": "FUNCTION", "name": "SUB", "arity": 2},
                {"type": "FUNCTION", "name": "MUL", "arity": 2},
                {"type": "FUNCTION", "name": "DIV", "arity": 2},
                {"type": "FUNCTION", "name": "COS", "arity": 1},
                {"type": "FUNCTION", "name": "SIN", "arity": 1}
            ],

            "terminal_nodes": [
                {"type": "CONSTANT", "value": 1.0},
            ],

            "input_variables": [
                {"type": "INPUT", "name": "x"}
            ],

            "data_file": "tests/data/sine.dat",

            "response_variables": [{"name": "y"}]
        }
        config.load_data(self.config)

        self.functions = {
            "ADD": "+",
            "SUB": "-",
            "MUL": "*",
            "DIV": "/",
            "POW": "**",
            "SIN": "math.sin",
            "COS": "math.cos",
            "RAD": "math.radians",
            "LN": "math.ln",
            "LOG": "math.log"
        }
        self.generator = TreeGenerator(self.config)
Пример #2
0
    def setUp(self):
        self.config = {
            "max_population": 50,

            "tree_generation": {
                "method": "FULL_METHOD",
                "initial_max_depth": 4
            },

            "evaluator": {
                "use_cache": True
            },

            "function_nodes": [
                {"type": "FUNCTION", "name": "ADD", "arity": 2},
                {"type": "FUNCTION", "name": "SUB", "arity": 2},
                {"type": "FUNCTION", "name": "MUL", "arity": 2},
                {"type": "FUNCTION", "name": "DIV", "arity": 2},
                {"type": "FUNCTION", "name": "COS", "arity": 1},
                {"type": "FUNCTION", "name": "SIN", "arity": 1}
            ],

            "terminal_nodes": [
                {"type": "CONSTANT", "value": 1.0},
            ],

            "input_variables": [
                {"type": "INPUT", "name": "x"}
            ],

            "data_file": "tests/data/sine.dat",

            "response_variables": [{"name": "y"}]
        }
        config.load_data(self.config)

        self.functions = GPFunctionRegistry("SYMBOLIC_REGRESSION")
        self.generator = TreeGenerator(self.config)
Пример #3
0
            "data_file":
            "data/iris.dat",
            "input_variables": [{
                "name": "sepal_length"
            }, {
                "name": "sepal_width"
            }, {
                "name": "petal_length"
            }, {
                "name": "petal_width"
            }],
            "response_variables": [{
                "name": "species"
            }]
        }
        load_data(config, script_path)
        functions = GPFunctionRegistry("CLASSIFICATION")
        generator = TreeGenerator(config)

        # genetic operators
        selection = Selection(config)
        crossover = TreeCrossover(config)
        mutation = TreeMutation(config)

        # run symbolic regression
        population = generator.init()

        start_time = time.time()
        details = play.play_details(
            population=population,
            evaluate=evaluate,
Пример #4
0
 def test_load_data(self):
     config.load_data(self.config)
     self.config["data"].pop("rows")
     self.assertEquals(self.config["data"], self.solution)
Пример #5
0
    def setUp(self):
        self.config = {
            "cartesian": {
                "rows": 1,
                "columns": 4,
                "levels_back": 4,

                "num_inputs": 12,
                "num_outputs": 1
            },

            "input_variables": [
                {"type": "INPUT", "name": "x"},
                {"type": "CONSTANT", "name": "0.0"},
                {"type": "CONSTANT", "name": "1.0"},
                {"type": "CONSTANT", "name": "2.0"},
                {"type": "CONSTANT", "name": "3.0"},
                {"type": "CONSTANT", "name": "4.0"},
                {"type": "CONSTANT", "name": "5.0"},
                {"type": "CONSTANT", "name": "6.0"},
                {"type": "CONSTANT", "name": "7.0"},
                {"type": "CONSTANT", "name": "8.0"},
                {"type": "CONSTANT", "name": "9.0"},
                {"type": "CONSTANT", "name": "10.0"}
            ],

            "function_nodes": [
                {"type": "FUNCTION", "name": "ADD", "arity": 2},
                {"type": "FUNCTION", "name": "SUB", "arity": 2},
                {"type": "FUNCTION", "name": "MUL", "arity": 2},
                {"type": "FUNCTION", "name": "DIV", "arity": 2},
                {"type": "FUNCTION", "name": "SIN", "arity": 1},
                {"type": "FUNCTION", "name": "COS", "arity": 1},
                {"type": "FUNCTION", "name": "RAD", "arity": 1}
            ],

            "response_variables" : [{"name": "y"}],

            "data_file": "../../data/sine.dat",

        }
        script_path = os.path.dirname(os.path.realpath(sys.argv[0]))
        load_data(self.config, script_path)

        # add constants
        rows = len(self.config["data"]["y"])
        for i in range(11):
            i = float(i)
            self.config["data"][str(i) + ".0"] = [i for j in range(rows)]

        self.functions = [
            functions.add_function,
            functions.sub_function,
            functions.mul_function,
            functions.div_function,
            functions.sin_function,
            functions.cos_function,
            functions.rad_function
        ]

        self.input_nodes = [
            "x",
            "0.0", "1.0", "2.0", "3.0", "4.0", "5.0",
            "6.0", "7.0", "8.0", "9.0", "10.0"
        ]
        self.func_nodes = [
            [2, 11, 11],    # 12
            [6, 0],         # 13
            [2, 12, 13],    # 14
            [4, 14],        # 15
        ]
        self.output_nodes = [15]

        self.cartesian = Cartesian(
            config={},
            rows=1,
            columns=4,
            levels_back=4,
            func_nodes=self.func_nodes,
            input_nodes=self.input_nodes,
            output_nodes=self.output_nodes
        )
Пример #6
0
    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)
    def setUp(self):
        self.config = {
            "max_population": 5,

            "tree_generation": {
                "tree_type": "CLASSIFICATION_TREE",
                "method": "FULL_METHOD",
                "initial_max_depth": 2
            },

            "evaluator": {
                "use_cache": True
            },

            "function_nodes": [
                {
                    "type": "CLASS_FUNCTION",
                    "name": "GREATER_THAN",
                    "arity": 2,

                    "data_range": {
                        "lower_bound": 0.0,
                        "upper_bound": 10.0,
                        "decimal_places": 0,
                    }
                },
                {
                    "type": "CLASS_FUNCTION",
                    "name": "LESS_THAN",
                    "arity": 2,

                    "data_range": {
                        "lower_bound": 0.0,
                        "upper_bound": 10.0,
                        "decimal_places": 0,
                    }
                },
                {
                    "type": "CLASS_FUNCTION",
                    "name": "EQUALS",
                    "arity": 2,
                    "decimal_precision": 2,
                    "data_range": {
                        "lower_bound": 0.0,
                        "upper_bound": 10.0,
                        "decimal_places": 0,
                    }
                }
            ],

            "terminal_nodes": [
                {
                    "type": "RANDOM_CONSTANT",
                    "name": "species",
                    "range": [
                        1.0,
                        2.0,
                        3.0
                    ]
                },
            ],

            "input_variables": [
                {"name": "sepal_length"},
                {"name": "sepal_width"},
                {"name": "petal_length"},
                {"name": "petal_width"}
            ],

            "class_attributes": [
                "sepal_length",
                "sepal_width",
                "petal_length",
                "petal_width"
            ],

            "data_file": "tests/data/iris.dat",

            "response_variables": [{"name": "species"}]
        }
        config.load_data(self.config)

        self.functions = GPFunctionRegistry("CLASSIFICATION")
        self.generator = TreeGenerator(self.config)
        self.population = self.generator.init()
Пример #8
0
    def setUp(self):
        self.config = {
            "max_population":
            5,
            "tree_generation": {
                "tree_type": "CLASSIFICATION_TREE",
                "method": "FULL_METHOD",
                "initial_max_depth": 2
            },
            "evaluator": {
                "use_cache": True
            },
            "function_nodes": [{
                "type": "CLASS_FUNCTION",
                "name": "GREATER_THAN",
                "arity": 2,
                "data_range": {
                    "lower_bound": 0.0,
                    "upper_bound": 10.0,
                    "decimal_places": 0,
                }
            }, {
                "type": "CLASS_FUNCTION",
                "name": "LESS_THAN",
                "arity": 2,
                "data_range": {
                    "lower_bound": 0.0,
                    "upper_bound": 10.0,
                    "decimal_places": 0,
                }
            }, {
                "type": "CLASS_FUNCTION",
                "name": "EQUALS",
                "arity": 2,
                "decimal_precision": 2,
                "data_range": {
                    "lower_bound": 0.0,
                    "upper_bound": 10.0,
                    "decimal_places": 0,
                }
            }],
            "terminal_nodes": [
                {
                    "type": "RANDOM_CONSTANT",
                    "name": "species",
                    "range": [1.0, 2.0, 3.0]
                },
            ],
            "input_variables": [{
                "name": "sepal_length"
            }, {
                "name": "sepal_width"
            }, {
                "name": "petal_length"
            }, {
                "name": "petal_width"
            }],
            "class_attributes":
            ["sepal_length", "sepal_width", "petal_length", "petal_width"],
            "data_file":
            "tests/data/iris.dat",
            "response_variables": [{
                "name": "species"
            }]
        }
        config.load_data(self.config)

        self.functions = GPFunctionRegistry("CLASSIFICATION")
        self.generator = TreeGenerator(self.config)
        self.population = self.generator.init()
Пример #9
0
                "type": "CONSTANT",
                "value": 10.0
            },
        ],
        "input_variables": [{
            "type": "INPUT",
            "name": "var1"
        }],
        "response_variables": [{
            "name": "answer"
        }],
        "data_file":
        "arabas_et_al-f1.dat"
    }
    config["max_population"] = 100000
    load_data(config, data_dir)
    generator = TreeGenerator(config)
    population = generator.init()
    results = []

    # TREE EVALUTOR 1
    start_time = time.time()
    tree_eval_1.evaluate(copy.deepcopy(population.individuals),
                         GPFunctionRegistry("SYMBOLIC_REGRESSION"), config,
                         results)
    end_time = time.time()
    time_taken = end_time - start_time
    print "Evaluator 1 took:", str(round(time_taken, 2)) + "s"

    # TREE EVALUTOR 2
    # functions = {
Пример #10
0
    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)
Пример #11
0
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
Пример #12
0
    def setUp(self):
        random.seed(0)
        self.config = {
            "max_population":
            20,
            "max_generation":
            5,
            "tree_generation": {
                "method": "GROW_METHOD",
                "initial_max_depth": 4
            },
            "evaluator": {
                "use_cache": True
            },
            "selection": {
                "method": "TOURNAMENT_SELECTION",
                "tournament_size": 2
            },
            "crossover": {
                "method": "POINT_CROSSOVER",
                "probability": 0.8
            },
            "mutation": {
                "methods": [
                    "POINT_MUTATION", "HOIST_MUTATION", "SUBTREE_MUTATION",
                    "SHRINK_MUTATION", "EXPAND_MUTATION"
                ],
                "probability":
                1.0
            },
            "function_nodes": [{
                "type": "FUNCTION",
                "name": "ADD",
                "arity": 2
            }, {
                "type": "FUNCTION",
                "name": "SUB",
                "arity": 2
            }, {
                "type": "FUNCTION",
                "name": "MUL",
                "arity": 2
            }, {
                "type": "FUNCTION",
                "name": "DIV",
                "arity": 2
            }, {
                "type": "FUNCTION",
                "name": "COS",
                "arity": 1
            }, {
                "type": "FUNCTION",
                "name": "SIN",
                "arity": 1
            }, {
                "type": "FUNCTION",
                "name": "RAD",
                "arity": 1
            }],
            "terminal_nodes": [{
                "type": "CONSTANT",
                "value": 1.0
            }, {
                "type": "CONSTANT",
                "value": 2.0
            }, {
                "type": "CONSTANT",
                "value": 2.0
            }, {
                "type": "CONSTANT",
                "value": 3.0
            }, {
                "type": "CONSTANT",
                "value": 4.0
            }, {
                "type": "CONSTANT",
                "value": 5.0
            }, {
                "type": "CONSTANT",
                "value": 6.0
            }, {
                "type": "CONSTANT",
                "value": 7.0
            }, {
                "type": "CONSTANT",
                "value": 8.0
            }, {
                "type": "CONSTANT",
                "value": 9.0
            }, {
                "type": "CONSTANT",
                "value": 10.0
            }],
            "input_variables": [{
                "type": "INPUT",
                "name": "x"
            }],
            "data_file":
            "tests/data/sine.dat",
            "response_variables": [{
                "name": "y"
            }]
        }
        config.load_data(self.config)
        self.functions = GPFunctionRegistry("SYMBOLIC_REGRESSION")
        self.generator = TreeGenerator(self.config)

        self.selection = Selection(self.config, recorder=None)
        self.crossover = TreeCrossover(self.config, recorder=None)
        self.mutation = TreeMutation(self.config, recorder=None)
Пример #13
0
    def setUp(self):
        self.config = {
            "max_population":
            50,
            "tree_generation": {
                "method": "FULL_METHOD",
                "initial_max_depth": 4
            },
            "evaluator": {
                "use_cache": True
            },
            "function_nodes": [{
                "type": "FUNCTION",
                "name": "ADD",
                "arity": 2
            }, {
                "type": "FUNCTION",
                "name": "SUB",
                "arity": 2
            }, {
                "type": "FUNCTION",
                "name": "MUL",
                "arity": 2
            }, {
                "type": "FUNCTION",
                "name": "DIV",
                "arity": 2
            }, {
                "type": "FUNCTION",
                "name": "COS",
                "arity": 1
            }, {
                "type": "FUNCTION",
                "name": "SIN",
                "arity": 1
            }],
            "terminal_nodes": [
                {
                    "type": "CONSTANT",
                    "value": 1.0
                },
            ],
            "input_variables": [{
                "type": "INPUT",
                "name": "x"
            }],
            "data_file":
            "tests/data/sine.dat",
            "response_variables": [{
                "name": "y"
            }]
        }
        config.load_data(self.config)

        self.functions = {
            "ADD": "+",
            "SUB": "-",
            "MUL": "*",
            "DIV": "/",
            "POW": "**",
            "SIN": "math.sin",
            "COS": "math.cos",
            "RAD": "math.radians",
            "LN": "math.ln",
            "LOG": "math.log"
        }
        self.generator = TreeGenerator(self.config)
Пример #14
0
    def setUp(self):
        random.seed(0)
        self.config = {
            "max_population" : 20,
            "max_generation" : 5,

            "tree_generation" : {
                "method" : "GROW_METHOD",
                "initial_max_depth" : 4
            },

            "evaluator": {
                "use_cache" : True
            },

            "selection" : {
                "method" : "TOURNAMENT_SELECTION",
                "tournament_size": 2
            },

            "crossover" : {
                "method" : "POINT_CROSSOVER",
                "probability" : 0.8
            },

            "mutation" : {
                "methods": [
                    "POINT_MUTATION",
                    "HOIST_MUTATION",
                    "SUBTREE_MUTATION",
                    "SHRINK_MUTATION",
                    "EXPAND_MUTATION"
                ],
                "probability" : 1.0
            },

            "function_nodes" : [
                {"type": "FUNCTION", "name": "ADD", "arity": 2},
                {"type": "FUNCTION", "name": "SUB", "arity": 2},
                {"type": "FUNCTION", "name": "MUL", "arity": 2},
                {"type": "FUNCTION", "name": "DIV", "arity": 2},
                {"type": "FUNCTION", "name": "COS", "arity": 1},
                {"type": "FUNCTION", "name": "SIN", "arity": 1},
                {"type": "FUNCTION", "name": "RAD", "arity": 1}
            ],

            "terminal_nodes" : [
                {"type": "CONSTANT", "value": 1.0},
                {"type": "CONSTANT", "value": 2.0},
                {"type": "CONSTANT", "value": 2.0},
                {"type": "CONSTANT", "value": 3.0},
                {"type": "CONSTANT", "value": 4.0},
                {"type": "CONSTANT", "value": 5.0},
                {"type": "CONSTANT", "value": 6.0},
                {"type": "CONSTANT", "value": 7.0},
                {"type": "CONSTANT", "value": 8.0},
                {"type": "CONSTANT", "value": 9.0},
                {"type": "CONSTANT", "value": 10.0}
            ],

            "input_variables" : [
                {"type": "INPUT", "name": "x"}
            ],

            "data_file" : "tests/data/sine.dat",

            "response_variables" : [{"name": "y"}]
        }
        config.load_data(self.config)
        self.functions = GPFunctionRegistry("SYMBOLIC_REGRESSION")
        self.generator = TreeGenerator(self.config)

        self.selection = Selection(self.config, recorder=None)
        self.crossover = TreeCrossover(self.config, recorder=None)
        self.mutation = TreeMutation(self.config, recorder=None)
Пример #15
0
            {"type": "CONSTANT", "value": 2.0},
            {"type": "CONSTANT", "value": 3.0},
            {"type": "CONSTANT", "value": 4.0},
            {"type": "CONSTANT", "value": 5.0},
            {"type": "CONSTANT", "value": 6.0},
            {"type": "CONSTANT", "value": 7.0},
            {"type": "CONSTANT", "value": 8.0},
            {"type": "CONSTANT", "value": 9.0},
            {"type": "CONSTANT", "value": 10.0},
        ],
        "input_variables": [{"type": "INPUT", "name": "var1"}],
        "response_variables": [{"name": "answer"}],
        "data_file": "arabas_et_al-f1.dat"
    }
    config["max_population"] = 10000
    load_data(config, data_dir)
    generator = TreeGenerator(config)
    population = generator.init()
    results = []

    # TREE EVALUTOR 1
    start_time = time.time()
    tree_eval_1.evaluate(
        copy.deepcopy(population.individuals),
        GPFunctionRegistry("SYMBOLIC_REGRESSION"),
        config,
        results
    )
    end_time = time.time()
    time_taken = end_time - start_time
    print "Evaluator 1 took:", str(round(time_taken, 2)) + "s"
Пример #16
0
 def test_load_data(self):
     config.load_data(self.config)
     self.config["data"].pop("rows")
     self.assertEquals(self.config["data"], self.solution)
Пример #17
0
    def setUp(self):
        self.config = {
            "cartesian": {
                "rows": 1,
                "columns": 4,
                "levels_back": 4,
                "num_inputs": 12,
                "num_outputs": 1
            },
            "input_variables": [{
                "type": "INPUT",
                "name": "x"
            }, {
                "type": "CONSTANT",
                "name": "0.0"
            }, {
                "type": "CONSTANT",
                "name": "1.0"
            }, {
                "type": "CONSTANT",
                "name": "2.0"
            }, {
                "type": "CONSTANT",
                "name": "3.0"
            }, {
                "type": "CONSTANT",
                "name": "4.0"
            }, {
                "type": "CONSTANT",
                "name": "5.0"
            }, {
                "type": "CONSTANT",
                "name": "6.0"
            }, {
                "type": "CONSTANT",
                "name": "7.0"
            }, {
                "type": "CONSTANT",
                "name": "8.0"
            }, {
                "type": "CONSTANT",
                "name": "9.0"
            }, {
                "type": "CONSTANT",
                "name": "10.0"
            }],
            "function_nodes": [{
                "type": "FUNCTION",
                "name": "ADD",
                "arity": 2
            }, {
                "type": "FUNCTION",
                "name": "SUB",
                "arity": 2
            }, {
                "type": "FUNCTION",
                "name": "MUL",
                "arity": 2
            }, {
                "type": "FUNCTION",
                "name": "DIV",
                "arity": 2
            }, {
                "type": "FUNCTION",
                "name": "SIN",
                "arity": 1
            }, {
                "type": "FUNCTION",
                "name": "COS",
                "arity": 1
            }, {
                "type": "FUNCTION",
                "name": "RAD",
                "arity": 1
            }],
            "response_variables": [{
                "name": "y"
            }],
            "data_file":
            "../../data/sine.dat",
        }
        script_path = os.path.dirname(os.path.realpath(sys.argv[0]))
        load_data(self.config, script_path)

        # add constants
        rows = len(self.config["data"]["y"])
        for i in range(11):
            i = float(i)
            self.config["data"][str(i) + ".0"] = [i for j in range(rows)]

        self.functions = [
            functions.add_function, functions.sub_function,
            functions.mul_function, functions.div_function,
            functions.sin_function, functions.cos_function,
            functions.rad_function
        ]

        self.input_nodes = [
            "x", "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "6.0", "7.0", "8.0",
            "9.0", "10.0"
        ]
        self.func_nodes = [
            [2, 11, 11],  # 12
            [6, 0],  # 13
            [2, 12, 13],  # 14
            [4, 14],  # 15
        ]
        self.output_nodes = [15]

        self.cartesian = Cartesian(config={},
                                   rows=1,
                                   columns=4,
                                   levels_back=4,
                                   func_nodes=self.func_nodes,
                                   input_nodes=self.input_nodes,
                                   output_nodes=self.output_nodes)
Пример #18
0
                "sepal_length",
                "sepal_width",
                "petal_length",
                "petal_width"
            ],

            "data_file": "data/iris.dat",
            "input_variables": [
                {"name": "sepal_length"},
                {"name": "sepal_width"},
                {"name": "petal_length"},
                {"name": "petal_width"}
            ],
            "response_variables": [{"name": "species"}]
        }
        load_data(config, script_path)
        functions = GPFunctionRegistry("CLASSIFICATION")
        generator = TreeGenerator(config)

        # genetic operators
        selection = Selection(config)
        crossover = TreeCrossover(config)
        mutation = TreeMutation(config)

        # run symbolic regression
        population = generator.init()

        start_time = time.time()
        details = play.play_details(
            population=population,
            evaluate=evaluate,
Пример #19
0
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