Ejemplo n.º 1
0
	def tournament(self,population,data,dataEvaluation,heuristicStrategy,attributeCount):
		gladiators = Population(self.gladiator,data,dataEvaluation,False,heuristicStrategy)
		for index in range(self.gladiator):
			chosenOne = random.randint(0,population.size()-1)
			gladiators.saveIndividuals(index,population.getIndividual(chosenOne))
		morpheus = gladiators.getFittest()
		return morpheus
Ejemplo n.º 2
0
def run_synth_test():
    """ Run a test with synthetic data and MCMC inference
    """
    # Parse command line args
    (options, args) = parse_cmd_line_args()
    print "Creating master population object"
    model = make_model(options.model, N=options.N)
    popn = Population(model)

    results_file = os.path.join(options.resultsDir, "geweke_results.pkl")
    if os.path.exists(results_file) and not options.force_recompute:
        with open(results_file) as f:
            x_smpls = cPickle.load(f)

    else:
        x0 = popn.sample()
        data = gen_synth_data(popn, x0, options.N, options.T)
        popn.set_data(data)

        # Perform inference
        N_samples = 1000
        x_smpls = geweke_test(popn, data, N_samples=N_samples)

        # Save results
        print "Saving results to %s" % results_file
        with open(results_file, "w") as f:
            cPickle.dump(x_smpls, f, protocol=-1)

    # Plot empirical parameter distributions
    print "Plotting results."
    plot_geweke_results(popn, x_smpls, model, resdir=options.resultsDir)
Ejemplo n.º 3
0
	def tourney(self):
		tourneyPop = Population(Breeder.tourneySize, False)

		for i in xrange(self.pop.size()):
			tourneyPop.addIndividual( self.pop.individuals[random.randrange(0,self.pop.size())] )

		return tourneyPop.getFittest()
Ejemplo n.º 4
0
 def test_evalutaion(self):
     pop1 = Population(4)
     result = pop1.evaluate()
     self.assertEqual(len(pop1.fitness_table), 4)
     self.assertEqual(pop1.fitness_table, result)
     self.assertIn(pop1.pop[0].fitness, [0, 1])
     self.assertEqual(pop1.pop[0].fitness, result[0])
class TestPopulationBitstring(unittest.TestCase):
    def setUp(self):
        bit0 = Bitstring("0000")
        bit1 = Bitstring("1111")
        col0 = ColicinBit(bit0)
        col1 = ColicinBit(bit1)
        org = Organism([col0], [ImmunityBit(bit0, 2)])
        self.pop = Population([org for _ in range(5)])

    def test_replicate(self):
        self.assertEqual(len(self.pop), 5)
        self.pop.replicate()
        self.assertEqual(len(self.pop), 10)

    def test_remove_self_toxic(self):
        self.assertEqual(len(self.pop), 5)
        self.pop.remove_self_toxic()
        self.assertEqual(len(self.pop), 5)

    def test_cull_by_iterative_colicin(self):
        self.assertEqual(len(self.pop), 5)
        self.pop.cull_by_iterative_colicin()
        self.assertEqual(len(self.pop), 5)

    def test_advance_generation(self):
        self.pop.advance_generation()

    def test_sample_with_replacement(self):
        self.pop.sample_with_replacement()
Ejemplo n.º 6
0
 def test_evolve(self):
     pop1 = Population(4)
     pop1.evolve()
     self.assertEqual(len(pop1.pop), 4)
     self.assertEqual(pop1.selected, [])
     self.assertEqual(pop1.selected_indexes, [])
     self.assertEqual(pop1.fitness_table, [])
Ejemplo n.º 7
0
	def test_population_step(self):
		p = Population()
		data_before = p.data[:]
		size_before = len(p)
		p.step()
		self.assertEqual(len(p.data), size_before)
		self.assertNotEqual(p.data, data_before)
Ejemplo n.º 8
0
class AI:
    def __init__(self):
        self.score = 0
        self.count = 0
        self.population = Population(10)

    def restart_game(self):
        if self.score > 0:
            print("{ ind_score: " + str(self.score) + " }")
            self.count += 1
            self.population.get_cur().score = self.score
            self.population.go_next()

    def get_allowed_moves(self, board):
        allowed = [False, False, False, False]

        for x in range(0, 4):
            for y in range(0, 4):
                if board[y][x] != None:
                    if y < 3 and board[y + 1][x] in (None, board[y][x]):
                        allowed[0] = True # DOWN
                    if y > 0 and board[y - 1][x] in (None, board[y][x]):
                        allowed[1] = True # UP
                    if x > 0 and board[y][x - 1] in (None, board[y][x]):
                        allowed[2] = True # LEFT
                    if x < 3 and board[y][x + 1] in (None, board[y][x]):
                        allowed[3] = True # RIGHT

        return allowed

    def get_move(self, score, board):
        self.score = score
        features = feature_reader.extract_features(board)
        g = self.population.get_cur()
        return g.decide(features, self.get_allowed_moves(board))
    def test_advance(self):
        big_pop = Population(
            [MockOrganism(1), MockOrganism(2),
                MockOrganism(90)], 3)

        big_pop.advance_generation()
        self.assertLessEqual(len(big_pop), 3)
    def test_leastfit_removal(self):
        this_pop = Population([MockOrganism(1), MockOrganism(2),
                               MockOrganism(100)], carrying_capacity=1)

        this_pop.remove_least_fit()
        self.assertIn(this_pop[0].eval_fit(),
                              [1, 2, 100])
        self.assertEqual(len(this_pop), 1)
Ejemplo n.º 11
0
 def test_normal_random_population_creation(self):
     # XXX: I suppose it's possible these asserts could fail because we're dealing with
     #      probabalistic outcomes; I haven't taken the time to prove these always work
     p = Population()
     p.randomize(1024)
     self.assertLess(len(p.coaches), len(p.students))
     self.assertLess(len(p.studying_coaches), len(p.coaches))
     self.assertLess(1, max([len(u.coaches()) for u in p.students]))
Ejemplo n.º 12
0
    def tournament_selection(self, pop):
        tournament = Population(self._tournament_size, False)

        for i in xrange(self._tournament_size):
            random_id = int(random.random() * pop.population_size())
            tournament.save_tour(i, pop.get_tour(random_id))

        return tournament.get_fittest()
Ejemplo n.º 13
0
def colicin_int_demo():
    org = Organism([Colicin(0)], [Immunity(0, 5)])
    pop = Population(org for _ in range(5))
    for gen in range(1000):
        pop.advance_generation()
        average_id = average_colicin_int(pop)
        if gen % 100 == 0:
            print("{}\t{}".format(gen, average_id))
Ejemplo n.º 14
0
    def create(self):

        population = Population()

        population.chromosomes = [self.chromosomeFactory.create() for _ in xrange(self.populationSize)]
        population.fitnessFunction = self.fitnessFunction

        return population
Ejemplo n.º 15
0
def sample_network_from_prior(model):
    """
    Sample a network from the prior
    """
    popn = Population(model)
    x_true = popn.sample()
    A_true = x_true['net']['graph']['A']

    return popn, x_true, A_true
Ejemplo n.º 16
0
 def test_pessimistic_random_population_creation(self):
     p = Population()
     # 10 students who are all infected, all teachers, and all study
     p.randomize(10, infect_rate=1, feature='A', coach_rate=1, coach_study_rate=1)
     self.assertEqual(10, p.N)
     self.assertEqual(p.N, len(p.population))
     self.assertEqual(len(p.population), len(p.infected))
     self.assertEqual(len(p.infected), len(p.students))
     self.assertEqual(len(p.students), len(p.coaches))
 def tournament_selection(self, population):
     population_size = len(population.individuals)
     tournament = Population(self.tournament_size, False)
     
     for i in range(0, self.tournament_size):
         randomId = int(random() * population_size)
         tournament.individuals.append(population.individuals[randomId])
     
     return tournament.get_fittest()
def tournament_selection(population):
    from random import random
    tournament = Population(tournament_size, False, population.tour_manager)
    
    for i in range(0, tournament_size):
        random_id = (int) (random() * tournament_size)
        tournament.save_tour(i, population.get_tour(random_id))
    
    fittest = tournament.get_fittest()
    return fittest
Ejemplo n.º 19
0
def evolve(target, replace):
    replacements = 0

    population = Population(500, target)
    while population.max_fitness() < len(target):
        replace(population)
        replacements = replacements + 1

    print("After",replacements,"replacements, the best individual is \"" +
          str(population.best_individual()) + "\".")
Ejemplo n.º 20
0
def initialize_client(model, data):
    """ Initialize a population objsect on the client
    """
    # Initialize a model with N neurons
    print "Initializing GLM"
    global popn
    popn = Population(model)

    # Initialize the GLM with the data
    popn.set_data(data)
Ejemplo n.º 21
0
def main(duration=10, pop_size=6):
    population = Population(pop_size)

    for i in range(duration):
        population.reproduce()
        population.survive()
        print([x.phenotype for x in population])

    genetic = [ind.genotype for ind in population]
    print(genetic)
Ejemplo n.º 22
0
def main():
    population = Population()
    fittest = population.fittest
    save(fittest)
    while True:
        time.sleep(0.5)
        population.nextGen()
        print 'Generation #{} fittest.weight: {} fittest.fitness: {}'.format(population.generation_counter, population.fittest.weight, population.fittest.fitness)
        if population.fittest.fitness > fittest.fitness:
            fittest = population.fittest
            save(fittest)
Ejemplo n.º 23
0
 def test_selection(self):
     pop1 = Population(4)
     result = pop1.evaluate()
     indexes, selected = pop1.selection()
     self.assertEqual(pop1.selected_indexes, indexes)
     self.assertEqual(pop1.selected, selected)
     self.assertEqual(len(selected), 2)
     self.assertEqual(len(indexes), 2)
     for ind in selected:
         self.assertEqual(ind.fitness, 1)
     for i in indexes:
         self.assertEqual(pop1.pop[i].fitness,1)
    def makeNewGeneration(self):
        new_population = Population()
        elected = self.population.getTheBest()
        new_population.add(elected)

        while len(new_population) < self.population_size:
            couple = self.population.selectACouple()
            child = Person.copulate(couple['man'], couple['woman'], self.contrainsts)
            if child is not None:
                new_population.add(child)

        self.population = new_population
Ejemplo n.º 25
0
	def evolve(self):
		newPop = Population(self.pop.size(), False)

		i = 0
		while i < self.pop.size():
			ind1 = self.tourney()
			ind2 = self.tourney()
			newInd = self.breed(ind1, ind2)
			newPop.addIndividual(newInd)
			i += 1

		return newPop
Ejemplo n.º 26
0
def colicin_bitstring_demo():
    mutate.mutation_rate = 0.05
    bit = Bitstring("0000000000000000000")
    bit2 = Bitstring("1000000000000000000")
    col = ColicinBit(bit)
    imm = ImmunityBit(bit2, 1)
    org = Organism([col], [imm, imm])
    pop = Population(org for _ in range(100))
    for gen in range(101):
        pop.advance_generation()
        if gen % 10 == 0:
            average_dist = average_colicin_bitstring_distance(pop, col)
            print("{}\t{}".format(gen, average_dist))
