Skip to main content

二分回溯算法,通过逻辑和-逻辑函数单元在有效数据集中查找符合逻辑单元判别条件的数据子集

Project description

二分回溯算法

一种节省步数的,效率高的回溯算法。

回溯逻辑

回溯步骤比较复杂,直接看代码可能会好一点

  • 定义逻辑和-逻辑函数单元:

    • logic_unit(self, effective_list: list) -> bool: 接收一个列表(有效数据集合)作为参数,遍历每一个元素,存在一个元素不符合定义的逻辑时就返回False,否则返回True
  • 进入dichotomy_backtracking_algorithm(effective_list)方法:

    1. 调用逻辑和-逻辑函数单元(传参:effective_list):返回true或者false

    2. 第一次调用:如果返回值为True,则直接退出(因为所有元素均符合定义逻辑),返回值为False,则将有效列表二分为两个子列表(子列表1,子列表2),将子列表1作为参数返回步骤1

    3. 非第一次调用:如果返回值为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.chance)
    print(rep)

dichback中AlgorithmSet类继承的时候强制要求定义逻辑元: logic_unit(self, effective_list: list) -> bool

同时提供self.counts属性,查看逻辑元调用次数

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

dichback-0.0.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dichback-0.0.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file dichback-0.0.1.tar.gz.

File metadata

  • Download URL: dichback-0.0.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for dichback-0.0.1.tar.gz
Algorithm Hash digest
SHA256 af097f7575defea2ded31ff90970228df2c4f2f2f30884df61cd5ca4b1f02434
MD5 6d347d08cfb58a4305fe63aeba54debc
BLAKE2b-256 47f54274561042adbe662588e107254f0667d169c97840a765b5bdb4f1796d1c

See more details on using hashes here.

Provenance

The following attestation bundles were made for dichback-0.0.1.tar.gz:

Publisher: python-publish.yml on SpeechlessMatt/Dichback

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dichback-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: dichback-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for dichback-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 42baefb350a11c28e97d94e0b956d0d26d4bf478d4286509845bce549886f63f
MD5 a6871cb6d40f53cdfe1dc4edfc32d8c0
BLAKE2b-256 469905689b2c39a5feaaeebd0374ed819f9682d3c3b79e75dd3967adf69fd432

See more details on using hashes here.

Provenance

The following attestation bundles were made for dichback-0.0.1-py3-none-any.whl:

Publisher: python-publish.yml on SpeechlessMatt/Dichback

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page