Exemplo n.º 1
0
def justsim(ind, swarm, targets, timesteps, trials=100):

    # Decode genome into executable behaviour tree
    bt = tg.tree().decode(ind, swarm, targets)
    bt.setup(timeout=15)
    fits = []

    dur = 0
    score = 0
    for k in range(trials):
        fitness = 0
        score = 0
        t = 0
        found = False
        bt = tg.tree().decode(ind, swarm, targets)
        swarm.beacon_set = []
        while t <= timesteps and found == False:
            t += 1
            bt.tick()
            swarm.iterate()
            swarm.get_state()
            score = targets.get_state(swarm, t)
            if targets.found == len(targets.targets):
                found = True

        maxsize = 300
        fitness = 0
        fitness = score / len(targets.targets)
        fitness = fitness - (len(ind.genome) / 1000000)
        if fitness < 0: fitness = 0
        print('fitness: ', fitness)

        targets.reset()
        swarm.reset()

    maxsize = 300
    fitness = 0
    fitness = score / (trials * len(targets.targets))
    fitness = fitness - (len(ind.genome) * 0.001)
    if fitness < 0: fitness = 0
    print('Average fitness: ', fitness)
    print(ind.tree)

    return fits
Exemplo n.º 2
0
def extinction(population, deathrate, indsize, blackboard):

	# Kill off proportion of population and replace with new individuals
	for n in range(len(population)):

		if random.uniform(0,1) <= deathrate:
			# kill individual and replace with new
			population[n] = tg.individual(tg.tree().make_tree(indsize, blackboard))

	return population
Exemplo n.º 3
0
def parallel(ind, swarm, targets, timesteps):
	
	bt = tg.tree().decode(ind, swarm, targets)
	tg.tree().ascii_tree(ind)
		
	# Set the number of trials per individual to determine fitness
	trials = 1
	dur = 0
	score = 0
	for k in range(trials):
		fitness = 0
		t = 0
		# RUN SIMULATION!!! ############################################
		found = False
		# IMPORTANT! need to reset behaviours after each run 
		swarm.beacon_set = []
		bt = tg.tree().decode(ind, swarm, targets)
		while t <= timesteps and found == False:
			t += 1
			bt.tick()
			swarm.iterate()
	
			swarm.get_state()
			score += targets.get_state(swarm, t)
			if targets.found == len(targets.targets):
				found = True
		
		targets.reset()
		swarm.reset()
		
	maxsize = 300
	fitness = 0
	fitness = score/(trials*len(targets.targets))
	fitness = fitness - (len(ind.genome)*0.001)
	if fitness < 0: fitness = 0

	ind.fitness = fitness

	return ind
Exemplo n.º 4
0
def serial(pop, oldswarm, boxes, genum, timesteps, treecost, field, grid):
	# Evaluate fitness of each individual in population
	for z in range(0, len(pop)):

		swarm = bsim.swarm()
		swarm.size = oldswarm.size
		swarm.behaviour = 'none'
		swarm.speed = 0.5
		swarm.origin = oldswarm.origin
		swarm.gen_agents()
		
		env = bsim.map()
		'''
		The map has to be set to the same as passed into the evolution!!!
		'''
		
		#swarm = oldswarm.copy()
		# Decode genome into executable behaviour tree
		print( 'Evaluating Individual: ', z, ' Gen: ', genum)
		bt = tg.tree().decode(pop[z], swarm, boxes)
		tg.tree().ascii_tree(pop[z])
		
		# Set the number of trials per individual to determine fitness
		trials = 1; dur = 0
		score = 0 ; totscore = 0
		record = np.zeros(trials)
		for k in range(trials):


			swarm = bsim.swarm()
			swarm.size = oldswarm.size
			swarm.behaviour = 'none'
			swarm.speed = 0.5
			swarm.gen_agents()
			swarm.grid = grid
			swarm.field = field
			
			env = bsim.map()
			'''
			The map has to be set to the same as passed into the evolution!!!
			'''
			env.bounded = True
			env.env1()
			env.gen()
			swarm.map = env

			boxes = bsim.boxes()
			boxes.set_state('state1')
			boxes.sequence = False
			boxes.radius = 3

			fitness = 0
			t = 0
			found = False
			# IMPORTANT! need to reset behaviours after each run 
			swarm.beacon_set = []
			bt = tg.tree().decode(pop[z], swarm, boxes)
			noise = np.random.uniform(-.1,.1,(timesteps, swarm.size, 2))

			# Reset score
			score = 0
			while t <= timesteps and found == False:
				
				bt.tick()
				swarm.iterate(noise[t-1])
				swarm.get_state()
				score = boxes.get_state(swarm,t)
				t += 1
			
			#score = boxes.tot_collected
			print('score = ' , score)
			boxes.reset()
			swarm.reset()
			
		print ('-------------------------------------------------------------------')
		
		maxsize = 300
		fitness = 0

		fitness = score/(len(boxes.boxes))

		fitness = fitness - (len(pop[z].genome)*treecost)
		if fitness < 0: fitness = 0
	
		print ('Individual fitness: %.3f'% fitness)
		pop[z].fitness = fitness
		print ('=================================================================================')
Exemplo n.º 5
0
    extinction_prob = -1
    deathrate = 0.9
    hall = []
    hallsize = 20
    newind = 0
    elitesize = 6
    treecost = 0.001
    op_prob = 0.25

    selectionNum = popsize - elitesize - newind

    # Generate starting population
    pop = []
    generations = [] * NGEN
    pop = [
        tg.individual(tg.tree().make_tree(indsize, blackboard))
        for t in range(popsize)
    ]

    #  Logging variables
    logfit = []
    logpop = []
    logavg = []
    logmax = []
    stats = {
        "avgsize": [],
        "stdsize": [],
        "meanfit": [],
        "stdfit": [],
        "maxfit": []
    }
Exemplo n.º 6
0
def serial1(pop, swarm, targets, genum, timesteps, treecost):
	# Evaluate fitness of each individual in population


	for z in range(0, len(pop)):

		swarmsize = 3
		swarm = asim.swarm()
		swarm.size = swarmsize
		swarm.behaviour = 'none'
		swarm.speed = 0.5
		swarm.origin = np.array([0, 0])
		swarm.gen_agents()


		# swarm = swarm.copy()
		# swarm.gen_agents()

		env = asim.map()
		env.exercise1()
		env.gen()
		swarm.map = env

		# Decode genome into executable behaviour tree
		print( 'Evaluating Individual: ', z, ' Gen: ', genum)
		bt = tg.tree().decode(pop[z], swarm, targets)
		tg.tree().ascii_tree(pop[z])
		
		# Set the number of trials per individual to determine fitness
		trials = 1; dur = 0
		score = 0 ; totscore = 0

		for k in range(trials):
			fitness = 0
			t = 0
			found = False
			# IMPORTANT! need to reset behaviours after each run 
			swarm.beacon_set = []
			bt = tg.tree().decode(pop[z], swarm, targets)
			while t <= timesteps and found == False:
				t += 1
				bt.tick()
				swarm.iterate()
				swarm.get_state()
				score = targets.get_state(swarm, t)
				if targets.found == len(targets.targets):
					found = True
			
			totscore += score
			targets.reset()
			swarm.reset()
			
		print ('-------------------------------------------------------------------')
		
		maxsize = 300
		fitness = 0
		fitness = totscore/(trials*len(targets.targets))
		fitness = fitness - (len(pop[z].genome)*treecost)
		if fitness < 0: fitness = 0
		
		print ('Individual fitness: ', fitness)
		pop[z].fitness = fitness
		print ('=================================================================================')