Ejemplo n.º 27
0
 def next_generation(self):
     self.world.stop()
     self.population.evaluate()
     new_pop = Population(rank_probability=self.RANK_PROBABILITY_CONSTANT, reverse_sort=False)
     for _ in xrange(self.RANDOM_ACTORS_NUMBER):
         new_pop.append(self.create_actor())
     for _ in xrange(self.POP_SIZE - self.RANDOM_ACTORS_NUMBER):
         new_genotype = Genotype.reproduce(self.population.select_by_rank().genotype,
                                           self.population.select_by_rank().genotype)
         new_pop.append(self.create_actor(genotype=new_genotype))
     self.population = new_pop
     self.current_generation += 1
     self.world.start()
class Environment():
    contrainsts = None
    turns = 1000
    convergence = 100
    population = None
    population_size = 20

    def __init__(self, filename=None, population_size=20, turns=100000, convergence=0.97):
        
        if filename is None:
            self.contrainsts = Event(size=20)
        else:
            self.contrainsts = Event(filename=filename)
        self.turns = turns
        self.convergence = convergence
        self.population_size = population_size


    def generatePopulation(self):
        self.population = Population(self.population_size, self.contrainsts)

    def makeNewGeneration(self):
        new_population = Population()
        elected = self.population.getTheBest()
        new_population.add(elected)

        while len(new_population) < self.population_size:
            couple = self.population.selectACouple()
            child = Person.copulate(couple['man'], couple['woman'], self.contrainsts)
            if child is not None:
                new_population.add(child)

        self.population = new_population

    def adapt(self):
        self.generatePopulation()
        #best_person = self.population.getTheBest()
        
        for turn in range(self.turns):
            self.makeNewGeneration()
            person = self.population.getTheBest()
            #convergence = self.population.calculateConvergence()
            #print "convergence is: "+str(convergence)
            print " i: "+str(turn)+" person "+str(person)
            #if person.weight < best_person.weight:
            #   best_person = person

            #if convergence >= self.convergence:
            #    break

        return self.population.getTheBest()
def run():
    cities = preprocessData("berlin.tsp")

    pop = Population(cities, GENERATION_SIZE)
    history = []
    i = 0
    best = pop.getBestMember().length()
    for j in range(SIMULATED_POPULATION_COUNT):
        # get next generation
        pop = pop.copyPopulation()
        pop.crossover(CROSSOVER_COUNT, CROSSOVER_SIZE_MIN, CROSSOVER_SIZE_MAX)
        pop.mutate(GENERATION_SIZE-CROSSOVER_COUNT, MUTATE_COUNT)

        best_member = pop.getBestMember()
        new_fitness = best_member.length()

        history.append(new_fitness)

        if(best > new_fitness):
            best = new_fitness

            if(PLOT_PATH):
                gra = int(122)
                plt.clf()
                for c in best_member._cities:
                    plt.plot([c.x], [c.y], color="red", aa=True, marker="o")
                    plt.annotate(int(c.name), (c.x, c.y), color="red")

                plt.title('Fitness = %.2fkm' % new_fitness)
                best_member.draw(plt, "#%02x%02x%02x" % (gra, gra, gra))
                plt.savefig("stats/fig%s.png" % i)
                i += 1
    return history
Ejemplo n.º 30
0
def transpogen(cipher, generations, individuals, size):
    start = time.time()
    print '[*] Genetic algorithm of {} individuals over {} generations'.format(individuals, generations)
    print '[*] Start at {}\n'.format(time.strftime('%X'))
    p = Population(cipher, individuals, size)

    for i in range(generations):
        p.generation()
        print '[+] ### Generation {} ### {}'.format(i, p)

    print '\n[*] End at {} ({} seconds)'.format(time.strftime('%X'), round(time.time() - start, 2))
    print '[*] Best solution is {}'.format(p.best())
    with open('plain.text', 'w') as solution_file:
        solution_file.write(p.plaintext())
Ejemplo n.º 31
0
        for j in range(int(col / 2), col):
            specimen_c.genotype_matrix[i][j] = specimen_b.genotype_matrix[i][j]

    return specimen_c


amount_of_species = 10
available_values = [1, 2, 5, 10, 20, 50]
statistical_day = np.random.random_integers(99, size=(10))
coin_to_save = [2, 5]
quantity_of_coins = 2  #rozmiar coin_to_save
expected_quantity_of_coins = [
    1, 2
]  #oczekiwana ilosc wydanych nominalow ktore chcemy zachowac

population = Population(amount_of_species, statistical_day)
population.calculate_changes_for_specimens(available_values)
population.rank()
population.calculate_cost(coin_to_save, quantity_of_coins,
                          expected_quantity_of_coins)
population.show_sorted_population_and_cost()
print("\n\n")
spec = Genotype()

spec = cross_mc(population.population[0], population.population[1])
print(spec.genotype_matrix)

for i in range(10):
    population.cross(coin_to_save, amount_of_species)
    population.calculate_cost(coin_to_save, quantity_of_coins,
                              expected_quantity_of_coins)
Ejemplo n.º 32
0
class Game(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)

        self.gap_factor = 0.25
        self.top = self.height / 2 + self.gap_factor * self.height
        self.middle = self.height / 2
        self.bottom = self.height / 2 - self.gap_factor * self.height
        self.show_game = True
        self.enemy_list = []
        self.frame_counter = 0
        self.display_info = True
        self.pop = Population(250, self)
        arcade.set_background_color(arcade.color.BLACK_OLIVE)

    def on_draw(self):
        """Called whenever we need to draw the window."""
        arcade.start_render()
        if not self.show_game:
            return

        players = [player for player in self.pop.pop.copy() if not player.dead]
        players.sort(key=lambda x: x.fitness, reverse=True)
        players_to_draw = min(len(players), 5)

        for i in range(players_to_draw):
            players[i].draw()

        if not self.pop.done():
            arcade.draw_text(
                f"Generation {self.pop.gen}\n\n" +
                f"Overall best score: {self.pop.best_score}",
                0.01 * self.width, 0.01 * self.height, arcade.color.WHITE, 14)
            arcade.draw_text("Current top player\n\n" +
                             f"Score: {players[0].score}\n" +
                             f"Fitness: {players[0].fitness}",
                             0.99 * self.width,
                             0.01 * self.height,
                             arcade.color.WHITE,
                             14,
                             align='right',
                             anchor_x='right')

        for enemy in self.enemy_list:
            enemy.draw()

    def on_update(self, delta_time):
        if self.pop.done() != True:
            self.frame_counter += 1
            if (self.frame_counter % 30 == 0):
                location = [self.bottom, self.middle, self.top]
                index = random.randrange(len(location))
                enemy = Enemy(self.width, location[index])
                self.enemy_list.append(enemy)

            for i, enemy in enumerate(self.enemy_list):
                enemy.update()
                if enemy.dead:
                    del self.enemy_list[i]
            self.enemy_list[:] = [
                enemy for enemy in self.enemy_list if not enemy.dead
            ]

            self.pop.update_alive()
            if self.display_info == True:
                print(f"Gen {self.pop.gen}")
                print(f"Best score: {self.pop.best_score}")
            self.display_info = False
        else:
            self.pop.natural_selection()
            self.reset_enemies()
            self.display_info = True

    def on_key_press(self, key, modifiers):
        """Called whenever the user presses a key."""
        if key == arcade.key.H:
            self.show_game = not self.show_game

    def on_key_release(self, key, modifiers):
        """Called whenever a user releases a key."""

    def reset_enemies(self):
        del self.enemy_list[:]
Ejemplo n.º 33
0
def run(pop,
        surveyList,
        nostdout=False,
        allsurveyfile=False,
        scint=False,
        accelsearch=False,
        jerksearch=False):

    """ Run the surveys and detect the pulsars."""

    # print the population
    if not nostdout:
        print "Running doSurvey on population..."
        print pop

    # loop over the surveys we want to run on the pop file
    surveyPops = []
    for surv in surveyList:
        s = Survey(surv)
        s.discoveries = 0
        if not nostdout:
            print "\nRunning survey {0}".format(surv)

        # create a new population object to store discovered pulsars in
        survpop = Population()
        # HERE SHOULD INCLUDE THE PROPERTIES OF THE ORIGINAL POPULATION

        # counters
        nsmear = 0
        nout = 0
        ntf = 0
        ndet = 0
        # loop over the pulsars in the population list
        for psr in pop.population:
            # pulsar could be dead (evolve!) - continue if so
            if psr.dead:
                continue

            # is the pulsar over the detection threshold?
            snr = s.SNRcalc(psr, pop)

            # add scintillation, if required
            # modifying S/N rather than flux is sensible because then
            # a pulsar can have same flux but change S/N in repeated surveys
            if scint:
                snr = s.scint(psr, snr)

            if snr > s.SNRlimit:
                ndet += 1
                psr.snr = snr
                survpop.population.append(psr)

                # check if the pulsar has been detected in other
                # surveys
                if not psr.detected:
                    # if not, set to detected and increment
                    # number of discoveries by the survey
                    psr.detected = True
                    s.discoveries += 1

            elif snr == -1.0:
                nsmear += 1
            elif snr == -2.0:
                nout += 1
            else:
                ntf += 1

        # report the results
        if not nostdout:
            print "Total pulsars in model = {0}".format(len(pop.population))
            print "Number detected by survey {0} = {1}".format(surv, ndet)
            print "Of which are discoveries = {0}".format(s.discoveries)
            print "Number too faint = {0}".format(ntf)
            print "Number smeared = {0}".format(nsmear)
            print "Number out = {0}".format(nout)
            print "\n"

        d = Detections(ndet=ndet,
                       ntf=ntf,
                       nsmear=nsmear,
                       nout=nout,
                       ndisc=s.discoveries)
        surveyPops.append([surv, survpop, d])

    if allsurveyfile:
        allsurvpop = Population()
        allsurvpop.population = [psr for psr in pop.population if psr.detected]
        surveyPops.append([None, allsurvpop, None])

    return surveyPops
class GPAlgorithm:
    def __init__(self, f, nrInd):
        self.n = 0
        self.input = []
        self.output = []
        self.inputTest = []
        self.outputTest = []
        self.inputTrain = []
        self.outputTrain = []
        self.filename = f
        self.nrInd = nrInd
        self.population = Population(nrInd)
        self.probability_mutate = 0.5
        self.probability_crossover = 0.5

    def run(self):
        global ITER
        self.loadData()
        self.population.evaluate(self.inputTrain, self.outputTrain)

        for i in range(ITER):
            print("Iteration: " + str(i))
            self.iteration(i)
            self.population.evaluate(self.inputTrain, self.outputTrain)
            self.population.selection(self.nrInd)
            best = self.population.best(1)[0]
            print("Best: " + str(best.root) + "\n" + "fitness: " +
                  str(best.fitness))

    def loadData(self):
        global TrainSIZE, HEADER
        print("Loading training data ... this may take a while")
        with open(self.filename, "r") as f:
            HEADER = f.readline().split(',')[1:-1]
            for line in f.readlines()[:10]:
                values = list(map(float, line.split(',')))
                self.input.append(values[1:-1])
                self.output.append(values[-1])
                self.n += 1
        shuffle(self.input)
        shuffle(self.output)

        self.inputTrain = self.input[:int(self.n * TrainSIZE)]
        self.outputTrain = self.output[:int(self.n * TrainSIZE)]
        self.inputTest = self.input[int(self.n * TrainSIZE):]
        self.outputTest = self.output[int(self.n * TrainSIZE):]

        print("Training size: " + str(len(self.inputTrain)))
        print("Testing size: " + str(len(self.outputTest)))

    def iteration(self, i):
        parents = range(self.nrInd)
        nrChildren = len(parents) // 2
        offspring = Population(nrChildren)
        for i in range(nrChildren):
            offspring.individuals[i] = Chromosome.crossover(
                self.population.individuals[i << 1],
                self.population.individuals[(i << 1) | 1],
                self.probability_crossover)
            offspring.individuals[i].mutate(self.probability_mutate)
        offspring.evaluate(self.inputTrain, self.outputTrain)
        self.population.reunion(offspring)
        self.population.selection(self.nrInd)
