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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
| import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D
def objective_function(x): if x.ndim == 1: x = x.reshape(1, -1) A = 10 n = x.shape[1] return A * n + np.sum(x**2 - A * np.cos(2 * np.pi * x), axis=1)
def analytical_gradient(x): A = 10 return 2 * x + 2 * np.pi * A * np.sin(2 * np.pi * x)
def water_flow_erosion_optimizer( func, grad_func, bounds, n_droplets=50, max_iterations=1000, learning_rate=0.1, erosion_rate=0.2, decay_rate=0.95, random_perturbation_scale=0.05, grid_res=50, verbose=False ): dim = len(bounds) if dim != 2 and verbose: print("Warning: Visualization is only for 2D. Running optimization in %dD." % dim)
droplets = np.zeros((n_droplets, dim)) for d_idx in range(dim): droplets[:, d_idx] = np.random.uniform(bounds[d_idx][0], bounds[d_idx][1], n_droplets)
min_b = np.array([b[0] for b in bounds]) max_b = np.array([b[1] for b in bounds]) water_level_grid = np.zeros((grid_res, grid_res))
def get_grid_idx(pos): scaled_pos = (pos - min_b) / (max_b - min_b) idx = np.floor(scaled_pos * grid_res).astype(int) idx = np.clip(idx, 0, grid_res - 1) return tuple(idx)
best_global_solution = droplets[0].copy() best_global_value = func(best_global_solution).item()
history = []
if verbose: print(f"Starting WFEO optimization with {n_droplets} droplets...")
for iteration in range(max_iterations): for i in range(n_droplets): current_pos = droplets[i].copy() original_grad = grad_func(current_pos) grid_idx = get_grid_idx(current_pos) current_water_level = water_level_grid[grid_idx] effective_grad = original_grad + current_water_level * np.sign(original_grad) new_pos = current_pos - learning_rate * effective_grad new_pos += np.random.normal(0, random_perturbation_scale, dim)
for d in range(dim): new_pos[d] = np.clip(new_pos[d], bounds[d][0], bounds[d][1]) new_grid_idx = get_grid_idx(new_pos) water_level_grid[new_grid_idx] += erosion_rate droplets[i] = new_pos
current_droplet_value = func(new_pos).item() if current_droplet_value < best_global_value: best_global_value = current_droplet_value best_global_solution = new_pos.copy()
water_level_grid *= decay_rate history.append(best_global_value) if verbose and iteration % 50 == 0: print(f"Iteration {iteration}: Best value = {best_global_value:.4f}")
if verbose: print("\nOptimization finished.") return best_global_solution, best_global_value, history, water_level_grid
def run_tests(params, num_tests=5): """为一组参数运行多次测试,并返回结果。""" print(f"\n--- Running Tests for Parameters: {params} ---") bounds = [(-5.12, 5.12), (-5.12, 5.12)] results = [] for i in range(num_tests): _, final_value, _, _ = water_flow_erosion_optimizer( objective_function, analytical_gradient, bounds, **params, verbose=False ) results.append(final_value) print(f"Test {i+1}: Final Best Value = {final_value:.4f}") avg_value = np.mean(results) std_value = np.std(results) print(f"Average Final Value: {avg_value:.4f} ± {std_value:.4f}") return avg_value, std_value, results
if __name__ == "__main__": bounds = [(-5.12, 5.12), (-5.12, 5.12)]
params_baseline = { 'n_droplets': 100, 'max_iterations': 1000, 'learning_rate': 0.1, 'erosion_rate': 0.005, 'decay_rate': 0.995, 'random_perturbation_scale': 0.01, 'grid_res': 100 }
params_aggressive = { 'n_droplets': 200, 'max_iterations': 1000, 'learning_rate': 0.1, 'erosion_rate': 0.01, 'decay_rate': 0.99, 'random_perturbation_scale': 0.05, 'grid_res': 100 }
params_conservative = { 'n_droplets': 50, 'max_iterations': 1000, 'learning_rate': 0.05, 'erosion_rate': 0.001, 'decay_rate': 0.999, 'random_perturbation_scale': 0.005, 'grid_res': 100 } print("--- 批量性能测试开始 ---") avg_baseline, std_baseline, results_baseline = run_tests(params_baseline) avg_aggressive, std_aggressive, results_aggressive = run_tests(params_aggressive) avg_conservative, std_conservative, results_conservative = run_tests(params_conservative) print("--- 批量性能测试结束 ---")
plt.figure(figsize=(8, 6)) plt.boxplot([results_baseline, results_aggressive, results_conservative], labels=['Baseline', 'Aggressive', 'Conservative']) plt.title('WFEO Performance Comparison Across Different Parameter Sets') plt.ylabel('Final Best Value') plt.xlabel('Parameter Strategy') plt.grid(True) plt.show()
print("\n--- 绘制一次详细运行结果(使用基准参数)---") final_solution, final_value, opt_history, final_water_grid = water_flow_erosion_optimizer( objective_function, analytical_gradient, bounds, **params_baseline, verbose=True )
print(f"\nFinal Best Solution: {final_solution}") print(f"Final Best Value: {final_value:.4f}")
fig = plt.figure(figsize=(15, 6)) ax1 = fig.add_subplot(121) ax1.plot(opt_history) ax1.set_title("Optimization Progress (Best Value per Iteration)") ax1.set_xlabel("Iteration") ax1.set_ylabel("Objective Function Value") ax1.grid(True)
ax2 = fig.add_subplot(122, projection='3d') x = np.linspace(bounds[0][0], bounds[0][1], 100) y = np.linspace(bounds[1][0], bounds[1][1], 100) X, Y = np.meshgrid(x, y) points = np.stack([X.flatten(), Y.flatten()], axis=1) Z = objective_function(points).reshape(X.shape)
ax2.plot_surface(X, Y, Z, cmap='viridis', alpha=0.7, rstride=1, cstride=1)
water_level_scale_factor = (np.max(Z) - np.min(Z)) / (np.max(final_water_grid) + 1e-9) * 0.1 Z_with_water = Z + final_water_grid * water_level_scale_factor ax2.plot_surface(X, Y, Z_with_water, cmap='Blues', alpha=0.3, rstride=1, cstride=1)
ax2.scatter(final_solution[0], final_solution[1], final_value, color='red', s=100, label='Final Best', zorder=10)
ax2.set_title("Energy Landscape with Final Water Levels") ax2.set_xlabel("X-axis") ax2.set_ylabel("Y-axis") ax2.set_zlabel("Energy") ax2.legend() plt.tight_layout() plt.show()
|