Skip to content

控制变量组合

在固定变量与组约束下枚举模型设定。

核心代码

py
def control_options(c, cap):
    """递归枚举并按数量约束剪枝,避免先创建完整幂集。"""
    groups, found = c['groups'], []
    suffix = [sum(len(g) for g in groups[i:]) for i in range(len(groups)+1)]
    def visit(i, chosen):
        if len(found) >= cap or len(chosen) > c['max_controls'] or len(chosen)+suffix[i] < c['min_controls']:
            return
        chosen_set=set(chosen) | set(c['fixed_controls'])
        remaining={v for group in groups[i:] for v in group}
        if any(len(chosen_set & set(group))>1 for group in c['exclusive_groups']):
            return
        if any(a in chosen_set and b not in chosen_set and b not in remaining for a,b in c['dependencies']):
            return
        if i == len(groups):
            if controls_valid(c, chosen):
                found.append(sorted(chosen))
            return
        visit(i+1, chosen)
        visit(i+1, chosen+groups[i])
    visit(0, [])
    return found

Released under the AGPL-3.0 License.