Ejemplo n.º 35
0
def test_get_number_immune():
    p1_pop = Population("New York", 20, p_virus, 1, 0.5)
    assert p1_pop.get_number_immune() is 10
    p1_pop = Population("New York", 20, p_virus, 1, 1.0)
    assert p1_pop.get_number_immune() is 20
Ejemplo n.º 36
0
class UI:
    
    def printMenu(self):
        print("0. Exit")
        print("1. Use EA")
        print("2. Use HillClimbing")
        print("3. Show EA's validation test")
        print("4. Use PSO")
        print("5. Show PSO's validation test")
    
    def __init__(self):
        self._population = Population(2,2)
        self._ea = EA(2)
        self._hc = HillClimbing(2)
        self._particles=ParticlePopulation(2,2)
        self._pso=PSO(self._particles)
        self._controller = Controller(self._population,self._ea,0,0,self._hc, self._particles.populationGenerate,self._pso)
      
        
    def readData(self):
        f = open("inputData.txt")
        x=f.readline().split()
        n=int(x[0])
        k=int(x[1])
        noIt=int(x[2])
        w=float(x[3])
        c1= float(x[4])
        c2= float(x[5])
        nSize=int(x[6])
        f.close()
        
        self._population.setSize(n,k)
        self._ea.setSize(n)
        self._hc.setSize(n)
        self._particles.setData(n,k)
        self._pso=PSO(self._particles)
        self._controller.setData(noIt,k)
        
        return (w,c1,c2,nSize)
        
    def runEA(self):
        (x,pop) = self._controller.iteration()
        for l in pop:
            print(l)
            
    def runHC(self):
        ind= self._controller.iterationHC()
        for l in ind:
            print(l)
            
    def validationEA(self):
        '''
        displays a graphical evaluation of the EA algorithm
        '''
        pop=[]
        avg=[]
        std=[]
        n=self._population._n
        for i in range(0,30):
            pop.append([])
            self._population= Population(n,40)
            self._controller= Controller(self._population,self._ea,1000,40,self._hc,self._particles,self._pso)
            for j in range (0,10):
                (ind,x)=self._controller.iteration()
                for k in ind:
                    pop[i].append(self._population.fitness(k))
            std.append(np.std(pop[i]))
            avg.append(np.mean(pop[i]))
        std =plt.plot(std,color='b',label='Standard deviation')
        avg =plt.plot(avg,color='r',label='Average')
        #plt.legend(handles[std,avg])
        plt.show()
                
    
    def runPSO(self,w,c1,c2,nSize):
        pop=self._controller.iterationPSO(w,c1,c2,nSize)
        best=pop[0]
        for i in pop:
            if i.bestFit<best.bestFit:
                best=i
        for l in best:
            print(l)
            
    def run(self):
        (w,c1,c2,nSize)=self.readData()
        while True:
            self.printMenu()
            opt = input("Choose an option: ")
            if opt == "1":
                self.runEA()
            elif opt == "2":
                self.runHC()
            elif opt == "3":
                self.validationEA()
            elif opt == "4":
                self.runPSO(w,c1,c2,nSize)
            elif opt == "0":
                return
Ejemplo n.º 37
0
class TestPopulation(TC):
    def setUp(self):
        self.orgs = [
            MockOrganism(1),
            MockOrganism(2),
            MockOrganism(3),
            MockOrganism(4)
        ]

        self.pop = Population(self.orgs)

    def test_init_nogivensize(self):
        self.assertEqual(self.pop.carrying_capacity, 4)

    def test_init_givensize(self):
        population = Population([1, 2, 3, 4], carrying_capacity=5)
        self.assertEqual(population.carrying_capacity, 5)

    def test_removal(self):
        population = Population([1, 2, 3], carrying_capacity=2)
        population.remove_at_random()
        self.assertEqual(len(population), 2)

    def test_leastfit_removal(self):
        this_pop = Population(
            [MockOrganism(1),
             MockOrganism(2),
             MockOrganism(100)],
            carrying_capacity=1)

        this_pop.remove_least_fit()
        self.assertIn(this_pop[0].eval_fit(), [1, 2, 100])
        self.assertEqual(len(this_pop), 1)

    def test_replicate(self):
        self.pop.replicate()
        self.assertEqual(8, len(self.pop))

    def test_advance(self):
        big_pop = Population(
            [MockOrganism(1),
             MockOrganism(2),
             MockOrganism(90)], 3)

        big_pop.advance_generation()
        self.assertLessEqual(len(big_pop), 3)

    def test_iter_len(self):
        self.assertEqual(4, len(self.pop))

        for org, poporg in zip(self.orgs, self.pop):
            self.assertEqual(org, poporg)

    def test_add_to_pop(self):
        org = MockOrganism(6)
        self.pop.add_to_pop(org)
        self.assertEqual(5, len(self.pop))

    def test_set(self):
        self.pop[2] = MockOrganism(6)
        print self.pop[2]
        self.assertEqual(self.pop[2].eval_fit(), MockOrganism(6).eval_fit())

    def test_full(self):
        self.assertTrue(self.pop.is_full())

    def test_max_fit(self):
        self.assertEqual(self.pop.max_fitness(), MockOrganism(4).eval_fit())

    def test_mean_fit(self):
        self.assertEqual(self.pop.mean_fitness(), 2.5)
Ejemplo n.º 38
0
class Game:
    def __init__(self):
        pygame.init()
        pygame.font.init()
        self.font = pygame.font.SysFont(pygame.font.get_default_font(), 20)
        self.screen = pygame.display.set_mode((500, 850))
        self.clock = pygame.time.Clock()
        self.fps = 60
        self.best = 0
        self.x = 0
        self.iteration = 1
        self.running = False
        self.all_sprites = pygame.sprite.Group()
        self.bottom_blocks = pygame.sprite.Group()
        self.blocks = pygame.sprite.Group()
        self.birds = pygame.sprite.Group()
        self.bg = pygame.transform.scale2x(pygame.image.load(os.path.join("sprites/bg.png")))
        self.population = Population(self, 50)
        self.population.init_population()

    def handle_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

    def get_next_bottom_block_data(self):
        next_block = None
        next_block_id = 0

        for block in self.bottom_blocks:
            if 100 < block.rect.x:
                next_block = block
                break

        return {'x': next_block.rect.x, 'y': next_block.rect.y}

    def remove_unused_blocks(self):
        for block in self.blocks:
            if block.rect.x <= -60:
                block.kill()

    def generate_block(self):
        h = random.randint(250, 500)
        block_top = Block(Block.TYPE_TOP, 700, h)
        block_bottom = Block(Block.TYPE_BOTTOM, 700, h)
        
        self.all_sprites.add(block_top)
        self.all_sprites.add(block_bottom)
        self.blocks.add(block_top)
        self.bottom_blocks.add(block_bottom)
        self.blocks.add(block_bottom)



    def update(self):

        for bird in self.birds:
            bird.update_sprite(self.screen)
            if not bird.is_killed:
                if pygame.sprite.spritecollide(bird, self.blocks, False):
                    bird.killed(self.x)
                elif bird.rect.y < -100 or 750 < bird.rect.y:
                    bird.killed(self.x)
        
        if self.population.get_alive_count() == 0:
            self.reset()
            return

        if self.x % 100 == 0:
            self.generate_block()

        self.x += 1
        self.remove_unused_blocks()
        self.all_sprites.update()

    def reset(self):
        self.x = 0
        self.birds.empty()
        self.blocks.empty()
        self.bottom_blocks.empty()
        self.all_sprites.empty()
        self.population.over()
        self.iteration += 1

    def redraw(self):
        self.screen.blit(self.bg, (0, 0))
        self.all_sprites.draw(self.screen)
        textsurface = self.font.render('Iteration: ' + str(self.iteration) + '  Current: ' + str(self.x) + '  Alive birds: ' + str(self.population.get_alive_count()) + '  Best result: ' + str(self.best), True, (255, 255, 255))
        self.screen.blit(textsurface, (0, 0))

    def run(self):
        base = Base(750)
        while True:
            self.handle_events()
            self.update()
            self.redraw()
            base.draw(self.screen)
            base.move()
            pygame.display.flip()
            self.clock.tick_busy_loop(self.fps)
Ejemplo n.º 39
0
class Game(object):
    ''' Class containing whole snake game.
    '''
    def __init__(self, window_y, window_x, population_size, mutation_rate,
                 crossover_rate):

        self.screen_size = (window_y, window_x)  #self-explanatory
        self.level = 0  #making gameboard (dummy for now)
        self.morsel = Apple(self.screen_size)  #making apple
        self.population = Population(  #snake family
            population_size=population_size,
            mutation_rate=mutation_rate,
            crossover_rate=crossover_rate,
            screen=self.screen_size)
        self.population.first_generation()  #cpus snakes
        self.pressed_key = 0  #dummy for exiting

    def game(self, stdscr):
        ''' Actual game'''

        self.level = GameBoard(self.screen_size)  #making gameboard

        #game loop
        while self.pressed_key != ord('q'):  #main while loop

            #PLAYER CONTROL SECTION
            self.pressed_key = self.level.board.getch(
            )  #getting key from player

            #CPUs SECTIONS
            for cpu in self.population.cpus:
                self.level.unprint(cpu)  #erasing all snakes

                cpu.move(self.morsel)  #generating moves

                if cpu.collides():  #collision checking
                    self.population.cpus.remove(cpu)
                    self.level.unprint(cpu)

                if cpu.eats(self.morsel):  #snake eats an apple
                    pass  #temporarly disabled

                if cpu.starved():  #if snake dies of
                    self.population.cpus.remove(cpu)  #starvation
                    self.level.unprint(cpu)  #it need to be killed

            #IS FUN OVER?
            if self.population.empty():  #checking if board has its winner
                self.level.unprint(self.population.cpus[0])
                self.population.new_generation()  #winner is having a child

            #SCREEN SECTION                           #printing everything out
            for cpu in self.population.cpus:  #printing every snake
                self.level.inprint(cpu)

            self.level.board.border()
            self.level.print_aux(
                self.morsel,  #print nesseceties
                self.population.generation)

            self.level.board.refresh()  #refreshing the screen

    def main(self):
        curses.wrapper(self.game)
Ejemplo n.º 40
0
def init_population(dic, puzzles, size=100, inhabitant_start_size=5):
    return Population(puzzles, size, inhabitant_start_size)
Ejemplo n.º 41
0
    def run(self):
        self.problem_class.pre_run_hook()

        population = Population(
            self.args.population_size, self.problem_class, self.genotype_class,
            self.individual_class, self.args.adult_selection_method,
            self.args.parent_selection_method, self.args.adult_pool_size,
            self.args.crossover_rate, self.args.mutation_rate)

        for generation in range(self.args.num_generations):
            if not self.args.silent:
                print '---------'
                print 'generation', generation

            population.set_generation(generation)
            population.generate_phenotypes()
            population.evaluate_all()
            if self.args.stop_early and population.is_solution_found:
                print 'A solution has been found in generation {}:'.format(
                    generation)
                print population.solution
                break
            population.adult_selection_handler.select_adults()
            population.log_stats(self.args.silent)
            population.parent_selection_handler.select_parents()
            population.reproduce()

        self.problem_class.post_run_hook(population)

        return population
