kaiwu.common package#
Module contents#
tool collection
- kaiwu.common.hamiltonian(ising_matrix, c_list)[source]#
Calculating the Hamiltonian
- Args:
ising_matrix (np.ndarray): Ising matrix.
c_list (np.ndarray): The set of variable combinations for which the Hamiltonian is to be computed.
- Returns:
np.ndarray: Set of Hamiltonians.
- Examples:
>>> import numpy as np >>> from kaiwu.common import hamiltonian >>> ising_matrix = -np.array([[ 0. , 1. , 0. , 1. , 1. ], ... [ 1. , 0. , 0. , 1., 1. ], ... [ 0. , 0. , 0. , 1., 1. ], ... [ 1. , 1., 1. , 0. , 1. ], ... [ 1. , 1., 1. , 1. , 0. ]]) >>> c_list = np.array([[1, 1, 1, 1, 1], [1, -1, 1, -1, 1]]) >>> h = hamiltonian(ising_matrix, c_list)
- kaiwu.common.check_symmetric(mat, tolerance=1e-08)[source]#
Check if the matrix is a symmetric matrix, allowing for a certain margin of error.
- kaiwu.common.set_log_level(level)[source]#
Set SDK log output level
The SDK outputs logs above the info level by default, and the output level can be modified through this function
- Args:
Level (str or int): supports logging. ERROR, logging.INFO,…, logging. ERROR or string ERROR, INFO,…, ERROR, case insensitive
- Examples:
>>> import kaiwu as kw >>> kw.common.set_log_level(level="DEBUG")
- kaiwu.common.set_log_path(path='/tmp/output.log')[source]#
Set the path to the SDK log output file; an absolute path is required.
- Args:
path (str): Custom log file output path
- Examples:
>>> import kaiwu as kw >>> kw.common.set_log_path("/tmp/output.log")
- class kaiwu.common.CheckpointManager[source]#
Bases:
objectManage checkpoints, saving the running state of objects for subsequent resumption from breakpoints. The directory for saving checkpoints can be specified by setting CheckpointManager.save_dir.
- Args:
save_dir (str): The directory where the checkpoint is saved.
- save_dir = None#
- classmethod get_path(obj)[source]#
Get the path to the object checkpoint
- Args:
obj (Object): The object to be stored
- Returns:
str: checkpoint path
- class kaiwu.common.BaseLoopController(max_repeat_step=inf, target_objective=-inf, no_improve_limit=inf, iterate_per_update=5)[source]#
Bases:
JsonSerializableMixinA loop controller is used to calculate the time. This is for debugging and testing the algorithm.
- Args:
max_repeat_step: Maximum number of steps, default value is math.inf.
target_objective: The target optimization function; optimization stops once the target is reached. The default value is -math.inf.
no_improve_limit: Convergence condition, specifies the number of updates to stop if no improvement is achieved. The default value is in math.inf.
iterate_per_update: The number of times to run before each update of the Hamiltonian, the default value is 5
- update_status(objective, unsatisfied_constraints_count=None)[source]#
Update status after calculating subissues
- Args:
Objective (float): objective function value
unsatisfied_constraints_count (int): number of unmet constraints
- to_json_dict(exclude_fields=('timer',))[source]#
Convert to JSON dictionary
- Returns:
Dict: json dictionary
- load_json_dict(json_dict)#
The dict recovery object read from the JSON file
- Returns:
Dict: json dictionary
- class kaiwu.common.OptimizerLoopController(max_repeat_step=inf, target_objective=-inf, no_improve_limit=20000, iterate_per_update=5)[source]#
Bases:
BaseLoopControllerOptimizer loop controller and calculate time. For debugging and testing algorithms
- Args:
max_repeat_step: Maximum number of steps, default value is math.inf.
target_objective: The target optimization function; optimization stops once the target is reached. The default value is -math.inf.
no_improve_limit: Convergence condition, stop if the specified number of updates is not improved, the default value is 20000
iterate_per_update: The number of times to run before each update of the Hamiltonian, the default value is 5
- is_finished()#
Determine whether to stop
- Returns:
Bool: Should we stop?
- load_json_dict(json_dict)#
The dict recovery object read from the JSON file
- Returns:
Dict: json dictionary
- restart()#
Reinitialize count
- to_json_dict(exclude_fields=('timer',))#
Convert to JSON dictionary
- Returns:
Dict: json dictionary
- update_status(objective, unsatisfied_constraints_count=None)#
Update status after calculating subissues
- Args:
Objective (float): objective function value
unsatisfied_constraints_count (int): number of unmet constraints
- class kaiwu.common.SolverLoopController(max_repeat_step=inf, target_objective=-inf, no_improve_limit=inf, iterate_per_update=5, stop_after_feasible_count=None)[source]#
Bases:
BaseLoopController,JsonSerializableMixinSolver loop controller and calculate time. For debugging and testing algorithms
- Args:
max_repeat_step (int): Maximum number of steps, default is math.inf
target_objective (float): Target optimization function, stop when reached, default value is -math.inf
no_improve_limit (int): Convergence condition, stop if the specified number of updates does not improve, the default value is 20000
iterate_per_update (int): The number of times to run before each update of the Hamiltonian, the default value is 5
stop_after_feasible_count (int): Stop after finding the specified number of feasible solutions.
- update_status(objective, unsatisfied_constraints_count=None)[source]#
Update status after calculating subissues
- Args:
Objective (float): objective function value
unsatisfied_constraints_count (int): number of unmet constraints
- load_json_dict(json_dict)#
The dict recovery object read from the JSON file
- Returns:
Dict: json dictionary
- restart()#
Reinitialize count
- to_json_dict(exclude_fields=('timer',))#
Convert to JSON dictionary
- Returns:
Dict: json dictionary
- class kaiwu.common.JsonSerializableMixin[source]#
Bases:
objectSerializer
- class kaiwu.common.HeapUniquePool(mat, size, size_limit)[source]#
Bases:
JsonSerializableMixinSolution. Use a heap for maintenance