Hyper-heuristic
This module contains the Hyperheuristic class.
Created on Thu Jan 9 15:36:43 2020
@author: Jorge Mario Cruz-Duarte (jcrvz.github.io), e-mail: j.m.cruzduarte@ieee.org
The Hyperheuristic class explores the space of possible metaheuristics to find
the best operator sequence for a given problem. The search is guided by a Simulated
Annealing acceptance criterion.
When TensorFlow is available, the hyper-heuristic can also leverage a neural-network
predictor (see customhys.machine_learning) to bias the operator selection.
Quick usage:
from customhys import benchmark_func as bf
from customhys.hyperheuristic import Hyperheuristic
func = bf.Rastrigin(10)
prob = {
"function": func.get_func_val,
"is_constrained": True,
"boundaries": func.get_search_range(),
}
hh = Hyperheuristic(
heuristic_space="default.txt",
problem=prob,
parameters={
"cardinality": 3,
"num_iterations": 100,
"num_agents": 30,
"num_replicas": 30,
"num_steps": 100,
"stagnation_percentage": 0.3,
"max_temperature": 200,
"cooling_rate": 0.05,
},
)
hh.run()
- class customhys.hyperheuristic.Hyperheuristic(heuristic_space='default.txt', problem=None, parameters=None, file_label='', weights_array=None)[source]
Bases:
objectThis is the Hyperheuristic class, each object corresponds to a hyper-heuristic process implemented with a heuristic collection from Operators to build metaheuristics using the Metaheuristic module.
- __init__(heuristic_space='default.txt', problem=None, parameters=None, file_label='', weights_array=None)[source]
Create a hyper-heuristic process using a operator collection as heuristic space.
- Parameters:
heuristic_space (str) – Optional. The heuristic space or search space collection. It could be a string indicating the file name, assuming it is located in the folder
./collections/, or a list with tuples (check the default collection./collections/default.txt') just likeoperators.build_operatorsgenerates. The default is ‘default.txt’.problem (dict) –
This is a dictionary containing the ‘function’ that maps a 1-by-D array of real values to a real value, ‘is_constrained’ flag that indicates the solution is inside the search space, and the ‘boundaries’ (a tuple with two lists of size D). These two lists correspond to the lower and upper limits of domain, such as:
boundaries = (lower_boundaries, upper_boundaries)Note: Dimensions (D) of search domain are read from these boundaries. The problem can be obtained from the
benchmark_funcmodule.parameters (dict) –
Parameters to implement the hyper-heuristic procedure, the following fields must be provided: ‘cardinality’, ‘num_iterations’, ‘num_agents’, ‘num_replicas’, ‘num_steps’, ‘stagnation_percentage’, ‘max_temperature’, and ‘cooling_rate’. The default is showing next:
- parameters = {cardinality=3, # Max. numb. of SOs in MHs, lvl:1
num_iterations=100, # Iterations a MH performs, lvl:1 num_agents=30, # Agents in population, lvl:1 num_replicas=50, # Replicas per each MH, lvl:2 num_steps=100, # Trials per HH step, lvl:2 stagnation_percentage=0.3, # Stagnation percentage, lvl:2 max_temperature=200, # Initial temperature (SA), lvl:2 cooling_rate=0.05} # Cooling rate (SA), lvl:2
Note: Level (lvl) flag corresponds to the heuristic level of the parameter. lvl:1 concerns to mid-level heuristics like metaheuristics, and lvl:2 to high-level heuristics like hyper-heuristics.
file_label (str) – Optional. Tag or label for saving files. The default is ‘’.
weights_array (numpy.array) – Optional. Weights of the search operators, if there is a-priori information about them. The default is None.
- evaluate_candidate_solution(encoded_sequence, initial_scheme=None)[source]
Evaluate the current sequence as a hyper/meta-heuristic. This process is repeated
parameters['num_replicas']times and, then, the performance is determined. In the end, the method returns the performance value and the details for all the runs. These details arehistorical_data,fitness_data,position_data, andfitness_stats. The elements from theencoded_sequencemust be in the range of thenum_operators. :param list encoded_sequence:Sequence of search operators. These must be in the tuple form (decoded version). Check the
metaheuristicmodule for further information.- Return float, dict:
Performance and raw data.
- brute_force(save_steps=True)[source]
This method performs a brute force procedure solving the problem via all the available search operators without integrating a high-level search method. So, each search operator is used as a 1-cardinality metaheuristic. Results are directly saved as json files. :return: None.
- basic_metaheuristics(save_steps=True)[source]
This method performs a brute force procedure solving the problem via all the predefined metaheuristics in ‘./collections/basicmetaheuristics.txt’. Many of them are 1-cardinality MHs but other are 2-cardinality ones. This process does not require a high-level search method. Results are directly saved as json files. :return: None.
- static get_performance(statistics)[source]
Return the performance from fitness values obtained from running a metaheuristic several times. This method uses the Median and Interquartile Range values for such a purpose:
performance = Med{fitness values} + IQR{fitness values}
Note: If an alternative formula is needed, check the commented options. :param dict statistics: Statistics of the given MH/HH. :return: The computed performance.
- static get_statistics(raw_data)[source]
Return statistics from all the fitness values found after running a metaheuristic several times. The oncoming statistics are
nob(number of observations),Min(minimum),Max(maximum),Avg(average),Std(standard deviation),Skw(skewness),Kur(kurtosis),IQR(interquartile range),Med(median), andMAD(Median absolute deviation). :param list raw_data: List of the fitness values. :return: dict: Statistics computed from the raw data.