Ejemplo n.º 42
0
    def nextGeneration(self, population, data, dataEvaluation,
                       heuristicStrategy, attributeCount):
        nextGen = Population(1 + (population.size() * 2), data, dataEvaluation,
                             False, heuristicStrategy)
        nextGen.saveIndividuals(0, population.getFittest())
        count = 1
        for index in range(population.size()):
            challenger = self.tournament(population, data, dataEvaluation,
                                         heuristicStrategy, attributeCount)
            champion = self.tournament(population, data, dataEvaluation,
                                       heuristicStrategy, attributeCount)
            nextGenChallenger, nextGenChamp = self.crossover(
                challenger, champion, int(np.floor(challenger.size() / 2)),
                heuristicStrategy)
            nextGen.saveIndividuals(count, nextGenChamp)
            nextGen.saveIndividuals(count + 1, nextGenChallenger)
            count += 2

        for index in range(nextGen.size()):
            neo = self.xmen(nextGen.getIndividual(index))
            nextGen.saveIndividuals(index, neo)
        return nextGen
Ejemplo n.º 43
0
                 capsize=5,
                 label='{0}nW, n~5000'.format(d2['power'].unique()[0]))
#    plt.errorbar(Population().rhosum(d2[xvar],P),d2[yvar],yerr=d2[yvar]*d2['g_std'], fmt='o', capsize=5, label='{0}nW, n~5000'.format(d2['power'].unique()[0]))
#  plt.semilogy(1/(d2[xvar]**2+Omega**2),d2[yvar], linestyle='', marker='^', label='{0}nW, n~5000'.format(d1['power'].unique()[0]))

if 'd3' in dir():
    plt.errorbar(d3[xvar],
                 d3[yvar],
                 yerr=d3[yvar] * d3['g_std'],
                 fmt='o',
                 capsize=5,
                 label='{0}nW, n~9000'.format(d3['power'].unique()[0]))
#    plt.errorbar(Population().rhosum(d3[xvar],P),d3[yvar],yerr=d3[yvar]*d3['g_std'], fmt='o', capsize=5, label='{0}nW, n~9000'.format(d3['power'].unique()[0]))
#  plt.semilogy(1/(d3[xvar]**2+Omega**2),d3[yvar], linestyle='', marker='*', label='{0}nW, n~9000'.format(d1['power'].unique()[0]))

plt.plot(xp, fitParams[0] * Population().rhosum(xp, P) + fitParams[1])

print("slope = {0:.3f}".format(fitParams[0]))
print("intercept = {0:.3f}".format(fitParams[1]))

plt.legend(fontsize=16)
#plt.ylim(0,1.2*max(d2[yvar]))
plt.xlabel('detuning (GHz)', fontsize=18)
#plt.xlabel('$\sum _{F\'=3,4,5} \ \dfrac{\Omega_{F\'}^{2}}{4 \Delta_{F\'}^{2} + 2 \Omega_{F\'}^{2}}$', fontsize=16)
plt.ylabel('$\Gamma_R$/2$\pi$ (kHz)', fontsize=18)
plt.xticks(fontsize=18)
plt.yticks(fontsize=18)
#ax.text(3500,8,'Slope = 17.85kHz/$\mu$W', fontsize=16)
plt.tight_layout()
plt.savefig('gamma_detuning.svg')
#plt.show()
Ejemplo n.º 44
0
 def test_removal(self):
     population = Population([1, 2, 3], carrying_capacity=2)
     population.remove_at_random()
     self.assertEqual(len(population), 2)
Ejemplo n.º 45
0
 def test_init_givensize(self):
     population = Population([1, 2, 3, 4], carrying_capacity=5)
     self.assertEqual(population.carrying_capacity, 5)
Ejemplo n.º 46
0
def fitFunc(x, a, b):
    return a * Population().rhosum(x, P) + b
Ejemplo n.º 47
0
import sys

# from logger import logger


# TEST PATHOGEN
def test_print_pathogen_info():
    ebola = Pathogen("ebola", 0.70, 0.25)
    ebola_greeting = ebola.print_info(False)
    assert "ebola" in ebola_greeting
    assert "70" in ebola_greeting
    assert "25" in ebola_greeting


virus = Pathogen("the black plague", 0.5, 0.5)
my_pop = Population("1437 France", 30, virus, 3, 0.5)


def clear_log_file():
    file = open("NO_FILE_SPECIFIED.md", "w+")
    file.write("# this file for testing")
    print("File cleared.")
    print(file.read())


def read_log_file():
    file = open("NO_FILE_SPECIFIED.md", "r")
    print("File read.")
    return file.read()

Ejemplo n.º 48
0
sim_root = '/home/danielk/IRF/E3D_PA/FP_sims/T_UHF_MicrosatR_v2'

#initialize the radar setup
radar = rlib.eiscat_uhf()

radar.set_FOV(max_on_axis=25.0, horizon_elevation=50.0)
radar.set_SNR_limits(min_total_SNRdb=14.0, min_pair_SNRdb=14.0)

radar.set_scan(scan)

fname = sim_root + '/population_{}.h5'.format(branch_name)
catname = sim_root + '/' + branch_name + '/catalogue_data.h5'

pop = Population.load(
    fname,
    propagator=_prop,
    propagator_options=_opts,
)

cat = Catalogue.from_file(pop, catname)

time_step = 80.0
#time_step = 600.0

t_end = (mjd - event_mjd + SIM_TIME / 24.0) * 3600.0 * 24.0
t_meas = (mjd - event_mjd) * 3600.0 * 24.0
#_deb_tt = np.arange(0, 3600.0*12.0, time_step, dtype=np.float64)
_deb_tt = np.arange(0, 3600.0 * 6.0, time_step, dtype=np.float64)
#_mes_tt = np.arange(t_meas, t_end, time_step, dtype=np.float64)
#tt = np.append(
#    _deb_tt,
Ejemplo n.º 49
0
class RootWindow(object):
    def __init__(self):
        self.win = tk.Tk()
        self.configWin()
        self.spawnTarget()
        self.spawnObst()
        self.pop = Population(100, self.canv, self.targetPos, self.obstPos)
        self.LIFESPAN = DNA().LIFESPAN
        self.cnt = 0
        self.win.mainloop()

    def configWin(self):
        col = '#%02x%02x%02x' % (51, 51, 51)
        self.fps = int(1000 / 60)
        self.canv = tk.Canvas(self.win, width=800, height=400, background=col)
        self.canv.pack()
        self.win.after(self.fps, self.update)

    def update(self):
        # DO ALL THE STUFF HERE
        self.pop.run(self.cnt)
        self.cnt += 1
        if (self.cnt == self.LIFESPAN):
            self.pop.evaluate()
            self.pop.selection()
            self.cnt = 0

        self.canv.update()
        self.win.after(self.fps, self.update)

    def spawnObst(self):
        l = ([300, 200], [500, 200], [600, 50], [600, 350])
        self.obstPos = np.array(l)
        for p in l:
            x0 = p[0] - 20
            x1 = p[0] + 20
            if (p[0] == 500):
                y0 = p[1] - 50
                y1 = p[1] + 50
            else:
                y0 = p[1] - 100
                y1 = p[1] + 100
            self.obstID = self.canv.create_rectangle(x0,
                                                     y0,
                                                     x1,
                                                     y1,
                                                     fill="red",
                                                     outline="red")

    def spawnTarget(self):
        self.targetPos = np.array([750, 200])
        x0 = self.targetPos[0] - 10
        x1 = self.targetPos[0] + 10
        y0 = self.targetPos[1] - 10
        y1 = self.targetPos[1] + 10
        self.targetId = self.canv.create_oval(x0,
                                              y0,
                                              x1,
                                              y1,
                                              fill="green",
                                              outline="green")
Ejemplo n.º 50
0
def main():
    """Parse option"""
    if len(sys.argv) != 1:
        #Evaluate a single genome
        if str(sys.argv[1]) == "-evaluate":
            initPygame("FlapAI: Evaluating")
            print(str(sys.argv[2]))
            net = load(str(sys.argv[2]))
            genome = Genome(net)
            global savestat
            savestat = False
            fitness = []
            for i in range(100):
                fit = playGame(genome)
                fitness.append(fit.fitness)
                print("fitness : %s " % fit.fitness)

            average = sum(fitness) / float(len(fitness))
            printc("Average fitness : %s" % average, "red")
            pygame.quit()
            sys.exit()
        #Show the stat of an experiment
        if str(sys.argv[1]) == "-stats":
            showStat(str(sys.argv[2]))

    #No argument, starting FlapAI
    initPygame("FlapAI: Learning")
    """Init the population"""
    global bestFitness
    bestFitness = 0

    population = Population()
    population.generateRandomPopulation()
    generation = 1
    maxgeneration = Config.maxGeneration

    lastgenerationaveragefitness = 0
    #Main Loop
    while generation <= maxgeneration:
        birdnmbr = 1

        for i in range(population.size()):

            genome = playGame(population.getGenome(i))
            population.setGenomeFitness(i, genome.fitness)
            informationforscreen = {
                'generation': generation,
                'birdnumber': birdnmbr,
                'lastfitness': genome.fitness,
                'lastgenerationaveragefitness': lastgenerationaveragefitness,
                'bestfitness': bestFitness
            }
            updateScreen(informationforscreen)
            if genome.fitness > bestFitness:
                #global bestFitness
                bestFitness = genome.fitness
                genome.network.save(today + "/bestfitness.json")
            birdnmbr += 1

        global fitnessovergeneration
        fitnessovergeneration.append(population.averageFitness())

        lastgenerationaveragefitness = population.averageFitness()

        global fittestovergeneration
        fittestovergeneration.append(population.findFittest().fitness)
        #Evolve the population
        population.evolvePopulation()
        generation += 1
Ejemplo n.º 51
0
    def __init__(self, window, cities_list, show_window=True):
        if show_window:
            self.ini = window.figure.add_subplot(211)
            self.end = window.figure.add_subplot(212)

        self._cities_list = cities_list
        random.seed()

        pop = Population(1000, True, self._cities_list)
        if show_window:
            self.plt_reload(211, self._cities_list, 'Starting')
        print 'Initial distance: %.4f' % (pop.get_fittest().get_distance())
        self.x_graph.append(0)
        self.y_graph.append(pop.get_fittest().get_distance())

        for j in xrange(100):
            new_population = Population(pop.population_size(), False)

            elitism_offset = 0
            if self._elitism:
                new_population.save_tour(0, pop.get_fittest())
                elitism_offset = 1

            for i in xrange(elitism_offset, new_population.population_size()):
                parent1 = self.tournament_selection(pop)
                parent2 = self.tournament_selection(pop)

                # Crossover parents
                child = self.crossover(parent1, parent2)

                new_population.save_tour(i, child)

            # mutate
            for i in xrange(elitism_offset, new_population.population_size()):
                self.mutate(new_population.get_tour(i))
            pop = new_population
            if show_window:
                self.plt_reload(212,
                                pop.get_fittest().get_tour(),
                                'Iteration: ' + str(j + 1))
            self.x_graph.append(self.x_graph[-1] + 1)
            self.y_graph.append(pop.get_fittest().get_distance())

        if show_window:
            self.plt_reload(212,
                            pop.get_fittest().get_tour(),
                            'Iteration: ' + str(j + 1))
        self.x_graph.append(self.x_graph[-1] + 1)
        self.y_graph.append(pop.get_fittest().get_distance())

        print 'Final distance: %.4f' % (pop.get_fittest().get_distance())
        self.best_distance = pop.get_fittest().get_distance()
