def crossover(self, first_parent, second_parent):
     tsize = len(self.data.ga_target)
     if self.data.string_search_crossover == StringSearchCrossOver.ONE_POINT:
         spos = int(random.randint(0, 32767) % tsize)
         return Citizen(first_parent.str[0:spos] +
                        second_parent.str[spos:tsize])
     elif self.data.string_search_crossover == StringSearchCrossOver.TWO_POINT:
         spos1 = int(random.randint(0, 32767) % tsize)
         spos2 = int(random.randint(0, 32767) % tsize)
         if spos1 > spos2:
             spos1, spos2 = spos2, spos1
         return Citizen(first_parent.str[0:spos1] +
                        second_parent.str[spos1:spos2] +
                        first_parent.str[spos2:tsize])
 def init_citizen(self):
     citizen = Citizen()
     tsize = len(self.data.ga_target)
     for j in range(tsize):
         citizen.str = citizen.str + str(
             chr((random.randint(0, 32767) % 90) + 32))
     return citizen
Пример #3
0
 def ox_crossover(self, first_parent, second_parent):
     chosen_entries = 0
     citizen = Citizen()
     first_parent_entries = []
     first_parent_chosen_values = []
     for i in range(self.data.queens_num):
         citizen.board.append(-1)
     while chosen_entries != self.data.queens_num / 2:
         entry = int(random.randint(0, 32767) % self.data.queens_num)
         if entry not in first_parent_entries:
             first_parent_entries.append(entry)
             first_parent_chosen_values.append(first_parent.board[entry])
             chosen_entries = chosen_entries + 1
             citizen.board[entry] = first_parent.board[entry]
     second_parent_chosen_values = []
     for i in range(self.data.queens_num):
         if citizen.board[i] == -1:
             for j in range(self.data.queens_num):
                 second_parent_value = second_parent.board[j]
                 if second_parent_value not in first_parent_chosen_values:
                     if second_parent_value not in second_parent_chosen_values:
                         citizen.board[i] = second_parent_value
                         second_parent_chosen_values.append(second_parent_value)
                         break
     for i in range(self.data.queens_num):
         if citizen.board[i] == -1:
             citizen.board[i] = int(random.randint(0, 32767) % self.data.queens_num)
     return citizen
Пример #4
0
    def check_actors(self, level, clock):
        if clock % 30000 < 20:
            Player().raise_level()
            level = Player().get_level()

        for citizen in self.citizens:
            if citizen.get_removable():
                self.citizens.remove(citizen)
            else:
                for tinfoil_head in self.tinfoil_heads:
                    citizen.check_infected(tinfoil_head)

        for tinfoil_head in self.tinfoil_heads:
            if tinfoil_head.get_removable():
                self.tinfoil_heads.remove(tinfoil_head)

        citizen_count = 4 + 2 * level
        citizens_to_add = citizen_count - len(self.citizens)
        for _ in range(citizens_to_add):
            self.citizens.append(Citizen(self.screen, self, self.size))

        tinfoil_heads_count = 2 * level
        tinfoil_heads_to_add = tinfoil_heads_count - len(self.tinfoil_heads)
        for _ in range(tinfoil_heads_to_add):
            self.tinfoil_heads.append(TinfoilHead(self.screen, self,
                                                  self.size))
Пример #5
0
    def init(self):
        self.citizens = []
        for i in range(self.default_population):
            self.citizens.append(Citizen(self, self.data_path))

        self.market = Market(self.data_path)

        self.day = 0
Пример #6
0
    def try_generate(self, game):
        if self.generate_after == 0:
            x = random.randrange(game.width)
            y = random.randrange(game.height)
            self.generate_after = self.delay
            return Citizen(x, y, game)

        self.generate_after -= 1
Пример #7
0
 def init_citizen(self):
     citizen = Citizen()
     for i in range(self.data.queens_num):
         citizen.board.append(i)
     # Randomize entries
     for col in range(self.data.queens_num):
         row = (random.randint(0, 32767) % self.data.queens_num)
         citizen.board[col] = row
     return citizen
 def init_citizen(self):
     citizen = Citizen()
     entries = []
     for i in range(self.genes_num):
         citizen.str = citizen.str + "-"
         entries.append(i)
     self.init_character(citizen, '?', entries)
     self.init_character(citizen, '1', entries)
     self.init_character(citizen, '0', entries)
     return citizen
Пример #9
0
 def init_citizen(self):
     citizen = Citizen()
     for j in range(self.items):
         choose = (random.randint(0, 32767) % 2)
         if choose == 1:
             citizen.knapsack.append(1)
         else:
             citizen.knapsack.append(0)
     self.validate_and_update_sack(citizen)
     return citizen
Пример #10
0
 def pmx_crossover(self, first_parent, second_parent):
     citizen = Citizen()
     for j in range(3):
         entry = int(random.randint(0, 32767) % self.data.queens_num)
         for i in range(self.data.queens_num):
             if first_parent.board[i] == second_parent.board[entry]:
                 citizen.board.append(first_parent.board[entry])
             else:
                 citizen.board.append(first_parent.board[i])
         citizen.board[entry] = second_parent.board[entry]
     return citizen
Пример #11
0
def init_citizens(nr_citizens, nr_sickdays, infection_distance,
                  infection_probability):
    citizens = []
    initial_positions = []
    for j in range(nr_citizens):
        citizen = Citizen(nr_sickdays, infection_distance,
                          infection_probability)
        initial_x, initial_y = citizen.init_pos()
        citizens.append(citizen)
        initial_positions.append([initial_x, initial_y])
    return citizens, initial_positions
