def test_if_returns_best_phenotype(): pop = Population(mock_params, mock_judge, 1, 5) ph1 = Phenotype(3) ph2 = Phenotype(3) ph3 = Phenotype(3) ph1.genes = [True, True, True] ph2.genes = [True, False, False] ph3.genes = [False, False, False] mock_judge.goal_eval(1, 5, ph1) mock_judge.goal_eval(1, 5, ph2) mock_judge.goal_eval(1, 5, ph3) pop.push_phenotype(ph1) pop.push_phenotype(ph2) pop.push_phenotype(ph3) assert pop.get_the_best_error() == [True, False, False]
conf_file.close() for i in range(__REQUEST_NUM__): try: r = requests.get(url_factory(_A_, _B_)) except Exception: print("Could not connect to server") exit() if r.status_code != 200: print("Non 2xx code returned from the server") exit() response = r.json() responses.append(response) ph = Phenotype(num_of_cards) for j in range(num_of_cards): if j+1 in response['A']: ph.genes[j] = True else: ph.genes[j] = False judge.goal_eval(_A_, _B_, ph) errors.append(ph.fitness) averages.append(np.mean(errors)) variances.append(np.var(errors)) devations.append(np.std(errors)) bar.next() bar.finish()
def test_if_correctly_pushes_new_phenotype(): pop = Population(mock_params, mock_judge, 10, 10) ph = Phenotype(3) pop.push_phenotype(ph) assert len(pop.parents) == 1
def test_if_correctly_evals_error(): judge = Judge([1, 2, 3]) ph1 = Phenotype(3) ph1.genes = [True, False, False] assert judge.goal_eval(6, 0, ph1) == 50
def test_if_correctly_evals_zero_on_perfect_match(): judge = Judge([1, 2, 3]) ph1 = Phenotype(3) ph1.genes = [True, True, True] assert judge.goal_eval(6, 0, ph1) == 0
def test_correct_argument(): ph = Phenotype(6) assert ph
def test_if_raises_typeerror_on_incorrect_fitness(): with pytest.raises(TypeError): ph = Phenotype(10).set_fitness('3')
def test_if_sets_fitness_correctly(): ph = Phenotype(10) ph.set_fitness(3) assert ph.fitness == 3
def test_if_raises_typeerror_on_incorrect_arg(): with pytest.raises(TypeError): ph = Phenotype('5')
def test_if_correctly_creates_genes(): ph = Phenotype(10) assert len(ph.genes) == 10