def crossover(self, parent_1: TestCase, parent_2: TestCase) -> Tuple[TestCase]: crossover_point = random.choice(range(len(parent_1.input))) offspring_1 = TestCase(input=parent_1.input[:crossover_point] + parent_2.input[crossover_point:]) offspring_2 = TestCase(input=parent_2.input[:crossover_point] + parent_1.input[crossover_point:]) offspring_1.execute_test_on(self.source_code) offspring_2.execute_test_on(self.source_code) return offspring_1, offspring_2
def test_testcase_1(): from sourcecode import SourceCode from testcase import TestCase sc = SourceCode(path="./evaluation/gcd.py") sc.create_file() test = TestCase(input=[12, 10]) test.execute_test_on(sc) assert test.get_output() == [2]
def test_testcase_2(): from sourcecode import SourceCode from testcase import TestCase path = "./evaluation/gcd.py" str_sc = "" with open(path, "r") as f: str_sc = f.read() sc = SourceCode(str_sc) sc.create_file() test = TestCase(input=[10, 10]) test.execute_test_on(sc) assert test.get_output() == [10]
def insert(self, solution: TestCase): solution.execute_test_on(self.source_code) self.solutions.append(solution)
def initial_population(self, population_size: int, test_type: int): self.solutions = [] for _ in range(population_size): test = TestCase(test_type=test_type) test.execute_test_on(self.source_code) self.solutions.append(test)