Exemplo n.º 7
0
def adveserial(popa, popb, swarma, swarmb, targets, genum, timesteps):


	# Two supervisors competing against each other for coverage
	for z in range(0, len(popa)):
		# Decode genome into executable behaviour tree
		print( 'Evaluating Individual: ', z, ' Gen: ', genum)
		bta = tg.tree().decode(popa[z], swarma, targets)
		tg.tree().ascii_tree(popa[z])

		btb = tg.tree().decode(popb[z], swarmb, targets)
		tg.tree().ascii_tree(popb[z])
		
		# Set the number of trials per individual to determine fitness
		trials = 1; dur = 0
		scorea = 0 ; scoreb = 0; totscore = 0
		for k in range(trials):
			fitness = 0
			t = 0
			found = False
			# IMPORTANT! need to reset behaviours after each run 
			
			bta = tg.tree().decode(popa[z], swarma, targets)
			btb = tg.tree().decode(popb[z], swarmb, targets)
			

			while t <= timesteps and found == False:
				t += 1
				bta.tick()
				btb.tick()
				swarma.iterate()
				swarma.get_state()
				swarmb.iterate()
				swarmb.get_state()
				scorea += targets.ad_state(swarma, t)
				scoreb += targets.ad_state(swarmb, t)
				if targets.found == len(targets.targets):
					found = True
			
			#totscore += score
			targets.reset()
			swarma.reset()
			swarmb.reset()
			
		print ('-------------------------------------------------------------------')
		
		maxsize = 300
		fitness = 0
		fitness = scorea/(trials*len(targets.targets))
		fitness = fitness - (len(popa[z].genome)/1000000)
		if fitness < 0: fitness = 0

		popa[z].fitness = fitness

		print ('Individual fitness A: ', fitness)
		popb[z].fitness = fitness
		print ('=================================================================================')

		fitness = 0
		fitness = scoreb/(trials*len(targets.targets))
		fitness = fitness - (len(popb[z].genome)/1000000)
		if fitness < 0: fitness = 0
		
		print ('Individual fitness B: ', fitness)
		popb[z].fitness = fitness
		print ('=================================================================================')
Exemplo n.º 8
0
def default(ind, swarm, targets, timesteps):

    # Decode genome into executable behaviour tree
    bt = tg.tree().decode(ind, swarm, targets)
    bt.setup(timeout=15)

    # Setup post tick handlers for tree animation
    snapshot_visitor = py_trees.visitors.SnapshotVisitor()
    bt.add_post_tick_handler(
        functools.partial(post_tick_handler, snapshot_visitor))
    bt.visitors.append(snapshot_visitor)

    # Setup plot
    lim = 40
    xmin = -lim
    xmax = lim
    ymin = -lim
    ymax = lim
    fig, ax = plt.subplots(facecolor=(.99, .99, .99))
    ax.set_xlim([xmin, xmax])
    ax.set_ylim([ymin, ymax])
    [
        ax.plot(swarm.agents[t][0], swarm.agents[t][1], 'bo')
        for t in range(swarm.size)
    ]
    plt.ion()
    plt.grid()
    fig.canvas.draw()
    plt.show()

    dur = 0
    score = 0
    fitness = 0
    t = 0
    fontsize = 12
    found = False
    swarm.beacon_set = []
    while t <= timesteps and found == False:
        t += 1
        # input()
        bt.tick()
        swarm.iterate()
        swarm.get_state()
        score = targets.get_state(swarm, t)

        ax.clear()
        ax.set_xlim([xmin, xmax])
        ax.set_ylim([ymin, ymax])
        plt.show()
        plt.grid()
        [
            ax.plot(swarm.agents[a][0], swarm.agents[a][1], 'bo')
            for a in range(swarm.size)
        ]
        [
            ax.plot([
                swarm.map.obsticles[a].start[0], swarm.map.obsticles[a].end[0]
            ], [
                swarm.map.obsticles[a].start[1], swarm.map.obsticles[a].end[1]
            ],
                    'k-',
                    lw=2) for a in range(len(swarm.map.obsticles))
        ]

        ax.text(5,
                41,
                'Swarm behviour: ' + swarm.behaviour + ', ' + str(swarm.param),
                fontsize=fontsize,
                color='green')
        ax.text(5,
                45,
                'Time: %d/%d' % (t, timesteps),
                fontsize=fontsize,
                color='purple')
        ax.text(-40,
                41,
                'Center of Mass: %.2f, %.2f' %
                (swarm.centermass[0], swarm.centermass[1]),
                fontsize=fontsize,
                color='green')
        ax.text(-40,
                45,
                'Spread: %.2f' % swarm.spread,
                fontsize=fontsize,
                color='red')
        ax.text(-20,
                45,
                'Coverage: %.2f' % targets.coverage,
                fontsize=fontsize,
                color='blue')

        #[ax.plot(swarm.beacon_set[a].pos[0],swarm.beacon_set[a].pos[1], 'ro', markersize=70, alpha=0.3) for a in range(len(swarm.beacon_set))]
        if swarm.beacon_att.size != 0:
            for a in range(0, len(swarm.beacon_att)):
                ax.plot(swarm.beacon_att[a][0],
                        swarm.beacon_att[a][1],
                        'go',
                        markersize=70,
                        alpha=0.3)

                #ax.text(swarm.beacon_set[a].pos[0],swarm.beacon_set[a].pos[1], 'A', fontsize=15, color='green')
        if swarm.beacon_rep.size != 0:
            for a in range(0, len(swarm.beacon_rep)):
                ax.plot(swarm.beacon_rep[a][0],
                        swarm.beacon_rep[a][1],
                        'ro',
                        markersize=70,
                        alpha=0.)
                #ax.text(swarm.beacon_set[a].pos[0],swarm.beacon_set[a].pos[1], 'R', fontsize=15, color='red')

        for n in range(0, len(targets.targets)):
            if targets.old_state[n] == False:
                ax.plot(targets.targets[n][0],
                        targets.targets[n][1],
                        'ro',
                        markersize=10,
                        alpha=0.5)
            else:
                ax.plot(targets.targets[n][0],
                        targets.targets[n][1],
                        'go',
                        markersize=10,
                        alpha=0.5)

        fig.canvas.draw()
        print('Time: ', t, '/', timesteps, end='\r')

    print('\n\nScore: ', score)
    print('len targets: ', len(targets.targets))
    maxsize = 300
    fitness = 0
    fitness = score / len(targets.targets)
    print('fitness pre cost: ', fitness)
    #fitness = fitness - (len(ind.genome)*0.001)

    return fitness
