from itertools import combinations
import threading
from collections import defaultdict
import math
from multiprocessing import Pool, freeze_support # 增加freeze_support导入
# 配置参数(针对目标值268668优化)
TARGET = 248790 # 目标值
BASE_VALUES = [38.5,44,61,70.5,75.5,93]# 基础系数列表
FLUCTUATION = 1.0 # 系数波动范围
MAX_SOLUTIONS = 5 # 每个组合的最大解数量
SOLVER_TIMEOUT = 180 # 适当减少超时时间
THREE_VAR_THRESHOLD = 259000 # 使用三个变量的阈值
PRODUCT_RANGE_THRESHOLD = 129000 # 乘积范围限制阈值
HIGH_TARGET_THRESHOLD = 259000 # 更高目标值阈值
SHOW_PROGRESS = True # 是否显示进度
MAX_SOLUTIONS_PER_COMB = 100 # 适当减少最大解数量
USE_MULTIPROCESSING = False # 暂时禁用多进程以排查错误
def is_valid_product(p):
"""检查单个乘积是否在有效范围内"""
try:
if TARGET > HIGH_TARGET_THRESHOLD: # TARGET > 259000
return p <= 129000 # 单个乘积上限
elif TARGET > PRODUCT_RANGE_THRESHOLD: # 129000 < TARGET <= 259000
return 74000 <= p <= 129000 # 单个乘积范围
else: # TARGET <= 129000
return True # 小目标值取消所有限制
except:
return False
def find_single_variable_solutions(values):
"""查找单个数的解(a*x = TARGET)"""
solutions = []
for a in values:
try: # 增加异常处理
if a == 0:
continue
quotient = TARGET / a
if not math.isclose(quotient, round(quotient), abs_tol=1e-9):
continue
x = round(quotient)
if 1 <= x <= 10000 and is_valid_product(a * x):
solutions.append((a, x))
if len(solutions) >= MAX_SOLUTIONS:
break
except Exception as e:
print(f"处理单变量 {a} 时出错: {e}")
return solutions
# 其他函数保持不变,但为每个主要函数添加基本的异常处理
def main():
try: # 主函数增加异常捕获
print(f"目标值: {TARGET}")
print(f"单个乘积上限: 129000")
print(f"理论上至少需要 {math.ceil(TARGET / 129000)} 个乘积项")
# 生成波动后的系数
FLUCTUATED_VALUES = [round(v - FLUCTUATION, 1) for v in BASE_VALUES] + \
[round(v + FLUCTUATION, 1) for v in BASE_VALUES]
FLUCTUATED_VALUES = list(set(FLUCTUATED_VALUES)) # 去重
# 尝试基础系数
print(f"\n==== 尝试基础系数 ====")
base_solutions = {
'single': run_with_timeout(find_single_variable_solutions, args=(BASE_VALUES,)),
'two': run_with_timeout(find_two_variable_solutions, args=(BASE_VALUES,)),
'three': []
}
has_solution = False
# 显示单变量解
if base_solutions['single']:
has_solution = True
display_solutions({a: [sol] for a, sol in zip(BASE_VALUES, base_solutions['single']) if sol}, 1)
# 显示双变量解
if base_solutions['two'] and len(base_solutions['two']) > 0:
has_solution = True
display_solutions(base_solutions['two'], 2)
# 尝试三变量解
print(f"\n==== 尝试三变量解 ====")
base_solutions['three'] = run_with_timeout(find_three_variable_solutions, args=(BASE_VALUES,))
if base_solutions['three'] and len(base_solutions['three']) > 0:
has_solution = True
display_solutions(base_solutions['three'], 3)
if has_solution:
print(f"\n使用基础系数列表,共找到有效解")
return
# 如果基础系数没有找到解,尝试波动系数
print(f"\n==== 尝试波动系数 ====")
fluctuated_solutions = {
'single': run_with_timeout(find_single_variable_solutions, args=(FLUCTUATED_VALUES,)),
'two': run_with_timeout(find_two_variable_solutions, args=(FLUCTUATED_VALUES,)),
'three': []
}
has_solution = False
# 显示单变量解
if fluctuated_solutions['single']:
has_solution = True
display_solutions({a: [sol] for a, sol in zip(FLUCTUATED_VALUES, fluctuated_solutions['single']) if sol}, 1)
# 显示双变量解
if fluctuated_solutions['two'] and len(fluctuated_solutions['two']) > 0:
has_solution = True
display_solutions(fluctuated_solutions['two'], 2)
# 尝试三变量解
print(f"\n==== 尝试三变量解 ====")
fluctuated_solutions['three'] = run_with_timeout(find_three_variable_solutions, args=(FLUCTUATED_VALUES,))
if fluctuated_solutions['three'] and len(fluctuated_solutions['three']) > 0:
has_solution = True
display_solutions(fluctuated_solutions['three'], 3)
if has_solution:
print(f"\n使用波动系数列表,共找到有效解")
return
# 如果所有系数集都没有找到解
print("\n没有找到符合条件的解,建议:")
print("1. 放宽单个乘积上限(当前为129000)")
print("2. 增加系数列表中的数值")
print("3. 延长求解超时时间")
except Exception as e:
print(f"程序执行出错: {e}")
if __name__ == "__main__":
freeze_support() # 增加多进程支持
main()
print(f"\n总耗时: {time.time() - start_time:.2f}秒")