binary
— Binary Test Problems¶
Common binary test problems.
Warning
Note that the problems in this module are all maximization problems.
Basic Problems¶
-
optproblems.binary.
one_max
(phenome)¶ The bare-bones one-max function.
-
class
optproblems.binary.
OneMax
(num_variables=30, phenome_preprocessor=None, **kwargs)¶ The most simple binary optimization problem.
-
__init__
(num_variables=30, phenome_preprocessor=None, **kwargs)¶ Constructor.
Parameters: - num_variables (int, optional) – The search space dimension.
- phenome_preprocessor (callable, optional) – A callable potentially applying transformations or checks to the phenome. Modifications should only be applied to a copy of the input. The (modified) phenome must be returned. Default behavior is to do no processing.
- kwargs – Arbitrary keyword arguments, passed through to the constructor of the super class.
-
get_locally_optimal_solutions
(max_number=None)¶ Return the optimal solution.
The returned solution does not yet contain the objective values.
Returns: solutions Return type: list of Individual
-
get_optimal_solutions
(max_number=None)¶ Return the optimal solution.
The returned solution does not yet contain the objective values.
Returns: solutions Return type: list of Individual
-
-
optproblems.binary.
leading_ones
(phenome)¶ The bare-bones leading-ones function.
-
class
optproblems.binary.
LeadingOnes
(num_variables=30, phenome_preprocessor=None, **kwargs)¶ Counts the number of contiguous ones from the start of the bit-string.
-
__init__
(num_variables=30, phenome_preprocessor=None, **kwargs)¶ Constructor.
Parameters: - num_variables (int, optional) – The search space dimension.
- phenome_preprocessor (callable, optional) – A callable potentially applying transformations or checks to the phenome. Modifications should only be applied to a copy of the input. The (modified) phenome must be returned. Default behavior is to do no processing.
- kwargs – Arbitrary keyword arguments, passed through to the constructor of the super class.
-
get_locally_optimal_solutions
(max_number=None)¶ Return the optimal solution.
The returned solution does not yet contain the objective values.
Returns: solutions Return type: list of Individual
-
get_optimal_solutions
(max_number=None)¶ Return the optimal solution.
The returned solution does not yet contain the objective values.
Returns: solutions Return type: list of Individual
-
-
optproblems.binary.
trailing_zeros
(phenome)¶ The bare-bones trailing-zeros function.
-
class
optproblems.binary.
LeadingOnesTrailingZeros
(num_variables=30, phenome_preprocessor=None, **kwargs)¶ A bi-objective binary problem.
-
__init__
(num_variables=30, phenome_preprocessor=None, **kwargs)¶ Constructor.
Parameters: - num_variables (int, optional) – The search space dimension.
- phenome_preprocessor (callable, optional) – A callable potentially applying transformations or checks to the phenome. Modifications should only be applied to a copy of the input. The (modified) phenome must be returned. Default behavior is to do no processing.
- kwargs – Arbitrary keyword arguments, passed through to the constructor of the super class.
-
get_optimal_solutions
(max_number=None)¶ Return Pareto-optimal solutions.
The returned solutions do not yet contain the objective values.
Parameters: max_number (int, optional) – Optionally restrict the number of solutions. Returns: solutions – The Pareto-optimal solutions Return type: list of Individual
-
Binary Multiple-Peaks Problems¶
-
optproblems.binary.
hamming_dist
(bitstring1, bitstring2)¶ Hamming distance.
-
class
optproblems.binary.
Peak
(position=None, slope=1.0, offset=0.0)¶ Helper class maintaining the data structures needed for one peak.
Inherits from
list
. The stored data is the position.-
__init__
(position=None, slope=1.0, offset=0.0)¶ Constructor.
Parameters: - position (sequence, optional) – A point in the search space that will attain the maximum of this peak. If omitted, a position will be drawn random uniformly in the search space.
- slope (float, optional) – The slope of this peak.
- offset (float, optional) – The offset of this peak.
-
-
class
optproblems.binary.
PeakProblem
(num_variables=10, peaks=None, **kwargs)¶ Abstract base class for binary multiple-peaks problems.
-
__init__
(num_variables=10, peaks=None, **kwargs)¶ Constructor.
Parameters: - num_variables (int, optional) – The search space dimension.
- peaks (sequence of Peak) – Previously prepared peaks. If None, a few peaks are generated randomly.
- kwargs – Arbitrary keyword arguments, passed through to the constructor of the super class.
-
get_closest_peaks
(phenome)¶ Return all closest peaks in terms of hamming distance to phenome.
Parameters: phenome (iterable) – The solution to which the distances are computed. Returns: closest_peaks Return type: list of Peak
-
get_locally_optimal_solutions
(max_number=None)¶ Return locally optimal solutions (includes global ones).
Parameters: max_number (int, optional) – Potentially restrict the number of optima. Returns: optima Return type: list of Individual
-
get_optimal_solutions
(max_number=None)¶ Return globally optimal solutions.
Parameters: max_number (int, optional) – Potentially restrict the number of optima. Returns: optima Return type: list of Individual
-
classmethod
rand_uniform_peaks
(num_peaks=2, num_variables=10, slope_range=(1.0, 1.0), offset_range=(0.0, 0.0))¶ Create peaks with random uniform distribution.
Parameters: - num_peaks (int, optional) – The number of peaks to generate.
- num_variables (int, optional) – The number of decision variables of the search space.
- slope_range (tuple of float, optional) – The slope parameter of peaks is drawn random uniformly from this range.
- offset_range (tuple of float, optional) – The offset parameter of peaks is drawn random uniformly from this range.
Returns: peaks
Return type: list of Peak
-
-
class
optproblems.binary.
NearestPeakProblem
(num_variables=10, peaks=None, **kwargs)¶ A binary test problem with a controllable number of local optima.
In this problem the nearest peak is responsible for the objective value of a solution. The mathematical definition can be found in [Jansen2016].
References
[Jansen2016] (1, 2, 3) Thomas Jansen; Christine Zarges (2016). Example Landscapes to Support Analysis of Multimodal Optimisation. In: Parallel Problem Solving from Nature - PPSN XIV, pp. 792-802, Springer. https://dx.doi.org/10.1007/978-3-319-45823-6_74 -
get_active_peak
(phenome)¶ Return the peak modeling the function at phenome.
-
get_basin
(phenome)¶ Return the peak modeling the function at phenome.
-
-
class
optproblems.binary.
WeightedNearestPeakProblem
(num_variables=10, peaks=None, **kwargs)¶ A multimodal binary test problem.
The objective value is determined by the peak with the highest function value at a given position. This approach is analogous to
optproblems.mpm.MultiplePeaksModel2
. The mathematical definition can be found in [Jansen2016].-
get_active_peak
(phenome)¶ Return the peak modeling the function at phenome.
-
get_basin
(phenome)¶ Return the peak in whose attraction basin phenome is located.
For this problem, the attraction basin may consist of several overlaid peaks. This method only yields an approximation, i.e., the returned peak may be not the one an ideal steepest descent algorithm would converge to.
-
-
class
optproblems.binary.
AllPeaksProblem
(num_variables=10, peaks=None, **kwargs)¶ A multimodal binary test problem.
In this case the landscape is generated by averaging all the peaks. Thus, the local optima of this problem are unknown and global optima are only known in the special case of identical slopes for all peaks. The mathematical definition can be found in [Jansen2016].
Helpers¶
-
class
optproblems.binary.
BinaryChecker
(num_variables=None, previous_preprocessor=None)¶ A pre-processor for checking if a phenome is binary.
Note
This class makes use of the decorator design pattern for potential chaining of pre-processors, see https://en.wikipedia.org/wiki/Decorator_pattern
-
__call__
(phenome)¶ Check constraints and raise exception if necessary.
-
__init__
(num_variables=None, previous_preprocessor=None)¶ Constructor.
Parameters: - num_variables (int, optional) – Optionally require a specific number of phenes.
- previous_preprocessor (callable, optional) – Another callable that processes the phenome before this one does.
-
Example¶
Obtaining the known optimum of a unimodal test problem.
import random
from optproblems import *
from optproblems.binary import OneMax
# the binary test problem OneMax counts the ones in the bitstring
onemax = OneMax(10)
global_optima = onemax.get_optimal_solutions()
# the known optima are not evaluated by default
for opt in global_optima:
print(opt.phenome, opt.objective_values)
print(onemax.consumed_evaluations, onemax.remaining_evaluations)
onemax.batch_evaluate(global_optima)
print("after evaluation:")
for opt in global_optima:
print(opt.phenome, opt.objective_values)
print(onemax.consumed_evaluations, onemax.remaining_evaluations)
rand_solution = Individual(phenome=[random.choice((0, 1)) for _ in range(10)])
onemax.evaluate(rand_solution)
print("after another evaluation:")
print(rand_solution.phenome, rand_solution.objective_values)
print(onemax.consumed_evaluations, onemax.remaining_evaluations)
# the global optimum is also a local one
local_optima = onemax.get_locally_optimal_solutions()
for opt in local_optima:
print(opt.phenome, opt.objective_values)