二分回溯算法,通过逻辑和-逻辑函数单元在有效数据集中查找符合逻辑单元判别条件的数据子集
Project description
二分回溯算法
一种节省步数的,效率高的回溯算法。
但是本算法依靠逻辑元,如果运行逻辑元的步数算上的话,并不节省步数。所以,如果有特殊的逻辑与-逻辑或元函数,在运算整体和的时候只需要一步,那本算法带来的优化效率是非常高的。
回溯逻辑
回溯步骤比较复杂,直接看代码可能会好一点
-
定义逻辑与-逻辑或函数单元:
- logic_unit(self, effective_list: list) -> bool: 接收一个列表(有效数据集合)作为参数,遍历每一个元素,存在一个元素不符合定义的逻辑时就返回
False,否则返回True
- logic_unit(self, effective_list: list) -> bool: 接收一个列表(有效数据集合)作为参数,遍历每一个元素,存在一个元素不符合定义的逻辑时就返回
-
进入dichotomy_backtracking_algorithm(effective_list)方法:
-
调用逻辑与-逻辑或函数单元(传参:effective_list):返回
True或者False -
第一次调用:如果返回值为
True,则直接退出(因为所有元素均符合定义逻辑),返回值为False,则将有效列表二分为两个子列表(子列表1,子列表2),将子列表1作为参数返回步骤1 -
非第一次调用:如果返回值为
True,那么该列表内的所有元素均符合定义逻辑,将该列表元素全部加入yes_list,这个时候回溯到它的父列表中的其他子列表中(由于二分法,一个父列表一般就两个子列表,也就是说每一个列表最多只有一个同父同级列表),如果其他子列表返回False,那么继续分割子列表,直到子列表长度小于等于lm(这一个值事实上会影响算法优化程度,因为这个值限制了子列表的最小长度,防止深度优先搜索过度深入造成浪费,也就是说当lm=有效列表长度//3-1的时候,理论上列表最多被分割4次),会把子列表元素逐个穷举,返回True,则加入yes_list,然后回溯,将它的同父同级列表作为参数返回步骤1
-
暂时还没有配图,所以可能不太好理解
下载和调用
pip install dichback
示例程序
from dichback import AlgorithmSet
class Al(AlgorithmSet):
LIST = [i for i in range(1, 100) if i%10 == 0]
def __init__(self):
super().__init__()
def logic_unit(self, effective_list: list) -> bool:
# LIST = [i for i in range(1, 100) if i%2 == 0]
# 也就是2,4,6,8,10,12...98
# 在1,2,3,4..99这个数据样本中LIST的离散程度非常大
# 因为在这个逻辑单元中相当于True,False,True.False...
for i in effective_list:
if not i in self.LIST:
return False
return True
if __name__ == '__main__':
a = Al()
rep = a.dichotomy_backtracking_algorithm([i for i in range(1, 100)])
print(a.counts)
print(rep)
dichback中AlgorithmSet类继承的时候强制要求定义逻辑元:
def logic_unit(self, effective_list: list) -> bool
同时提供self.counts属性,查看逻辑元调用次数
Dichback库的其他算法
这些算法都支持逻辑元,具体请查看代码注释
- 简单穷举法
- 普通二分法
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dichback-0.0.9.tar.gz.
File metadata
- Download URL: dichback-0.0.9.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e207c80ff65e377e7c0d2f0a80d157c501c63d480ad0cb59156e14578b32226
|
|
| MD5 |
620dc4cc4a80e0a94fcd330698da8cdc
|
|
| BLAKE2b-256 |
3113d7a316cb2896b3707944c94f7dcde0ff0818ef6208557765bf1c7e2bf2f0
|
Provenance
The following attestation bundles were made for dichback-0.0.9.tar.gz:
Publisher:
python-publish.yml on SpeechlessMatt/Dichback
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dichback-0.0.9.tar.gz -
Subject digest:
9e207c80ff65e377e7c0d2f0a80d157c501c63d480ad0cb59156e14578b32226 - Sigstore transparency entry: 155455348
- Sigstore integration time:
-
Permalink:
SpeechlessMatt/Dichback@24b4220e94d050a64afd56e432396c272118be53 -
Branch / Tag:
refs/tags/v0.0.9 - Owner: https://github.com/SpeechlessMatt
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@24b4220e94d050a64afd56e432396c272118be53 -
Trigger Event:
release
-
Statement type:
File details
Details for the file dichback-0.0.9-py3-none-any.whl.
File metadata
- Download URL: dichback-0.0.9-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
709e655b1713d82884726c26682f585905d828e512ed13f04d78d547c1755fe8
|
|
| MD5 |
d27134e543b207a8ce6d1b1e65916f1d
|
|
| BLAKE2b-256 |
730fb32ddb3c9ebe7b7e326f53bcd4886546f2e545fea57fed6ede0a665c1ab7
|
Provenance
The following attestation bundles were made for dichback-0.0.9-py3-none-any.whl:
Publisher:
python-publish.yml on SpeechlessMatt/Dichback
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dichback-0.0.9-py3-none-any.whl -
Subject digest:
709e655b1713d82884726c26682f585905d828e512ed13f04d78d547c1755fe8 - Sigstore transparency entry: 155455349
- Sigstore integration time:
-
Permalink:
SpeechlessMatt/Dichback@24b4220e94d050a64afd56e432396c272118be53 -
Branch / Tag:
refs/tags/v0.0.9 - Owner: https://github.com/SpeechlessMatt
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@24b4220e94d050a64afd56e432396c272118be53 -
Trigger Event:
release
-
Statement type: