SHARE
TWEET

Untitled

a guest Jan 2nd, 2020 131 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pandas as pd
  2. from random import randint, sample
  3.  
  4. # Generate data
  5. list_list = []
  6. for i in range(20):
  7.     for j in range(20):
  8.         if j == i:
  9.             continue
  10.         temp_list = [20] * 20
  11.         temp_list[i] = 19
  12.         temp_list[j] = 21
  13.         list_list = [temp_list] + list_list
  14.        
  15. df_dict = {}
  16. for i in range(20):
  17.     df_dict['col_' + str(i)] = [temp_list[i] for temp_list in list_list]
  18.    
  19. df = pd.DataFrame(df_dict)
  20.  
  21. # Loop through data finding solutions
  22. rand_rounds = 50
  23. max_on_one_side = 5
  24. min_on_one_side = 2
  25. index_sample = ['col_' + str(i) for i in list(range(20))]
  26. junk = ['left_sum', 'right_sum', 'result']
  27. one_best = df.shape[0]
  28.  
  29. def split_list(a_list):
  30.     """returns first half and second half of a list"""
  31.     half = len(a_list)//2
  32.     return a_list[:half], a_list[half:]
  33.  
  34. def get_score(solutions_df, index_left, index_right):
  35.     """Lower scores are better. Score returned is the maximum solutions remaining
  36.     of the three outcomes of a particular weighing."""
  37.     solutions_df['left_sum'] = solutions_df[index_left].sum(axis=1)
  38.     solutions_df['right_sum'] = solutions_df[index_right].sum(axis=1)
  39.     solutions_df['result'] = '=='
  40.     solutions_df.loc[solutions_df['left_sum'] > solutions_df['right_sum'], 'result'] = '>'
  41.     solutions_df.loc[solutions_df['left_sum'] < solutions_df['right_sum'], 'result'] = '<'
  42.     return solutions_df.result.value_counts().max()
  43.  
  44. # Notes
  45. # After round 1 we should have 125-130
  46. # After round 2 we should have < 44
  47. # After round 3 we should have < 15
  48. # After round 4 we should have < 7
  49. # After round 5 we should have < 3
  50. # After round 6 we should be done
  51.  
  52. result = {
  53.         'step_one_left':[],
  54.         'step_one_right':[],
  55.         'step_one_path':[],
  56.         'step_one_count':[],
  57.         'step_two_left':[],
  58.         'step_two_right':[],
  59.         'step_two_path':[],
  60.         'step_two_count':[],
  61.         'step_three_left':[],
  62.         'step_three_right':[],
  63.         'step_three_path':[],
  64.         'step_three_count':[],
  65.         'step_four_left':[],
  66.         'step_four_right':[],
  67.         'step_four_path':[],
  68.         'step_four_count':[],
  69.         'step_five_left':[],
  70.         'step_five_right':[],
  71.         'step_five_path':[],
  72.         'step_five_count':[],
  73.         'step_six_left':[],
  74.         'step_six_right':[],
  75.         'step_six_path':[],
  76.         'step_six_count':[],
  77.         }
  78. results_df = pd.DataFrame(result)
  79.  
  80. for rand in range(rand_rounds):
  81.     n = randint(min_on_one_side, max_on_one_side)
  82.     index_left, index_right = split_list(sample(index_sample, n*2))
  83.     new_score = get_score(df, index_left, index_right)
  84.     if new_score < one_best:
  85.         one_left, one_right = index_left, index_right
  86.         print("Round 1: "+str(one_best)+" to "+str(new_score)+" after "+str(rand)+" rounds.")
  87.         one_best = new_score
  88.         one_df_final = df.copy()
  89. for i in ['==','<','>']:
  90.     print("Beginning round two optimization for case: Left " + i + " Right.")
  91.     two_df = one_df_final.copy()
  92.     two_df = two_df.loc[two_df.result==i].reset_index(drop=True)
  93.     two_best = two_df.shape[0]
  94.     for rand in range(rand_rounds):
  95.         if i != '==':
  96.             break
  97.         n = randint(min_on_one_side, max_on_one_side)
  98.         index_left, index_right = split_list(sample(index_sample, n*2))
  99.         new_score = get_score(two_df, index_left, index_right)
  100.         if new_score < two_best:
  101.             two_left, two_right = index_left, index_right
  102.             print("Round 2: "+str(two_best)+" to "+str(new_score)+" after "+str(rand)+" rounds.")
  103.             two_best = new_score
  104.             two_df_final = two_df.copy()
  105.     for j in ['==','<','>']:
  106.         print("Beginning round three optimization for case: Left " + j + " Right.")
  107.         three_df = two_df_final.copy()
  108.         three_df = three_df.loc[three_df.result==j].reset_index(drop=True)
  109.         three_best = three_df.shape[0]
  110.         for rand in range(rand_rounds):
  111.             if j != '==':
  112.                     break
  113.             n = randint(min_on_one_side, max_on_one_side)
  114.             index_left, index_right = split_list(sample(index_sample, n*2))
  115.             new_score = get_score(three_df, index_left, index_right)
  116.             if new_score < three_best:
  117.                 three_left, three_right = index_left, index_right
  118.                 print("Round 3: "+str(three_best)+" to "+str(new_score)+" after "+str(rand)+" rounds.")
  119.                 three_best = new_score
  120.                 three_df_final = three_df.copy()
  121.         for k in ['==','<','>']:
  122.             print("Beginning round four optimization for case: Left " + k + " Right.")
  123.             four_df = three_df_final.copy()
  124.             four_df = four_df.loc[four_df.result==k].reset_index(drop=True)
  125.             four_best = four_df.shape[0]
  126.             for rand in range(rand_rounds):
  127.                 if k != '==':
  128.                     break
  129.                 n = randint(min_on_one_side, max_on_one_side)
  130.                 index_left, index_right = split_list(sample(index_sample, n*2))
  131.                 new_score = get_score(four_df, index_left, index_right)
  132.                 if new_score < four_best:
  133.                     four_left, four_right = index_left, index_right
  134.                     print("Round 4: "+str(four_best)+" to "+str(new_score)+" after "+str(rand)+" rounds.")
  135.                     four_best = new_score
  136.                     four_df_final = four_df.copy()
  137.             for l in ['==','<','>']:
  138.                 print("Beginning round five optimization for case: Left " + l + " Right.")
  139.                 five_df = four_df_final.copy()
  140.                 five_df = five_df.loc[five_df.result==l].reset_index(drop=True)
  141.                 five_best = five_df.shape[0]
  142.                 for rand in range(rand_rounds):
  143.                     if l != '==':
  144.                         break
  145.                     n = randint(min_on_one_side, max_on_one_side)
  146.                     index_left, index_right = split_list(sample(index_sample, n*2))
  147.                     new_score = get_score(five_df, index_left, index_right)
  148.                     if new_score < five_best:
  149.                         five_left, five_right = index_left, index_right
  150.                         print("Round 5: "+str(five_best)+" to "+str(new_score)+" after "+str(rand)+" rounds.")
  151.                         five_best = new_score
  152.                         five_df_final = five_df.copy()
  153.                 for m in ['==','<','>']:
  154.                     print("Beginning round six optimization for case: Left " + m + " Right.")
  155.                     six_df = five_df_final.copy()
  156.                     six_df = six_df.loc[six_df.result==m].reset_index(drop=True)
  157.                     six_best = six_df.shape[0]
  158.                     for rand in range(rand_rounds):
  159.                         if m != '==':
  160.                             break
  161.                         n = randint(min_on_one_side, max_on_one_side)
  162.                         index_left, index_right = split_list(sample(index_sample, n*2))
  163.                         new_score = get_score(six_df, index_left, index_right)
  164.                         if new_score < six_best:
  165.                             six_left, six_right = index_left, index_right
  166.                             print("Round 6: "+str(six_best)+" to "+str(new_score)+" after "+str(rand)+" rounds.")
  167.                             six_best = new_score
  168.                             six_df_final = six_df.copy()
  169.                     for n in ['==','<','>']:
  170.                         final_df = six_df_final.copy()
  171.                         final_df = final_df.loc[final_df.result==n].reset_index(drop=True)
  172.                         result = {
  173.                                 'step_one_left':one_left,
  174.                                 'step_one_right':one_right,
  175.                                 'step_one_path':"Left " + i + " Right",
  176.                                 'step_one_count':two_df.shape[0],
  177.                                 'step_two_left':two_left,
  178.                                 'step_two_right':two_right,
  179.                                 'step_two_path':"Left " + j + " Right",
  180.                                 'step_two_count':three_df.shape[0],
  181.                                 'step_three_left':three_left,
  182.                                 'step_three_right':three_right,
  183.                                 'step_three_path':"Left " + k + " Right",
  184.                                 'step_three_count':four_df.shape[0],
  185.                                 'step_four_left':four_left,
  186.                                 'step_four_right':four_right,
  187.                                 'step_four_path':"Left " + l + " Right",
  188.                                 'step_four_count':five_df.shape[0],
  189.                                 'step_five_left':five_left,
  190.                                 'step_five_right':five_right,
  191.                                 'step_five_path':"Left " + m + " Right",
  192.                                 'step_five_count':six_df.shape[0],
  193.                                 'step_six_left':six_left,
  194.                                 'step_six_right':six_right,
  195.                                 'step_six_path':"Left " + n + " Right",
  196.                                 'step_six_count':final_df.shape[0],
  197.                                 }
  198.                         results_df = results_df.append(result, ignore_index=True)
  199.  
  200. results_df.to_csv('brute_force_results.csv', index=False)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top