Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions iohblade/benchmarks/analysis/auto_correlation_base_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from numpy.typing import NDArray
from typing import Optional

from iohblade.misc.prepare_namespace import prepare_namespace, clean_local_namespace
from iohblade.misc.prepare_namespace import prepare_namespace

"""
Autocorrelation measures how similar a signal is to a shifted version of itself.
Expand Down Expand Up @@ -138,30 +138,28 @@ def make_format_prompt(self):

"""

def _get_time_series(self, code) -> tuple[NDArray[np.float64], Optional[Exception]]:
def _get_time_series(
self, code, name
) -> tuple[NDArray[np.float64], Optional[Exception]]:
local_parameters = {}

allowed = ["numpy", "scipy"]

try:
global_parameters = prepare_namespace(code, allowed)
exec(code, global_parameters, local_parameters)
local_parameters = clean_local_namespace(
local_parameters, global_parameters
)
cls = next(v for v in local_parameters.values() if isinstance(v, type))
try:
compiled_code = compile(code, name, "exec")
exec(compiled_code, global_parameters, local_parameters)
cls = local_parameters[name]
if self.best_solution:
f = np.asarray(
cls(best_known_configuration=self.best_known)(), dtype=np.float64
) # Runs if class has __init__(self, best_known_solution)
except:
else:
f = np.asarray(
cls()(), dtype=np.float64
) # Rollback to empty initantiation.

