# "algorithm": "This algorithm constructs a 3-AP-free set by greedily adding numbers based on a score that prioritizes elements that are less likely to form an arithmetic progression with already chosen elements, specifically by favoring numbers with fewer potential arithmetic progression partners given the current set S.}\n\n```python\nimport numpy as np\n\ndef heuristic(n, rng=None):\n if rng is None:\n rng = np.random.default_rng()\n elif isinstance(rng, int):\n rng = np.random.default_rng(rng)\n\n S = set()\n S_list = []\n \n # Pre-calculate potential AP partners for efficiency\n potential_partners = {i: set() for i in range(1, n + 1)}\n for i in range(1, n + 1):\n for j in range(i + 1, n + 1):\n # Check for z such that (i, j, z) or (j, i, z)\n z1 = 2 * j - i\n if 1 <= z1 <= n:\n potential_partners[i].add(z1)\n potential_partners[z1].add(i)\n z2 = 2 * i - j\n if 1 <= z2 <= n:\n potential_partners[j].add(z2)\n potential_partners[z2].add(j)\n \n # Check for z such that (i, z, j)\n if (i + j) % 2 == 0:\n z3 = (i + j) // 2\n if 1 <= z3 <= n:\n potential_partners[i].add(z3)\n potential_partners[z3].add(i)\n potential_partners[j].add(z3)\n potential_partners[z3].add(j)\n \n # Initialize scores: number of potential AP partners that are *not* yet in S\n scores = {i: len(potential_partners[i]) for i in range(1, n + 1)",
# "code": "import numpy as np\n\ndef heuristic(n, rng=None):\n if rng is None:\n rng = np.random.default_rng()\n elif isinstance(rng, int):\n rng = np.random.default_rng(rng)\n\n S = set()\n S_list = []\n \n # Pre-calculate potential AP partners for efficiency\n potential_partners = {i: set() for i in range(1, n + 1)}\n for i in range(1, n + 1):\n for j in range(i + 1, n + 1):\n # Check for z such that (i, j, z) or (j, i, z)\n z1 = 2 * j - i\n if 1 <= z1 <= n:\n potential_partners[i].add(z1)\n potential_partners[z1].add(i)\n z2 = 2 * i - j\n if 1 <= z2 <= n:\n potential_partners[j].add(z2)\n potential_partners[z2].add(j)\n \n # Check for z such that (i, z, j)\n if (i + j) % 2 == 0:\n z3 = (i + j) // 2\n if 1 <= z3 <= n:\n potential_partners[i].add(z3)\n potential_partners[z3].add(i)\n potential_partners[j].add(z3)\n potential_partners[z3].add(j)\n \n # Initialize scores: number of potential AP partners that are *not* yet in S\n scores = {i: len(potential_partners[i]) for i in range(1, n + 1)}\n \n candidates = list(range(1, n + 1))\n rng.shuffle(candidates) # Initial shuffle to break ties randomly\n\n # Sort candidates based on scores (lower score is better)\n candidates.sort(key=lambda x: scores[x])\n\n for x in candidates:\n is_ap_free = True\n for y in S_list:\n # Check for (y, x, z) or (z, x, y) where x is the middle term\n z1 = 2 * x - y\n if z1 in S:\n is_ap_free = False\n break\n\n # Check for (x, y, z) or (z, y, x) where y is the middle term\n z2 = 2 * y - x\n if z2 in S:\n is_ap_free = False\n break\n\n # Check for (x, z, y) or (y, z, x) where z is the middle term\n if (x + y) % 2 == 0:\n z3 = (x + y) // 2\n if z3 in S:\n is_ap_free = False\n break\n \n if is_ap_free:\n S.add(x)\n S_list.append(x)\n \n # Update scores of other numbers:\n # When x is added to S, any number 'k' that could form an AP with x\n # now has one less potential partner that is NOT in S.\n # This means scores[k] should decrease if k is one of x's potential partners.\n # For elements in S, their potential partners *in S* are now irrelevant for AP checks.\n # We can re-evaluate scores dynamically. A simpler approach is to remove x\n # from the potential_partners sets of other numbers.\n for partner in list(potential_partners[x]): # Iterate over a copy\n if partner in scores: # If partner is still a candidate\n scores[partner] -= 1\n # Remove x from partner's consideration to avoid double counting\n if x in potential_partners[partner]:\n potential_partners[partner].remove(x)\n\n # Remove x from the candidate pool and update scores of its partners that are not yet in S\n # This dynamic re-sorting can be costly. A fixed order based on initial scores is simpler.\n # For this heuristic, we rely on the initial sorted order of candidates.\n \n return S",
# "objective": -0.13086,
# "other_inf": None
# }
# for k in mydict.keys():
# print(mydict[k])
# print("-" * 50)
import numpy as np
def heuristic(n, rng=None):
if rng isNone:
rng = np.random.default_rng()
elifisinstance(rng,int):
rng = np.random.default_rng(rng)
S =set()
S_list =[]
# Pre-calculate potential AP partners for efficiency
potential_partners ={i: set()for i inrange(1, n + 1)}
for i inrange(1, n + 1):
for j inrange(i + 1, n + 1):
# Check for z such that (i, j, z) or (j, i, z)
z1 =2 * j - i
if1<= z1 <= n:
potential_partners[i].add(z1)
potential_partners[z1].add(i)
z2 =2 * i - j
if1<= z2 <= n:
potential_partners[j].add(z2)
potential_partners[z2].add(j)
# Check for z such that (i, z, j)
if(i + j) % 2==0:
z3 =(i + j) // 2
if1<= z3 <= n:
potential_partners[i].add(z3)
potential_partners[z3].add(i)
potential_partners[j].add(z3)
potential_partners[z3].add(j)
# Initialize scores: number of potential AP partners that are *not* yet in S
scores ={i: len(potential_partners[i])for i inrange(1, n + 1)}
candidates =list(range(1, n + 1))
rng.shuffle(candidates)# Initial shuffle to break ties randomly
# Sort candidates based on scores (lower score is better)
candidates.sort(key=lambda x: scores[x])
for x in candidates:
is_ap_free =True
for y in S_list:
# Check for (y, x, z) or (z, x, y) where x is the middle term
z1 =2 * x - y
if z1 in S:
is_ap_free =False
break
# Check for (x, y, z) or (z, y, x) where y is the middle term
z2 =2 * y - x
if z2 in S:
is_ap_free =False
break
# Check for (x, z, y) or (y, z, x) where z is the middle term
if(x + y) % 2==0:
z3 =(x + y) // 2
if z3 in S:
is_ap_free =False
break
if is_ap_free:
S.add(x)
S_list.append(x)
# Update scores of other numbers:
# When x is added to S, any number 'k' that could form an AP with x
# now has one less potential partner that is NOT in S.
# This means scores[k] should decrease if k is one of x's potential partners.
# For elements in S, their potential partners *in S* are now irrelevant for AP checks.
# We can re-evaluate scores dynamically. A simpler approach is to remove x
# from the potential_partners sets of other numbers.
for partner inlist(potential_partners[x]): # Iterate over a copy
if partner in scores: # If partner is still a candidate
scores[partner] -=1
# Remove x from partner's consideration to avoid double counting
if x in potential_partners[partner]:
potential_partners[partner].remove(x)
# Remove x from the candidate pool and update scores of its partners that are not yet in S
# This dynamic re-sorting can be costly. A fixed order based on initial scores is simpler.
# For this heuristic, we rely on the initial sorted order of candidates.