def two_point_crossover(dad, mom, bounds, nbits): offspring1 = [] offspring2 = [] for varidx in range(len(bounds)): first_cpoint = np.random.randint(0, nbits, size=1)[0] if first_cpoint == nbits - 1: second_cpoint = first_cpoint first_cpoint = np.random.randint(0, second_cpoint, size=1)[0] else: second_cpoint = np.random.randint(first_cpoint + 1, nbits, size=1)[0] var_os1 = dad[varidx][:first_cpoint] + mom[varidx][first_cpoint:second_cpoint] + dad[varidx][second_cpoint:] var_os2 = mom[varidx][:first_cpoint] + dad[varidx][first_cpoint:second_cpoint] + mom[varidx][second_cpoint:] ## Ajusta as variáveis que passarem dos limites estabelecidos min, max = bounds[varidx] decoded_os1 = decode(var_os1, bounds[varidx]) if decoded_os1 > max: var_os1 = '1111111111' elif decoded_os1 < min: var_os1 = '0000000000' decoded_os2 = decode(var_os2, bounds[varidx]) if decoded_os2 > max: var_os2 = '1111111111' elif decoded_os2 < min: var_os2 = '0000000000' offspring1.append(var_os1) offspring2.append(var_os2) # O valor da fitness function ainda não é calculado offspring1.append(0) offspring2.append(0) return (offspring1, offspring2)
def from_file(cls, connection_name: str) -> Optional['JiraConnection']: """ Raises ConfigError if file is missing, internal data fails validation, or assignee list fails to be queried """ config_file = cls._build_config(connection_name) if not os.path.isfile(config_file): raise ConfigError( 'Cannot initialize JIRA instance: {}. Missing config file: {}'. format(connection_name, cls._build_config(connection_name))) try: cp = configparser.RawConfigParser() cp.read(cls._build_config(connection_name)) url = cp.get('Connection', 'url').rstrip('/') user = decode(encode_password(), cp.get('Connection', 'user')) password = decode(encode_password(), cp.get('Connection', 'password')) result = JiraConnection(connection_name, url, user, password) result.possible_projects = cp.get('Connection', 'projects').split(',') return result except configparser.NoOptionError as e: print('Failed to create JiraConnection from file: {}. Error: {}'. format(config_file, str(e))) return None
def ShowResults(self) -> None: print('\nResults\n-------') if self.errors: print('%d Errors' % (len(self.errors))) for e in self.errors: print('\t' + decode(str(e))) print('\n') print('%d on %d Success' % (self.success, self.runs))
def evolve(self, f, maxgen, selec_method='tournament', pc=0.9, n_candidates=2): pop = self.initial_pop for i in range(self.npop): pop[i, 2] = f(decode(pop[i, 0], self.bounds[0]), decode(pop[i, 1], self.bounds[1])) pm = 1 / (self.npop * np.sqrt(self.nbits)) # Probabilidade de mutação ngen = 0 selector = Selector(selec_method) while ngen < maxgen: next_gen = np.empty((1, 3), dtype='object')[1::] # Preenche a próxima geração while next_gen.shape[0] < self.npop: # Faz o crossover utilizando 2 point crossover if np.random.uniform(size=1) < pc: # Utiliza torunament selection para criar o mating pool dad, mom = selector.select(pop, n_candidates) offspring1, offspring2 = two_point_crossover( dad, mom, self.bounds, self.nbits) # Acrescenta o indivíduo a próxima geração next_gen = np.vstack( (next_gen, np.array(offspring1), np.array(offspring2))) # Realiza a mutação nos indivíduos da geração seguinte next_gen = binary_mutation(next_gen, f, pm, self.bounds) # Junta as duas populações e seleciona os npop-melhores indivíduos joined_pop = np.vstack((pop, next_gen)) pop = joined_pop[np.argsort(joined_pop[:, 2])][range(self.npop), :] ngen += 1 # Retorna a população com os valores decodificados for i in range(self.npop): pop[i, 0] = decode(pop[i, 0], self.bounds[0]) pop[i, 1] = decode(pop[i, 1], self.bounds[1]) self.final_gen = pop
def load_connection_config(jenkins_manager: 'JenkinsManager', connection_name: str) -> None: config_file = build_config_file(jenkins_connections_dir, connection_name) if os.path.isfile(config_file): config_parser = RawConfigParser() config_parser.read(config_file) url = config_parser.get(SECTION_TITLE, 'url') auth = {} if config_parser.has_option(SECTION_TITLE, 'password'): auth['username'] = config_parser.get(SECTION_TITLE, 'username') auth['password'] = decode( encode_password(), config_parser.get(SECTION_TITLE, 'password')) try: jenkins_connection = JenkinsConnection(connection_name, url, auth=auth) if config_parser.has_option(SECTION_TITLE, 'views'): view_names = config_parser.get(SECTION_TITLE, 'views').split(',') print('Loading Jenkins views for connection: {}'.format( connection_name)) for view_name in view_names: JenkinsView.load_view_config(jenkins_connection, view_name) jenkins_manager.jenkins_connections[ jenkins_connection.name] = jenkins_connection except Exception as e: print( 'WARNING! Error occurred during creation of Jenkins instance: {}.' .format(e)) print( 'Skipping addition of this instance. Check routing to url: {}' .format(url)) pause() else: 'No config file for {}.'.format(connection_name)