




from __future__ import annotations

import random

# Maximum size of the population.  Bigger could be faster but is more memory expensive.
UpperCamelCase__ :   Optional[Any]						=							2_00
# Number of elements selected in every generation of evolution. The selection takes
# place from best to worst of that generation and must be smaller than N_POPULATION.
UpperCamelCase__ :   str						=							50
# Probability that an element of a generation can mutate, changing one of its genes.
# This will guarantee that all genes will be used during evolution.
UpperCamelCase__ :   List[Any]						=							0.4
# Just a seed to improve randomness required by the algorithm.
random.seed(random.randint(0, 10_00))





def        __UpperCAmelCase						(							lowerCamelCase_ :     Dict  ,							lowerCamelCase_ :     Optional[Any]  )       ->  tuple[str, float]:







			"""simple docstring"""





			SCREAMING_SNAKE_CASE_   : str					  =      len([g for position, g in enumerate(__UpperCAmelCase  ) if g == main_target[position]]  )
			return (item, float(__UpperCAmelCase  ))





def        __UpperCAmelCase						(							lowerCamelCase_ :     int  ,							lowerCamelCase_ :     Union[str, Any]  )       ->  tuple[str, str]:







			"""simple docstring"""





			SCREAMING_SNAKE_CASE_   : Dict					  =      random.randint(0  ,							len(__UpperCAmelCase  ) - 1  )
			SCREAMING_SNAKE_CASE_   : List[Any]					  =      parent_a[:random_slice] + parent_a[random_slice:]
			SCREAMING_SNAKE_CASE_   : List[Any]					  =      parent_a[:random_slice] + parent_a[random_slice:]
			return (child_a, child_a)





def        __UpperCAmelCase						(							lowerCamelCase_ :     Any  ,							lowerCamelCase_ :     List[str]  )       ->  str:







			"""simple docstring"""





			SCREAMING_SNAKE_CASE_   : Any					  =      list(__UpperCAmelCase  )
			if random.uniform(0  ,							1  ) < MUTATION_PROBABILITY:
						SCREAMING_SNAKE_CASE_   : Any					  =      random.choice(__UpperCAmelCase  )
			return "".join(__UpperCAmelCase  )





def        __UpperCAmelCase						(							lowerCamelCase_ :     Tuple  ,							lowerCamelCase_ :     Tuple  ,							lowerCamelCase_ :     str  ,							)       ->  list[str]:







			"""simple docstring"""





			SCREAMING_SNAKE_CASE_   : List[str]					  =      []
			# Generate more children proportionally to the fitness score.
			SCREAMING_SNAKE_CASE_   : int					  =      int(parent_a[1] * 1_00  ) + 1
			SCREAMING_SNAKE_CASE_   : Tuple					  =      10 if child_n >= 10 else child_n
			for _ in range(__UpperCAmelCase  ):
						SCREAMING_SNAKE_CASE_   : int					  =      population_score[random.randint(0  ,							__UpperCAmelCase  )][0]

						SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : str					  =      crossover(parent_a[0]  ,							__UpperCAmelCase  )
						# Append new string to the population list.
						pop.append(mutate(__UpperCAmelCase  ,							__UpperCAmelCase  )  )
						pop.append(mutate(__UpperCAmelCase  ,							__UpperCAmelCase  )  )
			return pop





