# How to use _valid_connection method in autotest

Best Python code snippet using autotest_python

cgp_py3.py

Source:cgp_py3.py

`1"""2Cartesian Genetic Programming3=============================4Genetic Programming is concerned with the automatic evolution (as in 5Darwinian evolution) of computational structures (such as mathematical 6equations, computer programs, digital circuits, etc.). CGP is a highly 7efficient and flexible form of Genetic Programming that encodes a graph 8representation of a computer program. It was developed by Julian Miller9in 1999. For more information see: http://cartesiangp.co.uk/10This script written by Petr Dvoracek in 2017. http://flea.name/11"""12import random 13from copy import deepcopy14# The main pillar of this algorithm lies in the directed oriented graph in15# which each node represents a specific operation. 16# The nodes are commonly arranged in square lattice.17DEFAULT_MATRIX_SIZE = (2, 5) # ROWS, COLS18# The interconnection between columns. 19DEFAULT_LEVEL_BACK = 5 # If the value is equal to number of columns then 20 # the interconnection is maximal.21# The operation set. 22# List of tuples(`str` name, `int` arity (inputs), `function` operation). 23DEFAULT_OPERATION_SET = set([("BUF", 1, lambda *arg: arg[0]),24 ("NOT", 1, lambda *arg: ~arg[0]), 25 ("AND", 2, lambda *arg: ~(arg[0] & arg[1])), 26 ("OR", 2, lambda *arg: ~(arg[0] | arg[1])), 27 ("XOR", 2, lambda *arg: arg[0] & arg[1]), 28 ("NAND", 2, lambda *arg: arg[0] | arg[1]),29 ("NOR", 2, lambda *arg: arg[0] ^ arg[1]), 30 ("XNOR", 2, lambda *arg: ~(arg[0] ^ arg[1])),31 ])32# Lets find full adder by default.33# For all input combinations define:34DEFAULT_INPUT_DATA = [0b00001111, # A35 0b00110011, # B36 0b01010101] # Carry in37# Define output combinations38DEFAULT_TRAINING_DATA = [0b01101001, # SUM39 0b00010111] # Carry out40# The CGP is a genetic algorithm. Therefore, we need to define evolutionary parameters.41# The number of candidate solutions.42DEFAULT_POP_SIZE = 5 43# Maximal generations (how long the algorithm will run)44DEFAULT_MAX_GENERATIONS = 10000 45# This function returns the list of objects with poistion `index` in the `nested_list` (list of arrays).46# Example: get_sublist(1, [("A", 3, 5), ("C", 1), ("D", "C")]) --> [3, 1, "C"]47get_sublist = lambda index, nested_list : list(map(lambda sublist: sublist[index], nested_list))48# How many genes will be mutated.49# 10% of the chromosome length can be mutated. Chromosome length = each_node * len(node) + len(last_node); The length of node is maximal arity + the operation.50DEFAULT_MUTATIONS = int((DEFAULT_MATRIX_SIZE[0] * DEFAULT_MATRIX_SIZE[1] * (max(get_sublist(1, DEFAULT_OPERATION_SET)) + 1) + len(DEFAULT_TRAINING_DATA)) * 0.10)51#DEFAULT_MUTATIONS = 452class CGP(object):53 # Printing if we find better fitness during the evolution.54 PRINT_INFO = False55 def __set_connections(self):56 """ Calculates the set of valid connections """57 # For each column create valid connection interval from previous `l-back` connections.58 self._valid_connection = [(self.rows * (column - self.level_back), self.rows * column) for column in range(self.columns)] 59 # Filter invalid indexes which are negative -> make them 060 self._valid_connection = list(map(lambda sublist: list(map(lambda x: x if (x >= 0) else 0, sublist)), self._valid_connection))61 def __test_operation_set(self, operation_set):62 """ Check the validity of operation set.63 64 Parameters65 operation_set : list of operations66 Each opeartaion is a tuple(`str` name, `int` arity, `function` operation). 67 Raises68 CGPError69 If the operation set contains inconsistent operations.70 """71 72 # Test for empty set.73 if not operation_set:74 raise CGPError("EMPTY_SET")75 # Test if each operation has a triplet of (str, int, function) and tests the arity.76 for operation in operation_set:77 if len(operation) != 3 or type(operation[0]) is not str \78 or type(operation[1]) is not int \79 or not callable(operation[2]):80 raise CGPError("INCONSISTENT_SET", value=operation)81 # Negative arity of function is stupid. Unless, you are doing some deep shit math. 82 if operation[1] < 0: 83 raise CGPError("INCONSISTENT_ARITY", value=operation)84 def set_data(self, input_data, training_data):85 """ Set Input and output data86 87 Parameters88 input_data : list89 The list of input data. In the case of digital circuit design, 90 this contains all the input combinations. 91 training_data : list92 The target circuit.93 """94 self.input_data_range = list(range(-len(input_data), 0))95 self.input_data = input_data96 self.training_data = training_data97 def set_matrix(self, matrix, level_back=0):98 """ Initate the matrix. 99 Parameters100 matrix : tuple (int, int)101 This tuple represents the size of matrix (rows, columns)102 Other Parameters103 level_back : int104 The interconnection between columns.105 """106 self.matrix = matrix # Though, it is not used.107 self.rows = matrix[0]108 self.columns = matrix[1]109 self.level_back = level_back if level_back > 0 else matrix[1] # If `level_back` has invalid value (0 or less), then the interconnection is maximal. 110 self.__set_connections()111 112 def set_operation_set(self, operation_set):113 """ Set the operation set.114 Parameters115 operation_set : list of operations116 Each opeartaion is a tuple(`str` name, `int` arity, `function` operation). 117 Raises118 CGPError119 If the operation set contains inconsistent operations.120 """121 self.__test_operation_set(operation_set)122 self.operation_set = operation_set123 # Transpose the operation set data.124 self.operations = get_sublist(2, operation_set)125 self.operations_arity = get_sublist(1, operation_set)126 self.operations_names = get_sublist(0, operation_set)127 # Get maximal arity128 self.maximal_arity = max(self.operations_arity)129 130 def __init__(self, matrix, operation_set, input_data, training_data, level_back=0):131 """ Initate the matrix, node arrangement, operation set and i/o data. 132 Parameters133 matrix : tuple (int, int)134 This tuple represents the size of matrix (rows, columns)135 operation set : list of tuples (`str` name, `int` arity, `function` operation). 136 The operation set.137 input_data : list138 The list of input data. In the case of digital circuit design, 139 this contains all the input combinations. 140 training_data : list141 The target circuit.142 Other Parameters143 level_back : int144 The interconnection between columns.145 Raises146 CGPError147 If the operation set contains inconsistent operations.148 """149 super(CGP, self).__init__()150 151 self.set_matrix(matrix, level_back=level_back)152 self.set_operation_set(operation_set)153 self.set_data(input_data, training_data)154 def __create_chromosome(self):155 """ Initate the chromosome. 156 The chromsome encodes the interconnection of the CGP graph. See: http://www.oranchak.com/cgp/doc/fig2.jpg157 """158 # Init array for chromosome159 chromosome = []160 # This function selects random value from input data or previous node in the graph.161 connect = lambda connection: random.choice(self.input_data_range + list(range(connection[0], connection[1])))162 163 # Nodes may differ by their connection between columns. The interconnection is depended on the value of level back. 164 for column_index in range(self.columns):165 # Get valid connection range for nodes in the column166 valid_con = self._valid_connection[column_index]167 # Function for the creation of the node. Firstly creates the connections, then adds the node operation. 168 create_node = lambda: [connect(valid_con) for _ in range(self.maximal_arity)] + [random.choice(self.operations)]169 chromosome += [create_node() for _ in range(self.rows)]170 # Add last `node` which binds the primary outputs of the graph to nodes.171 valid_con = (0, len(chromosome))172 chromosome += [[connect(valid_con) for _ in self.training_data]]173 return chromosome174 def __init_pop(self, pop_size):175 """ Initiate the population of `pop_size` size """176 self.pop = [self.__create_chromosome() for _ in range(pop_size)]177 # The best individual is saved in `self.best_chrom` with the best fitness `self.fitness`.178 self.best_chrom = deepcopy(self.pop[0])179 self.best_fitness = self.max_fitness180 def __mutate_gene(self, chromosome):181 """ Mutate random value in a `chromsome`. 182 183 Parameters184 chromosome : chromsome strucutre185 The representation of candidate solution which is subject to a mutation. 186 """187 # This function selects random value from input data or previous node in the graph.188 connect = lambda connection: random.choice(self.input_data_range + list(range(connection[0], connection[1])))189 # Select some node190 random_node_idx = random.randint(0, len(chromosome) - 1)191 random_node = chromosome[random_node_idx]192 193 # And select an index from the node194 random_idx = random.randint(0, len(random_node) - 1) # Note: A <= rnd <= B195 previous_value = random_node[random_idx]196 if random_node is chromosome[-1]:197 # We mutate the last node198 while previous_value == random_node[random_idx]:199 random_node[random_idx] = connect((0, len(chromosome)))200 elif random_idx == self.maximal_arity:201 # We mutate the operation (indexing from 0)202 while previous_value == random_node[random_idx]:203 random_node[random_idx] = random.choice(self.operations)204 else:205 # We mutate the connection206 while previous_value == random_node[random_idx]:207 random_node[random_idx] = connect(self._valid_connection[ random_node_idx // self.rows ])208 def mutate(self):209 """ Creates a copy of the best solution and mutates it random-times.210 Return211 chromsome : chromosome structure212 The representation of the other candidate solution, which is similar to the best found solution.213 """214 # Copy the best chromsome215 chromosome = deepcopy(self.best_chrom)216 # And mutate it as much as possible217 for _ in range(random.randint(1, self.mutation_rate+1)): # Mutate it at least once. Else the chromsome wouldn't change.218 self.__mutate_gene(chromosome)219 return chromosome220 def init_eval(self):221 """ This function is started before the run of evolution. """222 # Buffer for the evaluation.223 buffer_data = deepcopy(self.input_data) + list(range(self.rows * self.columns + len(self.training_data)))224 buffer_indexes = list(range(-len(self.input_data), len(buffer_data) - len(self.input_data)))225 self.buffer = dict(zip(buffer_indexes, buffer_data))226 227 # Maximal fitness228 self.max_fitness = len(self.training_data) * (2 ** len(self.input_data))229 # Fitness mask.230 # V mask V The number of bites V # We are doing parallel evaluation. 231 self.bitmask = 2 ** (2 ** len(self.input_data)) - 1232 # Set generations.233 self.generation = 0234 def chrom_to_str(self, chromosome):235 """ Creates a string representation of chromosome, in which the nodes are aligned into a lattice. 236 Each node has syntax `index` : [`previous index`, ... , `previous index`, `Function name`] 237 If the node was primary output, then the node is ended with a string `<- `. 238 The primary outputs are also printed in the last line for the sake of their permutation. 239 Parameters240 chromsome : chromosome structure241 The representation of chromosome.242 Returns243 str244 The string representation of chromosome.245 """246 # Some math stuff247 max_input_strlen = len(str(self.columns * self.rows))248 max_length_of_operation_string = max(list(map(len, self.operations_names)))249 chrom_str = ""250 for i in range(self.rows):251 line = ""252 for j in range(self.columns):253 # Grab the node index 254 node_idx = j * self.rows + i255 node = chromosome[node_idx]256 # Find the inputs, and align them.257 inputs = node[:-1] # The last node is operation; the rest are the inputs.258 aligned_inputs = list(map(lambda x: str(x).rjust(max_input_strlen), inputs))259 # Find out the operation name and align it.260 operation = node[-1]261 operation_index = self.operations.index(operation)262 operation_name = self.operations_names[operation_index]263 aligned_operation_name = operation_name.rjust(max_length_of_operation_string)264 # save it265 if node_idx in chromosome[-1]:266 flag = "<- "267 else:268 flag = " "269 line += str(node_idx).rjust(max_input_strlen) + ": [" + ", ".join(aligned_inputs) + ", " + aligned_operation_name + "]" + flag270 chrom_str += line + "\n"271 return chrom_str + str(chromosome[-1])272 273 def eval(self, chromosome):274 """ Evaluates the `chromosome` and returns the fitness value. Also checks if we found a better solution.275 Parameters276 chromsome : chromosome structure277 The representation of a candidate solution.278 Returns279 int : fitness value280 The quality of the candidate solution.281 TODO282 Optimise it. Skip neutral mutations. Skip unused nodes. (Maybe use C/C++ function?)283 Move the fitness check 284 """285 fitness = 0 286 # Evaluate each node.287 for idx, node in enumerate(chromosome[:-1]):288 # TODO SKIP unused nodes289 # Save the value of the operation `node[-1]`. The arguments are given in `node[:-1]` and they are the indexes into the `buffer`.290 self.buffer[idx] = node[-1](*[self.buffer[i] for i in node[:-1]])291 # Grab the value 292 for idx, target in zip(chromosome[-1], self.training_data):293 #print idx, target, self.bitmask294 fitness += bin((self.buffer[idx] ^ target) & self.bitmask).count("1")295 # Checks if we found a better solution.296 # Shouldnt be here.297 if fitness <= self.best_fitness:298 if CGP.PRINT_INFO and fitness < self.best_fitness:299 print("Generation: " + str(self.generation).rjust(10) + "\tFitness: " + str(fitness))300 self.best_fitness = fitness301 self.best_chrom = deepcopy(chromosome)302 return fitness303 def run(self, pop_size, mutations, generations):304 """ Runs the evolution.305 Parameters306 chromsome : chromosome structure307 The representation of chromosome.308 """309 # Init evaluation310 self.init_eval()311 # Init mutation312 self.mutation_rate = mutations313 # Creates first population `self.pop` and evaluates it `self.eval`.314 self.__init_pop(pop_size)315 # Run evolution316 for self.generation in range(generations):317 # Evaluate pop318 list(map(self.eval, self.pop))319 # Mutate pop; fix the peroformance 320 new_pop = [self.mutate() for _ in self.pop]321 self.pop = new_pop322 #break323 324 def __str__(self):325 return self.chrom_to_str(self.best_chrom)326 327class CGPError(Exception):328 def __init__(self, error_code, value=""):329 """ Inits the exception. """330 self.error_code = error_code331 self.value = value332 def __str__(self):333 """ Print error message, depending on the given error code. """334 error_msg = {335 "EMPTY_SET" : "The operation set is empty.",336 "INCONSISTENT_SET" : "The operation set contains inconsistent operation: " + repr(self.value) + "\n"\337 + "Please use triplet (`str` name, `int` arity, `function` operation)",338 "INCONSISTENT_ARITY" : "The arity of operation " + repr(self.value) +" can not be negative.", # Note: if you are doing some high math sh.t, then write your own CGP with blackjack and h..kers.339 }.get(self.error_code, "Unknown error - " + repr(self.error_code))340 return error_msg341if __name__ == '__main__':342 CGP.PRINT_INFO = True343 cgp = CGP(DEFAULT_MATRIX_SIZE, DEFAULT_OPERATION_SET, DEFAULT_INPUT_DATA, DEFAULT_TRAINING_DATA)344 cgp.run(DEFAULT_POP_SIZE, DEFAULT_MUTATIONS, DEFAULT_MAX_GENERATIONS)...`