Exemplo n.º 9
0
def default_ad(inda, indb, swarma, swarmb, targets, timesteps):

    # Decode genome into executable behaviour tree
    bta = tg.tree().decode(inda, swarma, targets)
    bta.setup(timeout=15)

    btb = tg.tree().decode(indb, swarmb, targets)
    btb.setup(timeout=15)

    # Setup plot
    lim = 40
    xmin = -lim
    xmax = lim
    ymin = -lim
    ymax = lim
    fig, ax = plt.subplots(facecolor=(.99, .99, .99))
    ax.set_xlim([xmin, xmax])
    ax.set_ylim([ymin, ymax])
    plt.ion()
    plt.grid()
    fig.canvas.draw()
    plt.show()

    dur = 0
    scorea = 0
    scoreb = 0
    fitness = 0
    t = 0
    fontsize = 12
    found = False

    while t <= timesteps and found == False:
        t += 1
        # input()
        bta.tick()
        btb.tick()
        swarma.iterate()
        swarma.get_state()
        swarmb.iterate()
        swarmb.get_state()

        scorea += targets.ad_state(swarma, t)
        scoreb += targets.ad_state(swarmb, t)

        ax.clear()
        ax.set_xlim([xmin, xmax])
        ax.set_ylim([ymin, ymax])
        plt.show()
        plt.grid()

        [
            ax.plot(swarma.agents[a][0], swarma.agents[a][1], 'bo')
            for a in range(swarma.size)
        ]
        [
            ax.plot(swarmb.agents[a][0], swarmb.agents[a][1], 'ro')
            for a in range(swarmb.size)
        ]
        [
            ax.plot([
                swarma.map.obsticles[a].start[0],
                swarma.map.obsticles[a].end[0]
            ], [
                swarma.map.obsticles[a].start[1],
                swarma.map.obsticles[a].end[1]
            ],
                    'k-',
                    lw=2) for a in range(len(swarma.map.obsticles))
        ]

        ax.text(5,
                41,
                'Swarm behviour A: ' + swarma.behaviour + ', ' +
                str(swarma.param),
                fontsize=fontsize,
                color='green')
        ax.text(5,
                45,
                'Swarm behviour B: ' + swarmb.behaviour + ', ' +
                str(swarmb.param),
                fontsize=fontsize,
                color='green')
        #ax.text(-40, 41, 'Center of Mass: %.2f, %.2f' % (swarm.centermass[0], swarm.centermass[1]), fontsize=fontsize, color='green')
        #ax.text(-40, 45, 'Spread: %.2f' % swarm.spread, fontsize=fontsize, color='red')
        ax.text(-20,
                45,
                'Coverage: %.2f' % targets.coverage,
                fontsize=fontsize,
                color='blue')

        for n in range(0, len(targets.targets)):
            if targets.old_state[n] == False:
                ax.plot(targets.targets[n][0],
                        targets.targets[n][1],
                        'ro',
                        markersize=10,
                        alpha=0.5)
            else:
                ax.plot(targets.targets[n][0],
                        targets.targets[n][1],
                        'go',
                        markersize=10,
                        alpha=0.5)

        fig.canvas.draw()
        print('Time: ', t, '/', timesteps, end='\r')

    print('\n\nScore A: ', scorea)
    print('\nScore B: ', scoreb)
    print('len targets: ', len(targets.targets))
    maxsize = 300
    fitness = 0
    fitness = score / len(targets.targets)
    print('fitness pre cost: ', fitness)
    fitness = fitness - (len(ind.genome) * 0.001)
    if fitness < 0: fitness = 0
    print('Individual fitness: ', fitness)
    input()
    return fitness
