python - 如何修复我的代码来解决这个难题?(Python)
问题描述
这是我正在解决的难题的图表: 其中 A、B、C、D 是 0 或 1 的四个数字,而 alpha 是一些随机常数(可以是正数或负数,但忽略它们时的情况'是 0)。目标是在给定随机 alpha 数组的情况下确定列表 [A,B,C,D]。这里有一些规则:
如果 alpha > 0,则相邻点具有不同的值。(例如:如果 alpha_A = 4.5,则 A,B = 01 或 10。
如果 alpha <0,则相邻点具有相同的值。(例如:如果 alpha_B = -4.5,则 B,C = 00 或 11。
使两个相邻点对于最高的 alpha_i 保持不变,然后从第二高的绝对值开始,迭代到第二低的绝对值。只有当 alpha_i 具有最小绝对值时,才能违反规则 1 和 2。
这是我拥有的当前代码:
alpha = np.random.normal(0, 10, N_object4)
pa = np.abs(alpha)
N_object4 = 4
num = pa.argsort()[-3:][::-1] # Index of descending order for abs
print(alpha)
print(num)
ABCD = np.zeros(N_object4).tolist()
if alpha[num[0]] > 0:
ABCD[num[0]+1] = 1 # The largest assignment stays constant.
for i in range (1,len(num)): # Iterating from the 2nd largest abs(alpha) to the 2nd smallest.
if i == 3:
num[i+1] = num[0]
elif alpha[num[i]] > 0:
if np.abs(num[i]-num[0]) == 1 or 2:
ABCD[num[i+1]] = 1-ABCD[num[i]]
elif np.abs(num[i]-num[0]) == 3:
ABCD[num[i]] = 1-ABCD[num[0]]
elif alpha[num[i]] < 0:
if np.abs(num[i]-num[0]) == 1 or 2:
**ABCD[num[i+1]] = ABCD[num[i]]**
elif np.abs(num[i]-num[0]) == 3:
ABCD[num[i]] = ABCD[num[0]]
我想我的代码中仍然缺少一些东西。我当前的版本有一个错误(用**标记):
IndexError: index 3 is out of bounds for axis 0 with size 3
我想知道我的错误在哪里?
此外,所需输出的示例:如果
alpha = [12.74921599, -8.01870123, 11.07638142, -3.51723019]
然后
num = [0, 2, 1]
ABCD的正确列表应该是:
ABCD = [0, 1, 1, 0] (or [1, 0, 0, 1])
我以前的版本可以给我一个输出,但列表 ABCD 看起来不正确。
非常感谢您阅读我的问题,非常感谢您的帮助:)
解决方案
如果发现一些气味,请查看您的代码:
nums
是 3 个元素长,似乎你想要num = pa.argsort()[-4:][::-1]
您在其他地方也有索引问题:
if i == 3:
num[i+1] = num[0]
元素4
不在数组中
我个人的建议:鉴于问题的规模很小,摆脱numpy
并只使用 python 列表。
另一个观察结果(我花了好几年才学会)是,对于小问题,蛮力是可以的。
这是我的蛮力解决方案,有更多时间,可以改进它以进行回溯或动态编程:
import random
# To make things simpler, I will consider A = 0, and so on
pairs = [
(0, 1),
(1, 2),
(2, 3),
(3, 0),
]
# Ensure no zeros on our list
alphas = [(random.choice([1, -1]) * random.uniform(1, 10), pairs[n]) for n in range(4)]
print(alphas)
# Sort descending in
alphas.sort(reverse=True, key=lambda n: abs(n[0]))
print(alphas[:-1])
def potential_solutions():
"""Generator of potential solutions"""
for n in range(16):
# This just abuses that in binary, numbers from 0 to 15 cover all posible configurations
# I use shift and masking to get each bit and return a list
yield [n >> 3 & 1 , n >> 2 & 1, n >> 1 & 1, n & 1]
def check(solution):
"""Takes a candidate solution and check if is ok"""
for alpha, (right, left) in alphas[:-1]:
if alpha < 0 and solution[right] != solution[left]:
return False
elif alpha > 0 and solution[right] == solution[left]:
return False
return True
# Look for correct solutions
for solution in potential_solutions():
if check(solution):
print(solution)
推荐阅读
- elasticsearch - 如何使用 S3 为 EFK 堆栈配置日志的长期保留?
- html - 如何排除css中的特定类?
- javascript - Internet Explorer 中的 Javascript 无效字符错误,但不是 Google Chrome
- c++ - gcc 和 clang 在表达式是否是常量评估上存在分歧
- ansible - Ansible:如何获取任务名称作为变量
- python - 在 django 中从数据库中打开特定的 pdf 文件
- postgresql - Postgresql:使用不同的参数多次重复查询 - 比联合更清洁的方法?
- python-3.x - OSError:[E050] 找不到模型“de”。它似乎不是快捷链接、Python 包或数据目录的有效路径
- python - 如何在熊猫中设置日期时间地板操作的基础?
- java - 与选择混淆(浮点数)