kaiwu.common package#
Module contents#
通用工具集合
- kaiwu.common.hamiltonian(ising_matrix, c_list)[源代码]#
计算哈密顿量.
- Args:
ising_matrix (np.ndarray): Ising 矩阵.
c_list (np.ndarray): 要计算哈密顿量的变量组合集合.
- Returns:
np.ndarray: 哈密顿量集合.
- 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.set_log_level(level)[源代码]#
设置SDK日志输出级别
SDK默认输出INFO级别以上的日志,可通过此函数修改输出级别
- Args:
level (str or int): 支持传入 logging.ERROR, logging.INFO、... 、logging.ERROR或字符串ERROR、INFO、... 、ERROR
- Examples:
>>> import kaiwu as kw >>> kw.common.set_log_level(level="DEBUG")
- kaiwu.common.set_log_path(path='/tmp/output.log')[源代码]#
设置SDK日志输出文件路径, 需要绝对路径.
- Args:
path (str): 自定义日志文件输出路径
- Examples:
>>> import kaiwu as kw >>> kw.common.set_log_path("/tmp/output.log")
- class kaiwu.common.CheckpointManager[源代码]#
基类:
object管理 checkpoint, 保存对象的运行状态,用于后续断点处恢复运行 通过设置 CheckpointManager.save_dir,可以指定 checkpoint 的保存目录。
- Args:
save_dir (str): checkpoint 的保存目录。
- save_dir = None#
- class kaiwu.common.BaseLoopController(max_repeat_step=inf, target_objective=-inf, no_improve_limit=inf, iterate_per_update=5)[源代码]#
-
循环控制器,并计算时间。用于调试和测试算法
- Args:
max_repeat_step: 最大步数,默认值为math.inf
target_objective:目标优化函数,达到即停止,默认值为-math.inf
no_improve_limit:收敛条件,指定更新次数没有改进则停止,默认值为math.inf
iterate_per_update:每次更新哈密顿量前运行的次数,默认值为5
- update_status(objective, unsatisfied_constraints_count=None)[源代码]#
在计算子问题后更新状态
- Args:
objective (float): 目标函数值
unsatisfied_constraints_count (int): 未满足约束项的数量
- load_json_dict(json_dict)#
从json文件读取的dict恢复对象
- Returns:
dict: json字典
- class kaiwu.common.OptimizerLoopController(max_repeat_step=inf, target_objective=-inf, no_improve_limit=20000, iterate_per_update=5)[源代码]#
-
Optimizer循环控制器,并计算时间。用于调试和测试算法
- Args:
max_repeat_step: 最大步数,默认值为math.inf
target_objective:目标优化函数,达到即停止,默认值为-math.inf
no_improve_limit:收敛条件,指定更新次数没有改进则停止,默认值为20000
iterate_per_update:每次更新哈密顿量前运行的次数,默认值为5
- is_finished()#
判断是否应该停止
- Returns:
bool: 是否应该停止
- load_json_dict(json_dict)#
从json文件读取的dict恢复对象
- Returns:
dict: json字典
- restart()#
重新初始化计数
- to_json_dict(exclude_fields=('timer',))#
转化为json字典
- Returns:
dict: json字典
- update_status(objective, unsatisfied_constraints_count=None)#
在计算子问题后更新状态
- Args:
objective (float): 目标函数值
unsatisfied_constraints_count (int): 未满足约束项的数量
- class kaiwu.common.SolverLoopController(max_repeat_step=inf, target_objective=-inf, no_improve_limit=inf, iterate_per_update=5, stop_after_feasible_count=None)[源代码]#
基类:
BaseLoopController,JsonSerializableMixinSolver循环控制器,并计算时间。用于调试和测试算法
- Args:
max_repeat_step (int): 最大步数,默认值为math.inf
target_objective (float): 目标优化函数,达到即停止,默认值为-math.inf
no_improve_limit (int): 收敛条件,指定更新次数没有改进则停止,默认值为20000
iterate_per_update (int): 每次更新哈密顿量前运行的次数,默认值为5
stop_after_feasible_count (int): 找到指定数量的可行解后停止
- update_status(objective, unsatisfied_constraints_count=None)[源代码]#
在计算子问题后更新状态
- Args:
objective (float): 目标函数值
unsatisfied_constraints_count (int): 未满足约束项的数量
- load_json_dict(json_dict)#
从json文件读取的dict恢复对象
- Returns:
dict: json字典
- restart()#
重新初始化计数
- to_json_dict(exclude_fields=('timer',))#
转化为json字典
- Returns:
dict: json字典