Exemple #1
0
 def test_summary_logger_can_accept_kwargs(self, tmpdir, simple_chromosomes, simple_evaluation_function):
     log_file = tmpdir.join('log.txt')
     logger = SummaryLogger(target=log_file, stdout=False)
     pop = Population(chromosomes=simple_chromosomes,
                      eval_function=simple_evaluation_function)
     pop.evaluate()
     # lets make a first simple log
     logger.log(pop, foo="bar", buzz="meh")
     with open(log_file, "r") as f:
         read_lines = f.readlines()
         assert len(read_lines) == 1
         first_line = read_lines[0]
         assert "bar" in first_line
         assert "meh" in first_line
         last_entry = first_line.split(",")
         assert len(last_entry) == 6
     # lets log another one
     logger.log(pop, buzz="moh")
     with open(log_file, "r") as f:
         read_lines = f.readlines()
         assert len(read_lines) == 2
         first_line = read_lines[-1]
         assert "moh" in first_line
         last_entry = first_line.split(",")
         assert len(last_entry) == 5
Exemple #2
0
    def test_evaluate_func(self, simple_chromosomes):
        def evaluation_function(x):
            sleep(self.latency)
            return x * x

        pop = Population(simple_chromosomes, eval_function=evaluation_function)
        t0 = time()
        pop.evaluate()
        t1 = time()
        single_proc_time = t1 - t0
        for individual in pop:
            assert evaluation_function(
                individual.chromosome) == individual.fitness
        # with concurrency
        pop = Population(simple_chromosomes,
                         eval_function=evaluation_function,
                         concurrent_workers=self.cpus)
        t0 = time()
        pop.evaluate()
        t1 = time()
        multi_proc_time = t1 - t0
        for individual in pop:
            assert evaluation_function(
                individual.chromosome) == individual.fitness
        if self.cpus > 1:
            assert multi_proc_time < single_proc_time
Exemple #3
0
 def test_documented_best(self):
     pop = Population(chromosomes=[100, 100, 100],
                      eval_function=lambda x: x * 2,
                      maximize=True)
     assert pop.documented_best is None
     pop.evaluate()
     assert pop.documented_best.fitness == pop.current_best.fitness
     pop.mutate(mutate_function=lambda x: x - 10, probability=1).evaluate()
     assert pop.documented_best.fitness - 20 == pop.current_best.fitness
Exemple #4
0
 def test_mutate_resets(self):
     pop = Population(chromosomes=[1, 1, 1],
                      eval_function=float,
                      maximize=True)
     assert pop.current_best is None and pop.current_worst is None
     pop.evaluate()
     assert pop.current_best.fitness == 1 and pop.current_worst.fitness == 1
     pop.mutate(lambda x: x)
     assert pop.current_best is None and pop.current_worst is None
Exemple #5
0
 def test_current_worst(self, simple_chromosomes):
     for maximize, worst in ((False, max(simple_chromosomes)),
                             (True, min(simple_chromosomes))):
         pop = Population(chromosomes=simple_chromosomes,
                          eval_function=float,
                          maximize=maximize)
         assert pop.current_worst is None
         pop.evaluate()
         assert pop.current_worst.chromosome == worst
Exemple #6
0
 def test_summarylogger_can_write_to_stdout(self, capsys, simple_chromosomes, simple_evaluation_function):
     logger = SummaryLogger(target=None, stdout=True)
     pop = Population(chromosomes=range(10),
                      eval_function=lambda x: x)
     pop.evaluate()
     logger.log(pop)
     logger.log(pop)
     read_stdout = [line for line in capsys.readouterr().out.split('\n') if line != '']
     assert len(read_stdout) == 2
Exemple #7
0
    def test_evaluate_func(self, simple_chromosomes):
        def evaluation_function(x):
            return x * x

        pop = Population(simple_chromosomes, eval_function=evaluation_function)
        pop.evaluate()
        for individual in pop:
            assert evaluation_function(
                individual.chromosome) == individual.fitness
