ratwolf
Bronze Coder
Below is a Python program that implements a simple yet unsolved mathematical problem.
A validation result is displayed in the plot title.
Your Task:
A validation result is displayed in the plot title.
Your Task:
- Run the code and observe the output or analytically follow the code's logic.
- Identify the mathematical problem being implemented.
- Explain why this problem is unsolved and what makes it interesting.
Python:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
def metric(a, b):
return np.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)
def scatter_data(count, scale=10):
return np.random.rand(count, 2) * scale
def link_nodes(data, threshold):
links = []
for i in range(len(data)):
links.append([])
for j in range(len(data)):
if i != j and metric(data[i], data[j]) <= threshold:
links[i].append(j)
return links
def categorize(data, labels, threshold=1.0):
total = len(data)
assignments = [-1] * total
links = link_nodes(data, threshold)
order = sorted(range(total), key=lambda i: len(links[i]), reverse=True)
for i in order:
taken_labels = {assignments[j] for j in links[i] if assignments[j] != -1}
available_labels = [label for label in range(labels) if label not in taken_labels]
if available_labels:
assignments[i] = available_labels[0]
else:
assignments[i] = labels - 1
return assignments
def verify(data, assignments, threshold=1.0):
for i in range(len(data)):
for j in range(i + 1, len(data)):
if metric(data[i], data[j]) <= threshold and assignments[i] == assignments[j]:
return False
return True
def visualize(data, assignments, labels, status):
cmap = ListedColormap(plt.cm.tab10.colors[:labels])
scatter = plt.scatter(data[:, 0], data[:, 1], c=assignments, cmap=cmap, s=100, vmin=0, vmax=labels-1)
cbar = plt.colorbar(scatter, ticks=range(labels))
cbar.set_label("Group")
plt.title(f"Spatial Conflict Resolution\nVerification: {'Pass' if status else 'Fail'}")
plt.xlabel("Axis-1")
plt.ylabel("Axis-2")
plt.show()
def execute():
count = 100
labels = 7
np.random.seed(42)
data = scatter_data(count)
assignments = categorize(data, labels)
status = verify(data, assignments)
visualize(data, assignments, labels, status)
if __name__ == "__main__":
execute()