File size: 5,165 Bytes
aaebaab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import numpy as np
from scipy.optimize import minimize

# 参数
p = np.array([0.8, 0.5, 0.3, 0.2, 0.12, 0.07])
q = 1 - p  # 失效概率
N_total = 80

def objective(n):
    """目标函数:Σ n_i * [1 - q_i^{n_i}]"""
    return np.sum(n * (1 - q**n))

# 修正约束条件
constraints = [
    {'type': 'eq', 'fun': lambda n: np.sum(n) - N_total}
]

# 设置边界为至少1个节点
bounds = [(1, N_total)] * 6

# 初始猜测(均匀分配)
n0 = np.ones(6) * N_total / 6
n0 = np.maximum(n0, 1)  # 确保初始值 >=1

# 优化(使用连续实数)
res = minimize(lambda n: -objective(n), n0, 
               bounds=bounds,
               constraints=constraints,
               method='SLSQP',
               options={'maxiter': 50,
                        'ftol': 1e-6,
                        'disp': True})
n_opt = res.x
max_value = objective(n_opt)

print("最优节点分配(连续解):")
for i in range(6):
    print(f"层 {i+1}: {n_opt[i]:.2f} 个节点")
print(f"\n最大值: {max_value:.4f}")

# 检查整数解
from itertools import product

def integer_search(center, radius=3):
    best_val = -1
    best_n = None
    
    # 生成附近整数组合(确保每个值至少为1)
    ranges = []
    for i in range(6):
        start = max(1, int(center[i]) - radius)  # 至少为1
        end = min(N_total, int(center[i]) + radius)  # 最多N_total
        ranges.append(range(start, end + 1))
    
    # 限制搜索范围,避免组合爆炸
    count = 0
    max_combinations = 100000  # 限制搜索组合数
    
    for combo in product(*ranges):
        count += 1
        if count > max_combinations:
            break
            
        if sum(combo) == N_total and all(x >= 1 for x in combo):
            val = objective(np.array(combo))
            if val > best_val:
                best_val = val
                best_n = combo
    
    # 如果没找到合适的解,尝试更简单的启发式搜索
    if best_n is None:
        print("直接搜索未找到合适解,使用四舍五入法...")
        # 四舍五入并调整到总和为80
        rounded = np.round(n_opt).astype(int)
        diff = N_total - np.sum(rounded)
        
        # 调整差值
        if diff > 0:
            # 从最小值开始加
            sorted_idx = np.argsort(n_opt - rounded)
            for i in range(diff):
                rounded[sorted_idx[i]] += 1
        elif diff < 0:
            # 从最大值开始减
            sorted_idx = np.argsort(rounded - n_opt)[::-1]
            for i in range(-diff):
                if rounded[sorted_idx[i]] > 1:  # 确保至少为1
                    rounded[sorted_idx[i]] -= 1
        
        # 确保所有值至少为1
        rounded = np.maximum(rounded, 1)
        best_n = tuple(rounded)
        best_val = objective(np.array(best_n))
    
    return best_n, best_val

int_n, int_val = integer_search(n_opt, radius=3)
print("\n近似最优整数解:")
for i in range(6):
    print(f"层 {i+1}: {int_n[i]} 个节点")
print(f"整数值: {int_val:.4f}")

# 验证
print(f"\n验证:")
print(f"总和: {sum(int_n)}")
print(f"所有节点 ≥ 1: {all(x >= 1 for x in int_n)}")

# 附加:使用更智能的整数搜索方法
print("\n\n备选:使用动态规划寻找最优整数解...")

# 由于节点数较少,可以尝试更系统的方法
def find_optimal_integer():
    from itertools import combinations_with_replacement
    import math
    
    best_val = -1
    best_n = None
    
    # 使用星棒法生成所有可能的组合
    # C(N_total-1, 6-1) = C(79, 5) ≈ 2.3 million 仍然很大
    # 使用更高效的方法:从连续解开始,在附近搜索
    
    # 生成所有6个数字总和为80的组合,每个至少为1
    # 这相当于找5个切割点
    
    # 由于组合数仍然较大,我们使用更智能的剪枝
    
    # 简化的搜索:先固定前5层,最后一层由总和决定
    candidates = []
    
    # 放宽搜索半径
    radius = 4
    center = np.round(n_opt).astype(int)
    
    for n1 in range(max(1, center[0]-radius), center[0]+radius+1):
        for n2 in range(max(1, center[1]-radius), center[1]+radius+1):
            for n3 in range(max(1, center[2]-radius), center[2]+radius+1):
                for n4 in range(max(1, center[3]-radius), center[3]+radius+1):
                    for n5 in range(max(1, center[4]-radius), center[4]+radius+1):
                        n6 = N_total - (n1+n2+n3+n4+n5)
                        if n6 >= 1:
                            combo = (n1, n2, n3, n4, n5, n6)
                            # 检查是否在合理范围内
                            if all(abs(combo[i] - center[i]) <= radius+2 for i in range(6)):
                                val = objective(np.array(combo))
                                if val > best_val:
                                    best_val = val
                                    best_n = combo
    
    return best_n, best_val

opt_int_n, opt_int_val = find_optimal_integer()
print("\n优化后的整数解:")
for i in range(6):
    print(f"层 {i+1}: {opt_int_n[i]} 个节点")
print(f"优化整数值: {opt_int_val:.4f}")