Fitness

class iohblade.fitness.Fitness(value: dict[str, float] | None = None)

Bases: object

A class for multi-objective fitness management. Meant for easy comparison between fitness values.

Note: Do NOT use sort on this value — sorting makes certain assumptions that cannot be guaranteed by multi-objective fitnesses.

Usage: Initialise with a dict[str, float] multi-objective fitness. Comparisons are defined in terms of Pareto dominance (minimisation by default, i.e. lower is better):

  • a < b : a strictly dominates b in minimisation; vice-versa in maximisation.

  • a > b : a strictly dominates b in maximisation; vice-versa in minimisation.

  • a <= b : a dominates or is on the same Pareto-front as b (minimisation).

  • a >= b : a dominates or is on the same Pareto-front as b (maximisation).

  • a == b : a and b have exactly the same fitness on every objective.

Parameters:

value (dict[str, float] | None) – Mapping from objective name to objective value. Pass None (or omit) to create an empty Fitness object.

classmethod from_dict(data: dict[str, float]) → Fitness

Construct a Fitness from a plain dict (e.g. after json.loads).

keys()

Return the objective names stored in this Fitness object.

to_dict() → dict[str, float]

Return a plain dict copy of the objective values.

to_vector() → list[float]

Return objective values as a sorted list (sorted by objective name).

iohblade.fitness.from_minimised(objective, minimisation: bool = False) → float

Inverse of to_minimised(); non-finite objectives become the worst fitness for the problem’s direction.

iohblade.fitness.is_better(candidate, incumbent, minimisation: bool = False) → bool

Whether fitness candidate is strictly better than incumbent.

A non-finite fitness (a failed evaluation, NaN, None) is never better, and any finite fitness is better than a non-finite incumbent. Multi-objective Fitness values are compared by Pareto dominance.

Parameters:
  • candidate – Fitness of the new solution (float or Fitness).

  • incumbent – Fitness to beat (float, Fitness or None).

  • minimisation – Whether lower values are better.

Returns:

True if candidate should replace incumbent.

Return type:

bool

iohblade.fitness.to_minimised(fitness, minimisation: bool = False) → float

Convert a scalar BLADE fitness to an objective that is minimised.

For wrapping libraries that always minimise (EoH, ReEvo): the fitness is negated when the problem maximises, and a failed evaluation (non-finite or non-numeric fitness) becomes inf, the worst objective.

iohblade.fitness.worst_fitness(minimisation: bool = False) → float

The worst possible scalar fitness: inf when minimising, else -inf.

class iohblade.benchmarks.toy_multiobjective.ToyMultiObjective(budget: int = 100, name: str = 'ToyMultiObjective', eval_timeout: int = 120)

Bases: Problem

Toy bi-objective problem for demonstrating multi-objective support.

The generated algorithm should search for a scalar \(x \in [-1, 3]\) that simultaneously minimises:

  • f1 = \(x^2\) (distance to 0, in [0, 9])

  • f2 = \((x - 2)^2\) (distance to 2, in [0, 9])

Both objectives conflict: a perfect solution for f1 (x = 0) is the worst possible for f2 and vice-versa.

The returned Fitness uses negated values so that larger is better, which is consistent with BLADE’s maximisation convention:

  • fitness["f1"] = −best_f1 (maximise → minimise f1)

  • fitness["f2"] = −best_f2 (maximise → minimise f2)

Parameters:
  • budget – Number of function evaluations the generated algorithm may use.

  • name – Problem name (shown in logs and plots).

evaluate(solution: Solution) → Solution

Execute the generated algorithm and score it with a Fitness object.

Parameters:

solution – A Solution whose code attribute must define a class BiSphereSearcher with a run(objective, budget) method.

Returns:

The same solution with fitness set to a Fitness mapping {"f1": -best_f1, "f2": -best_f2}, where best_f1 and best_f2 are the lowest values the harness observed. A failed solution gets -inf on both objectives.

Return type:

Solution

example_prompt = 'Here is a simple reference implementation:\n\n```python\nimport numpy as np\n\nclass BiSphereSearcher:\n    """Reference searcher for the toy bi-objective benchmark.\n\n    Uniformly samples `budget` points in [-1, 3] and evaluates each\n    of them with the objective provided by the harness.\n    """\n\n    def run(self, objective, budget: int = 100):\n        rng = np.random.default_rng(42)\n        best_f1, best_f2 = float("inf"), float("inf")\n        for x in rng.uniform(-1.0, 3.0, size=budget):\n            f1, f2 = objective(x)\n            best_f1, best_f2 = min(best_f1, f1), min(best_f2, f2)\n        return best_f1, best_f2\n```\n'
format_prompt = 'Your solution must be a Python class named `BiSphereSearcher` with a\n`run(self, objective, budget: int = 100)` method.\nWrap the class in a Python code block:\n\n```python\nclass BiSphereSearcher:\n    ...\n```\n'
get_config() → dict[str, Any]

The problem’s properties, logged with every run.

Returns:

A dictionary with the keys tags (list[str]), name (str), prompt (str), minimisation (bool), evaluator (str) and config (dict: extra configuration of the problem, such as HPO settings).

Return type:

dict[str, Any]

get_prompt() → str

Get the full prompt describing the problem and how to format the answer.

task_prompt = 'You are given a bi-objective optimisation task. Your goal is to write\na Python class that searches for a scalar value x in [-1, 3] and\nsimultaneously minimises:\n\n    f1(x) = x ** 2\n    f2(x) = (x - 2) ** 2\n\nThese objectives conflict: the optimal x for f1 is 0, while the\noptimal x for f2 is 2. You should try to find a good compromise.\n\nYour class must be named `BiSphereSearcher` and implement the\nfollowing interface:\n\n    def run(self, objective, budget: int = 100):\n        ...\n\n`objective(x)` evaluates one point and returns the tuple `(f1, f2)`.\nIt may be called at most `budget` times; further calls raise an\nexception. Values of x outside [-1, 3] are clipped to that interval,\nand x must be a finite number. Your score is the best (lowest) f1\nand the best (lowest) f2 among the points you evaluated with\n`objective`; the value returned by `run` is ignored. You are free to\nuse any search strategy you like (random search, grid search,\ngradient-free optimisation, etc.).\n'
test(solution: Solution) → Solution

Run evaluate as the test phase (same logic as training).

to_dict() → dict

Returns a dictionary representation of the problem including all parameters.

Returns:

Dictionary representation of the problem.

Return type:

dict