QHyper.util#
This module contains utility functions that are used across the project.
Functions
- weighted_avg_evaluation(results, score_function, penalty=0, limit_results=None, normalize=True)[source]#
Calculate weighted average evaluation of results.
Example:
results = solver.solve() score = weighted_avg_evaluation( results.probabilities, solver.problem.get_score, penalty=3, limit_results=100, normalize=True)
- Parameters:
results (np.recarray) – Results to evaluate. It should contain variables and probability.
score_function (Callable[[np.record, float], float]) – Function to evaluate results. Most often it’s a problem’s get_score method.
penalty (float, optional) – Penalty for the constraint violation, by default 0
limit_results (int, optional) – Number of results to evaluate, by default None
normalize (bool, optional) – Normalize the score, by default True, applicable when the limit is set
- Returns:
Weighted average evaluation of results.
- Return type:
float
- sort_solver_results(results, limit_results=None)[source]#
Sort solver results by probability.
Example:
results = solver.solve() sorted_results = sort_solver_results(results.probabilities, 100)
- Parameters:
results (np.recarray) – Results to sort. It should contain variables and probability.
limit_results (int, optional) – Number of results to return, by default None
- Returns:
Sorted results.
- Return type:
np.recarray
- add_evaluation_to_results(results, score_function, penalty=0)[source]#
Add evaluation to results.
Example:
results = solver.solve() add_evaluation_to_results( results.probabilities, solver.problem.get_score)
- Parameters:
results (np.recarray) – Results to evaluate. It should contain variables and probability.
score_function (Callable[[np.record, float], float]) – Function to evaluate results. Most often it’s a problem’s get_score method.
penalty (float, optional) – Penalty for the constraint violation, by default 0
- Returns:
Results with evaluation added. Can be found under ‘evaluation’ key.
- Return type:
np.recarray
- search_for(class_type, path)[source]#
This function searches for classes of a given type in a given path.
If class contains a name attribute, it will be used as a key in the returned dictionary. Otherwise, the class name will be used. Either way, the key will be lowercased.
- Parameters:
class_type (type) – Type of the class to search for e.g. Problem, Solver.
path (str) – Path to the file or directory to search in.
- Returns:
Dictionary of found classes with their names as keys and classes as values.
- Return type:
dict[str, type]