Ejemplo n.º 52
0
      if population.population[i][j].is_dead:
        canvas.create_rectangle(x, y, x+square_dimension, y+square_dimension, fill='red')
      else:
        if population.population[i][j].is_infected:
          canvas.create_rectangle(x, y, x+square_dimension, y+square_dimension, fill='yellow')
        else:
          canvas.create_rectangle(x, y, x+square_dimension, y+square_dimension, fill='green')

sim = Simulation()
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 600
sim_window = tkinter.Tk()
sim_window.title("Epidemic Outbreak")
sim_canvas = tkinter.Canvas(sim_window, width=WINDOW_WIDTH, height=WINDOW_HEIGHT, bg='lightblue')
sim_canvas.pack(side=tkinter.LEFT)

pop = Population(sim)
pop.initial_infection(sim)
pop.display_statistics(sim)
input("Press Enter to begin simulation.")
for i in range(1, sim.sim_days):
  pop.spread_infection(sim)
  pop.update(sim)
  pop.display_statistics(sim)
  graphics(sim, pop, sim_canvas)

  sim_window.update()

  if i != sim.sim_days-1:
    sim_canvas.delete('all')
Ejemplo n.º 53
0
    def __init__(self, dataset, n_iter, pop_size, batch_size, epochs, min_layer, max_layer, \
        conv_prob, pool_prob, fc_prob, max_conv_kernel, max_out_ch, max_fc_neurons, dropout_rate):
        
        self.pop_size = pop_size
        self.n_iter = n_iter
        self.epochs = epochs

        self.batch_size = batch_size
        self.gBest_acc = np.zeros(n_iter)
        self.gBest_test_acc = np.zeros(n_iter)

        if dataset == "mnist":
            input_width = 28
            input_height = 28
            input_channels = 1
            output_dim = 10

            (self.x_train, self.y_train), (self.x_test, self.y_test) = mnist.load_data()
        
        if dataset == "fashion-mnist":
            input_width = 28
            input_height = 28
            input_channels = 1
            output_dim = 10

            (self.x_train, self.y_train), (self.x_test, self.y_test) = fashion_mnist.load_data()

            self.x_train = self.x_train.astype('float32')
            self.x_test = self.x_test.astype('float32')
            self.x_train /= 255
            self.x_test /= 255

        if dataset == "mnist-background-images":
            input_width = 28
            input_height = 28
            input_channels = 1
            output_dim = 10

            train = np.loadtxt("./datasets/mnist-background-images/mnist_background_images_train.amat")
            test = np.loadtxt("./datasets/mnist-background-images/mnist_background_images_test.amat")

            self.x_train = train[:, :-1]
            self.x_test = test[:, :-1]

            # Reshape images to 28x28
            self.x_train = np.reshape(self.x_train, (-1, 28, 28))
            self.x_test = np.reshape(self.x_test, (-1, 28, 28))

            self.y_train = train[:, -1]
            self.y_test = test[:, -1]

        if dataset == "mnist-rotated-digits":
            input_width = 28
            input_height = 28
            input_channels = 1
            output_dim = 10

            train = np.loadtxt("./datasets/mnist-rotated-digits/mnist_all_rotation_normalized_float_train_valid.amat")
            test = np.loadtxt("./datasets/mnist-rotated-digits/mnist_all_rotation_normalized_float_test.amat")

            self.x_train = train[:, :-1]
            self.x_test = test[:, :-1]

            # Reshape images to 28x28
            self.x_train = np.reshape(self.x_train, (-1, 28, 28))
            self.x_test = np.reshape(self.x_test, (-1, 28, 28))

            self.y_train = train[:, -1]
            self.y_test = test[:, -1]

        if dataset == "mnist-random-background":
            input_width = 28
            input_height = 28
            input_channels = 1
            output_dim = 10

            train = np.loadtxt("./datasets/mnist-random-background/mnist_background_random_train.amat")
            test = np.loadtxt("./datasets/mnist-random-background/mnist_background_random_test.amat")

            self.x_train = train[:, :-1]
            self.x_test = test[:, :-1]

            # Reshape images to 28x28
            self.x_train = np.reshape(self.x_train, (-1, 28, 28))
            self.x_test = np.reshape(self.x_test, (-1, 28, 28))

            self.y_train = train[:, -1]
            self.y_test = test[:, -1]

        if dataset == "mnist-rotated-with-background":
            input_width = 28
            input_height = 28
            input_channels = 1
            output_dim = 10

            train = np.loadtxt("./datasets/mnist-rotated-with-background/mnist_all_background_images_rotation_normalized_train_valid.amat")
            test = np.loadtxt("./datasets/mnist-rotated-with-background/mnist_all_background_images_rotation_normalized_test.amat")

            self.x_train = train[:, :-1]
            self.x_test = test[:, :-1]

            # Reshape images to 28x28
            self.x_train = np.reshape(self.x_train, (-1, 28, 28))
            self.x_test = np.reshape(self.x_test, (-1, 28, 28))

            self.y_train = train[:, -1]
            self.y_test = test[:, -1]

        if dataset == "rectangles":
            input_width = 28
            input_height = 28
            input_channels = 1
            output_dim = 2

            train = np.loadtxt("./datasets/rectangles/rectangles_train.amat")
            test = np.loadtxt("./datasets/rectangles/rectangles_test.amat")

            self.x_train = train[:, :-1]
            self.x_test = test[:, :-1]

            # Reshape images to 28x28
            self.x_train = np.reshape(self.x_train, (-1, 28, 28))
            self.x_test = np.reshape(self.x_test, (-1, 28, 28))

            self.y_train = train[:, -1]
            self.y_test = test[:, -1]

        if dataset == "rectangles-images":
            input_width = 28
            input_height = 28
            input_channels = 1
            output_dim = 2

            train = np.loadtxt("./datasets/rectangles-images/rectangles_im_train.amat")
            test = np.loadtxt("./datasets/rectangles-images/rectangles_im_test.amat")

            self.x_train = train[:, :-1]
            self.x_test = test[:, :-1]

            # Reshape images to 28x28
            self.x_train = np.reshape(self.x_train, (-1, 28, 28))
            self.x_test = np.reshape(self.x_test, (-1, 28, 28))

            self.y_train = train[:, -1]
            self.y_test = test[:, -1]

        if dataset == "convex":
            input_width = 28
            input_height = 28
            input_channels = 1
            output_dim = 2

            train = np.loadtxt("./datasets/convex/convex_train.amat")
            test = np.loadtxt("./datasets/convex/convex_test.amat")

            self.x_train = train[:, :-1]
            self.x_test = test[:, :-1]

            # Reshape images to 28x28
            self.x_train = np.reshape(self.x_train, (-1, 28, 28))
            self.x_test = np.reshape(self.x_test, (-1, 28, 28))

            self.y_train = train[:, -1]
            self.y_test = test[:, -1]

        self.x_train = self.x_train.reshape(self.x_train.shape[0], self.x_train.shape[1], self.x_train.shape[2], input_channels)
        self.x_test = self.x_test.reshape(self.x_test.shape[0], self.x_test.shape[1], self.x_test.shape[2], input_channels)

        self.y_train = keras.utils.to_categorical(self.y_train, output_dim)
        self.y_test = keras.utils.to_categorical(self.y_test, output_dim)

        print("Initializing population...")
        self.population = Population(pop_size, min_layer, max_layer, input_width, input_height, input_channels, conv_prob, pool_prob, fc_prob, max_conv_kernel, max_out_ch, max_fc_neurons, output_dim)
        
        print("Verifying accuracy of the current gBest...")
        print(self.population.particle[0])
        self.gBest = deepcopy(self.population.particle[0])
        self.gBest.model_compile(dropout_rate)
        hist = self.gBest.model_fit(self.x_train, self.y_train, batch_size=batch_size, epochs=epochs)
        test_metrics = self.gBest.model.evaluate(x=self.x_test, y=self.y_test, batch_size=batch_size)
        self.gBest.model_delete()
        
        self.gBest_acc[0] = hist.history['accuracy'][-1]
        self.gBest_test_acc[0] = test_metrics[1]
        
        self.population.particle[0].acc = hist.history['accuracy'][-1]
        self.population.particle[0].pBest.acc = hist.history['accuracy'][-1]

        print("Current gBest acc: " + str(self.gBest_acc[0]) + "\n")
        print("Current gBest test acc: " + str(self.gBest_test_acc[0]) + "\n")

        print("Looking for a new gBest in the population...")
        for i in range(1, self.pop_size):
            print('Initialization - Particle: ' + str(i+1))
            print(self.population.particle[i])

            self.population.particle[i].model_compile(dropout_rate)
            hist = self.population.particle[i].model_fit(self.x_train, self.y_train, batch_size=batch_size, epochs=epochs)
           
            self.population.particle[i].acc = hist.history['accuracy'][-1]
            self.population.particle[i].pBest.acc = hist.history['accuracy'][-1]

            if self.population.particle[i].pBest.acc >= self.gBest_acc[0]:
                print("Found a new gBest.")
                self.gBest = deepcopy(self.population.particle[i])
                self.gBest_acc[0] = self.population.particle[i].pBest.acc
                print("New gBest acc: " + str(self.gBest_acc[0]))

                test_metrics = self.gBest.model.evaluate(x=self.x_test, y=self.y_test, batch_size=batch_size)
                self.gBest_test_acc[0] = test_metrics[1]
                print("New gBest test acc: " + str(self.gBest_acc[0]))
            
            self.population.particle[i].model_delete()
            self.gBest.model_delete()
Ejemplo n.º 54
0
# with open('preferences_12.csv') as csv_file:
with open(CSV_FILE) as csv_file:
    readCSV = csv.reader(csv_file, delimiter=',')
    iter_csv = iter(readCSV)
    names = next(iter_csv)
    for name in names:
        global_names.append(name)
    # i = 0
    # for row in readCSV:
    #     person = Person(i, row)
    #     guest_list.insert(i, person)
    #     i = i + 1
    names.pop(0)  # gets rid of the ''

    for index, row in enumerate(iter_csv):
        if len(row) < 1:
            continue
        name = row.pop(0)
        person = Person(index, names[index], row)
        if len(person.preferences) > 0:
            guest_list.insert(index, person)

number_tables = math.ceil(number_guests / table_size)
print("The number of guests and tables for this simulation is:",
      len(guest_list), number_tables)

population = Population(number_tables, table_size, guest_list)
population.evolve_generations(
    NUMBER_GENERATIONS,
    False)  # True A only, False B runs with Diversity enhancement
Ejemplo n.º 55
0
ANT_ID = 0
def get_ant_id():
    global ANT_ID
    ANT_ID += 1
    return ANT_ID

def create_new_ant():
    sensing_area = random.randint(1, 10)
    p_repeat = random.random()
    p_target = random.random()
    p_pheromone = random.random()
    new_ant = Ant(sensing_area, p_repeat, p_target, p_pheromone, get_id = get_ant_id)
    return new_ant


pop = Population(create_new_ant, pop_size=c.POP_SIZE)
env = Environment(N=c.GRID_SIZE, foodRemaining=c.FOOD_INITIAL)

#fitMatrix = np.zeros((c.NUM_GENS, c.POP_SIZE))

for g in range(c.NUM_GENS):
    if g==c.NUM_GENS-1:
        c.VISUALS = True
    #reset grid before each simulation
    env.create_grid()
    data = pop.evaluate(env)
    fitVec = pop.get_fitness()
    print('Generation %03d'%g, ['%0.3f'%x for x in fitVec])
    #fitMatrix[g] = fitVec
    pop.selection()
    
Ejemplo n.º 56
0
def run(pop,
        surveyList,
        nostdout=False,
        allsurveyfile=False,
        scint=False,
        accelsearch=False,
        jerksearch=False,
        rratssearch=False):
    """ Run the surveys and detect the pulsars."""

    # print the population
    if not nostdout:
        print "Running doSurvey on population..."
        print pop

    # loop over the surveys we want to run on the pop file
    surveyPops = []
    for surv in surveyList:
        s = Survey(surv)
        s.discoveries = 0
        if not nostdout:
            print "\nRunning survey {0}".format(surv)

        # create a new population object to store discovered pulsars in
        survpop = Population()
        # HERE SHOULD INCLUDE THE PROPERTIES OF THE ORIGINAL POPULATION

        # counters
        nsmear = 0
        nout = 0
        ntf = 0
        ndet = 0
        nbr = 0
        # loop over the pulsars in the population list
        for psr in pop.population:
            # pulsar could be dead (evolve!) - continue if so
            if psr.dead:
                continue

            # is the pulsar over the detection threshold?
            snr = s.SNRcalc(psr, pop, accelsearch, jerksearch, rratssearch)
            #print snr
            ######################################################################################
            #This section is to add in degradation due to orbital motion of DNS pulsars.
            #Remove this if doing literally anything else.
            #Right now this is very ad-hoc and manual. Optimization possible, maybe not worth right now.
            deg_fac_1102 = {
                'PALFA_one_v_older': 0.9997,
                'PHSURV': 0.9997,
                'HTRU_low_1757': 0.9912,
                '1534_survey': 0.9999,
                'PMSURV': 0.4649
            }  #This is for 1913+1102
            deg_fac_1913 = {
                'PALFA_one_v_older': 0.9953,
                'PHSURV': 0.9956,
                'HTRU_low_1757': 0.9569,
                '1534_survey': 0.9999,
                'PMSURV': 0.7915
            }
            deg_fac_1946 = {
                'PALFA_one_v_older': 0.9514,
                'PHSURV': 0.9543,
                'HTRU_low_1757': 0.6513,
                '1534_survey': 0.9999,
                'PMSURV': 0.2368
            }
            deg_fac_1757 = {
                'PALFA_one_v_older': 0.9710,
                'PHSURV': 0.9716,
                'HTRU_low_1757': 0.9255,
                '1534_survey': 0.9999,
                'PMSURV': 0.5080
            }
            deg_fac_1534 = {
                'PALFA_one_v_older': 0.9999,
                'PHSURV': 0.9999,
                'HTRU_low_1757': 0.9994,
                '1534_survey': 0.9999,
                'PMSURV': 0.7759
            }
            deg_fac_0737A = {
                'PALFA_one_v_older': 0.9910,
                'PHSURV': 0.9916,
                'HTRU_low_1757': 0.8371,
                '1534_survey': 0.9999,
                'PMSURV': 0.2991
            }
            deg_fac_0737B = {
                'PALFA_one_v_older': 0.9999,
                'PHSURV': 0.9999,
                'HTRU_low_1757': 0.9999,
                '1534_survey': 0.9999,
                'PMSURV': 1
            }
            deg_fac_1756 = {
                'PALFA_one_v_older': 0.9999,
                'PHSURV': 0.9999,
                'HTRU_low_1757': 0.9982,
                '1534_survey': 0.9999,
                'PMSURV': 0.5598
            }
            deg_fac_1906 = {
                'PALFA_one_v_older': 0.9999,
                'PHSURV': 0.9999,
                'HTRU_low_1757': 0.9994,
                '1534_survey': 0.9999,
                'PMSURV': 0.7337
            }

            snr = snr * (deg_fac_1906[surv]**2
                         )  #Please change this for each DNS
            ######################################################################################
            # add scintillation, if required
            # modifying S/N rather than flux is sensible because then
            # a pulsar can have same flux but change S/N in repeated surveys
            if scint:
                snr = s.scint(psr, snr)

            if snr > s.SNRlimit:
                ndet += 1
                psr.snr = snr
                survpop.population.append(psr)

                # check if the pulsar has been detected in other
                # surveys
                if not psr.detected:
                    # if not, set to detected and increment
                    # number of discoveries by the survey
                    psr.detected = True
                    s.discoveries += 1

            elif snr == -1.0:
                nsmear += 1
            elif snr == -2.0:
                nout += 1
            elif snr == -3.0:
                nbr += 1
            else:
                ntf += 1

        # report the results
        if not nostdout:
            print "Total pulsars in model = {0}".format(len(pop.population))
            print "Number detected by survey {0} = {1}".format(surv, ndet)
            print "Of which are discoveries = {0}".format(s.discoveries)
            print "Number too faint = {0}".format(ntf)
            print "Number smeared = {0}".format(nsmear)
            print "Number out = {0}".format(nout)
            if rratssearch:
                print "Number didn't burst = {0}".format(nbr)
            print "\n"

        d = Detections(ndet=ndet,
                       ntf=ntf,
                       nsmear=nsmear,
                       nout=nout,
                       nbr=nbr,
                       ndisc=s.discoveries)
        surveyPops.append([surv, survpop, d])

    if allsurveyfile:
        allsurvpop = Population()
        allsurvpop.population = [psr for psr in pop.population if psr.detected]
        surveyPops.append([None, allsurvpop, None])

    return surveyPops
Ejemplo n.º 57
0
                alpha = deepcopy(fittest)

        if cycle % 100 == 0:
            print('current cycle: ' + str(cycle) + ', current fitness: ' + str(alpha.fitness))

        if alpha.fitness == 26:
            break

        if ISLAND_CROSSOVER_RATE > randint(0, 100):
            basic_sample = sample(populations[0].individuals, 5)
            catastrophe_sample = sample(populations[1].individuals, 5)
            crowding_sample = sample(populations[2].individuals, 5)
            populations[0].replace(catastrophe_sample)
            populations[0].replace(crowding_sample)
            populations[1].replace(basic_sample)
            populations[1].replace(crowding_sample)
            populations[2].replace(basic_sample)
            populations[2].replace(catastrophe_sample)

        cycle += 1

    print('Fittest individual in history of all islands: ' + str(alpha.fitness) + ' (cycle ' + str(cycle) + ')')


population = Population(POPULATION_SIZE)

