def mutationBlockNumber(self, individuals, n_mutations, max_difference): """ Mutates n_mutations individuals and it adds or removes up to max_difference blocks """ for a in range(n_mutations): n_blocks = random.randint(-max_difference, max_difference) indv_mut = individuals[random.randint(0, len(individuals) - 1)] if (n_blocks > 0): ny = math.floor((MAX_Y - MIN_Y) / SMALLEST_STEP) nx = math.floor((MAX_X - MIN_X) / SMALLEST_STEP) for b in range(n_blocks): x = random.randint(0, nx) y = random.randint(0, ny) block = BlockGene(type=random.randint(1, len(BLOCKS) - 1), pos=(MIN_X + SMALLEST_STEP * x, MIN_Y + SMALLEST_STEP * y), r=random.randint(0, len(ROTATION) - 1)) indv_mut.appendBlock(block) else: for b in range(-n_blocks): indv_mut.removeBlock( random.randint(0, len(indv_mut.blocks()) - 1))
def _initRandomBlock(self): """ Returns a randomly initialized BlockGene """ return BlockGene(type=Random.randint(1, len(BLOCKS) - 1), pos=(Random.uniform(MIN_X, MAX_X), Random.uniform(MIN_Y, MAX_Y)), r=Random.randint(0, len(ROTATION) - 1))
def _initDiscreteBlock(self): """ Returns a randomly initialized BlockGene positioned in the grid given by the smallest Block size """ ny = floor((MAX_Y - MIN_Y) / SMALLEST_STEP) nx = floor((MAX_X - MIN_X) / SMALLEST_STEP) x = Random.randint(0, nx) y = Random.randint(0, ny) return BlockGene(type = Random.randint(1, len(BLOCKS)-1), pos = (MIN_X + SMALLEST_STEP * x, MIN_Y + SMALLEST_STEP * y), r = Random.randint(0, len(ROTATION) - 1), m=Random.randint(0, len(MATERIALS) - 1))
def mutationBlockRotation(self, individuals, percentage_mutations): """ Mutates a percentage of individuals by adding 45 or -45 to the rotation of one of their blocks""" sample = random.sample( individuals, min(math.floor(len(individuals) * percentage_mutations), len(individuals))) for indv_mut in sample: block_i = random.randint(0, len(indv_mut.blocks()) - 1) block = BlockGene(type=indv_mut.blocks()[block_i].type, pos=(indv_mut.blocks()[block_i].x, indv_mut.blocks()[block_i].y), r=indv_mut.blocks()[block_i].rot) block.rot = (block.rot + random.choice([-1, 1])) % len(ROTATION) indv_mut.updateBlock(block_i, block)
def mutationBlockType(self, individuals, percentage_mutations): """ Mutates a percentage of individuals by changing the block type of one of their blocks""" sample = random.sample( individuals, min(math.floor(len(individuals) * percentage_mutations), len(individuals))) for indv_mut in sample: block_i = random.randint(0, len(indv_mut.blocks()) - 1) block = BlockGene(type=indv_mut.blocks()[block_i].type, pos=(indv_mut.blocks()[block_i].x, indv_mut.blocks()[block_i].y), r=indv_mut.blocks()[block_i].rot) block.type = (block.type + random.choice([-1, 1])) % len(BLOCKS) indv_mut.updateBlock(block_i, block)
def test_cross_common(self): common = [ BlockGene(type=1, pos=[0, 0], r=0), BlockGene(type=1, pos=[0.42, 0.42], r=0), BlockGene(type=1, pos=[0.42, 2.5], r=0) ] uncommon = [ BlockGene(type=7, pos=[0.5, 1.5], r=2), BlockGene(type=7, pos=[0.8, 2.0], r=2) ] with mock.patch('random.shuffle', lambda x: uncommon): result = self.evolution.crossMaintainCommon( [self.individual_1, self.individual_2]) self.assertTrue( np.all( np.asarray([ x for x in result[0].blocks() if x in common + uncommon[:1] ])) and np.all( np.asarray([ x for x in result[1].blocks() if x in common + uncommon[1:] ])))
def test_weird_case(self): individual = LevelIndividual([ BlockGene(type=6, pos=[1.47, -2.88], r=1), BlockGene(type=3, pos=[0.0, -2.88], r=1), BlockGene(type=4, pos=[2.31, -0.5699999999999998], r=3), BlockGene(type=5, pos=[4.62, -1.41], r=3), BlockGene(type=5, pos=[1.26, -0.5699999999999998], r=1), BlockGene(type=2, pos=[0.0, -0.7799999999999998], r=3), BlockGene(type=3, pos=[4.41, -2.25], r=2), BlockGene(type=5, pos=[2.31, -3.09], r=0) ]) print(individual.numberOverlappingBlocks()) individual.calculatePreFitness() print(individual.fitness) individual.calculateFitness([22.38628, 12.7887]) print(individual.fitness)
def setUp(self): self.block1 = BlockGene(type=0, pos=[0, 0], r=0) self.block2 = BlockGene(type=1, pos=[0, 0], r=1) self.individual = LevelIndividual([self.block1]) self.blocklist1 = [ BlockGene(type=0, pos=[0, 0], r=0), BlockGene(type=0, pos=[0.42, 0.42], r=0), BlockGene(type=0, pos=[0.42, 2.5], r=0), BlockGene(type=7, pos=[0.5, 1.5], r=2) ] self.individual2 = LevelIndividual(self.blocklist1)
def mutationBlockPositionY(self, individuals, percentage_mutations): """ Mutates a percentage of individuals by adding a value from [-1,0)(0,1] to the y coordinate of one of their blocks""" sample = random.sample( individuals, min(math.floor(len(individuals) * percentage_mutations), len(individuals))) for indv_mut in sample: block_i = random.randint(0, len(indv_mut.blocks()) - 1) block = BlockGene(type=indv_mut.blocks()[block_i].type, pos=(indv_mut.blocks()[block_i].x, indv_mut.blocks()[block_i].y), r=indv_mut.blocks()[block_i].rot) block.y = block.y + random.choice( [random.uniform(-1, -0.01), random.uniform(0.01, 1)]) indv_mut.updateBlock(block_i, block)
from math import floor import copy from AngryBirdsGA import * import AngryBirdsGA.SeparatingAxisTheorem as SAT from AngryBirdsGA.BlockGene import BlockGene PREFIXED_INIT = [[BlockGene(type=7,pos=((MIN_X+MAX_X)/2, MIN_Y+0.11),r=0), BlockGene(type=7,pos=((MIN_X+MAX_X)/2, MIN_Y+2.39),r=0), BlockGene(type=7,pos=((MIN_X+MAX_X)/2-0.92,MIN_Y+1.23),r=2), BlockGene(type=7,pos=((MIN_X+MAX_X)/2+0.92,MIN_Y+1.23),r=2)], [BlockGene(type=7,pos=((MIN_X+MAX_X)/2, MIN_Y+2.17),r=0), BlockGene(type=7,pos=((MIN_X+MAX_X)/2-0.92,MIN_Y+1.06),r=2), BlockGene(type=7,pos=((MIN_X+MAX_X)/2+0.92,MIN_Y+1.06),r=2), BlockGene(type=7,pos=((MIN_X+MAX_X)/2, MIN_Y+1.06),r=2)], [BlockGene(type=7,pos=((MIN_X+MAX_X)/2, MIN_Y+2.17),r=0), BlockGene(type=7,pos=((MIN_X+MAX_X)/2-0.92,MIN_Y+1.06),r=2), BlockGene(type=7,pos=((MIN_X+MAX_X)/2+0.92,MIN_Y+1.06),r=2)], [BlockGene(type=7,pos=((MIN_X+MAX_X)/2+0.73,MIN_Y+1.23),r=2), BlockGene(type=7,pos=((MIN_X+MAX_X)/2-0.73,MIN_Y+1.23),r=2), BlockGene(type=6,pos=((MIN_X+MAX_X)/2, MIN_Y+0.11),r=0), BlockGene(type=6,pos=((MIN_X+MAX_X)/2, MIN_Y+2.39),r=0)], ] class LevelIndividual: def __init__(self, blocks): self._blocks = blocks self.fitness = None self.base_fitness = 0 self.n_overlapping = self._initOverlappingBlocks()
def setUp(self): self.gene = BlockGene(type=1, pos=[0, 0], r=1) self.gene1 = BlockGene(type=2, pos=[0, 0], r=0)
def test_equals(self): self.assertTrue(self.gene == BlockGene(type=1, pos=[0, 0], r=1))
def setUp(self): self.evolution = op.Evolution(game_path="", write_path="", read_path="") self.population_mock_1 = [ LevelIndividual([]), LevelIndividual([]), LevelIndividual([]), LevelIndividual([]) ] self.population_mock_2 = [ LevelIndividual([]), LevelIndividual([]), LevelIndividual([]), LevelIndividual([]) ] for n in range(len(self.population_mock_1)): self.population_mock_1[n].fitness = n for n in range(len(self.population_mock_2)): self.population_mock_2[n].fitness = n + len(self.population_mock_1) self.individual_1 = LevelIndividual([ BlockGene(type=1, pos=[0, 0], r=0), BlockGene(type=1, pos=[0.42, 0.42], r=0), BlockGene(type=1, pos=[0.42, 2.5], r=0), BlockGene(type=7, pos=[0.5, 1.5], r=2) ]) self.individual_2 = LevelIndividual([ BlockGene(type=1, pos=[0, 0], r=0), BlockGene(type=1, pos=[0.42, 0.42], r=0), BlockGene(type=1, pos=[0.42, 2.5], r=0), BlockGene(type=7, pos=[0.8, 2.0], r=2) ]) self.merged_blocks = [ BlockGene(type=1, pos=[0, 0], r=0), BlockGene(type=1, pos=[0.42, 0.42], r=0), BlockGene(type=1, pos=[0.42, 2.5], r=0), BlockGene(type=7, pos=[0.5, 1.5], r=2), BlockGene(type=7, pos=[0.8, 2.0], r=2) ]
def test_overlapping_blocks_append(self): self.individual2.appendBlock(BlockGene(type=7, pos=[0.5, 1.5], r=0)) self.assertEqual(self.individual2.numberOverlappingBlocks(), 8)
def test_overlapping_blocks_update(self): self.individual2.updateBlock(3, BlockGene(type=4, pos=[0.5, 1.5], r=0)) self.assertEqual(self.individual2.numberOverlappingBlocks(), 2)
def test_try_append_block_success(self): block = BlockGene(type=0, pos=[2, 2], r=0) self.assertTrue( self.individual.tryAppendBlock(block) and self.individual.blocks() == [self.block1, block])