Advertisement
Guest User

Max-3set-cover Prototype Approximation

a guest
Jan 16th, 2021
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.18 KB | None | 0 0
  1. import json
  2. from copy import deepcopy
  3. import random
  4. # integers only and list of lists only
  5. s = []
  6. for a in range(1, 52):
  7.     s.append(a)
  8. # Contains only One Solution
  9. 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]]
  10. c_copy = deepcopy(c)
  11. random.seed()
  12. miss = []
  13. delete = []
  14. def input_validation():
  15.     # checking for missing elements.
  16.     for a in c:
  17.         for b in a:
  18.             miss.append(b)
  19.     for d in s:
  20.         if d not in miss:
  21.             print('False', d)
  22.     # Throw out unwanted orderings of sub-lists
  23.     for a in range(0, len(c)):
  24.         c[a] = sorted(c[a])
  25.     # Lists are treated as sets, so lists
  26.     # with repeating elements is thrown out
  27.     for r in range(0, len(c)):
  28.         if len(c[r]) != len(set(c[r])):
  29.             c[r] = [['xx']]
  30.     # Throw out lists that have elements that do
  31.     # not exist in s.
  32.     # Throw out lists with more than 3
  33.     # Throw out lists less than 3
  34.     for a in range(0, len(c)):
  35.       for b in c[a]:
  36.         if c[a].count(b) > 1:
  37.           delete.append(c[a])
  38.           break
  39.         if len(c[a]) != 3:
  40.           delete.append(c[a])
  41.       for bb in c[a]:
  42.         if bb not in s:
  43.           delete.append(c[a])
  44.     for b in delete:
  45.       if b in c:
  46.         del c[c.index(b)]
  47. s_copy = deepcopy(s)
  48. input_validation()
  49. # remove repeating lists
  50. c = [c[x] for x in range(len(c)) if not(c[x] in c[:x])]
  51. def bijective_mapp():
  52.     for a in range(0, len(s)):
  53.         s[a] = a+1
  54.     for b in range(0, len(c)):
  55.         for bb in range(0, len(c[b])):
  56.             c[b][bb] = s_copy.index(c[b][bb])+1
  57. bijective_mapp()
  58. def shuff(c, n):
  59.     for i in range(n-1,0,-1):
  60.         j = random.randint(0,i)
  61.         c[i],c[j] = c[j],c[i]
  62. c.append(sorted(c[len(c)-1]))
  63. n = len(s)
  64. steps = n*(2**1)*((n*(2**1))-1)*((n*(2**1))-2)//6
  65. reset = 0
  66. c_elem = set()
  67. set_cover = []
  68. partial = []
  69. for a in range(0, steps + 1):
  70.     reset = reset + 1
  71.     if reset > len(c):
  72.         c_elem = set()
  73.         reset = 0
  74.         x={sublist[0] for sublist in c}
  75.         order=list(range(len(x)))
  76.         shuff(order, len(order))
  77.         ord_map=dict(zip(x,order))
  78.         c = sorted(c, key=lambda sublist: ord_map[sublist[0]])
  79.         set_cover = []
  80.     c.append(c[0])
  81.     del(c[0])
  82.     for l in c:
  83.         if not any(v in c_elem for v in l):
  84.             set_cover.append(l)
  85.             c_elem.update(l)
  86.     # if exact solution not found,
  87.     # then use partial solution
  88.     if set_cover not in partial:
  89.         partial.append(set_cover)
  90. list_of_partials = [len(r) for r in partial]
  91. k = partial[list_of_partials.index(max(list_of_partials))]
  92. # Reversing the Bijective mapping
  93. # to the original input.
  94. for a in range(0, len(k)):
  95.     for b in range(0, len(k[a])):
  96.         l = s.index(k[a][b])
  97.         k[a][b] = s_copy[l]
  98. for a in range(0, len(k)):
  99.   k[a] = sorted(k[a])
  100. for a in c_copy:
  101.   if sorted(a) in k:
  102.     l = k.index(sorted(a))
  103.     k[l] = a
  104. print(k)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement