옵투나 오류수정정

This commit is contained in:
Your Name
2026-08-21 20:18:24 +09:00
parent 0780b2cdd0
commit 3eaa3b61df
14 changed files with 370 additions and 106 deletions

View File

@@ -173,6 +173,69 @@ def leftover_trials(study_trials: int, n_finished: int) -> int:
return max(0, t - c)
def clamp_optimize_n_trials(
study: Any,
n_trials: int,
*,
log: Optional[logging.Logger] = None,
) -> int:
"""
Optuna study.optimize(n_trials=N) 은 **추가** N회.
study-trials 목표가 있으면 남은 횟수로 잘라서, 목표 초과 연타를 막는다.
"""
lg = log or logger
try:
n = max(0, int(n_trials or 0))
except (TypeError, ValueError):
n = 0
try:
goal = parse_study_trials_value((study.user_attrs or {}).get(KIS_STUDY_TRIALS_ATTR))
except Exception:
goal = 0
if goal <= 0:
return max(1, n) if n > 0 else 0
_c, _r, n_f = count_study_states(study)
left = leftover_trials(goal, n_f)
if left <= 0:
lg.info(
"📌 study-trials 목표 도달 — 추가 trial 생략 (끝난시도=%s / 목표=%s)",
n_f, goal,
)
return 0
if n <= 0 or n > left:
lg.warning(
"⚠️ --trials=%s → leftover %s 로 축소 (끝난시도=%s / study-trials=%s)",
n, left, n_f, goal,
)
return left
return n
def make_study_goal_stop_callback(log: Optional[logging.Logger] = None):
"""trial 끝날 때마다 study-trials 도달이면 study.stop()."""
lg = log or logger
def _cb(study: Any, _trial: Any) -> None:
try:
goal = parse_study_trials_value((study.user_attrs or {}).get(KIS_STUDY_TRIALS_ATTR))
except Exception:
return
if goal <= 0:
return
_c, _r, n_f = count_study_states(study)
if n_f >= goal:
lg.info(
"📌 study-trials 도달 → optimize 중지 (끝난시도=%s / 목표=%s)",
n_f, goal,
)
try:
study.stop()
except Exception:
pass
return _cb
def leftover_note(
study_trials: int,
n_finished: int,