From 808ba51caeaf5037921ce06679121dda241c8c14 Mon Sep 17 00:00:00 2001 From: "Luke A. Patton" <lap368@drexel.edu> Date: Thu, 11 Mar 2021 17:21:39 +0000 Subject: [PATCH] Uploaded generateTransformers --- Transformers/generateTransformers.py | 158 +++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 Transformers/generateTransformers.py diff --git a/Transformers/generateTransformers.py b/Transformers/generateTransformers.py new file mode 100644 index 0000000..997391e --- /dev/null +++ b/Transformers/generateTransformers.py @@ -0,0 +1,158 @@ +from getFullList import getFullList +from Transformer import OneToOneNumberTransformer +def read_matrix(n): + answer = [] + with open(str(n)+ "_matrix.csv") as ofile: + for line in ofile: + answer.append([(int(num) if num != '' else None) for num in line.split(",") ]) + return answer + +def identify_generators(matrix): + n = len(matrix) - 1 + positions = len(matrix[0]) -1 + + occurrences_to_positions = {} + + for num in range(2, int(((n+1)/2))): + for pos in range(1, positions-1): + occurrences = matrix[num+1][pos+1] + if occurrences in occurrences_to_positions: + occurrences_to_positions[occurrences].append((num, pos)) + else: + occurrences_to_positions[occurrences] = [(num,pos)] + + + + for occurrences in occurrences_to_positions: + if type(occurrences_to_positions[occurrences]) == list: + pairs = allPairsInList(occurrences_to_positions[occurrences]) + for pair in pairs: + if len(make_generator(matrix, getFullList(n), pair[0][0], pair[0][1], pair[1][0], pair[1][1])) > 0: + print(pair, "works") + + +def make_generator(matrix, ns1d0s, r1, c1, r2, c2): + source_num = matrix[r1 + 1][0] + source_pos = matrix[0][c1+ 1] + + dest_num = matrix[r2 + 1][0] + dest_pos = matrix[0][c2+1] + + + source_matching_sequences = [a for a in ns1d0s if a[source_pos] == source_num] + dest_matching_sequences = [a for a in ns1d0s if a[dest_pos] == dest_num] + + + source_all_nums = [] + for source_sequence in source_matching_sequences: + source_all_nums.extend(source_sequence) + + dest_all_nums = [] + for dest_sequence in dest_matching_sequences: + dest_all_nums.extend(dest_sequence) + + source_frequencies = [] + dest_frequencies = [] + for num in range(len(matrix)): + source_frequencies.append(source_all_nums.count(num)) + dest_frequencies.append(dest_all_nums.count(num)) + + if set(source_frequencies) != set(dest_frequencies): + return [] + + + + mapping_dict = {} + + for i in range(len(matrix)): + if i == 0: + mapping_dict[0] = 0 + continue + elif i == 1: + mapping_dict[1] = 1 + continue + elif i == r1: + mapping_dict[r1] = r2 + continue + + matches = [] + for j in range(len(matrix)): + if source_frequencies[i] == dest_frequencies[j]: + matches.append(j) + + if len(matches) == 1: + mapping_dict[i] = matches[0] + elif len(matches) == 0: + return [] + else: + if source_frequencies[i] == 0: + mapping_dict[i] = matches[0] + else: + mapping_dict[i] = matches + + all_possible_mappings = make_possible_mappings(mapping_dict) + all_possible_generators = [OneToOneNumberTransformer(mapping) for mapping in all_possible_mappings] + + good_fits = [] + for generator in all_possible_generators: + if generator.check(source_matching_sequences, dest_matching_sequences): + good_fits.append(generator) + + return good_fits + + + +def make_possible_mappings(mapping_dict): + possible_dicts = [{}] + for key in mapping_dict: + if type(mapping_dict[key]) == int: + for possible_dict in possible_dicts: + possible_dict[key] = mapping_dict[key] + else: + new_possibilities = [] + for value in mapping_dict[key]: + for possible in possible_dicts: + copied_possible = possible.copy() + copied_possible[key] = value + new_possibilities.append(copied_possible) + possible_dicts = new_possibilities + + return possible_dicts + + +# This function generates all the pairs of objects in a given list of numbers, +# specifically it generates combinations rather than permutations (i.e. if a, b +# is in the list of pairs, then b, a will not be). + +# input: a list of objects (list of objects of type i) +# output: all pairs of those objects (list of lists of objects of type i) +def allPairsInList(objectList): + combinations = allPermutationsOfLength(objectList,2) + for [a,b] in combinations: + if [b,a] in combinations: + combinations.remove([b,a]) + + return combinations + +# This function generates all permutations of a certain length from a list of +# objects. Since they are permutations, sequences containing the same objects +# in a different order will occur (e.g. if 1,2,3 is a permutation, 3,2,1 will +# be as well). + +# input: a list of objects (list of objects of type i), a length (int) +# output: all permutations of the given length of those objects +# (list of lists of objects of type i) +def allPermutationsOfLength(objectList, length): + if length == 0: + return [[]] + else: + combos = [] + for number in objectList: + subList = objectList.copy() + subList.remove(number) + subCombos = allPermutationsOfLength(subList, length-1) + for combo in subCombos: + combo.append(number) + combos.append(combo) + return combos + -- GitLab