From 03a25e010a9a07fb79e29883bd1adbe4fcc450b6 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Tue, 9 May 2023 18:36:06 -0700 Subject: [PATCH] move fib and `prune_results` into utilities --- thursday/go_benchmark_it.py | 38 +++-------------------------------- thursday/utilities/math.py | 4 ++++ thursday/utilities/scoring.py | 31 ++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 35 deletions(-) diff --git a/thursday/go_benchmark_it.py b/thursday/go_benchmark_it.py index 97f7a71..ccd720c 100644 --- a/thursday/go_benchmark_it.py +++ b/thursday/go_benchmark_it.py @@ -2,7 +2,6 @@ from .go_benchmark_lists import * from .go_benchmarks import problems_2d, problems_3d, problems_4d from .parties import parties from .utilities import OWrap, COWrap, m1, m33, m36, prog -from .utilities import perform_another_experimental_scoring_method import numpy as np all_problems = { @@ -155,12 +154,11 @@ for problem_list in GO_BENCHMARKS.values(): def main(argv, display=True): + from .utilities import fib + from .utilities import prune_results, perform_another_experimental_scoring_method from tqdm import tqdm import sys - def fib(n): - return pow(2 << n, n + 1, (4 << 2 * n) - (2 << n) - 1) % (2 << n) - def optimizer_filter(name): # worst scoring optimizers: (awards=(5, 3, 2, 1)) (obj=146, opt=389) (dims=2, evals=80) # evosax_pbt with (score: 0, price: 593) -593 @@ -262,36 +260,6 @@ def main(argv, display=True): return s - def prune_results(results, multiple): - # if there are more than `multiple` results for one optimizer+objective pair, - # then trim the bottom and top until there are only `multiple` left. - new_results = {} - for obj_name, obj_res in results.items(): - new_res = {} - for fopt, opt_name, extra in sorted(obj_res): - l = new_res.setdefault(opt_name, [[], []]) - l[0].append(fopt) - l[1].append(extra) - slices = {} - for opt_name, res in new_res.items(): - # in the event that an odd number of results needs to be trimmed, - # prefer trimming from the bottom (i.e. worse solutions get removed first). - fopts, extras = res - down = (len(fopts) - multiple) // 2 - up = len(fopts) - (len(fopts) - multiple + 1) // 2 - slices[opt_name] = slice(down, up) - for opt_name, res in new_res.items(): - fopts, extras = res - s = slices[opt_name] - fopts, extras = fopts[s], extras[s] - if not no_summary: - assert len(fopts) == multiple, (len(fopts), multiple) - if len(fopts) == multiple: - for fopt, extra in zip(fopts, extras): - result = (fopt, opt_name, extra) - new_results.setdefault(obj_name, []).append(result) - return results - reset = "\033[m" quieter = True @@ -388,7 +356,7 @@ def main(argv, display=True): run += 1 all_results = results - results = prune_results(results, multiple) + results = prune_results(results, multiple, _check=not no_summary) scores, prices = {}, {} all_opt_names = set() diff --git a/thursday/utilities/math.py b/thursday/utilities/math.py index 6930cd2..25c7bd8 100644 --- a/thursday/utilities/math.py +++ b/thursday/utilities/math.py @@ -21,3 +21,7 @@ def phi(d): for i in range(30 if d == 1 else max(10, 28 - d)): x = pow(1 + x, 1 / (d + 1)) return x + + +def fib(n): + return pow(2 << n, n + 1, (4 << 2 * n) - (2 << n) - 1) % (2 << n) diff --git a/thursday/utilities/scoring.py b/thursday/utilities/scoring.py index 02dd04e..c7a1ddf 100644 --- a/thursday/utilities/scoring.py +++ b/thursday/utilities/scoring.py @@ -1,3 +1,34 @@ +def prune_results(results, multiple, _check=False): + # if there are more than `multiple` results for one optimizer+objective pair, + # then trim the bottom and top until there are only `multiple` left. + new_results = {} + for obj_name, obj_res in results.items(): + new_res = {} + for fopt, opt_name, extra in sorted(obj_res): + l = new_res.setdefault(opt_name, [[], []]) + l[0].append(fopt) + l[1].append(extra) + slices = {} + for opt_name, res in new_res.items(): + # in the event that an odd number of results needs to be trimmed, + # prefer trimming from the bottom (i.e. worse solutions get removed first). + fopts, extras = res + down = (len(fopts) - multiple) // 2 + up = len(fopts) - (len(fopts) - multiple + 1) // 2 + slices[opt_name] = slice(down, up) + for opt_name, res in new_res.items(): + fopts, extras = res + s = slices[opt_name] + fopts, extras = fopts[s], extras[s] + if _check: + assert len(fopts) == multiple, (len(fopts), multiple) + if len(fopts) == multiple: + for fopt, extra in zip(fopts, extras): + result = (fopt, opt_name, extra) + new_results.setdefault(obj_name, []).append(result) + return results + + def perform_another_experimental_scoring_method(results): if len(results) and len(something := next(iter(results.values()))[0]) == 3: history_length = len(something[2])