Пример #12
0
    def load(self):
        print(self.file_name)
        with open(self.file_name) as json_file:
            data = json.load(json_file)
            json_file.close()

            self.citizens = []
            for citizen_data in data['citizens']:
                self.citizens.append(
                    Citizen(self, self.data_path, citizen_data))

            if len(self.citizens) < self.default_population:
                self.citizens.append(Citizen(self, self.data_path))

            if len(self.citizens) > self.default_population:
                self.citizens = self.citizens[0:-1]

            self.market = Market(self.data_path, data['market'])

            self.day = data['day']
Пример #13
0
    def init(self):
        self.camera_pos = 0, 0
        self.game_over = False
        self.display = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
        self.surface = pygame.Surface((self.width, self.height))
        self.screen_width, self.screen_height = pygame.display.get_surface(
        ).get_size()
        self.clock = pygame.time.Clock()
        self.buildings.clear()
        self.objects.clear()
        self.enemies.clear()
        self.fellows.clear()
        if self.boss is not None:
            self.boss.game = None
        self.boss = None
        with open('Map/map.txt', 'r') as f:
            x = 0
            y = 0
            for line in f.readlines():
                for s in line:
                    if s == '#':
                        self.buildings.append(MainBuilding(x, y, self))
                    if s == '1':
                        self.buildings.append(HighBuilding(x, y, self))
                    if s == '@':
                        self.buildings.append(GreenBuilding(x, y, self))
                    if s == 'U':
                        self.buildings.append(UrfuBuilding(x, y, self))
                        self.boss = Boss(x - 200, y - 200, self)
                    x += Building.size * 2
                x = 0
                y += Building.size
        self.player = Player(150, 150, self)
        self.objects.append(self.player)
        self.objects.extend(self.buildings)
        self.keydown_handlers.clear()
        self.keyup_handlers.clear()
        self.mouse_handlers.clear()
        self.player.setup_handlers(self.keydown_handlers, self.keyup_handlers,
                                   self.mouse_handlers)

        for i in range(MAX_ENEMIES_COUNT // 2):
            self.enemies.append(self.create_hero(Enemy))
        for i in range(MAX_ENEMIES_COUNT // 2):
            self.enemies.append(self.create_hero(ShootingEnemy))
        self.objects.append(Citizen(150, 250, self))
        for i in range(MAX_CITIZENS_COUNT):
            self.objects.append(self.create_hero(Citizen))
        self.objects.extend(self.enemies)
        self.player.on_pos_changed = self.change_camera_pos
        self.ui = lives.Lives(10, 10, self)
Пример #14
0
def register():
    form = RegisterForm(request.form)
    if request.method == 'POST':
        if form.validate():
            name = form.name.data
            password = form.password.data
            # Save to DB
            manager = CitizenManager(main_db)
            user = Citizen(name, password)
            if manager.persist(user):
                flash('Registrace proběhla úspěšně, nyní se prosím přihlašte.',
                      'success')
                return redirect(url_for('login'))
            else:
                error = 'Uživatel s tímto jménem již existuje.'
                return render_template('register.html', error=error, form=form)
        else:
            error = "Registrace se nezdařila"
            return render_template('register.html', error=error, form=form)
    return render_template('register.html', form=form)
Пример #15
0
 def crossover(self, first_parent, second_parent):
     citizen = Citizen()
     overall_weights = 0
     chosen_items = []
     not_chosen_items = []
     for i in range(self.items):
         if first_parent.knapsack[i] == 1 or second_parent.knapsack[i] == 1:
             citizen.knapsack.append(1)
             chosen_items.append(i)
         else:
             citizen.knapsack.append(0)
             not_chosen_items.append(i)
     if chosen_items:
         citizen.knapsack[random.choice(chosen_items)] = 0
     if not_chosen_items:
         citizen.knapsack[random.choice(not_chosen_items)] = 1
         citizen.knapsack[random.choice(not_chosen_items)] = 1
     for i in range(self.items):
         if citizen.knapsack[i] == 1:
             overall_weights = overall_weights + self.weights[i]
     citizen.capacity = overall_weights
     self.validate_and_update_sack(citizen)
     return citizen
Пример #16
0
 def init_population(self, population, buffer):
     for i in range(self.data.ga_popsize):
         population.append(self.problem.init_citizen())
     for i in range(self.data.ga_popsize):
         buffer.append(Citizen())
Пример #17
0
 def spawn_citizen(self, location=(0, 0), ai_type="learning"):
     self.citizens.append(Citizen(location, ai_type))
     self.state[(location[0], location[1], self.object_layer["C"])] = 1
Пример #18
0
sanitizers = []

for i in range(10):
    temp = Sanitizer()
    sanitizers.append(temp)

people = []

numInfected = 1
numCitizens = 30

# Get list of infected and non infected citizen objects
while len(people) < numCitizens:
    if len(people) < numInfected:
        people.append(Citizen(True))
    else:
        people.append(Citizen(False))

print(people[0].infected)
print(people[1].infected)

# Main loop
running = True
while running:

    # RBG Values
    screen.fill((192, 192, 192))
    screen.blit(background, (0, 0))

    for event in pygame.event.get():
 def crossover(self, first_parent, second_parent):
     tsize = self.genes_num
     spos = int(random.randint(0, 32767) % tsize)
     return Citizen(first_parent.str[0:spos] + second_parent.str[spos:tsize])