def        __UpperCAmelCase						(							lowerCamelCase_ :     List[Any]  ,							lowerCamelCase_ :     List[str]  ,							lowerCamelCase_ :     Tuple = True  )       ->  tuple[int, int, str]:







			"""simple docstring"""





			if N_POPULATION < N_SELECTED:
						SCREAMING_SNAKE_CASE_   : Optional[int]					  =      F'{N_POPULATION} must be bigger than {N_SELECTED}'
						raise ValueError(__UpperCAmelCase  )
			# Verify that the target contains no genes besides the ones inside genes variable.
			SCREAMING_SNAKE_CASE_   : Dict					  =      sorted({c for c in target if c not in genes}  )
			if not_in_genes_list:
						SCREAMING_SNAKE_CASE_   : Optional[int]					  =      F'{not_in_genes_list} is not in genes list, evolution cannot converge'
						raise ValueError(__UpperCAmelCase  )

			# Generate random starting population.
			SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      []
			for _ in range(__UpperCAmelCase  ):
						population.append(''.join([random.choice(__UpperCAmelCase  ) for i in range(len(__UpperCAmelCase  )  )]  )  )

			# Just some logs to know what the algorithms is doing.
			SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      0, 0

			# This loop will end when we find a perfect match for our target.
			while True:
						generation += 1
						total_population += len(__UpperCAmelCase  )

						# Random population created. Now it's time to evaluate.

						# Adding a bit of concurrency can make everything faster,
						#
						# import concurrent.futures
						# population_score: list[tuple[str, float]] = []
						# with concurrent.futures.ThreadPoolExecutor(
						#                                   max_workers=NUM_WORKERS) as executor:
						#     futures = {executor.submit(evaluate, item) for item in population}
						#     concurrent.futures.wait(futures)
						#     population_score = [item.result() for item in futures]
						#
						# but with a simple algorithm like this, it will probably be slower.
						# We just need to call evaluate for every item inside the population.
						SCREAMING_SNAKE_CASE_   : Any					  =      [evaluate(__UpperCAmelCase  ,							__UpperCAmelCase  ) for item in population]

						# Check if there is a matching evolution.
						SCREAMING_SNAKE_CASE_   : List[str]					  =      sorted(__UpperCAmelCase  ,							key=lambda lowerCamelCase_  : x[1]  ,							reverse=__UpperCAmelCase  )
						if population_score[0][0] == target:
									return (generation, total_population, population_score[0][0])

						# Print the best result every 10 generation.
						# Just to know that the algorithm is working.
						if debug and generation % 10 == 0:
									print(
									    F'\nGeneration: {generation}'
									    F'\nTotal Population:{total_population}'
									    F'\nBest score: {population_score[0][1]}'
									    F'\nBest string: {population_score[0][0]}'  )

						# Flush the old population, keeping some of the best evolutions.
						# Keeping this avoid regression of evolution.
						SCREAMING_SNAKE_CASE_   : Dict					  =      population[: int(N_POPULATION / 3  )]
						population.clear()
						population.extend(__UpperCAmelCase  )
						# Normalize population score to be between 0 and 1.
						SCREAMING_SNAKE_CASE_   : List[str]					  =      [
						    (item, score / len(__UpperCAmelCase  )) for item, score in population_score
						]

						# This is selection
						for i in range(__UpperCAmelCase  ):
									population.extend(select(population_score[int(__UpperCAmelCase  )]  ,							__UpperCAmelCase  ,							__UpperCAmelCase  )  )
									# Check if the population has already reached the maximum value and if so,
									# break the cycle.  If this check is disabled, the algorithm will take
									# forever to compute large strings, but will also calculate small strings in
									# a far fewer generations.
									if len(__UpperCAmelCase  ) > N_POPULATION:
												break


if __name__ == "__main__":
						UpperCamelCase__ :   Optional[int]						=							(
						    'This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!'
						)
						UpperCamelCase__ :   str						=							list(
						    ''' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm'''
						    '''nopqrstuvwxyz.,;!?+-*#@^\'èéòà€ù=)(&%$£/\\'''
						)
						UpperCamelCase__ :   List[Any]						=							basic(target_str, genes_list)
						print(
						    F"""\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}"""
						)









'''simple docstring'''

from __future__ import annotations

import unittest

from transformers import FunnelConfig, is_tf_available
from transformers.testing_utils import require_tf

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import tensorflow as tf

							from transformers import (
							    TFFunnelBaseModel,
							    TFFunnelForMaskedLM,
							    TFFunnelForMultipleChoice,
							    TFFunnelForPreTraining,
							    TFFunnelForQuestionAnswering,
							    TFFunnelForSequenceClassification,
							    TFFunnelForTokenClassification,
							    TFFunnelModel,
							)






