def main(): draw_unit = DrawUnit() times = [] elements = [] instances = 5 # data = gen_data(40, capacity_type='average') # s, p, b = knapsack(data) # print(s.profit) # p_max = max(p) data = gen_data(100, capacity_type='restrictive') populations = [init_population(100, data)] draw_unit.add_pop_to_plot(populations[-1], len(populations) - 1) for i in range(5): for _ in range(10): populations.append( next_generation(data, populations[-1], crossover_rate=1, mutation_rate=0.5)) draw_unit.add_pop_to_plot(populations[-1], len(populations) - 1) print(populations[-1][0].gene) draw_unit.show()
def test_gen_data_weak_corr(self): n = random.randint(100, 200) v = random.randint(100, 150) r = random.randint(20, 60) data = gen_data(n=n, v=v, r=r, correlation='weak') self.assertTrue( 0.2 < corrcoef(data['weights'], data['profits'])[0, 1] < 1)
def test_gen_data_strong_corr(self): n = random.randint(100, 200) r = random.randint(20, 60) data = gen_data(n=n, r=r, correlation='strong') self.assertEqual('strong', data['correlation']) self.assertAlmostEqual( 1, corrcoef(data['weights'], data['profits'])[0, 1])
def test_no_overfilling_repair(self): data = gen_data(seed=0) c = Chromosome([0] * data['n'], data) c[0] = 1 weight_sum = data['weights'][0] copied = c.copy() c.repair() self.assertEqual(copied.gene, c.gene) self.assertLessEqual(weight_sum, data['capacity'])
def test_greedy_repair(self): data = gen_data(n=10, capacity=1, seed=0) c = Chromosome([1]*10, data) self.assertGreater(c.weight_sum, data['capacity']) copied = c.copy() c.repair() self.assertNotEqual(copied.gene, c.gene) weight_sum = sum([c.gene[i] * data['weights'][i] for i in range(len(c.gene))]) self.assertLessEqual(weight_sum, data['capacity'])
def test_machine_independent_seed(self): data = { 'n': 5, 'weights': [6.8, 1.2, 3.5, 3.0, 7.6], 'profits': [8.6, 5.1, 2.7, 0.20000000000000018, 7.699999999999999], 'ratios': [ 1.2647058823529411, 4.25, 0.7714285714285715, 0.06666666666666672, 1.013157894736842 ], 'sorted_indices': [1, 0, 4, 2, 3], 'capacity': 20, 'capacity_type': 'restrictive', 'correlation': 'weak', 'seed': 42 } self.assertEqual(data, gen_data(n=5, seed=42))
def test_gen_data_explicit_capacity(self): v = 10 data = gen_data(v=v, capacity=100, capacity_type='restrictive') self.assertNotEqual(20, data['capacity']) self.assertEqual(100, data['capacity'])
def test_gen_data_in_range(self): v = random.randint(50, 150) data = gen_data(n=100, v=v) self.assertTrue(all([1 <= x <= v for x in data['weights']]))
def test_selection_output_size(self): data = gen_data() population = init_population(50, data, 'vanilla') selected = selection(population, 0, 50) self.assertEqual(50, len(selected))
def test_gen_data_field_n(self): n = random.randint(100, 200) data = gen_data(n=n) self.assertEqual(n, data['n'])
def test_gen_data_seed(self): random_seed = random.randint(0, 1_000_000) data1 = gen_data(seed=random_seed) data2 = gen_data(seed=random_seed) self.assertEqual(data1, data2)
def test_gen_data_if_sorted(self): data = gen_data() self.assertTrue( all(data['ratios'][i] >= data['ratios'][j] for i, j in zip( data['sorted_indices'][:-2], data['sorted_indices'][:-1])))
def test_init_population_size(self): data = gen_data() population = init_population(100, data, 'vanilla') self.assertEqual(100, len(population))
def test_no_overfilling(self): data = gen_data(seed=0) c = Chromosome([0]*data['n'], data) c[0] = 1 weight_sum = data['weights'][0] self.assertLessEqual(weight_sum, data['capacity'])
def test_gen_data_restrictive_capacity(self): v = random.randint(1, 100) data = gen_data(v=v, capacity_type='restrictive') self.assertEqual(2 * v, data['capacity'])
def test_gen_data_not_none(self): data = gen_data() self.assertTrue(all([value is not None for _, value in data.items()]))
def test_gen_data_average_capacity(self): n = random.randint(100, 200) data = gen_data(n=n, capacity_type='average') self.assertEqual(0.5 * sum(data['weights']), data['capacity'])
def test_gen_data_list_size(self): n = random.randint(5, 100) data = gen_data(n=n) self.assertEqual(n, len(data['weights'])) self.assertEqual(n, len(data['profits']))
def test_gen_data_field_ratio(self): data = gen_data() for i in range(data['n']): self.assertEqual(data['ratios'][i], data['profits'][i] / data['weights'][i])
def test_gen_data_negative_profits(self): data = gen_data(n=1_000_000) self.assertTrue(all([x > 0.0 for x in data['profits']]))
seed = args.seed if args.file is None: # Prepare args for generator if args.type.isnumeric(): capacity = int(args.type) capacity_type = 'custom' else: capacity = None capacity_type = args.type.lower() gen_args = (args.items, args.weight, args.value, args.correlation, capacity, capacity_type, seed) # Generate data data = gen_data(*gen_args) else: try: data = json.load(open(args.file, 'r')) except FileNotFoundError: print('File {} not found'.format(args.file)) exit(0) # Start algorithm if algorithm == 'bnb': func_args = (data, args.depth) result = algorithms[algorithm](func_args) elif algorithm == 'genetic': func_args = (data, args.method, args.pop_size, args.crossover, args.mutation, seed) result = algorithms[algorithm](func_args, args.generations)
def setUp(self) -> None: self.data = gen_data()