cgp.py

Source:cgp.py

`1"""2Cartesian Genetic Programming3=============================4Genetic Programming is concerned with the automatic evolution (as in 5Darwinian evolution) of computational structures (such as mathematical 6equations, computer programs, digital circuits, etc.). CGP is a highly 7efficient and flexible form of Genetic Programming that encodes a graph 8representation of a computer program. It was developed by Julian Miller9in 1999. For more information see: http://cartesiangp.co.uk/10This script written by Petr Dvoracek in 2017. http://flea.name/11"""12import random 13from copy import deepcopy14# The main pillar of this algorithm lies in the directed oriented graph in15# which each node represents a specific operation. 16# The nodes are commonly arranged in square lattice.17DEFAULT_MATRIX_SIZE = (2, 5) # ROWS, COLS18# The interconnection between columns. 19DEFAULT_LEVEL_BACK = 5 # If the value is equal to number of columns then 20 # the interconnection is maximal.21# The operation set. 22# List of tuples(`str` name, `int` arity (inputs), `function` operation). 23DEFAULT_OPERATION_SET = set([("BUF", 1, lambda *arg: arg[0]),24 ("NOT", 1, lambda *arg: ~arg[0]), 25 ("NAND", 2, lambda *arg: ~(arg[0] & arg[1])), 26 ("NOR", 2, lambda *arg: ~(arg[0] | arg[1])), 27 ("AND", 2, lambda *arg: arg[0] & arg[1]), 28 ("OR", 2, lambda *arg: arg[0] | arg[1]),29 ("XOR", 2, lambda *arg: arg[0] ^ arg[1]), 30 ("XNOR", 2, lambda *arg: ~(arg[0] ^ arg[1])),31 ])32# Lets find full adder by default.33# For all input combinations define:34DEFAULT_INPUT_DATA = [0b00001111, # A35 0b00110011, # B36 0b01010101] # Carry in37# Define output combinations38DEFAULT_TRAINING_DATA = [0b01101001, # SUM39 0b00010111] # Carry out40# The CGP is a genetic algorithm. Therefore, we need to define evolutionary parameters.41# The number of candidate solutions.42DEFAULT_POP_SIZE = 5 43# Maximal generations (how long the algorithm will run)44DEFAULT_MAX_GENERATIONS = 10000 45# This function returns the list of objects with poistion `index` in the `nested_list` (list of arrays).46# Example: get_sublist(1, [("A", 3, 5), ("C", 1), ("D", "C")]) --> [3, 1, "C"]47get_sublist = lambda index, nested_list : map(lambda sublist: sublist[index], nested_list)48# How many genes will be mutated.49# 10% of the chromosome length can be mutated. Chromosome length = each_node * len(node) + len(last_node); The length of node is maximal arity + the operation.50DEFAULT_MUTATIONS = int((DEFAULT_MATRIX_SIZE[0] * DEFAULT_MATRIX_SIZE[1] * (max(get_sublist(1, DEFAULT_OPERATION_SET)) + 1) + len(DEFAULT_TRAINING_DATA)) * 0.10)51#DEFAULT_MUTATIONS = 452class CGP(object):53 # Printing if we find better fitness during the evolution.54 PRINT_INFO = False55 def __set_connections(self):56 """ Calculates the set of valid connections """57 # For each column create valid connection interval from previous `l-back` connections.58 self._valid_connection = [(self.rows * (column - self.level_back), self.rows * column) for column in xrange(self.columns)] 59 # Filter invalid indexes which are negative -> make them 060 self._valid_connection = map(lambda sublist: map(lambda x: x if (x >= 0) else 0, sublist), self._valid_connection)61 def __test_operation_set(self, operation_set):62 """ Check the validity of operation set.63 64 Parameters65 operation_set : list of operations66 Each opeartaion is a tuple(`str` name, `int` arity, `function` operation). 67 Raises68 CGPError69 If the operation set contains inconsistent operations.70 """71 72 # Test for empty set.73 if not operation_set:74 raise CGPError("EMPTY_SET")75 # Test if each operation has a triplet of (str, int, function) and tests the arity.76 for operation in operation_set:77 if len(operation) != 3 or type(operation[0]) is not str \78 or type(operation[1]) is not int \79 or not callable(operation[2]):80 raise CGPError("INCONSISTENT_SET", value=operation)81 # Negative arity of function is stupid. Unless, you are doing some deep shit math. 82 if operation[1] < 0: 83 raise CGPError("INCONSISTENT_ARITY", value=operation)84 def set_data(self, input_data, training_data):85 """ Set Input and output data86 87 Parameters88 input_data : list89 The list of input data. In the case of digital circuit design, 90 this contains all the input combinations. 91 training_data : list92 The target circuit.93 """94 self.input_data_range = range(-len(input_data), 0)95 self.input_data = input_data96 self.training_data = training_data97 def set_matrix(self, matrix, level_back=0):98 """ Initate the matrix. 99 Parameters100 matrix : tuple (int, int)101 This tuple represents the size of matrix (rows, columns)102 Other Parameters103 level_back : int104 The interconnection between columns.105 """106 self.matrix = matrix # Though, it is not used.107 self.rows = matrix[0]108 self.columns = matrix[1]109 self.level_back = level_back if level_back > 0 else matrix[1] # If `level_back` has invalid value (0 or less), then the interconnection is maximal. 110 self.__set_connections()111 112 def set_operation_set(self, operation_set):113 """ Set the operation set.114 Parameters115 operation_set : list of operations116 Each opeartaion is a tuple(`str` name, `int` arity, `function` operation). 117 Raises118 CGPError119 If the operation set contains inconsistent operations.120 """121 self.__test_operation_set(operation_set)122 self.operation_set = operation_set123 # Transpose the operation set data.124 self.operations = get_sublist(2, operation_set)125 self.operations_arity = get_sublist(1, operation_set)126 self.operations_names = get_sublist(0, operation_set)127 # Get maximal arity128 self.maximal_arity = max(self.operations_arity)129 130 def __init__(self, matrix, operation_set, input_data, training_data, level_back=0):131 """ Initate the matrix, node arrangement, operation set and i/o data. 132 Parameters133 matrix : tuple (int, int)134 This tuple represents the size of matrix (rows, columns)135 operation set : list of tuples (`str` name, `int` arity, `function` operation). 136 The operation set.137 input_data : list138 The list of input data. In the case of digital circuit design, 139 this contains all the input combinations. 140 training_data : list141 The target circuit.142 Other Parameters143 level_back : int144 The interconnection between columns.145 Raises146 CGPError147 If the operation set contains inconsistent operations.148 """149 super(CGP, self).__init__()150 151 self.set_matrix(matrix, level_back=level_back)152 self.set_operation_set(operation_set)153 self.set_data(input_data, training_data)154 def __create_chromosome(self):155 """ Initate the chromosome. 156 The chromsome encodes the interconnection of the CGP graph. See: http://www.oranchak.com/cgp/doc/fig2.jpg157 """158 # Init array for chromosome159 chromosome = []160 # This function selects random value from input data or previous node in the graph.161 connect = lambda connection: random.choice(self.input_data_range + range(connection[0], connection[1]))162 163 # Nodes may differ by their connection between columns. The interconnection is depended on the value of level back. 164 for column_index in xrange(self.columns):165 # Get valid connection range for nodes in the column166 valid_con = self._valid_connection[column_index]167 # Function for the creation of the node. Firstly creates the connections, then adds the node operation. 168 create_node = lambda: [connect(valid_con) for _ in xrange(self.maximal_arity)] + [random.choice(self.operations)]169 chromosome += [create_node() for _ in xrange(self.rows)]170 # Add last `node` which binds the primary outputs of the graph to nodes.171 valid_con = (0, len(chromosome))172 chromosome += [[connect(valid_con) for _ in self.training_data]]173 return chromosome174 def __init_pop(self, pop_size):175 """ Initiate the population of `pop_size` size """176 self.pop = [self.__create_chromosome() for _ in xrange(pop_size)]177 # The best individual is saved in `self.best_chrom` with the best fitness `self.fitness`.178 self.best_chrom = deepcopy(self.pop[0])179 self.best_fitness = self.max_fitness180 def __mutate_gene(self, chromosome):181 """ Mutate random value in a `chromsome`. 182 183 Parameters184 chromosome : chromsome strucutre185 The representation of candidate solution which is subject to a mutation. 186 """187 # This function selects random value from input data or previous node in the graph.188 connect = lambda connection: random.choice(self.input_data_range + range(connection[0], connection[1]))189 # Select some node190 random_node_idx = random.randint(0, len(chromosome) - 1)191 random_node = chromosome[random_node_idx]192 193 # And select an index from the node194 random_idx = random.randint(0, len(random_node) - 1) # Note: A <= rnd <= B195 previous_value = random_node[random_idx]196 if random_node is chromosome[-1]:197 # We mutate the last node198 while previous_value == random_node[random_idx]:199 random_node[random_idx] = connect((0, len(chromosome)))200 elif random_idx == self.maximal_arity:201 # We mutate the operation (indexing from 0)202 while previous_value == random_node[random_idx]:203 random_node[random_idx] = random.choice(self.operations)204 else:205 # We mutate the connection206 while previous_value == random_node[random_idx]:207 random_node[random_idx] = connect(self._valid_connection[ random_node_idx / self.rows ])208 def mutate(self):209 """ Creates a copy of the best solution and mutates it random-times.210 Return211 chromsome : chromosome structure212 The representation of the other candidate solution, which is similar to the best found solution.213 """214 # Copy the best chromsome215 chromosome = deepcopy(self.best_chrom)216 # And mutate it as much as possible217 for _ in xrange(random.randint(1, self.mutation_rate+1)): # Mutate it at least once. Else the chromsome wouldn't change.218 self.__mutate_gene(chromosome)219 return chromosome220 def init_eval(self):221 """ This function is started before the run of evolution. """222 # Buffer for the evaluation.223 buffer_data = deepcopy(self.input_data) + range(self.rows * self.columns + len(self.training_data))224 buffer_indexes = range(-len(self.input_data), len(buffer_data) - len(self.input_data))225 self.buffer = dict(zip(buffer_indexes, buffer_data))226 227 # Maximal fitness228 self.max_fitness = len(self.training_data) * (2 ** len(self.input_data))229 # Fitness mask.230 # V mask V The number of bites V # We are doing parallel evaluation. 231 self.bitmask = 2 ** (2 ** len(self.input_data)) - 1232 # Set generations.233 self.generation = 0234 def chrom_to_str(self, chromosome):235 """ Creates a string representation of chromosome, in which the nodes are aligned into a lattice. 236 Each node has syntax `index` : [`previous index`, ... , `previous index`, `Function name`] 237 If the node was primary output, then the node is ended with a string `<- `. 238 The primary outputs are also printed in the last line for the sake of their permutation. 239 Parameters240 chromsome : chromosome structure241 The representation of chromosome.242 Returns243 str244 The string representation of chromosome.245 """246 # Some math stuff247 max_input_strlen = len(str(self.columns * self.rows))248 max_length_of_operation_string = max(map(len, self.operations_names))249 chrom_str = ""250 for i in xrange(self.rows):251 line = ""252 for j in xrange(self.columns):253 # Grab the node index 254 node_idx = j * self.rows + i255 node = chromosome[node_idx]256 # Find the inputs, and align them.257 inputs = node[:-1] # The last node is operation; the rest are the inputs.258 aligned_inputs = map(lambda x: str(x).rjust(max_input_strlen), inputs)259 # Find out the operation name and align it.260 operation = node[-1]261 operation_index = self.operations.index(operation)262 operation_name = self.operations_names[operation_index]263 aligned_operation_name = operation_name.rjust(max_length_of_operation_string)264 # save it265 if node_idx in chromosome[-1]:266 flag = "<- "267 else:268 flag = " "269 line += str(node_idx).rjust(max_input_strlen) + ": [" + ", ".join(aligned_inputs) + ", " + aligned_operation_name + "]" + flag270 chrom_str += line + "\n"271 return chrom_str + str(chromosome[-1])272 273 def eval(self, chromosome):274 """ Evaluates the `chromosome` and returns the fitness value. Also checks if we found a better solution.275 Parameters276 chromsome : chromosome structure277 The representation of a candidate solution.278 Returns279 int : fitness value280 The quality of the candidate solution.281 TODO282 Optimise it. Skip neutral mutations. Skip unused nodes. (Maybe use C/C++ function?)283 Move the fitness check 284 """285 fitness = 0 286 # Evaluate each node.287 for idx, node in enumerate(chromosome[:-1]):288 # TODO SKIP unused nodes289 # Save the value of the operation `node[-1]`. The arguments are given in `node[:-1]` and they are the indexes into the `buffer`.290 self.buffer[idx] = node[-1](*[self.buffer[i] for i in node[:-1]])291 # Grab the value 292 for idx, target in zip(chromosome[-1], self.training_data):293 #print idx, target, self.bitmask294 fitness += bin((self.buffer[idx] ^ target) & self.bitmask).count("1")295 # Checks if we found a better solution.296 # Shouldnt be here.297 if fitness <= self.best_fitness:298 if CGP.PRINT_INFO and fitness < self.best_fitness:299 print "Generation: " + str(self.generation).rjust(10) + "\tFitness: " + str(fitness)300 self.best_fitness = fitness301 self.best_chrom = deepcopy(chromosome)302 return fitness303 def run(self, pop_size, mutations, generations):304 """ Runs the evolution.305 Parameters306 chromsome : chromosome structure307 The representation of chromosome.308 """309 # Init evaluation310 self.init_eval()311 # Init mutation312 self.mutation_rate = mutations313 # Creates first population `self.pop` and evaluates it `self.eval`.314 self.__init_pop(pop_size)315 # Run evolution316 for self.generation in xrange(generations):317 # Evaluate pop318 map(self.eval, self.pop)319 # Mutate pop; fix the peroformance 320 new_pop = [self.mutate() for _ in self.pop]321 self.pop = new_pop322 #break323 324 def __str__(self):325 return self.chrom_to_str(self.best_chrom)326 327class CGPError(Exception):328 def __init__(self, error_code, value=""):329 """ Inits the exception. """330 self.error_code = error_code331 self.value = value332 def __str__(self):333 """ Print error message, depending on the given error code. """334 error_msg = {335 "EMPTY_SET" : "The operation set is empty.",336 "INCONSISTENT_SET" : "The operation set contains inconsistent operation: " + repr(self.value) + "\n"\337 + "Please use triplet (`str` name, `int` arity, `function` operation)",338 "INCONSISTENT_ARITY" : "The arity of operation " + repr(self.value) +" can not be negative.", # Note: if you are doing some high math sh.t, then write your own CGP with blackjack and h..kers.339 }.get(self.error_code, "Unknown error - " + repr(self.error_code))340 return error_msg341if __name__ == '__main__':342 CGP.PRINT_INFO = True343 cgp = CGP(DEFAULT_MATRIX_SIZE, DEFAULT_OPERATION_SET, DEFAULT_INPUT_DATA, DEFAULT_TRAINING_DATA)344 cgp.run(DEFAULT_POP_SIZE, DEFAULT_MUTATIONS, DEFAULT_MAX_GENERATIONS)...`

