Fitness¶
- class iohblade.fitness.Fitness(value: dict[str, float] | None = None)¶
Bases:
objectA class for multi-objective fitness management. Meant for easy comparison between fitness values.
Note: Do NOT usesorton this value — sorting makes certain assumptions that cannot be guaranteed by multi-objective fitnesses.Usage: Initialise with adict[str, float]multi-objective fitness. Comparisons are defined in terms of Pareto dominance (minimisation by default, i.e. lower is better):a < b:astrictly dominatesbin minimisation; vice-versa in maximisation.a > b:astrictly dominatesbin maximisation; vice-versa in minimisation.a <= b:adominates or is on the same Pareto-front asb(minimisation).a >= b:adominates or is on the same Pareto-front asb(maximisation).a == b:aandbhave 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
Fitnessfrom a plain dict (e.g. afterjson.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
candidateis strictly better thanincumbent.A non-finite fitness (a failed evaluation, NaN,
None) is never better, and any finite fitness is better than a non-finite incumbent. Multi-objectiveFitnessvalues 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
candidateshould replaceincumbent.- 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:
infwhen minimising, else-inf.
- class iohblade.benchmarks.toy_multiobjective.ToyMultiObjective(budget: int = 100, name: str = 'ToyMultiObjective', eval_timeout: int = 120)¶
Bases:
ProblemToy 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
Fitnessuses 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
Solutionwhosecodeattribute must define a classBiSphereSearcherwith arun(objective, budget)method.- Returns:
The same solution with
fitnessset to aFitnessmapping{"f1": -best_f1, "f2": -best_f2}, wherebest_f1andbest_f2are the lowest values the harness observed. A failed solution gets-infon both objectives.- Return type:
- 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) andconfig(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'¶
- to_dict() dict¶
Returns a dictionary representation of the problem including all parameters.
- Returns:
Dictionary representation of the problem.
- Return type:
dict