def test_bitwise_mutation(self): # zero length testchrome = gac.Chromosome() pm = None testchrome.bitwise_mutation(pm) self.assertEqual(testchrome.bool_chrome, []) # change none testchrome = gac.Chromosome() num_bool = 10 testchrome.pop_chrome(num_bool, 0, 0, 0, 0, 0, 0, 0) beforetest = list(testchrome.bool_chrome) pm = 0.0 testchrome.bitwise_mutation(pm) self.assertEqual(beforetest, testchrome.bool_chrome) # change all testchrome = gac.Chromosome() num_bool = 15 testchrome.pop_chrome(num_bool, 0, 0, 0, 0, 0, 0, 0) beforetest = list(testchrome.bool_chrome) ans = [False if i else True for i in beforetest] pm = 1.0 testchrome.bitwise_mutation(pm) self.assertEqual(testchrome.bool_chrome, ans)
def __init__(self): logging.getLogger() self.fit_sum = 0 self.fit_avg = 0 self.fit_max = 0 self.fit_std_dev = 0 self.max_chrome = gac.Chromosome() self.chrome = [gac.Chromosome() for i in range(param.pop_size)]
def test_creep_mutation(self): num_int = 5 ilow = [-30, -20, -10, 0, 10] ihigh = [-10, 0, 10, 20, 30] sigma = 1.0 testchrome = gac.Chromosome() # zero length pm = 1.0 testchrome.creep_mutation(pm, [], [], sigma) self.assertEqual(testchrome.int_chrome, []) # change none testchrome.pop_chrome(0, num_int, 0, 0, ilow, ihigh, 0, 0) before = list(testchrome.int_chrome) pm = 0.0 testchrome.creep_mutation(pm, ilow, ihigh, sigma) self.assertEqual(before, testchrome.int_chrome) # change all testchrome.pop_chrome(0, num_int, 0, 0, ilow, ihigh, 0, 0) before = list(testchrome.int_chrome) pm = 1.0 testchrome.creep_mutation(pm, ilow, ihigh, sigma) for i, chrome in enumerate(testchrome.int_chrome): self.assertLessEqual(chrome, ihigh[i]) self.assertGreaterEqual(chrome, ilow[i])
def test_insert_mutation(self): num_perm = 10 testchrome = gac.Chromosome() # zero length testchrome.insert_mutation() self.assertEqual(testchrome.perm_chrome, []) testchrome.perm_chrome = [0, 1, 2] print('zzzzzzzzzzzzzzzzzzzzzzzz', len(testchrome.permchrome)) testchrome.insert_mutation()
def main(args): logging.basicConfig(format='', level=logging.DEBUG) chrome = gac.Chromosome() chrome.pop_chrome(param.num_bool, \ param.num_int, \ param.num_dbl, \ param.num_perm, \ param.int_low_bnd, \ param.int_high_bnd, \ param.dbl_low_bnd, \ param.dbl_high_bnd, \ param.perm_len) chrome.evaluate() print(chrome.__str__())
def parent_selection(self): ''' Select parents to create offspring from existing population. ''' logging.debug('Selecting parents for breeding...') mating_pool = [gac.Chromosome() for i in range(param.pop_size)] # populate rest of chromes if param.parent_choice == 'fps': gap.fitness_proportional_selection(param.pop_size, \ self.chrome, \ self.fit_avg, \ self.fit_std_dev, \ param.s) elif param.parent_choice == 'ranking': gap.ranking_selection(param.pop_size, \ self.chrome, \ param.s) elif param.parent_choice == 'tournament': gap.tournament_selection(param.pop_size, \ self.chrome, \ mating_pool, \ param.s) else: pass if param.parent_choice != 'tournament': if param.selection_prob == 'roulette': gap.roulette_wheel_sampling(param.pop_size, \ self.chrome, \ mating_pool) elif param.selection_prob == 'sus': gap.stochastic_universal_sampling(param.pop_size, \ self.chrome, \ mating_pool) else: pass logging.debug('Mating pool...') for i in range(param.pop_size): logging.debug(str(mating_pool[i])) logging.debug(' ') # replace entire generation by its offspring for i in range(param.pop_size): self.chrome[i] = copy.deepcopy(mating_pool[i])