Exemplo n.º 10
0
def mutate_single(ind, mutrate, growrate, growdepth, blackboard):

	
	for b in range(0, len(ind.genome)):

		if random.random() <= mutrate:
			if random.random() <= growrate and b != 0 and b < len(ind.genome) and type(ind.genome[b]) is not Operator:
				# Grow a randomly generated tree
				
				subtree = tg.tree().make_tree(growdepth, blackboard)

				del ind.genome[b]
				ind.genome[b:b] = subtree[:]

			else:
				if type(ind.genome[b]) is Operator:
					ind.genome[b].type = random.choice(blackboard["operators"])

				if type(ind.genome[b]) is Action:
					ind.genome[b].type = random.choice(blackboard["actions"])

				if type(ind.genome[b]) is Param:

					choice = random.choice(['type','param'])


					if choice == 'type':
						node = tg.Param()
						node.generate(blackboard)
						ind.genome[b] = node.copy()

					if choice == 'param':
						if ind.genome[b].type == 'random':
							ind.genome[b].param = random.choice(blackboard["randparam"])
						elif ind.genome[b].type == 'rot_anti':
							ind.genome[b].param = random.choice(blackboard["rotparam"])
						elif ind.genome[b].type == 'rot_clock':
							ind.genome[b].param = random.choice(blackboard["rotparam"])
						else:
							ind.genome[b].param = random.choice(blackboard["dirparam"])

				if type(ind.genome[b]) is Env_control:

					choice = random.choice(['type','pos'])
					if choice == 'type':
						ind.genome[b].type = random.choice(blackboard["envcontrol"])
					if choice == 'pos':
						ind.genome[b].pos[0] = random.choice(blackboard["envcontrol_x"])
						ind.genome[b].pos[1] = random.choice(blackboard["envcontrol_y"])
				
				if type(ind.genome[b]) is Condition:
					
					choice = random.choice(['var','value','op'])
					if choice == 'var':
						# Generate new condition node
						node = tg.Condition()
						node.generate(blackboard)
						ind.genome[b] = node.copy()
					if choice == 'op':
						ind.genome[b].op = random.choice(['<','>'])
					if choice == 'value':
						if ind.genome[b].var == 'density':
							ind.genome[b].value = random.choice(blackboard["density"])
						if ind.genome[b].var == 'centery':
							ind.genome[b].value = random.choice(blackboard["centery"])
						if ind.genome[b].var == 'centerx':
							ind.genome[b].value = random.choice(blackboard["centerx"])
						if ind.genome[b].var == 'coverage':
							ind.genome[b].value = random.choice(blackboard["coverage"])

	return ind