# basic_evolution(population, True)
# evolution_by_catastrophe(population, True)
# evolution_by_deterministic_crowding(population, True)
island_evolution()
Ejemplo n.º 58
0
def generate(ngen,
             surveyList=None,
             pDistType='lnorm',
             radialDistType='lfl06',
             radialDistPars=7.5,
             electronModel='ne2001',
             pDistPars=[2.7, -0.34],
             siDistPars=[-1.6, 0.35],
             lumDistType='lnorm',
             lumDistPars=[-1.1, 0.9],
             zscaleType='exp',
             zscale=0.33,
             duty_percent=6.,
             scindex=-3.86,
             gpsArgs=[None, None],
             doubleSpec=[None, None],
             nostdout=False,
             pattern='gaussian',
             orbits=False):
    """
    Generate a population of pulsars.

    Keyword args:
    ngen -- the number of pulsars to generate (or detect)
    surveyList -- a list of surveys you want to use to try and detect
        the pulsars
    pDistType -- the pulsar period distribution model to use (def=lnorm)
    radialDistType -- radial distribution model
    electronModel -- mode to use for Galactic electron distribution
    pDistPars -- parameters to use for period distribution
    siDistPars -- parameters to use for spectral index distribution
    lumDistPars -- parameters to use for luminosity distribution
    radialDistPars -- parameters for radial distribution
    zscale -- if using exponential z height, set it here (in kpc)
    scindex -- spectral index of the scattering model
    gpsArgs -- add GPS-type spectrum sources
    doubleSpec -- add double-spectrum type sources
    nostdout -- (bool) switch off stdout
    """

    pop = Population()

    # check that the distribution types are supported....
    if lumDistType not in ['lnorm', 'pow']:
        print "Unsupported luminosity distribution: {0}".format(lumDistType)

    #if pDistType not in ['lnorm', 'norm', 'cc97', 'lorimer12']:
    #    print "Unsupported period distribution: {0}".format(pDistType)
    if pDistType not in ['lnorm', 'norm', 'cc97', 'lorimer12', 'lorimer15']:
        print "Unsupported period distribution: {0}".format(pDistType)

    if radialDistType not in [
            'lfl06', 'yk04', 'isotropic', 'slab', 'disk', 'gauss'
    ]:
        print "Unsupported radial distribution: {0}".format(radialDistType)

    # Edited by Shi Dai, 2017/03/22
    if electronModel not in ['ne2001', 'lmt85', 'ymw16']:
        print "Unsupported electron model: {0}".format(electronModel)

    if pattern not in ['gaussian', 'airy']:
        print "Unsupported gain pattern: {0}".format(pattern)

    if duty_percent < 0.:
        print "Unsupported value of duty cycle: {0}".format(duty_percent)

    # need to use properties in this class so they're get/set-type props
    pop.pDistType = pDistType
    pop.radialDistType = radialDistType
    pop.electronModel = electronModel
    pop.lumDistType = lumDistType

    pop.rsigma = radialDistPars
    pop.pmean, pop.psigma = pDistPars
    pop.simean, pop.sisigma = siDistPars

    pop.gpsFrac, pop.gpsA = gpsArgs
    pop.brokenFrac, pop.brokenSI = doubleSpec

    if pop.lumDistType == 'lnorm':
        pop.lummean, pop.lumsigma = \
                lumDistPars[0], lumDistPars[1]
    else:
        try:
            pop.lummin, pop.lummax, pop.lumpow = \
                lumDistPars[0], lumDistPars[1], lumDistPars[2]
        except ValueError:
            raise PopulateException('Not enough lum distn parameters')

    pop.zscaleType = zscaleType
    pop.zscale = zscale

    # store the dict of arguments inside the model. Could be useful.
    try:
        argspec = inspect.getargspec(generate)
        key_values = [(arg, locals()[arg]) for arg in argspec.args]
        pop.arguments = {key: value for (key, value) in key_values}
    except SyntaxError:
        pass

    if not nostdout:
        print "\tGenerating pulsars with parameters:"
        param_string_list = []
        for key, value in key_values:
            s = ": ".join([key, str(value)])
            param_string_list.append(s)

        # join this list of strings, and print it
        s = "\n\t\t".join(param_string_list)
        print "\t\t{0}".format(s)

        # set up progress bar for fun :)
        prog = ProgressBar(min_value=0,
                           max_value=ngen,
                           width=65,
                           mode='dynamic')

    # create survey objects here and put them in a list
    if surveyList is not None:
        surveys = [Survey(s, absolute_importpattern) for s in surveyList]
        # initialise these counters to zero
        for surv in surveys:
            surv.ndet = 0  # number detected
            surv.nout = 0  # number outside survey region
            surv.nsmear = 0  # number smeared out
            surv.ntf = 0  # number too faint

            # surv.gainpat=pattern
    else:
        # make an empty list here - makes some code just a little
        # simpler - can still loop over an empty list (ie zero times)
        surveys = []

    Lorimer15 = np.loadtxt(
        '/Users/dai02a/Soft/psrpop/PsrPopPy/lib/python/lorimer15')
    while pop.ndet < ngen:
        # Declare new pulsar object
        p = Pulsar()

        # period, alpha, rho, width distribution calls
        # Start creating the pulsar!
        if pop.pDistType == 'lnorm':
            p.period = dists.drawlnorm(pop.pmean, pop.psigma)
        elif pop.pDistType == 'norm':
            p.period = random.gauss(pop.pmean, pop.psigma)
        elif pop.pDistType == 'cc97':
            p.period = _cc97()
        elif pop.pDistType == 'gamma':
            print "Gamma function not yet supported"
            sys.exit()
        elif pop.pDistType == 'lorimer12':
            p.period = _lorimer2012_msp_periods()
        elif pop.pDistType == 'lorimer15':
            #p.period = _lorimer2015_msp_periods()
            p.period = Lorimer15[pop.ndet]

        if duty_percent > 0.:
            # use a simple duty cycle for each pulsar
            # with a log-normal scatter
            width = (float(duty_percent) / 100.) * p.period**0.9
            width = math.log10(width)
            width = dists.drawlnorm(width, 0.3)

            p.width_degree = width * 360. / p.period
        else:
            # use the model to caculate if beaming
            p.alpha = _genAlpha()

            p.rho, p.width_degree = _genRhoWidth(p)

            if p.width_degree == 0.0 and p.rho == 0.0:
                continue
            # is pulsar beaming at us? If not, move on!
            p.beaming = _beaming(p)
            if not p.beaming:
                continue

        # Spectral index stuff here

        # suppose it might be nice to be able to have GPS sources
        # AND double spectra. But for now I assume only have one or
        # none of these types.
        if random.random() > pop.gpsFrac:
            # This will evaluate true when gpsArgs[0] is NoneType
            # might have to change in future
            p.gpsFlag = 0
        else:
            p.gpsFlag = 1
            p.gpsA = pop.gpsA

        if random.random() > pop.brokenFrac:
            p.brokenFlag = 0
        else:
            p.brokenFlag = 1
            p.brokenSI = pop.brokenSI

        p.spindex = random.gauss(pop.simean, pop.sisigma)

        # get galactic position
        # first, Galactic distribution models
        if pop.radialDistType == 'isotropic':
            # calculate gl and gb randomly
            p.gb = math.degrees(math.asin(random.random()))
            if random.random() < 0.5:
                p.gb = 0.0 - p.gb
            p.gl = random.random() * 360.0

            # use gl and gb to compute galactic coordinates
            # pretend the pulsar is at distance of 1kpc
            # not sure why, ask Dunc!
            p.galCoords = go.lb_to_xyz(p.gl, p.gb, 1.0)

        elif pop.radialDistType == 'slab':
            p.galCoords = go.slabDist()
            p.gl, p.gb = go.xyz_to_lb(p.galCoords)

        elif pop.radialDistType == 'disk':
            p.galCoords = go.diskDist()
            p.gl, p.gb = go.xyz_to_lb(p.galCoords)

        else:  # we want to use exponential z and a radial dist
            if pop.radialDistType == 'lfl06':
                p.r0 = go.lfl06()
            elif pop.radialDistType == 'yk04':
                p.r0 = go.ykr()
            elif pop.radialDistType == 'gauss':
                # guassian of mean 0
                # and stdDev given by parameter (kpc)
                p.r0 = random.gauss(0., pop.rsigma)

            # then calc xyz,distance, l and b
            if pop.zscaleType == 'exp':
                zheight = go._double_sided_exp(zscale)
            else:
                zheight = random.gauss(0., zscale)
            gx, gy = go.calcXY(p.r0)
            p.galCoords = gx, gy, zheight
            p.gl, p.gb = go.xyz_to_lb(p.galCoords)

        p.dtrue = go.calc_dtrue(p.galCoords)

        # Edited by Shi Dai, 2017/03/22
        # then calc DM  using fortran libs
        #if pop.electronModel == 'ne2001':
        #    p.dm = go.ne2001_dist_to_dm(p.dtrue, p.gl, p.gb)
        #elif pop.electronModel == 'lmt85':
        #    p.dm = go.lmt85_dist_to_dm(p.dtrue, p.gl, p.gb)

        #p.scindex = scindex
        ## then calc scatter time
        #p.t_scatter = go.scatter_bhat(p.dm, p.scindex)

        if pop.electronModel == 'ne2001':
            p.dm = go.ne2001_dist_to_dm(p.dtrue, p.gl, p.gb)
            p.t_scatter = go.scatter_bhat(p.dm, p.scindex)
        elif pop.electronModel == 'lmt85':
            p.dm = go.lmt85_dist_to_dm(p.dtrue, p.gl, p.gb)
            p.t_scatter = go.scatter_bhat(p.dm, p.scindex)
        elif pop.electronModel == 'ymw16':
            p.dm, p.t_scatter = go.ymw16_dist_to_dm(p.dtrue, p.gl, p.gb)

        p.scindex = scindex
        # then calc scatter time

        if pop.lumDistType == 'lnorm':
            p.lum_1400 = dists.drawlnorm(pop.lummean, pop.lumsigma)
        else:
            p.lum_1400 = dists.powerlaw(pop.lummin, pop.lummax, pop.lumpow)

        # add in orbital parameters
        if orbits:
            orbitalparams.test_1802_2124(p)
            print p.gb, p.gl

        # if no surveys, just generate ngen pulsars
        if surveyList is None:
            calc_delta(p)
            pop.population.append(p)
            pop.ndet += 1
            if not nostdout:
                prog.increment_amount()
                print prog, '\r',
                sys.stdout.flush()
        # if surveys are given, check if pulsar detected or not
        # in ANY of the surveys
        else:
            # just a flag to increment if pulsar is detected
            detect_int = 0
            for surv in surveys:
                # do SNR calculation
                SNR = surv.SNRcalc(p, pop)

                if SNR > surv.SNRlimit:
                    # SNR is over threshold

                    # increment the flag
                    # and survey ndetected
                    detect_int += 1
                    surv.ndet += 1
                    continue

                elif SNR == -1:
                    # pulse is smeared out
                    surv.nsmear += 1
                    continue

                elif SNR == -2:
                    # pulsar is outside survey region
                    surv.nout += 1
                    continue

                else:
                    # pulsar is just too faint
                    surv.ntf += 1
                    continue

            # added by Shi Dai, 2017/02/07
            #p.snr = SNR

            # add the pulsar to the population
            pop.population.append(p)

            # if detected, increment ndet (for whole population)
            # and redraw the progress bar
            if detect_int:
                pop.ndet += 1
                if not nostdout:
                    prog.increment_amount()
                    print prog, '\r',
                    sys.stdout.flush()

    # print info to stdout
    if not nostdout:
        print "\n"
        print "  Total pulsars = {0}".format(len(pop.population))
        print "  Total detected = {0}".format(pop.ndet)
        # print "  Number not beaming = {0}".format(surv.nnb)

        for surv in surveys:
            print "\n  Results for survey '{0}'".format(surv.surveyName)
            print "    Number detected = {0}".format(surv.ndet)
            print "    Number too faint = {0}".format(surv.ntf)
            print "    Number smeared = {0}".format(surv.nsmear)
            print "    Number outside survey area = {0}".format(surv.nout)

    return pop
Ejemplo n.º 59
0
            points = npr.choice(idx, n_random, replace=False)

            if (crate > npr.random()):
                for j in points:
                    gene1[j] = gene2[j]
            child.setGene(gene1)
            children.append(child)
        return (children)


if __name__ == "__main__":
    from readDataset import readDataset
    from individual import Individual
    from select import Select
    dataset = readDataset("./dataset/binary.txt")
    population = Population()
    for i in range(5):
        ind = Individual()
        ind.createGene(dataset, 10)
        population.addInd(ind)

    def evaluate(ind):
        fitness = sum(ind)
        return (fitness)

    population.calcFitness(evaluate)
    population.show()
    parents = Select.Tournament(population, 5, 3, "max")
    for ind in parents:
        ind.show()
    print("Onepoint")
