# aig-project **Repository Path**: lllique/aig-project ## Basic Information - **Project Name**: aig-project - **Description**: 这是一个分析aig并且对其进行优化的项目 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-06-15 - **Last Updated**: 2026-06-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # AIG Coursework Tool 这是一个面向 AIG/AAG 作业的完整 Python 小项目,包含: - AIG 数据结构:`Node`、`Edge`、`AIG` - 节点/边增删:支持直接边和反相边 - `.aag` 文件解析 - 输入/输出数量、Latch 数量、AND 数量、not(非门数量)、深度统计 - 结构哈希优化:删除输入完全相同的冗余 AND 节点 - 命令行工具:`aig -f xxx.aag` - Web 可视化页面:`aig -w` ## 1. 环境要求 Python 3.9+,不依赖第三方库。 ## 2. 直接运行 ```bash cd aig_project python aig.py -f example/example/small-aig/01-adder.aag python aig.py -f example/example/small-aig/01-adder.aag ``` 输出示例: ```text read_aig example/small-aig/01-adder.aag print_stats pis=2, pos=2, area=4, depth=2, not=4 optimize # removed_and_nodes = 1 print_stats pis=2, pos=2, area=3, depth=2, not=4 ``` 只看优化前: ```bash python aig.py -f example/small-aig/01-adder.aag --no-optimize ``` 输出 JSON: ```bash python aig.py -f example/small-aig/01-adder.aag --json ``` ## 3. 安装为 `aig` 命令 ```bash cd aig_project pip install -e . aig -f example/small-aig/01-adder.aag aig -w ``` ## 4. 打开 Web 可视化服务 ```bash python aig.py -w --host 127.0.0.1 --port 8000 ``` 浏览器打开: ```text http://127.0.0.1:8000/ ``` 也可以启动时预加载某个文件: ```bash python aig.py -w -f examples/problem_example.aag ``` ## 5. 代码结构 ```text aig_project/ ├── aig.py # 免安装启动脚本 ├── pyproject.toml # 安装后提供 aig 命令 ├── README.md ├── aigtool/ │ ├── core.py # Node / Edge / AIG / depth / optimize │ ├── parser.py # AAG 文件解析 │ ├── analyze.py # CLI 和 Web 共用分析入口 │ ├── cli.py # 命令行参数 -f / -w │ ├── web.py # 内置 Web 页面与 API │ └── __main__.py └── example/ └── ....aag ``` ## 6. 优化算法说明 使用结构哈希 Structural Hashing: 1. 遍历所有 AND 节点。 2. 将两个输入 literal 排序,得到规范化 key。 3. 如果 key 第一次出现,则保留该 AND 节点。 4. 如果 key 已经出现,则说明当前节点与之前节点结构等价,把当前节点替换为已有节点。 5. 更新后续 AND 节点和输出引用,删除冗余节点。 例如: ```text 6 2 4 # node3 = input1 & input2 8 4 2 # node4 = input2 & input1 ``` 由于 AND 交换律,两者是同一个逻辑结构,因此可以删除其中一个。 ## 7. 注意事项 - 目前主要支持课程要求中的组合逻辑 AAG 文件。 - 对 Latch 行会跳过并保留 latch 数量,但不建模时序语义。 - `not` 表示非门数量,按“不同的反相 literal”统计:同一个反相信号即使被多次使用,也只计 1 次;统计范围包括 AND 输入和输出 literal。命令行中显示为 `not= 数量`。 - 为了兼容旧版本,JSON 中同时保留 `inverters` 字段;课程展示时重点看 `not` 字段即可。 ## not=? 显示说明 项目中统一使用 `not=?` 表示非门数量,也就是 AAG literal 中所有反相边/反相输出的总数。例如 `not=3` 表示当前 AIG 中共有 3 处反相连接。命令行统计、JSON 字段 `not_text`、Web 统计卡片都会显示这个值。图中的边标签只用于区分“直接边/反相边”,避免和总非门数量混淆。 ## 8. 关于 `not` 统计口径 `not` 不是“反相边出现次数”,而是“不同反相 literal 的数量”。例如同一个 `17` literal 如果被多个 AND 输入复用,仍然只算 1 个 NOT。这个口径与提供的 `s1196.txt`、`s1488.txt`、`z4ml.txt` 等标准结果一致。 命令行输出使用测试文件风格: ```text pis=32, pos=32, area=477, depth=19, not= 361 ```