Spaces:
Sleeping
Sleeping
File size: 1,274 Bytes
8e3a425 | 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 | """core/optimizer.py — FellowOptimizer (hill-climbing adaptatif)"""
import math, random
from typing import Callable, List, Tuple
class FellowOptimizer:
def __init__(self, objective: Callable[[List[float]], float],
bounds: List[Tuple[float, float]], max_evals: int = 500):
self.obj = objective
self.bounds = bounds
self.max_evals = max_evals
def optimize(self) -> Tuple[List[float], List[float]]:
ndim = len(self.bounds)
best = [random.uniform(lo, hi) for lo, hi in self.bounds]
best_val = self.obj(best)
history = [best_val]
step_size = 0.1
for i in range(self.max_evals - 1):
# Décroissance adaptative du pas
decay = 1.0 - (i / self.max_evals) * 0.5
candidate = [
max(lo, min(hi, x + random.gauss(0, step_size * (hi - lo) * decay)))
for x, (lo, hi) in zip(best, self.bounds)
]
val = self.obj(candidate)
if val < best_val:
best, best_val = candidate, val
step_size = min(0.2, step_size * 1.1)
else:
step_size = max(0.01, step_size * 0.98)
history.append(best_val)
return best, history
|