Exemplo n.º 11
0
def mutate(offspring, mutrate, growrate, growdepth, blackboard):

	for a in range(0, len(offspring)):
		for b in range(0, len(offspring[a].genome)):

			if random.random() <= mutrate:
				if random.random() <= growrate and b != 0 and b < len(offspring[a].genome) and type(offspring[a].genome[b]) is not Operator:
					# Grow a randomly generated tree
					
					subtree = tg.tree().make_tree(growdepth, blackboard)

					del offspring[a].genome[b]
					offspring[a].genome[b:b] = subtree[:]

				else:
					if type(offspring[a].genome[b]) is Operator:
						offspring[a].genome[b].type = random.choice(blackboard["operators"])

					if type(offspring[a].genome[b]) is Action:
						offspring[a].genome[b].type = random.choice(blackboard["actions"])

					if type(offspring[a].genome[b]) is Param:

						choice = random.choice(['type','param'])

						if choice == 'type':
							# Switch behaviour to another random behaviour
							node = tg.Param()
							node.generate(blackboard)
							offspring[a].genome[b] = node.copy()
						else:
							if offspring[a].genome[b].type == 'random':
								offspring[a].genome[b].param = random.choice(blackboard["randparam"])
							elif offspring[a].genome[b].type == 'rotate_clock' or offspring[a].genome[b].type == 'rotate_anti':
								offspring[a].genome[b].param = random.choice(blackboard["rotparam"])
							else:
								offspring[a].genome[b].param = random.choice(blackboard["dirparam"])


					if type(offspring[a].genome[b]) is Env_control:

						choice = random.choice(['type','pos'])
						if choice == 'type':
							offspring[a].genome[b].type = random.choice(blackboard["envcontrol"])
						if choice == 'pos':
							offspring[a].genome[b].pos[0] = random.choice(blackboard["envcontrol_x"])
							offspring[a].genome[b].pos[1] = random.choice(blackboard["envcontrol_y"])
					
					if type(offspring[a].genome[b]) is Condition:
						
						choice = random.choice(['var','value','op'])
						if choice == 'var':
							# Generate new condition node
							node = tg.Condition()
							node.generate(blackboard)
							offspring[a].genome[b] = node
						if choice == 'op':
							offspring[a].genome[b].op = random.choice(['<','>'])
						if choice == 'value':
							if offspring[a].genome[b].var == 'density':
								offspring[a].genome[b].value = random.choice(blackboard["density"])
							if offspring[a].genome[b].var == 'centery':
								offspring[a].genome[b].value = random.choice(blackboard["dimy"])
							if offspring[a].genome[b].var == 'centerx':
								offspring[a].genome[b].value = random.choice(blackboard["dimx"])
							if offspring[a].genome[b].var == 'agentsy':
								offspring[a].genome[b].value = random.choice(blackboard["dimy"])
							if offspring[a].genome[b].var == 'agentsx':
								offspring[a].genome[b].value = random.choice(blackboard["dimx"])
							if offspring[a].genome[b].var == 'mediany':
								offspring[a].genome[b].value = random.choice(blackboard["dimy"])
							if offspring[a].genome[b].var == 'medianx':
								offspring[a].genome[b].value = random.choice(blackboard["dimx"])
							if offspring[a].genome[b].var == 'coverage':
								offspring[a].genome[b].value = random.choice(blackboard["coverage"])
							if offspring[a].genome[b].var == 'belief':
								offspring[a].genome[b].value = random.choice(blackboard["belief"])

	return offspring