Ejemplo n.º 60
0
def generate(ngen,
             surveyList=None,
             age_max=1.0E9,
             pDistPars=[0.3, 0.15],
             pDistType = 'norm' ,           # P distirbution for MSPs (Lorimer et al. 2015)
             bFieldPars=[12.65, 0.55],
             birthVPars=[0.0, 180.],
             siDistPars=[-1.6, 0.35],
             alignModel='orthogonal',
             lumDistType='fk06',
             lumDistPars=[-1.5, 0.5],
             alignTime=None,
             spinModel='fk06',
             beamModel='tm98',
             birthVModel='gaussian',
             electronModel='ne2001',
             braking_index=0,
             zscale=0.05,
             duty=5.,
             scindex=-3.86,
             widthModel=None,
             nodeathline=False,
             efficiencycut=None,
             nostdout=False,
             nospiralarms=False,
             keepdead=False,
             dosurveyList = None,
             makepop=False):

    pop = Population()

    # set the parameters in the population object

    pop.pmean, pop.psigma = pDistPars
    pop.bmean, pop.bsigma = bFieldPars

    if lumDistType == 'pow':
        try:
            pop.lummin, pop.lummax, pop.lumpow = \
                lumDistPars[0], lumDistPars[1], lumDistPars[2]
        except ValueError:
            raise EvolveException('Not enough lum distn parameters for "pow"')

    elif lumDistType == 'fk06':
        pop.lumPar1, pop.lumPar2 = lumDistPars[0], lumDistPars[1]
        if len(lumDistPars) == 3:
            pop.lumPar3 = lumDistPars[2]
        else:
            pop.lumPar3 = 0.18

    else:
        pop.lumPar1, pop.lumPar2 = lumDistPars

    pop.simean, pop.sisigma = siDistPars
    pop.birthvmean, pop.birthvsigma = birthVPars

    pop.alignModel = alignModel
    pop.alignTime = alignTime
    pop.spinModel = spinModel
    pop.beamModel = beamModel
    pop.birthVModel = birthVModel
    pop.electronModel = electronModel

    pop.braking_index = braking_index
    pop.deathline = not nodeathline
    pop.nospiralarms = nospiralarms

    pop.zscale = zscale

    if widthModel == 'kj07':
        print "\tLoading KJ07 models...."
        kj_p_vals, kj_pdot_vals, kj_dists = beammodels.load_kj2007_models()
        print "\tDone\n"

    if not nostdout:
        print "\tGenerating evolved pulsars with parameters:"
        print "\t\tngen = {0}".format(ngen)
        print "\t\tUsing electron distn model {0}".format(
                                        pop.electronModel)
        print "\n\t\tPeriod mean, sigma = {0}, {1}".format(
                                                    pop.pmean,
                                                    pop.psigma)
        print "\t\tLuminosity mean, sigma = {0}, {1}".format(
                                                    pop.lummean,
                                                    pop.lumsigma)
        print "\t\tSpectral index mean, sigma = {0}, {1}".format(
                                                    pop.simean,
                                                    pop.sisigma)
        print "\t\tGalactic z scale height = {0} kpc".format(
                                                    pop.zscale)
        if widthModel is None:
            print "\t\tWidth {0}% ".format(duty)
        else:
            print "\t\tUsing Karastergiou & Johnston beam width model"

        # set up progress bar for fun :)
        prog = ProgressBar(min_value=0,
                           max_value=ngen,
                           width=65,
                           mode='dynamic')

    # create survey objects here and put them in a list
    if surveyList is not None:
        surveys = [Survey(s) for s in surveyList]
    else:
        # make an empty list here - makes some code just a little
        # simpler - can still loop over an empty list (ie zero times)
        surveys = []

    # initialise these counters to zero
    for surv in surveys:
        surv.ndet = 0  # number detected
        surv.nout = 0  # number outside survey region
        surv.nsmear = 0  # number smeared out
        surv.ntf = 0  # number too faint

    # create dosurvey objects here and put them in a list
    if dosurveyList is not None:
        dosurveys = [Survey(s) for s in dosurveyList]
        # initialise these counters to zero
        for surv in dosurveys:
            surv.ndet = 0  # number detected
            surv.nout = 0  # number outside survey region
            surv.nsmear = 0  # number smeared out
            surv.ntf = 0  # number too faint

            # surv.gainpat=pattern
    else:
        # make an empty list here - makes some code just a little
        # simpler - can still loop over an empty list (ie zero times)
        dosurveys = []

    # this is the nitty-gritty loop for generating the pulsars
    ntot = 0   #total number of pulsars generated by the while loop (including dead, and the ones not beaming towards us)
    n_alive = 0  #total number of pulsars which are alive (beaming + not beaming towards Earth)
    n_alive_beam = 0 #total number of alive pulsars beaming towards Earth    
    while pop.ndet < ngen:
        pulsar = Pulsar()

        # initial age for pulsar
        pulsar.age = random.random() * age_max
        # initial period
        pulsar.p0 = -1.
        while (pulsar.p0 <= 0.):
          if(pDistType == 'norm'):
            pulsar.p0 = random.gauss(pop.pmean, pop.psigma)
          elif(pDistType == 'drl15'):
            L_p0 = np.random.lognormal(mean = pop.pmean, sigma = pop.psigma)  
            pulsar.p0 = np.exp(L_p0)/1000         # period in seconds
        # initial magnetic field (in Gauss)
        pulsar.bfield_init = 10**random.gauss(pop.bmean, pop.bsigma)

        # aligment angle
        alignpulsar(pulsar, pop)

        # braking index
        if pop.braking_index == 0:
            pulsar.braking_index = 2.5 + 0.5 * random.random()
        else:
            pulsar.braking_index = float(pop.braking_index)

        # apply relevant spin down model
        pulsar.dead = False  # pulsar should start alive!

        if pop.spinModel == 'fk06':
            spindown_fk06(pulsar)

            # apply deathline if relevant
            if pop.deathline:
                bhattacharya_deathperiod_92(pulsar)

        elif pop.spinModel == 'tk01':
            spindown_tk01(pulsar)

            # apply deathline if relevant
            if pop.deathline:
                bhattacharya_deathperiod_92(pulsar)
 
        elif pop.spinModel == 'cs06':
            # contopoulos and spitkovsky
            spindown_cs06(pulsar, pop)

        # if period > 10 seconds, just try a new one
        if pulsar.period > 10000.0 or pulsar.period < 1.5:
            continue
        # cut on pdot too - this doesn't help
        if pulsar.pdot > 1.e-11 or pulsar.pdot < 1.e-21:
            continue

        # define pulse width (default = 6% = 18 degrees)
        if widthModel is None:
            width = (float(duty)/100.) * pulsar.period**0.9
            width = math.log10(width)
            width = dists.drawlnorm(width, 0.3)
        elif widthModel == 'kj07':
            # Karastergiou & Johnston beam model

            # find closest p, pdot in the kj lists
            logp = math.log10(pulsar.period)
            logpdot = math.log10(pulsar.pdot)

            p_idx = (np.abs(kj_p_vals - logp)).argmin()
            pd_idx = (np.abs(kj_pdot_vals - logpdot)).argmin()

            # pick a width from relevant model
            dist = kj_dists[p_idx][pd_idx][2]
            width = np.random.choice(dist)

            """
            width = beammodels.kj2007_width(pulsar)
            no_width = 0
            while width  == 0.0:
                no_width+=1
                width = beammodels.kj2007_width(pulsar)

                # if we get to five, then try a new pulsar
                if no_width == 5:
                    no_width = 0
                    continue
            """

        else:
            print "Undefined width model!"
            sys.exit()
        # print width
        # print pulsar.period, width, pulsar.pdot
        if width == 0.0:
            # some kj2007 models make many zero-width sources. Skip!
            continue
        pulsar.width_degree = 360. * width / pulsar.period

        # incrementing the total number of physical pulsars generated by
        # the model after the unphysical ones are removed
        ntot += 1
        # plough on - only if the pulsar isn't dead!
        if not pulsar.dead or keepdead:
            n_alive+=1
            # is the pulsar beaming?
            pulsar_beaming(pulsar, pop.beamModel)
            # if not, then skip onto next pulsar
            if not pulsar.beaming:
                continue

            # position of the pulsar
            galacticDistribute(pulsar, pop)
            # birthvelocity
            birthVelocity(pulsar, pop)

            # model the xyz velocity
            go.vxyz(pulsar)

            # luminosity
            if lumDistType == 'fk06':
                luminosity_fk06(pulsar,
                                alpha=pop.lumPar1,
                                beta=pop.lumPar2,
                                gamma=pop.lumPar3)

            elif lumDistType == 'lnorm':
                pulsar.lum_1400 = dists.drawlnorm(pop.lumPar1, pop.lumPar2)

            elif lumDistType == 'pow':
                pulsar.lum_1400 = dists.powerlaw(pop.lummin,
                                                 pop.lummax,
                                                 pop.lumpow)
            else:
                # something's wrong!
                raise EvolveException('Invalid luminosity distn selected')

            # apply efficiency cutoff
            if efficiencycut is not None:
                if pulsar.efficiency() > efficiencycut:
                    pulsar.dead = True
                    if not keepdead:
                        continue

            # spectral index
            pulsar.spindex = random.gauss(pop.simean, pop.sisigma)

            # calculate galactic coords and distance
            pulsar.gl, pulsar.gb = go.xyz_to_lb(pulsar.galCoords)
            pulsar.dtrue = go.calc_dtrue(pulsar.galCoords)

            # then calc DM  using fortran libs
            if pop.electronModel == 'ne2001':
                pulsar.dm = go.ne2001_dist_to_dm(pulsar.dtrue,
                                                 pulsar.gl,
                                                 pulsar.gb)
            elif pop.electronModel == 'lmt85':
                pulsar.dm = go.lmt85_dist_to_dm(pulsar.dtrue,
                                                pulsar.gl,
                                                pulsar.gb)
            else:
                raise EvolveException('Invalid electron dist model selected')

            pulsar.scindex = scindex
            pulsar.t_scatter = go.scatter_bhat(pulsar.dm,
                                               pulsar.scindex)

            # if surveys are given, check if pulsar detected or not
            # in ANY of the surveys
            if surveyList is not None:
                detect_int = 0  # just a flag if pulsar is detected
                for surv in surveys:
                    SNR = surv.SNRcalc(pulsar, pop)

                    if SNR > surv.SNRlimit:
                        # SNR is over threshold
                        # increment the flag
                        # and survey ndetected
                        detect_int += 1
                        surv.ndet += 1
                        continue

                    elif SNR == -1:
                        # pulse is smeared out
                        surv.nsmear += 1
                        continue

                    elif SNR == -2:
                        # pulsar is outside survey region
                        surv.nout += 1
                        continue

                    else:
                        # pulsar is just too faint
                        surv.ntf += 1
                        continue

                # if detected, increment ndet (for whole population)
                # and redraw the progress bar
                if detect_int:
                    pop.ndet += 1
                    # update the counter
                    if not nostdout:
                        prog.increment_amount()
                        print prog, '\r',
                        sys.stdout.flush()

            else:
                # no survey list, just add the pulsar to population,
                # and increment number of pulsars
                pop.ndet += 1
                # update the counter
                if not nostdout:
                    prog.increment_amount()
                    print prog, '\r',
                    sys.stdout.flush()

            # increment the total number of alive pulsars beaming towards Earth
            # should be different from n_alive (as the loop was stopped if pulsar
            # was found to not beam towards Earth)
            # is incremented only when pulsar was beaming towards us
            n_alive_beam += 1            
            
            # pulsar isn't dead, and makepop True, add the pulsar to population!
            if makepop == True:
                pop.population.append(pulsar)

            # if dosurveys are given, check if pulsar detected or not
            # in each of the surveys or any of the survey
            # just a flag to increment if pulsar is detected
            if dosurveyList is not None:
                for surv in dosurveys:
                    SNR = surv.SNRcalc(pulsar, pop)

                    if SNR > surv.SNRlimit:
                        # SNR is over threshold
                        # increment survey ndetected
                        surv.ndet += 1
                        continue

                    elif SNR == -1:
                        # pulse is smeared out
                        surv.nsmear += 1
                        continue

                    elif SNR == -2:
                        # pulsar is outside survey region
                        surv.nout += 1
                        continue

                    else:
                        # pulsar is just too faint
                        surv.ntf += 1
                        continue

        else:
            # pulsar is dead. If no survey list,
            # just increment number of pulsars
            if surveyList is None:
                pop.ndet = ntot 
                # update the counter
                if not nostdout:
                    prog.increment_amount()
                    print prog, '\r',
                    sys.stdout.flush()

    if not nostdout:
        print "\n\n"
#        print "  Total pulsars = {0}".format(len(pop.population))
#        print "  Total detected = {0}".format(pop.ndet)
        print "  Total pulsars generated = {0}".format(ntot)
        print "  Total living pulsars = {0}".format(n_alive)
        print "  Total living pulsars beaming towards Earth = {0}".format(n_alive_beam)

        if surveyList is not None:
            print "  Total detected by all surveys = {0}".format(pop.ndet)


        for surv in surveys:
            print "\n  Results for survey '{0}'".format(surv.surveyName)
            print "    Number detected = {0}".format(surv.ndet)
            print "    Number too faint = {0}".format(surv.ntf)
            print "    Number smeared = {0}".format(surv.nsmear)
            print "    Number outside survey area = {0}".format(surv.nout)

        for surv in dosurveys:
            print "\n  Dosurvey Results for survey '{0}'".format(surv.surveyName)
            print "    Number detected = {0}".format(surv.ndet)
            print "    Number too faint = {0}".format(surv.ntf)
            print "    Number smeared = {0}".format(surv.nsmear)
            print "    Number outside survey area = {0}".format(surv.nout)

    dosurvey_result = []
    for surv in dosurveys:
        dosurvey_result.append([surv.surveyName, surv.ndet, surv.ntf, surv.nsmear, surv.nout])

    # save list of arguments into the pop
    #try:
    #    argspec = inspect.getargspec(generate)
    #    key_values = [(arg, locals()[arg]) for arg in argspec.args]
    #    pop.arguments = {key: value for (key, value) in key_values}
    #except SyntaxError:
     #   pass

    return pop, dosurvey_result