分离算法

This commit is contained in:
2025-10-23 23:45:40 +08:00
parent 3fef587ff5
commit 809a6dbe75
9 changed files with 181 additions and 81 deletions

View File

@@ -0,0 +1,33 @@
from typing import Dict, Any, Callable, TypedDict
class AlgorithmConfig(TypedDict):
algo_name: str
defaults: Dict[str, Any]
revisor: Callable[[dict, int, bool], None]
_algorithms: Dict[str, AlgorithmConfig] = {}
def register_algorithm(algo_name: str, defaults: Dict[str, Any], revisor: Callable):
_algorithms[algo_name] = {
'algo_name': algo_name,
'defaults': defaults,
'revisor': revisor
}
def get_algorithm(algo_name: str) -> AlgorithmConfig:
if algo_name not in _algorithms:
raise ValueError(f"算法 {algo_name}' 未找到, 可用值: {list_algorithms()}")
return _algorithms[algo_name]
def list_algorithms() -> list[str]:
return list(_algorithms.keys())
# 导入注册
from . import supermemo2
register_algorithm(
algo_name=supermemo2.algo_name,
defaults=supermemo2.defaults,
revisor=supermemo2.revisor
)
__all__ = ['get_algorithm', 'list_algorithms', 'register_algorithm', 'AlgorithmConfig']

View File

@@ -0,0 +1,66 @@
import heurams.services.timer as timer
from typing import TypedDict
algo_name = "supermemo2"
class AlgodataDict(TypedDict):
efactor: float
real_rept: int
rept: int
interval: int
last_date: int
next_date: int
is_activated: int
last_modify: float
defaults = {
'efactor': 2.5,
'real_rept': 0,
'rept': 0,
'interval': 0,
'last_date': 0,
'next_date': 0,
'is_activated': 0,
'last_modify': timer.get_timestamp()
}
def revisor(algodata: dict, feedback: int = 5, is_new_activation: bool = False):
"""SM-2 算法迭代决策机制实现
根据 quality(0 ~ 5) 进行参数迭代最佳间隔
quality 由主程序评估
Args:
quality (int): 记忆保留率量化参数
"""
if feedback == -1:
return
algodata[algo_name]['efactor'] = algodata[algo_name]['efactor'] + (
0.1 - (5 - feedback) * (0.08 + (5 - feedback) * 0.02)
)
algodata[algo_name]['efactor'] = max(1.3, algodata[algo_name]['efactor'])
if feedback < 3:
algodata[algo_name]['rept'] = 0
algodata[algo_name]['interval'] = 0
else:
algodata[algo_name]['rept'] += 1
algodata[algo_name]['real_rept'] += 1
if is_new_activation:
algodata[algo_name]['rept'] = 0
algodata[algo_name]['efactor'] = 2.5
if algodata[algo_name]['rept'] == 0:
algodata[algo_name]['interval'] = 1
elif algodata[algo_name]['rept'] == 1:
algodata[algo_name]['interval'] = 6
else:
algodata[algo_name]['interval'] = round(
algodata[algo_name]['interval'] * algodata[algo_name]['efactor']
)
algodata[algo_name]['last_date'] = timer.get_daystamp()
algodata[algo_name]['next_date'] = timer.get_daystamp() + algodata[algo_name]['interval']
algodata[algo_name]['last_modify'] = timer.get_timestamp()