def test_generation(self): # まず初期化する test_ga = ga.GeneticAlgorithm( mutation=self._mutation, cross=self._cross, situation=self._situation, elite_num=self._elite_num, population=self._population, crossover_method=self._crossover_method, fitness_function=self._fitness_function, ) population_dept = self._db_facade.select_department('populations') test_ga.generation(steps=20, experiment_id=1000, log_span=10, population_dept=population_dept)
def __init__(self, situation, candle_type, pair, population, mutation, cross, elite_num, host, fitness_function_name, crossover_name, hyper_params=None): """ :param situation: Situation 遺伝子の要素の取りうる範囲などを表す :param candle_type: string ロウソク足データの種類 :param pair: string コインの種類 :param population: int 個体数 :param mutation: int 突然変異のパーセンテージ :param cross: int 交叉のパーセンテージ :param elite_num: int 世代交代時に残すエリート個体数 :param host: string dbの接続先 :param fitness_function_name: string 適応度関数の名称 :param crossover_name: string 交叉手法の名称 :param hyper_params: dict ハイパーパラメータ """ self._db_facade = facade.Facade(host=host) ff_facade = fffacade.Facade(candle_type=candle_type, coin=pair) c_facade = cfacade.CrossoverFacade() expt_logs_dept = self._db_facade.select_department('experiment_logs') # 適応度関数 fitness_function = ff_facade.select_department( function_name=fitness_function_name, db_dept=expt_logs_dept, hyper_params=hyper_params) # 交叉手法 crossover_method = c_facade.select_department( crossover_name=crossover_name) self._ga = ga.GeneticAlgorithm(mutation=mutation, cross=cross, situation=situation, elite_num=elite_num, population=population, crossover_method=crossover_method, fitness_function=fitness_function) self._hyper_params = hyper_params self.__pair = pair
def test_determine_next_generation(self): """ 世代交代で遺伝子の次元が適当どうかチェックする """ # まず初期化する test_ga = ga.GeneticAlgorithm( mutation=self._mutation, cross=self._cross, situation=self._situation, elite_num=self._elite_num, population=self._population, crossover_method=self._crossover_method, fitness_function=self._fitness_function, ) result = test_ga.init_population() test_ga.set_geno_type(result) geno_type = test_ga.determine_next_generation() self.assertEqual(True, np.all(np.equal(result, geno_type)))
def test_init_population(self): """ 遺伝子初期化時に次元が適当かどうかのチェック """ test_ga = ga.GeneticAlgorithm( mutation=self._mutation, cross=self._cross, situation=self._situation, elite_num=self._elite_num, population=self._population, crossover_method=self._crossover_method, fitness_function=self._fitness_function, ) result = test_ga.init_population() self.assertIsInstance(result, type(np.asarray([1]))) for i in result: self.assertLessEqual(i[0], 50) self.assertLessEqual(1, i[0]) self.assertIsInstance(i, type(np.asarray([]))) self.assertEqual(self._population, len(result)) del test_ga
def test_calc_fitness(self): """ 適応度の次元が適当かどうかのチェック """ test_ga = ga.GeneticAlgorithm( mutation=self._mutation, cross=self._cross, situation=self._situation, elite_num=self._elite_num, population=self._population, crossover_method=self._crossover_method, fitness_function=self._fitness_function, ) result = test_ga.init_population() test_ga.set_geno_type(result) fitness = test_ga.calc_fitness(should_log=False) fitness_list = list() for i in range(self._population): fitness_list.append(i + 1) test_fitness = np.asarray(fitness_list) self.assertEqual(True, np.all(np.equal(test_fitness, fitness))) del test_ga
def test__init__(self): """ 1. mutation(突然変異のパーセンテージ)は0 ~ 100 2. cross(交叉のパーセンテージ)は0 ~ 100 3. situation(遺伝子の特徴量を表す)はmodules.feature.Situationクラスのインスタンス 4. population(個体数)がelite_num(エリートの個体数)より大きい 5. crossover_method(交叉の手法を表す)はmodules.crossover.Crossoverクラスのインスタンス 6. fitness_function(適応度関数を表す)はmodules.fitnessfunction.FitnessFunctionクラスのインスタンス """ try: # mutation(突然変異のパーセンテージ)は0 ~ 100 test_ga = ga.GeneticAlgorithm( mutation=101, cross=self._cross, situation=self._situation, elite_num=self._elite_num, population=self._population, crossover_method=self._crossover_method, fitness_function=self._fitness_function) self.assertEquals(1, -1) except TypeError: pass try: # mutation(突然変異のパーセンテージ)は0 ~ 100 test_ga = ga.GeneticAlgorithm( mutation=-1, cross=self._cross, situation=self._situation, elite_num=self._elite_num, population=self._population, crossover_method=self._crossover_method, fitness_function=self._fitness_function) self.assertEquals(1, -1) except TypeError: pass try: # cross(交叉のパーセンテージ)は0 ~ 100 test_ga = ga.GeneticAlgorithm( mutation=self._mutation, cross=101, situation=self._situation, elite_num=self._elite_num, population=self._population, crossover_method=self._crossover_method, fitness_function=self._fitness_function) self.assertEquals(1, -1) except TypeError: pass try: # cross(交叉のパーセンテージ)は0 ~ 100 test_ga = ga.GeneticAlgorithm( mutation=self._mutation, cross=-1, situation=self._situation, elite_num=self._elite_num, population=self._population, crossover_method=self._crossover_method, fitness_function=self._fitness_function) self.assertEquals(1, -1) except TypeError: pass try: # situation(遺伝子の特徴量を表す)はmodules.feature.Situationクラスのインスタンス temp_ga = ga.GeneticAlgorithm( mutation=self._mutation, cross=self._cross, situation=[(1, 50), (50, 100), (1, 50)], elite_num=self._elite_num, population=self._population, crossover_method=self._crossover_method, fitness_function=self._fitness_function) self.assertEquals(1, -1) except TypeError: pass try: # population(個体数)がelite_num(エリートの個体数)より大きい temp_ga = ga.GeneticAlgorithm( mutation=self._mutation, cross=self._cross, situation=self._situation, elite_num=101, population=100, crossover_method=self._crossover_method, fitness_function=self._fitness_function) self.assertEquals(1, -1) except ArithmeticError: pass try: # crossover_method(交叉の手法を表す)はmodules.crossover.Crossoverクラスのインスタンス test_ga = ga.GeneticAlgorithm( mutation=self._mutation, cross=self._cross, situation=self._situation, elite_num=self._elite_num, population=self._population, crossover_method=100, fitness_function=self._fitness_function) self.assertEquals(1, -1) except TypeError: pass try: # fitness_function(適応度関数を表す)はmodules.fitnessfunction.FitnessFunctionクラスのインスタンス test_ga = ga.GeneticAlgorithm( mutation=self._mutation, cross=self._cross, situation=self._situation, elite_num=self._elite_num, population=self._population, crossover_method=self._crossover_method, fitness_function=100, ) self.assertEquals(1, -1) except TypeError: pass