Exemple #8
0
 def test_weights(self, simple_chromosomes, simple_evaluation_function):
     for maximize in (False, True):
         pop = Population(chromosomes=simple_chromosomes,
                          eval_function=simple_evaluation_function,
                          maximize=maximize)
         with raises(RuntimeError):
             assert min(pop._individual_weights) >= 0
         pop.evaluate()
         assert max(pop._individual_weights) == 1
         assert min(pop._individual_weights) == 0
         if maximize:
             assert pop._individual_weights[0] == 0
         else:
             assert pop._individual_weights[0] == 1
Exemple #9
0
 def test_baselogger_can_accept_kwargs(self, tmpdir, simple_chromosomes, simple_evaluation_function):
     log_file = tmpdir.join('log.txt')
     logger = BaseLogger(target=log_file, stdout=False)
     pop = Population(chromosomes=simple_chromosomes, eval_function=simple_evaluation_function)
     pop.evaluate()
     # we should see that a file was created with an appropriate number of rows
     logger.log(pop, foo="bar")
     with open(log_file, "r") as f:
         assert len(f.readlines()) == len(simple_chromosomes)
         assert all(["bar" in line for line in f.readlines()])
     # we should see that a file was created with an appropriate number of rows
     logger.log(pop, foo="meh")
     with open(log_file, "r") as f:
         assert len(f.readlines()) == (2 * len(simple_chromosomes))
         assert all(['meh' in line for line in f.readlines()[-10:]])
Exemple #10
0
 def test_baselogger_write_file_no_stdout(self, tmpdir, capsys, simple_chromosomes, simple_evaluation_function):
     log_file = tmpdir.join('log.txt')
     logger = BaseLogger(target=log_file, stdout=False)
     pop = Population(chromosomes=simple_chromosomes, eval_function=simple_evaluation_function)
     # we should see that a file was created with an appropriate number of rows
     pop.evaluate()
     logger.log(pop)
     with open(log_file, "r") as f:
         assert len(f.readlines()) == len(simple_chromosomes)
     # we should see that a file was created with an appropriate number of rows
     logger.log(pop)
     with open(log_file, "r") as f:
         assert len(f.readlines()) == (2 * len(simple_chromosomes))
     read_stdout = [line for line in capsys.readouterr().out.split('\n') if line != '']
     # there should be nothing printed
     assert len(read_stdout) == 0
Exemple #11
0
 def test_summarylogger_write_file_mo_stdout(self, tmpdir, capsys):
     log_file = tmpdir.join('log.txt')
     logger = SummaryLogger(target=log_file, stdout=False)
     pop = Population(chromosomes=range(10), eval_function=lambda x: x)
     # we should see that a file was created with an appropriate number of rows
     pop.evaluate()
     logger.log(pop)
     with open(log_file, "r") as f:
         assert len(f.readlines()) == 1
     # we should see that a file was created with an appropriate number of rows
     logger.log(pop)
     with open(log_file, "r") as f:
         assert len(f.readlines()) == 2
     read_stdout = [line for line in capsys.readouterr().out.split('\n') if line != '']
     # there should be nothing printed
     assert len(read_stdout) == 0
Exemple #12
0
 def test_evaluate_lambda(self, simple_chromosomes):
     # without concurrency (note that I'm abusing a boolean operator to introduce some latency)
     pop = Population(simple_chromosomes,
                      eval_function=lambda x: (sleep(self.latency) or x))
     t0 = time()
     pop.evaluate()
     t1 = time()
     single_proc_time = t1 - t0
     for individual in pop:
         assert individual.chromosome == individual.fitness
     # with concurrency
     pop = Population(simple_chromosomes,
                      eval_function=lambda x: (sleep(self.latency) or x),
                      concurrent_workers=self.cpus)
     t0 = time()
     pop.evaluate()
     t1 = time()
     multi_proc_time = t1 - t0
     for individual in pop:
         assert individual.chromosome == individual.fitness
     if self.cpus > 1:
         assert multi_proc_time < single_proc_time
Exemple #13
0
 def test_evaluate_lambda(self, simple_chromosomes):
     pop = Population(simple_chromosomes, eval_function=lambda x: x)
     pop.evaluate()
     for individual in pop:
         assert individual.chromosome == individual.fitness