Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from copy import deepcopy
- import random
- # integers only and list of lists only
- s = []
- for a in range(1, 52):
- s.append(a)
- # Contains only One Solution
- c = [[4, 5, 39], [10, 11, 17], [4, 5, 44], [1, 2, 27], [1, 2, 41], [10, 11, 31], [4, 5, 51], [10, 11, 40], [1, 2, 37], [10, 11, 29], [7, 8, 24], [4, 5, 7], [4, 5, 35], [7, 8, 51], [1, 2, 44], [7, 8, 38], [7, 8, 40], [1, 2, 17], [7, 8, 9], [7, 8, 17], [4, 5, 11], [7, 8, 31], [1, 2, 3], [4, 5, 36], [10, 11, 23], [4, 5, 31], [10, 11, 51], [10, 11, 36], [1, 2, 48], [7, 8, 42], [16, 17, 18], [10, 11, 21], [1, 2, 7], [7, 8, 48], [10, 11, 42], [7, 8, 21], [7, 8, 10], [4, 5, 24], [34, 35, 36], [1, 2, 25], [1, 2, 46], [7, 8, 26], [1, 2, 50], [37, 38, 39], [1, 2, 21], [46, 47, 48], [4, 5, 13], [1, 2, 39], [1, 2, 19], [7, 8, 35], [4, 5, 8], [7, 8, 47], [1, 2, 22], [25, 26, 27], [7, 8, 22], [4, 5, 20], [4, 5, 9], [4, 5, 22], [10, 11, 46], [4, 5, 23], [7, 8, 41], [10, 11, 38], [1, 2, 31], [7, 8, 12], [1, 2, 49], [10, 11, 48], [10, 11, 45], [10, 11, 18], [4, 5, 16], [4, 5, 18], [28, 29, 30], [4, 5, 33], [1, 2, 20], [1, 2, 24], [7, 8, 32], [4, 5, 27], [7, 8, 36], [4, 5, 28], [4, 5, 29], [7, 8, 18], [1, 2, 42], [1, 2, 32], [1, 2, 9], [1, 2, 23], [1, 2, 14], [1, 2, 36], [4, 5, 32], [4, 5, 45], [7, 8, 20], [7, 8, 34], [4, 5, 37], [1, 2, 38], [31, 32, 33], [7, 8, 43], [4, 5, 21], [10, 11, 44], [1, 2, 6], [7, 8, 25], [4, 5, 50], [10, 11, 22], [7, 8, 50], [10, 11, 27], [7, 8, 28], [10, 11, 26], [1, 2, 47], [4, 5, 38], [4, 5, 30], [1, 2, 12], [10, 11, 50], [10, 11, 13], [10, 11, 19], [1, 2, 33], [4, 5, 19], [4, 5, 46], [7, 8, 46], [1, 2, 30], [7, 8, 27], [10, 11, 34], [10, 11, 39], [1, 2, 26], [4, 5, 17], [4, 5, 41], [4, 5, 10], [10, 11, 49], [10, 11, 32], [1, 2, 43], [19, 20, 21], [10, 11, 15], [7, 8, 15], [1, 2, 35], [1, 2, 8], [7, 8, 37], [7, 8, 44], [7, 8, 13], [22, 23, 24], [10, 11, 47], [10, 11, 24], [7, 8, 30], [10, 11, 12], [10, 11, 35], [7, 8, 49], [4, 5, 48], [1, 2, 4], [1, 2, 28], [10, 11, 28], [1, 2, 13], [1, 2, 16], [10, 11, 20], [7, 8, 14], [1, 2, 10], [40, 41, 42], [1, 2, 51], [1, 2, 34], [10, 11, 16], [7, 8, 11], [10, 11, 33], [7, 8, 23], [7, 8, 45], [43, 44, 45], [1, 2, 45], [1, 2, 18], [4, 5, 25], [4, 5, 47], [13, 14, 15], [4, 5, 14], [4, 5, 40], [4, 5, 34], [7, 8, 33], [1, 2, 15], [10, 11, 43], [4, 5, 49], [1, 2, 5], [10, 11, 37], [7, 8, 19], [49, 50, 51], [1, 2, 11], [4, 5, 43], [7, 8, 16], [4, 5, 6], [1, 2, 29], [4, 5, 26], [7, 8, 39], [4, 5, 12], [7, 8, 29], [1, 2, 40], [4, 5, 15], [4, 5, 42], [10, 11, 25], [10, 11, 14], [10, 11, 30], [10, 11, 41]]
- c_copy = deepcopy(c)
- random.seed()
- miss = []
- delete = []
- def input_validation():
- # checking for missing elements.
- for a in c:
- for b in a:
- miss.append(b)
- for d in s:
- if d not in miss:
- print('False', d)
- # Throw out unwanted orderings of sub-lists
- for a in range(0, len(c)):
- c[a] = sorted(c[a])
- # Lists are treated as sets, so lists
- # with repeating elements is thrown out
- for r in range(0, len(c)):
- if len(c[r]) != len(set(c[r])):
- c[r] = [['xx']]
- # Throw out lists that have elements that do
- # not exist in s.
- # Throw out lists with more than 3
- # Throw out lists less than 3
- for a in range(0, len(c)):
- for b in c[a]:
- if c[a].count(b) > 1:
- delete.append(c[a])
- break
- if len(c[a]) != 3:
- delete.append(c[a])
- for bb in c[a]:
- if bb not in s:
- delete.append(c[a])
- for b in delete:
- if b in c:
- del c[c.index(b)]
- s_copy = deepcopy(s)
- input_validation()
- # remove repeating lists
- c = [c[x] for x in range(len(c)) if not(c[x] in c[:x])]
- def bijective_mapp():
- for a in range(0, len(s)):
- s[a] = a+1
- for b in range(0, len(c)):
- for bb in range(0, len(c[b])):
- c[b][bb] = s_copy.index(c[b][bb])+1
- bijective_mapp()
- def shuff(c, n):
- for i in range(n-1,0,-1):
- j = random.randint(0,i)
- c[i],c[j] = c[j],c[i]
- c.append(sorted(c[len(c)-1]))
- n = len(s)
- steps = n*(2**1)*((n*(2**1))-1)*((n*(2**1))-2)//6
- reset = 0
- c_elem = set()
- set_cover = []
- partial = []
- for a in range(0, steps + 1):
- reset = reset + 1
- if reset > len(c):
- c_elem = set()
- reset = 0
- x={sublist[0] for sublist in c}
- order=list(range(len(x)))
- shuff(order, len(order))
- ord_map=dict(zip(x,order))
- c = sorted(c, key=lambda sublist: ord_map[sublist[0]])
- set_cover = []
- c.append(c[0])
- del(c[0])
- for l in c:
- if not any(v in c_elem for v in l):
- set_cover.append(l)
- c_elem.update(l)
- # if exact solution not found,
- # then use partial solution
- if set_cover not in partial:
- partial.append(set_cover)
- list_of_partials = [len(r) for r in partial]
- k = partial[list_of_partials.index(max(list_of_partials))]
- # Reversing the Bijective mapping
- # to the original input.
- for a in range(0, len(k)):
- for b in range(0, len(k[a])):
- l = s.index(k[a][b])
- k[a][b] = s_copy[l]
- for a in range(0, len(k)):
- k[a] = sorted(k[a])
- for a in c_copy:
- if sorted(a) in k:
- l = k.index(sorted(a))
- k[l] = a
- print(k)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement