move objective wrapper into its own file
This commit is contained in:
parent
34713232e3
commit
258c42d963
|
@ -1,3 +1,4 @@
|
||||||
|
from .closures import *
|
||||||
from .colors import *
|
from .colors import *
|
||||||
from .prog80 import prog
|
from .prog80 import prog
|
||||||
from .scoring import *
|
from .scoring import *
|
||||||
|
|
38
thursday/utilities/closures.py
Normal file
38
thursday/utilities/closures.py
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
from .utils import scalar_softplus
|
||||||
|
from .utils_np import do_bounding
|
||||||
|
|
||||||
|
check = object() # secret "key" to pass to wrap_untrustworthy to extract feval_count
|
||||||
|
final = object() # secret "key" to pass to wrap_untrustworthy to extract results
|
||||||
|
|
||||||
|
|
||||||
|
class ExhaustedTrialsError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def wrap_untrustworthy(
|
||||||
|
objective, n_trials, *, raising=False, bounding=None, softplus=False, eps=0.0
|
||||||
|
):
|
||||||
|
# also handles bounding now, so it may be used for other purposes as well. whoops.
|
||||||
|
feval_count = 0
|
||||||
|
best_so_far = None
|
||||||
|
|
||||||
|
def _objective(x):
|
||||||
|
nonlocal feval_count, best_so_far
|
||||||
|
if x is check:
|
||||||
|
return feval_count
|
||||||
|
if x is final:
|
||||||
|
assert best_so_far is not None
|
||||||
|
fopt, xopt = best_so_far
|
||||||
|
return fopt, xopt, feval_count
|
||||||
|
if raising and feval_count >= n_trials:
|
||||||
|
raise ExhaustedTrialsError()
|
||||||
|
if bounding is not None:
|
||||||
|
x = do_bounding(x, bounding)
|
||||||
|
fx = objective(x)
|
||||||
|
feval_count += 1
|
||||||
|
if n_trials is None or feval_count <= n_trials:
|
||||||
|
if best_so_far is None or fx < best_so_far[0]:
|
||||||
|
best_so_far = (fx, x.copy())
|
||||||
|
return scalar_softplus(fx) + eps if softplus else fx
|
||||||
|
|
||||||
|
return _objective
|
|
@ -3,12 +3,6 @@ import math
|
||||||
|
|
||||||
feps = 2.0**-23.0
|
feps = 2.0**-23.0
|
||||||
tiniest = 2.0**-1022.0
|
tiniest = 2.0**-1022.0
|
||||||
check = object() # secret "key" to pass to wrap_untrustworthy to extract feval_count
|
|
||||||
final = object() # secret "key" to pass to wrap_untrustworthy to extract results
|
|
||||||
|
|
||||||
|
|
||||||
class ExhaustedTrialsError(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def scalar_softplus(x):
|
def scalar_softplus(x):
|
||||||
|
@ -30,35 +24,6 @@ def phi(d):
|
||||||
return x
|
return x
|
||||||
|
|
||||||
|
|
||||||
def wrap_untrustworthy(
|
|
||||||
objective, n_trials, *, raising=False, bounding=None, softplus=False, eps=0.0
|
|
||||||
):
|
|
||||||
# also handles bounding now, so it may be used for other purposes as well. whoops.
|
|
||||||
feval_count = 0
|
|
||||||
best_so_far = None
|
|
||||||
|
|
||||||
def _objective(x):
|
|
||||||
nonlocal feval_count, best_so_far
|
|
||||||
if x is check:
|
|
||||||
return feval_count
|
|
||||||
if x is final:
|
|
||||||
assert best_so_far is not None
|
|
||||||
fopt, xopt = best_so_far
|
|
||||||
return fopt, xopt, feval_count
|
|
||||||
if raising and feval_count >= n_trials:
|
|
||||||
raise ExhaustedTrialsError()
|
|
||||||
if bounding is not None:
|
|
||||||
x = do_bounding(x, bounding)
|
|
||||||
fx = objective(x)
|
|
||||||
feval_count += 1
|
|
||||||
if n_trials is None or feval_count <= n_trials:
|
|
||||||
if best_so_far is None or fx < best_so_far[0]:
|
|
||||||
best_so_far = (fx, x.copy())
|
|
||||||
return scalar_softplus(fx) + eps if softplus else fx
|
|
||||||
|
|
||||||
return _objective
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class KeyData:
|
class KeyData:
|
||||||
key: str
|
key: str
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# i've separated numpy-dependent methods from the rest of the utils.
|
# i've separated numpy-dependent methods from the rest of the utils.
|
||||||
from . import AcquireForWriting, merge_summaries, feps, m33, m34, m93
|
from .colors import m33, m34, m93
|
||||||
|
from .utils import AcquireForWriting, merge_summaries, feps
|
||||||
from time import time
|
from time import time
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user