Commit ·
efac84e
1
Parent(s): 1e393c3
division by zero resloved, and clipping
Browse files
greedyOptim/advanced_optimizers.py
CHANGED
|
@@ -95,7 +95,8 @@ class CMAESOptimizer:
|
|
| 95 |
# Update evolution paths
|
| 96 |
self.ps = (1 - self.cs) * self.ps + np.sqrt(self.cs * (2 - self.cs) * self.mu_eff) * (self.mean - old_mean) / self.sigma
|
| 97 |
|
| 98 |
-
|
|
|
|
| 99 |
< (1.4 + 2 / (self.n + 1)) * np.sqrt(self.n))
|
| 100 |
|
| 101 |
self.pc = (1 - self.cc) * self.pc + hsig * np.sqrt(self.cc * (2 - self.cc) * self.mu_eff) * (self.mean - old_mean) / self.sigma
|
|
@@ -154,6 +155,7 @@ class ParticleSwarmOptimizer:
|
|
| 154 |
self.w = 0.7 # Inertia weight
|
| 155 |
self.c1 = 1.5 # Cognitive parameter
|
| 156 |
self.c2 = 1.5 # Social parameter
|
|
|
|
| 157 |
|
| 158 |
def _decode(self, x: np.ndarray) -> np.ndarray:
|
| 159 |
"""Decode continuous values to discrete actions."""
|
|
@@ -199,6 +201,9 @@ class ParticleSwarmOptimizer:
|
|
| 199 |
self.c1 * r1 * (p_best_positions[i] - positions[i]) +
|
| 200 |
self.c2 * r2 * (g_best_position - positions[i]))
|
| 201 |
|
|
|
|
|
|
|
|
|
|
| 202 |
positions[i] += velocities[i]
|
| 203 |
|
| 204 |
# Bound positions
|
|
|
|
| 95 |
# Update evolution paths
|
| 96 |
self.ps = (1 - self.cs) * self.ps + np.sqrt(self.cs * (2 - self.cs) * self.mu_eff) * (self.mean - old_mean) / self.sigma
|
| 97 |
|
| 98 |
+
# Fix: Use (gen + 1) to avoid division by zero in first iteration
|
| 99 |
+
hsig = (np.linalg.norm(self.ps) / np.sqrt(1 - (1 - self.cs) ** (2 * (gen + 1)))
|
| 100 |
< (1.4 + 2 / (self.n + 1)) * np.sqrt(self.n))
|
| 101 |
|
| 102 |
self.pc = (1 - self.cc) * self.pc + hsig * np.sqrt(self.cc * (2 - self.cc) * self.mu_eff) * (self.mean - old_mean) / self.sigma
|
|
|
|
| 155 |
self.w = 0.7 # Inertia weight
|
| 156 |
self.c1 = 1.5 # Cognitive parameter
|
| 157 |
self.c2 = 1.5 # Social parameter
|
| 158 |
+
self.v_max = 2.0 # Maximum velocity (for clamping)
|
| 159 |
|
| 160 |
def _decode(self, x: np.ndarray) -> np.ndarray:
|
| 161 |
"""Decode continuous values to discrete actions."""
|
|
|
|
| 201 |
self.c1 * r1 * (p_best_positions[i] - positions[i]) +
|
| 202 |
self.c2 * r2 * (g_best_position - positions[i]))
|
| 203 |
|
| 204 |
+
# Clamp velocity to prevent explosion
|
| 205 |
+
velocities[i] = np.clip(velocities[i], -self.v_max, self.v_max)
|
| 206 |
+
|
| 207 |
positions[i] += velocities[i]
|
| 208 |
|
| 209 |
# Bound positions
|