base.py

Source:base.py

`...60 self.client = DatabaseClient(self)61 self.creation = DatabaseCreation(self)62 self.introspection = DatabaseIntrospection(self)63 self.validation = DatabaseValidation()64 def _valid_connection(self):65 if self.connection is not None:66 try:67 self.connection.ping()68 return True69 except DatabaseError:70 self.connection.close()71 self.connection = None72 return False73 def _cursor(self):74 if not self._valid_connection():75 kwargs = {76 #'conv': django_conversions,77 'charset': 'utf8',78 'use_unicode': True,79 }80 settings_dict = self.settings_dict81 if settings_dict['DATABASE_USER']:82 kwargs['user'] = settings_dict['DATABASE_USER']83 if settings_dict['DATABASE_NAME']:84 kwargs['db'] = settings_dict['DATABASE_NAME']85 if settings_dict['DATABASE_PASSWORD']:86 kwargs['passwd'] = settings_dict['DATABASE_PASSWORD']87 if settings_dict['DATABASE_HOST'].startswith('/'):88 kwargs['unix_socket'] = settings_dict['DATABASE_HOST']89 elif settings_dict['DATABASE_HOST']:90 kwargs['host'] = settings_dict['DATABASE_HOST']91 if settings_dict['DATABASE_PORT']:92 kwargs['port'] = int(settings_dict['DATABASE_PORT'])93 kwargs.update(settings_dict['DATABASE_OPTIONS'])94 self.connection = eventlet.db_pool.ConnectionPool.connect(MySQLdb, 15, **kwargs)95 connection_created.send(sender=self.__class__)96 cursor = mysqldb_base.CursorWrapper(self.connection.cursor())97 return cursor98 def _rollback(self):99 try:100 BaseDatabaseWrapper._rollback(self)101 except Database.NotSupportedError:102 pass103 def get_server_version(self):104 if not self.server_version:105 if not self._valid_connection():106 self.cursor()107 self.server_version = self.connection._obj.get_server_info()108 #m = server_version_re.match(self.connection.get_server_version())109 #if not m:110 # raise Exception('Unable to determine MySQL version from version string %r' % self.connection.get_server_version())111 #self.server_version = tuple([int(x) for x in m.groups()])...`

## Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.