return f, None
except Exception as e:
print("\t Exception in `auto_correlation_ineq1.py`, " + e.__repr__())
return (
np.ndarray(
[
Expand Down
14 changes: 7 additions & 7 deletions iohblade/benchmarks/analysis/auto_correlation_ineq1.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(
Problem.__init__(self, name=self.task_name)

self.task_prompt = self.make_task_prompt("minimize max_t (f*f)(t) / (∫ f)^2")
self.example_prompt = self.make_example_prompt("AutoCorrCandidate")
self.example_prompt = self.make_example_prompt("AutoCorrCandidate_1")
self.format_prompt = self.make_format_prompt()
self.dependencies += [
"scipy"
Expand All @@ -49,12 +49,11 @@ def evaluate(self, solution: Solution) -> Solution:
code = solution.code

try:
f, err = self._get_time_series(code)
f, err = self._get_time_series(code, solution.name)
if err is not None:
raise err
except Exception as e:
print("\t Exception in `auto_correlation_ineq1.py`, " + e.__repr__())
solution.set_scores(float("inf"), f"exec-error {e}", "exec-failed")
solution = solution.set_scores(float("inf"), e)
return solution

try:
Expand All @@ -70,11 +69,12 @@ def evaluate(self, solution: Solution) -> Solution:
raise ValueError("Integral ∫f must be > 0 for C1")

score = float(np.max(g) / (I * I)) # minimize
solution.set_scores(
score, f"C1 ratio = {score:.6g}, best known = {self.best_known:.6g}"
solution = solution.set_scores(
score,
f"C1 ratio = {score:.6g}, best known = {self.best_known:.6g}; soln={f}",
)
except Exception as e:
solution.set_scores(float("inf"), f"calc-error {e}", "calc-failed")
solution = solution.set_scores(float("inf"), f"calc-error {e}", e)
return solution

def test(self, solution: Solution) -> Solution:
Expand Down
11 changes: 5 additions & 6 deletions iohblade/benchmarks/analysis/auto_correlation_ineq2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AutoCorrIneq2(AutoCorrBaseSpec, Problem):
functionality.
Optimisation:
\[\min -(||f*f||_2^2 / (||f*f||_1 • ||f*f||_\infty))\]
Best known auto-correlation 1 score by alpha evolve: is C_2 >= 0.8962 (prev 0.8892).
Best known auto-correlation 2 score by alpha evolve: is C_2 >= 0.8962 (prev 0.8892).
"""

def __init__(
Expand Down Expand Up @@ -42,12 +42,11 @@ def evaluate(self, solution: Solution) -> Solution:
code = solution.code

try:
f, err = self._get_time_series(code)
f, err = self._get_time_series(code, name=solution.name)
if err is not None:
raise err
except Exception as e:
print("\t Exception in `auto_correlation_ineq2.py`, " + e.__repr__())
solution.set_scores(float("-inf"), f"exec-error {e}", "exec-failed")
solution = solution.set_scores(float("-inf"), f"exec-error {e}", e)
return solution

try:
Expand All @@ -67,11 +66,11 @@ def evaluate(self, solution: Solution) -> Solution:
raise ValueError("Denominator zero in C2 ratio")

score = L2sq / den # maximize in paper
solution.set_scores(
solution = solution.set_scores(
score, f"C2 ratio = {score:.6g}, best known = {self.best_known:.6g}"
)
except Exception as e:
solution.set_scores(float("-inf"), f"calc-error {e}", "calc-failed")
solution = solution.set_scores(float("-inf"), f"calc-error {e}", e)
return solution

def test(self, solution: Solution) -> Solution:
Expand Down
12 changes: 6 additions & 6 deletions iohblade/benchmarks/analysis/auto_correlation_ineq3.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AutoCorrIneq3(AutoCorrBaseSpec, Problem):
functionality.
Optimisation:
\[\max_t |||f*f||(t)| / (∫f)^2 \]
Best known auto-correlation 1 score by alpha evolve: is C_3 <= 1.4557 (prev 1.4581).
Best known auto-correlation 3 score by alpha evolve: is C_3 <= 1.4557 (prev 1.4581).
"""

def __init__(
Expand All @@ -29,7 +29,7 @@ def __init__(
Problem.__init__(self, name=self.task_name)

self.task_prompt = self.make_task_prompt("minimize max_t |(f*f)(t)| / (∫ f)^2")
self.example_prompt = self.make_example_prompt("AutoCorreCandidate_2")
self.example_prompt = self.make_example_prompt("AutoCorreCandidate_3")
self.format_prompt = self.make_format_prompt()

self.dependencies += ["scipy"]
Expand All @@ -40,12 +40,12 @@ def evaluate(self, solution: Solution) -> Solution:
code = solution.code

try:
f, err = self._get_time_series(code)
f, err = self._get_time_series(code, name=solution.name)
if err is not None:
raise err
except Exception as e:
print("\t Exception in `auto_correlation_ineq3.py`, " + e.__repr__())
solution.set_scores(float("inf"), f"exec-error {e}", "exec-failed")
solution = solution.set_scores(float("inf"), f"exec-error {e}", e)
return solution

try:
Expand All @@ -59,11 +59,11 @@ def evaluate(self, solution: Solution) -> Solution:
raise ValueError("Integral ∫f must be nonzero for C3")

score = float(np.max(np.abs(g)) / (I * I)) # minimize
solution.set_scores(
solution = solution.set_scores(
score, f"C3 ratio = {score:.6g}, best known = {self.best_known:.6g}"
)
except Exception as e:
solution.set_scores(float("inf"), f"calc-error {e}", "calc-failed")
solution = solution.set_scores(float("inf"), f"calc-error {e}", e)
return solution

def test(self, solution: Solution) -> Solution:
Expand Down
6 changes: 3 additions & 3 deletions iohblade/benchmarks/analysis/get_analysis_problems.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,9 +1068,9 @@ def get_analysis_problems(use_best: bool) -> list[AutoCorrBaseSpec]:

Returns:
An array of benchmark objects as follows:
array[0] = Auto Correlation Inrquality 1
array[1] = Auto Correlation Inrquality 2
array[2] = Auto Correlation Inrquality 3
array[0] = Auto Correlation Inequality 1
array[1] = Auto Correlation Inequality 2
array[2] = Auto Correlation Inequality 3

"""
if use_best:
Expand Down
22 changes: 11 additions & 11 deletions iohblade/benchmarks/combinatorics/erdos_min_overlap.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(
* Overlap functional uses zero-extension of g outside [-1,1].
* Optimize the objective:
* minimize sup_{x ∈ [-2,2]} ∫_{-1}^{1} f(t) · g(x+t) dt, with g = 1 - f
* Do not use scipy's interp1d, it is no longer supported.
* Do not use scipy's interp1d, it is no depricated.
"""

self.task_prompt += f"""
Expand Down Expand Up @@ -121,27 +121,27 @@ def _sup_overlap(self, f: np.ndarray, g: np.ndarray) -> float:
def evaluate(self, solution: Solution, explogger=None):
local_ns = {}
code = solution.code

name = solution.name if solution.name else "ErdosCandidate"
try:
safe_globals = prepare_namespace(code, self.dependencies)
compiled = compile(code, filename=name, mode="exec")
exec(compiled, safe_globals, local_ns)

exec(code, safe_globals, local_ns)
local_ns = clean_local_namespace(local_ns, safe_globals)
cls = next(v for v in local_ns.values() if isinstance(v, type))
cls = local_ns[name]

try:
if self.best_solution is not None:
f = np.asarray(
cls(best_known_configuration=self.best_solution)(), dtype=np.float64
)
except:
else:
f = np.asarray(cls()(), dtype=np.float64)
except Exception as e:
solution.set_scores(float("inf"), f"exec-error {e}", "exec-failed")
solution = solution.set_scores(float("inf"), f"exec-error {e}", e)
return solution

try:
if f.ndim != 1 or f.size != self.n_bins:
raise ValueError(f"f must be length {self.n_bins}")
raise ValueError(f"f must be length {self.n_bins} got {f.shape}.")
# bounds f ∈ [0,1] within tolerance
if np.any(f < -self.tolerance) or np.any(f > 1.0 + self.tolerance):
raise ValueError("entries of f must lie in [0,1]")
Expand All @@ -155,9 +155,9 @@ def evaluate(self, solution: Solution, explogger=None):

score = self._sup_overlap(f, g) # minimize
msg = f"Score = {score:.6g}; with configuration: N={self.n_bins}, dx={dx:.6g}, If={I_f:.6g}.\n\t Best known score = {self.best_known}"
solution.set_scores(score, msg)
solution = solution.set_scores(score, msg)
except Exception as e:
solution.set_scores(float("inf"), f"calc-error {e}", "calc-failed")
solution = solution.set_scores(float("inf"), f"calc-error {e}")

return solution

Expand Down
15 changes: 10 additions & 5 deletions iohblade/benchmarks/fourier/fourier_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,23 @@ def make_task_prompt(self, formula: str) -> str:
- Objective (minimize):
- """
+ formula
+ """
+ f"""
- Tip: enforce structure (e.g., small |c|, P(0)≈0) to aid root placement.
K = {self.n_terms}."
"""
)

def make_example_prompt(self, class_name: str) -> str:
accept_best_configuration = ""
accept_best_configuration = """
def __init__(self, n_terms: int):
# Accepts number of terms K for the problem.


"""
if self.best_known_configuration is not None:
accept_best_configuration = """
def __init__(self, best_known_configuration: list[float] | None):
# Accepts a best known configuration (if available) for the problem, as a initial configuration, which is then
def __init__(self, n_terms: int, best_known_configuration: list[float] | None):
# Accepts a mumber of terms K and best known configuration (if available) for the problem, as a initial configuration, which is then
optimised for better results.
"""
return f"""
Expand All @@ -75,7 +80,7 @@ class {class_name}:
{accept_best_configuration}
def __call__(self):
# Return K={self.n_terms} coefficients for H_0, H_4, H_8, ...
return [0.33, -0.01, -9e-05][: {self.n_terms}]
return [...., 0.33, -0.01, -9e-05][: {self.n_terms}]
```
"""

Expand Down
5 changes: 4 additions & 1 deletion iohblade/benchmarks/fourier/get_fourier_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def get_fourier_problems(use_best: bool) -> list[UncertaintyInequality]:
array[0] = Fourier Uncertainty Inequality benchmark object.

"""
ue1 = UncertaintyInequality(best_solution=best_known_configuration)
if use_best:
ue1 = UncertaintyInequality(best_solution=best_known_configuration)
else:
ue1 = UncertaintyInequality()

return [ue1]
25 changes: 13 additions & 12 deletions iohblade/benchmarks/fourier/uncertainty_inequality.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from iohblade.problem import Problem
from iohblade.solution import Solution
from iohblade.misc.prepare_namespace import prepare_namespace, clean_local_namespace
from iohblade.misc.prepare_namespace import prepare_namespace

from iohblade.benchmarks.fourier.fourier_base import FourierBase

Expand Down Expand Up @@ -114,23 +114,25 @@ def _check_tail_nonnegative(self, hcoef: np.ndarray, r: float) -> None:

def evaluate(self, solution: Solution, explogger=None):
code = solution.code

name = solution.name
# 1) execute candidate
try:
safe_globals = prepare_namespace(code, self.dependencies)
local_ns = {}
exec(code, safe_globals, local_ns)
local_ns = clean_local_namespace(local_ns, safe_globals)
cls = next(v for v in local_ns.values() if isinstance(v, type))
try:
safe_globals = prepare_namespace(code, self.dependencies)

compiled_code = compile(code, filename=name, mode="exec")
exec(compiled_code, safe_globals, local_ns)
cls = local_ns[name]

if self.best_known_configuration is not None:
c = np.asanyarray(
cls(self.best_known_configuration)(), dtype=np.float64
cls(self.n_terms, self.best_known_configuration)(), dtype=np.float64
)
except:
else:
c = np.asarray(cls(self.n_terms)(), dtype=np.float64)

except Exception as e:
solution.set_scores(float("inf"), f"exec-error {e}", "exec-failed")
solution.set_scores(float("inf"), f"exec-error {e}", e)
return solution

# 2) validate and score
Expand Down Expand Up @@ -167,8 +169,7 @@ def evaluate(self, solution: Solution, explogger=None):
f"Score = {score:.9g}; r_max={r:.6g}; best known score = {self.best_known}",
)
except Exception as e:
solution.set_scores(float("inf"), f"calc-error {e}", "calc-failed")

solution.set_scores(float("inf"), f"calc-error {e}", e)
return solution

def test(self, solution: Solution) -> Solution:
Expand Down
1 change: 0 additions & 1 deletion iohblade/benchmarks/geometry/get_geometry_problems.py
Original file line number Diff line number Diff line change
Expand Up @@ -7874,7 +7874,6 @@ def get_kissing_number_11D_problems(use_best: bool) -> list[KissingNumber11D]:
if use_best:
kn = KissingNumber11D(best_solution=best_kissing_number_11D)
return [kn]

kn = KissingNumber11D()
return [kn]

Expand Down
16 changes: 9 additions & 7 deletions iohblade/benchmarks/geometry/heilbronn_convex_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,19 @@ def evaluate(self, solution: Solution, explogger=None):
try:
safe = prepare_namespace(code, self.dependencies)
local_ns = {}
exec(code, safe, local_ns)
local_ns = clean_local_namespace(local_ns, safe)
cls = next(v for v in local_ns.values() if isinstance(v, type))
try:

compiled_code = compile(code, solution.name, "exec")
exec(compiled_code, safe, local_ns)
cls = local_ns[solution.name]

if self.best_solution is not None:
result = cls(self.n_points, self.best_solution)()
except:
else:
result = cls(self.n_points)()
P = self.to_np_points(result)
except Exception as e:
# tb = e.__traceback__
solution.set_scores(float("-inf"), f"exec-error \n{e}", "exec-failed")
solution.set_scores(float("-inf"), f"exec-error \n{e}", e)
return solution

try:
Expand All @@ -144,7 +146,7 @@ def evaluate(self, solution: Solution, explogger=None):
f"min_triangle_area={min_area:.6g}, {'best known = ' + str(self.best_known) if self.best_known is not None else ''}.",
)
except Exception as e:
solution.set_scores(float("-inf"), f"calc-error {e}", "calc-failed")
solution.set_scores(float("-inf"), f"calc-error {e}", e)
return solution

def test(self, solution):
Expand Down
Loading