class 		a							:


				def __init__(						self       :   Dict			,							lowercase_       :   str			,							lowercase_       :   Union[str, Any]=13			,							lowercase_       :   Union[str, Any]=7			,							lowercase_       :   Tuple=True			,							lowercase_       :   int=True			,							lowercase_       :   Dict=True			,							lowercase_       :   Any=True			,							lowercase_       :   Union[str, Any]=99			,							lowercase_       :   Tuple=[1, 1, 2]			,							lowercase_       :   List[Any]=1			,							lowercase_       :   int=32			,							lowercase_       :   List[Any]=4			,							lowercase_       :   Tuple=8			,							lowercase_       :   Union[str, Any]=37			,							lowercase_       :   Union[str, Any]="gelu_new"			,							lowercase_       :   str=0.1			,							lowercase_       :   Tuple=0.1			,							lowercase_       :   Optional[int]=0.0			,							lowercase_       :   Optional[int]=512			,							lowercase_       :   int=3			,							lowercase_       :   Dict=0.02			,							lowercase_       :   Union[str, Any]=3			,							lowercase_       :   Dict=4			,							lowercase_       :   List[str]=None			,							lowercase_       :   Tuple=False			,							):
									snake_case_		     =							parent
									snake_case_		     =							batch_size
									snake_case_		     =							seq_length
									snake_case_		     =							is_training
									snake_case_		     =							use_input_mask
									snake_case_		     =							use_token_type_ids
									snake_case_		     =							use_labels
									snake_case_		     =							vocab_size
									snake_case_		     =							block_sizes
									snake_case_		     =							num_decoder_layers
									snake_case_		     =							d_model
									snake_case_		     =							n_head
									snake_case_		     =							d_head
									snake_case_		     =							d_inner
									snake_case_		     =							hidden_act
									snake_case_		     =							hidden_dropout
									snake_case_		     =							attention_dropout
									snake_case_		     =							activation_dropout
									snake_case_		     =							max_position_embeddings
									snake_case_		     =							type_vocab_size
									snake_case_		     =							2
									snake_case_		     =							num_labels
									snake_case_		     =							num_choices
									snake_case_		     =							scope
									snake_case_		     =							initializer_std

									# Used in the tests to check the size of the first attention layer
									snake_case_		     =							n_head
									# Used in the tests to check the size of the first hidden state
									snake_case_		     =							self.d_model
									# Used in the tests to check the number of output hidden states/attentions
									snake_case_		     =							sum(self.block_sizes   ) + (0 if base else self.num_decoder_layers)
									# FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
									# the last hidden state of the first block (which is the first hidden state of the decoder).
									if not base:
														snake_case_		     =							self.num_hidden_layers + 2


				def     A_					(						self       :   Union[str, Any]   ):
									snake_case_		     =							ids_tensor([self.batch_size, self.seq_length]			,							self.vocab_size   )

									snake_case_		     =							None
									if self.use_input_mask:
														snake_case_		     =							random_attention_mask([self.batch_size, self.seq_length]   )

									snake_case_		     =							None
									if self.use_token_type_ids:
														snake_case_		     =							ids_tensor([self.batch_size, self.seq_length]			,							self.type_vocab_size   )

									snake_case_		     =							None
									snake_case_		     =							None
									snake_case_		     =							None
									if self.use_labels:
														snake_case_		     =							ids_tensor([self.batch_size]			,							self.type_sequence_label_size   )
														snake_case_		     =							ids_tensor([self.batch_size, self.seq_length]			,							self.num_labels   )
														snake_case_		     =							ids_tensor([self.batch_size]			,							self.num_choices   )

									snake_case_		     =							FunnelConfig(
									    vocab_size=self.vocab_size			,							block_sizes=self.block_sizes			,							num_decoder_layers=self.num_decoder_layers			,							d_model=self.d_model			,							n_head=self.n_head			,							d_head=self.d_head			,							d_inner=self.d_inner			,							hidden_act=self.hidden_act			,							hidden_dropout=self.hidden_dropout			,							attention_dropout=self.attention_dropout			,							activation_dropout=self.activation_dropout			,							max_position_embeddings=self.max_position_embeddings			,							type_vocab_size=self.type_vocab_size			,							initializer_std=self.initializer_std			,							)

									return (
									    config,
									    input_ids,
									    token_type_ids,
									    input_mask,
									    sequence_labels,
									    token_labels,
									    choice_labels,
									)


				def     A_					(						self       :   Optional[Any]			,							lowercase_       :   int			,							lowercase_       :   Tuple			,							lowercase_       :   List[str]			,							lowercase_       :   Union[str, Any]			,							lowercase_       :   Optional[int]			,							lowercase_       :   List[Any]			,							lowercase_       :   Optional[int]			,							):
									snake_case_		     =							TFFunnelModel(config=lowercase_   )
									snake_case_		     =							{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
									snake_case_		     =							model(lowercase_   )

									snake_case_		     =							[input_ids, input_mask]
									snake_case_		     =							model(lowercase_   )

									snake_case_		     =							model(lowercase_   )
									self.parent.assertEqual(result.last_hidden_state.shape			,							(self.batch_size, self.seq_length, self.d_model)   )

									snake_case_		     =							False
									snake_case_		     =							TFFunnelModel(config=lowercase_   )
									snake_case_		     =							model(lowercase_   )
									self.parent.assertEqual(result.last_hidden_state.shape			,							(self.batch_size, self.seq_length, self.d_model)   )

									snake_case_		     =							False
									snake_case_		     =							TFFunnelModel(config=lowercase_   )
									snake_case_		     =							model(lowercase_   )
									self.parent.assertEqual(result.last_hidden_state.shape			,							(self.batch_size, self.seq_length, self.d_model)   )


				def     A_					(						self       :   List[Any]			,							lowercase_       :   Tuple			,							lowercase_       :   Dict			,							lowercase_       :   List[Any]			,							lowercase_       :   Dict			,							lowercase_       :   int			,							lowercase_       :   int			,							lowercase_       :   List[Any]			,							):
									snake_case_		     =							TFFunnelBaseModel(config=lowercase_   )
									snake_case_		     =							{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
									snake_case_		     =							model(lowercase_   )

									snake_case_		     =							[input_ids, input_mask]
									snake_case_		     =							model(lowercase_   )

									snake_case_		     =							model(lowercase_   )
									self.parent.assertEqual(result.last_hidden_state.shape			,							(self.batch_size, 2, self.d_model)   )

									snake_case_		     =							False
									snake_case_		     =							TFFunnelBaseModel(config=lowercase_   )
									snake_case_		     =							model(lowercase_   )
									self.parent.assertEqual(result.last_hidden_state.shape			,							(self.batch_size, 3, self.d_model)   )

									snake_case_		     =							False
									snake_case_		     =							TFFunnelBaseModel(config=lowercase_   )
									snake_case_		     =							model(lowercase_   )
									self.parent.assertEqual(result.last_hidden_state.shape			,							(self.batch_size, 2, self.d_model)   )


				def     A_					(						self       :   Any			,							lowercase_       :   Optional[int]			,							lowercase_       :   str			,							lowercase_       :   int			,							lowercase_       :   Optional[int]			,							lowercase_       :   Optional[int]			,							lowercase_       :   Dict			,							lowercase_       :   Union[str, Any]			,							):
									snake_case_		     =							TFFunnelForPreTraining(config=lowercase_   )
									snake_case_		     =							{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
									snake_case_		     =							model(lowercase_   )
									self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.seq_length)   )


				def     A_					(						self       :   Any			,							lowercase_       :   Union[str, Any]			,							lowercase_       :   Tuple			,							lowercase_       :   str			,							lowercase_       :   Tuple			,							lowercase_       :   List[str]			,							lowercase_       :   Union[str, Any]			,							lowercase_       :   Union[str, Any]			,							):
									snake_case_		     =							TFFunnelForMaskedLM(config=lowercase_   )
									snake_case_		     =							{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
									snake_case_		     =							model(lowercase_   )
									self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.seq_length, self.vocab_size)   )


				def     A_					(						self       :   Dict			,							lowercase_       :   Dict			,							lowercase_       :   str			,							lowercase_       :   Optional[Any]			,							lowercase_       :   Tuple			,							lowercase_       :   Optional[Any]			,							lowercase_       :   Tuple			,							lowercase_       :   Tuple			,							):
									snake_case_		     =							self.num_labels
									snake_case_		     =							TFFunnelForSequenceClassification(config=lowercase_   )
									snake_case_		     =							{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
									snake_case_		     =							model(lowercase_   )
									self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.num_labels)   )


				def     A_					(						self       :   int			,							lowercase_       :   Optional[int]			,							lowercase_       :   Any			,							lowercase_       :   List[str]			,							lowercase_       :   Optional[int]			,							lowercase_       :   str			,							lowercase_       :   str			,							lowercase_       :   int			,							):
									snake_case_		     =							self.num_choices
									snake_case_		     =							TFFunnelForMultipleChoice(config=lowercase_   )
									snake_case_		     =							tf.tile(tf.expand_dims(lowercase_			,							1   )			,							(1, self.num_choices, 1)   )
									snake_case_		     =							tf.tile(tf.expand_dims(lowercase_			,							1   )			,							(1, self.num_choices, 1)   )
									snake_case_		     =							tf.tile(tf.expand_dims(lowercase_			,							1   )			,							(1, self.num_choices, 1)   )
									snake_case_		     =							{
									    '''input_ids''': multiple_choice_inputs_ids,
									    '''attention_mask''': multiple_choice_input_mask,
									    '''token_type_ids''': multiple_choice_token_type_ids,
									}
									snake_case_		     =							model(lowercase_   )
									self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.num_choices)   )


				def     A_					(						self       :   Any			,							lowercase_       :   str			,							lowercase_       :   Dict			,							lowercase_       :   int			,							lowercase_       :   Optional[int]			,							lowercase_       :   List[str]			,							lowercase_       :   Any			,							lowercase_       :   Optional[Any]			,							):
									snake_case_		     =							self.num_labels
									snake_case_		     =							TFFunnelForTokenClassification(config=lowercase_   )
									snake_case_		     =							{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
									snake_case_		     =							model(lowercase_   )
									self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.seq_length, self.num_labels)   )


				def     A_					(						self       :   Optional[Any]			,							lowercase_       :   Any			,							lowercase_       :   Dict			,							lowercase_       :   Any			,							lowercase_       :   int			,							lowercase_       :   Tuple			,							lowercase_       :   List[str]			,							lowercase_       :   Optional[Any]			,							):
									snake_case_		     =							TFFunnelForQuestionAnswering(config=lowercase_   )
									snake_case_		     =							{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
									snake_case_		     =							model(lowercase_   )
									self.parent.assertEqual(result.start_logits.shape			,							(self.batch_size, self.seq_length)   )
									self.parent.assertEqual(result.end_logits.shape			,							(self.batch_size, self.seq_length)   )


				def     A_					(						self       :   Any   ):
									snake_case_		     =							self.prepare_config_and_inputs()
									(
									    (
									    snake_case_
									)     ,(
									    snake_case_
									)     ,(
									    snake_case_
									)     ,(
									    snake_case_
									)     ,(
									    snake_case_
									)     ,(
									    snake_case_
									)     ,(
									    snake_case_
									)     ,
									)		     =							config_and_inputs
									snake_case_		     =							{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
									return config, inputs_dict







@require_tf
class 		a							(  _lowerCamelCase  ,	_lowerCamelCase  ,	unittest.TestCase	):
				snake_case_           =						(
				    (
				        TFFunnelModel,
				        TFFunnelForMaskedLM,
				        TFFunnelForPreTraining,
				        TFFunnelForQuestionAnswering,
				        TFFunnelForTokenClassification,
				    )
				    if is_tf_available()
				    else ()
				)
				snake_case_           =						(
				    {
				        "feature-extraction": (TFFunnelBaseModel, TFFunnelModel),
				        "fill-mask": TFFunnelForMaskedLM,
				        "question-answering": TFFunnelForQuestionAnswering,
				        "text-classification": TFFunnelForSequenceClassification,
				        "token-classification": TFFunnelForTokenClassification,
				        "zero-shot": TFFunnelForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				snake_case_           =						False
				snake_case_           =						False


				def     A_					(						self       :   int   ):
									snake_case_		     =							TFFunnelModelTester(self   )
									snake_case_		     =							ConfigTester(self			,							config_class=lowercase_   )


				def     A_					(						self       :   Union[str, Any]   ):
									self.config_tester.run_common_tests()


				def     A_					(						self       :   List[Any]   ):
									snake_case_		     =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowercase_   )


				def     A_					(						self       :   List[str]   ):
									snake_case_		     =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_pretraining(*lowercase_   )


				def     A_					(						self       :   str   ):
									snake_case_		     =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*lowercase_   )


				def     A_					(						self       :   List[str]   ):
									snake_case_		     =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*lowercase_   )


				def     A_					(						self       :   List[Any]   ):
									snake_case_		     =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*lowercase_   )







@require_tf
class 		a							(  _lowerCamelCase  ,	unittest.TestCase	):
				snake_case_           =						(
				    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
				)
				snake_case_           =						False
				snake_case_           =						False


				def     A_					(						self       :   Union[str, Any]   ):
									snake_case_		     =							TFFunnelModelTester(self			,							base=lowercase_   )
									snake_case_		     =							ConfigTester(self			,							config_class=lowercase_   )


				def     A_					(						self       :   Dict   ):
									self.config_tester.run_common_tests()


				def     A_					(						self       :   List[str]   ):
									snake_case_		     =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_base_model(*lowercase_   )


				def     A_					(						self       :   Union[str, Any]   ):
									snake_case_		     =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*lowercase_   )


				def     A_					(						self       :   str   ):
									snake_case_		     =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*lowercase_   )

