Exemple #1
0
def evaluate_trees():
    results = []
    response = {}

    # parse incomming data
    if request.data is not None:
        incomming = json.loads(request.data)
        config = incomming["config"]
        individuals = incomming["individuals"]

        # convert dict to trees
        parser = TreeGenerator(config)
        for individual in list(individuals):
            tree = parser.generate_tree_from_dict(individual)
            individuals.append(tree)
            individuals.remove(individual)

        evaluate(individuals, functions, config, results)

        # jsonify results
        response["results"] = []
        for individual in results:
            result = {
                "id": individual.tree_id,
                "score": individual.score,
            }
            response["results"].append(result)

    else:
        response = {"status": PlayNodeStatus.ERROR}

    return jsonify(response)
Exemple #2
0
    def test_record_evaulation(self):
        # record evaluation
        results = []
        evaluate(self.population.individuals,
                 self.functions,
                 self.config,
                 results,
                 recorder=self.json_store)

        # assert
        record = self.json_store.generation_record
        # import pprint
        # pprint.pprint(record)
        self.assertEquals(record["evaluation"]["cache_size"], 10)
        self.assertEquals(record["evaluation"]["match_cached"], 0)
    def test_record_evaulation(self):
        # record evaluation
        results = []
        evaluate(
            self.population.individuals,
            self.functions,
            self.config,
            results,
            recorder=self.json_store
        )

        # assert
        record = self.json_store.generation_record
        # import pprint
        # pprint.pprint(record)
        self.assertEquals(record["evaluation"]["cache_size"], 10)
        self.assertEquals(record["evaluation"]["match_cached"], 0)
    def test_evaluate(self):
        population = self.generator.init()
        results = []

        start_time = time.time()
        evaluator.evaluate(
            population.individuals,
            self.functions,
            self.config,
            results
        )
        end_time = time.time()
        print("GP run took: %2.2fsecs\n" % (end_time - start_time))

        population.individuals = results

        # assert
        for individual in population.individuals:
            self.assertTrue(individual.score is not None)
            self.assertTrue(individual.score > 0)
Exemple #5
0
    def test_reproduce(self):
        tests = 1

        for i in range(tests):
            population = self.generator.init()

            res = []
            evaluate(population.individuals, self.functions, self.config, res)
            population.individuals = res

            # print "POPULATION"
            # for i in population.individuals:
            #     print i, i.score
            # print "\n"

            self.selection.select(population)

            # print "SELECTION"
            # for i in population.individuals:
            #     print i, i.score
            # print "\n"

            # reproduce
            play_details = play.play_details(
                population=population,
                selection=self.selection,
                crossover=self.crossover,
                mutation=self.mutation,
                evaluate=None,
                config=self.config
            )
            play.play_ga_reproduce(play_details)

            # print "REPRODUCE"
            # for i in population.individuals:
            #     print i, i.score

            # assert
            max_pop = self.config["max_population"]
            self.assertEquals(len(population.individuals), max_pop)
            self.assertTrue(population.config is self.config)
Exemple #6
0
    def test_reproduce(self):
        tests = 1

        for i in range(tests):
            population = self.generator.init()

            res = []
            evaluate(population.individuals, self.functions, self.config, res)
            population.individuals = res

            # print "POPULATION"
            # for i in population.individuals:
            #     print i, i.score
            # print "\n"

            self.selection.select(population)

            # print "SELECTION"
            # for i in population.individuals:
            #     print i, i.score
            # print "\n"

            # reproduce
            play_details = play.play_details(population=population,
                                             selection=self.selection,
                                             crossover=self.crossover,
                                             mutation=self.mutation,
                                             evaluate=None,
                                             config=self.config)
            play.play_ga_reproduce(play_details)

            # print "REPRODUCE"
            # for i in population.individuals:
            #     print i, i.score

            # assert
            max_pop = self.config["max_population"]
            self.assertEquals(len(population.individuals), max_pop)
            self.assertTrue(population.config is self.config)
Exemple #7
0
        "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 = {
    #     "ADD": "+",
    #     "SUB": "-",
    #     "MUL": "*",
    #     "DIV": "/",
    #     "POW": "**",
    #     "SIN": "math.sin",
    #     "COS": "math.cos",
    #     "RAD": "math.radians",
Exemple #8
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"

    # TREE EVALUTOR 2
    functions = {
        "ADD": "+",
        "SUB": "-",
        "MUL": "*",
        "DIV": "/",
        "POW": "**",
        "SIN": "math.sin",
        "COS": "math.cos",
Exemple #9
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)
Exemple #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)