Exemplo n.º 12
0
def mutate(offspring, mutrate, growrate, growdepth):

    for a in range(0, len(offspring)):
        for b in range(0, len(offspring[a].genome)):

            if random.random() <= mutrate:
                if random.random() <= growrate and b != 0 and b < len(
                        offspring[a].genome) and type(
                            offspring[a].genome[b]) is not Operator:
                    # Grow a randomly generated tree

                    subtree = tg.tree().make_tree(growdepth)

                    del offspring[a].genome[b]
                    offspring[a].genome[b:b] = subtree[:]

                else:

                    if type(offspring[a].genome[b]) is Action:
                        offspring[a].genome[b].type = random.choice([
                            'disperse', 'north', 'south', 'west', 'east',
                            'northwest', 'southwest', 'northeast', 'northwest'
                        ])

                    if type(offspring[a].genome[b]) is Param:
                        if offspring[a].genome[b].type == 'aggregate':
                            offspring[a].genome[b].param = random.choice(
                                [30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80])
                        else:
                            offspring[a].genome[b].param = random.choice([
                                1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55,
                                60
                            ])

                    if type(offspring[a].genome[b]) is Env_control:

                        choice = random.choice(['type', 'pos'])
                        if choice == 'type':
                            offspring[a].genome[b].type = random.choice(
                                ['attract', 'repel'])
                        if choice == 'pos':
                            offspring[a].genome[b].pos[0] = random.choice([
                                -34, -32, -30, -28, -26, -24, -22, -20, -18,
                                -16, -14, -12, -10, -8, -6, -4, -2, 0, 2, 4, 6,
                                8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30,
                                32, 34
                            ])
                            offspring[a].genome[b].pos[1] = random.choice([
                                -34, -32, -30, -28, -26, -24, -22, -20, -18,
                                -16, -14, -12, -10, -8, -6, -4, -2, 0, 2, 4, 6,
                                8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30,
                                32, 34
                            ])

                    if type(offspring[a].genome[b]) is Condition:

                        # offspring[a].genome[b].generate()
                        choice = random.choice(['var', 'value', 'op'])
                        if choice == 'var':
                            offspring[a].genome[b].var = random.choice(
                                ['centerx', 'centery', 'density'])
                        if choice == 'op':
                            offspring[a].genome[b].op = random.choice(
                                ['<', '>'])
                        if choice == 'value':
                            if offspring[a].genome[b].var == 'density':
                                offspring[a].genome[b].value = random.choice([
                                    1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
                                    25, 27, 29, 31, 33, 35
                                ])
                            if offspring[a].genome[b].var == 'centery':
                                offspring[a].genome[b].value = random.choice([
                                    -34, -32, -30, -28, -26, -24, -22, -20,
                                    -18, -16, -14, -12, -10, -8, -6, -4, -2, 0,
                                    2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24,
                                    26, 28, 30, 32, 34
                                ])
                            if offspring[a].genome[b].var == 'centerx':
                                offspring[a].genome[b].value = random.choice([
                                    -34, -32, -30, -28, -26, -24, -22, -20,
                                    -18, -16, -14, -12, -10, -8, -6, -4, -2, 0,
                                    2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24,
                                    26, 28, 30, 32, 34
                                ])
                            if offspring[a].genome[b].var == 'coverage':
                                offspring[a].genome[b].value = random.choice([
                                    0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9
                                ])

    return offspring
Exemplo n.º 13
0
    selectionNum = popsize - elitesize
    # The test duration for each search attempt

    #Log simulation settings
    filename = 'exercise1'
    filename = 'results/exercise1'
    hfile = filename + '_' + 'hallfame'
    pop_file = filename + '_' + 'pop'

    op.log_settings(swarm, popsize, indsize, tournsize, elitesize, mutrate,
                    NGEN, targets, state, timesteps, filename)

    # Generate starting population
    pop = []
    generations = [] * NGEN
    pop = [tg.individual(tg.tree().make_tree(indsize)) for t in range(popsize)]
    generations.append(pop)

    #  Logging variables
    logfit = []
    logpop = []
    logavg = []
    logmax = []

    # Start evolution!
    for i in range(0, NGEN):

        print('GEN: ', i)
        newpop = []
        # Serial execution
        evaluate.serial1(pop, swarm, targets, i, timesteps, treecost)