decision-tree-diagrams 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- decision_tree_diagrams-0.2.0/LICENSE +21 -0
- decision_tree_diagrams-0.2.0/MANIFEST.in +1 -0
- decision_tree_diagrams-0.2.0/PKG-INFO +154 -0
- decision_tree_diagrams-0.2.0/README.md +120 -0
- decision_tree_diagrams-0.2.0/examples/delivery-cost.json +22 -0
- decision_tree_diagrams-0.2.0/examples/delivery-cost.png +0 -0
- decision_tree_diagrams-0.2.0/examples/inspect-or-replace.json +37 -0
- decision_tree_diagrams-0.2.0/examples/inspect-or-replace.png +0 -0
- decision_tree_diagrams-0.2.0/examples/market-payoff.json +22 -0
- decision_tree_diagrams-0.2.0/examples/market-payoff.png +0 -0
- decision_tree_diagrams-0.2.0/examples/supplier-selection.png +0 -0
- decision_tree_diagrams-0.2.0/examples/supplier-selection.py +22 -0
- decision_tree_diagrams-0.2.0/pyproject.toml +22 -0
- decision_tree_diagrams-0.2.0/setup.cfg +4 -0
- decision_tree_diagrams-0.2.0/src/decision_tree_diagrams/__init__.py +13 -0
- decision_tree_diagrams-0.2.0/src/decision_tree_diagrams/__main__.py +4 -0
- decision_tree_diagrams-0.2.0/src/decision_tree_diagrams/cli.py +38 -0
- decision_tree_diagrams-0.2.0/src/decision_tree_diagrams/drawing.py +249 -0
- decision_tree_diagrams-0.2.0/src/decision_tree_diagrams/jsonio.py +53 -0
- decision_tree_diagrams-0.2.0/src/decision_tree_diagrams/model.py +125 -0
- decision_tree_diagrams-0.2.0/src/decision_tree_diagrams/table.py +177 -0
- decision_tree_diagrams-0.2.0/src/decision_tree_diagrams.egg-info/PKG-INFO +154 -0
- decision_tree_diagrams-0.2.0/src/decision_tree_diagrams.egg-info/SOURCES.txt +29 -0
- decision_tree_diagrams-0.2.0/src/decision_tree_diagrams.egg-info/dependency_links.txt +1 -0
- decision_tree_diagrams-0.2.0/src/decision_tree_diagrams.egg-info/entry_points.txt +2 -0
- decision_tree_diagrams-0.2.0/src/decision_tree_diagrams.egg-info/requires.txt +1 -0
- decision_tree_diagrams-0.2.0/src/decision_tree_diagrams.egg-info/top_level.txt +1 -0
- decision_tree_diagrams-0.2.0/tests/test_drawing.py +52 -0
- decision_tree_diagrams-0.2.0/tests/test_io.py +71 -0
- decision_tree_diagrams-0.2.0/tests/test_table.py +72 -0
- decision_tree_diagrams-0.2.0/tests/test_tree.py +43 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ManavA
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
recursive-include examples *.json *.png *.py
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: decision-tree-diagrams
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Evaluate and draw decision trees and weighted decision tables with Matplotlib.
|
|
5
|
+
License: MIT License
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2026 ManavA
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
|
|
27
|
+
Project-URL: Repository, https://github.com/ManavA/decision-tree-diagrams
|
|
28
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
29
|
+
Requires-Python: >=3.10
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
License-File: LICENSE
|
|
32
|
+
Requires-Dist: matplotlib>=3.7
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
# Decision Tree Diagrams
|
|
36
|
+
|
|
37
|
+
Draw decision trees and calculate expected costs or payoffs by backward induction. Use weighted decision tables to rank alternatives against a common set of criteria.
|
|
38
|
+
|
|
39
|
+
Squares mark decisions, circles mark chance nodes, and dots mark outcomes. A node's value summarizes the costs or payoffs from that point onward.
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
With Python 3.10 or later, install the GitHub release with:
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
python -m pip install "git+https://github.com/ManavA/decision-tree-diagrams.git@v0.2.0"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
To run the examples, clone the repository and run the commands below from its root.
|
|
50
|
+
|
|
51
|
+
## Command line
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
decision-tree-diagrams evaluate examples/delivery-cost.json
|
|
55
|
+
decision-tree-diagrams render examples/delivery-cost.json delivery.svg --units USD
|
|
56
|
+
decision-tree-diagrams render examples/market-payoff.json market.pdf --objective max --style modern
|
|
57
|
+
decision-tree-diagrams render examples/inspect-or-replace.json pump.png --style top-down
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The CLI writes a diagram in the format given by the output extension. Use SVG or PDF for vector output or PNG for raster output. Use `--title` to add a heading and `--units` to label numeric values. Units beginning with `$` use a dollar prefix and up to two decimal places, so `--units '$M'` displays amounts such as `$9.71M`. The `evaluate` command emits structured JSON. Both commands take `--objective min` (the default) or `--objective max`.
|
|
61
|
+
|
|
62
|
+
## Examples and styles
|
|
63
|
+
|
|
64
|
+
### Delivery cost · classic
|
|
65
|
+
|
|
66
|
+
A courier charges a flat fee. In-house delivery adds a dispatch fee and incurs costs based on traffic. The minimum expected cost is 86.50 USD. Classic uses orthogonal branches and marks unselected actions with double slashes. All styles show optimal actions with thicker lines, draw zero-probability outcomes with dashes, and align terminal values for comparison.
|
|
67
|
+
|
|
68
|
+

|
|
69
|
+
|
|
70
|
+
### Market payoff · modern
|
|
71
|
+
|
|
72
|
+
A reserved sales booth brings in a fixed amount. The outdoor stall pays a setup fee and sales depend on the weather. The maximum expected payoff is 208.50 USD. Modern uses diagonal branches and a green accent.
|
|
73
|
+
|
|
74
|
+

|
|
75
|
+
|
|
76
|
+
### Inspect or replace · top-down
|
|
77
|
+
|
|
78
|
+
A pump can be replaced immediately or inspected first. If inspection finds only wear, a second decision compares repair and replacement. The minimum expected cost is 131 USD. Top-down uses a vertical layout with the initial decision at the top.
|
|
79
|
+
|
|
80
|
+

|
|
81
|
+
|
|
82
|
+
## JSON format
|
|
83
|
+
|
|
84
|
+
Start with any JSON example under `examples/` and edit its labels, values, and branches. The file contains one root node, with child nodes nested inside branches.
|
|
85
|
+
|
|
86
|
+
| Node type | Required fields |
|
|
87
|
+
| --- | --- |
|
|
88
|
+
| `decision` | `type`, `label`, `branches` |
|
|
89
|
+
| `chance` | `type`, `label`, `branches` |
|
|
90
|
+
| `terminal` | `type`, `label`, `value` |
|
|
91
|
+
|
|
92
|
+
Each decision or chance node has a nonempty `branches` array. Each branch has a `child` node and may have a string `label` or numeric `value` (default zero). Chance branches also require `probability`, a number between zero and one. Probabilities from the same chance node must sum to one, within an absolute tolerance of 0.000000001. Probabilities can be zero. Unknown fields and nonfinite numeric values are rejected.
|
|
93
|
+
|
|
94
|
+
A terminal value is the cost or payoff at that outcome. A branch value is a one-time increment on that edge, added to its child’s expected value. Enter each cost or payoff once, on the edge where it is incurred or at a terminal outcome. Use positive costs when minimizing and negative expenses when maximizing net payoff.
|
|
95
|
+
|
|
96
|
+
## Python
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from decision_tree_diagrams import Branch, Chance, Decision, Terminal, evaluate, render
|
|
100
|
+
|
|
101
|
+
tree = Decision("Ship a parcel", [
|
|
102
|
+
Branch(Terminal("Courier quote", 95), label="Courier"),
|
|
103
|
+
Branch(Chance("Traffic", [
|
|
104
|
+
Branch(Terminal("Normal", 55), probability=0.7),
|
|
105
|
+
Branch(Terminal("Heavy", 120), probability=0.3),
|
|
106
|
+
]), value=12, label="In-house"),
|
|
107
|
+
])
|
|
108
|
+
|
|
109
|
+
result = evaluate(tree)
|
|
110
|
+
print(result.expected_value) # 86.5
|
|
111
|
+
print(result.chosen_edges) # (1,)
|
|
112
|
+
|
|
113
|
+
fig = render(tree, style="classic", units="USD", title="Delivery cost")
|
|
114
|
+
fig.savefig("delivery.pdf")
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
`load_json(path)` validates a JSON file and returns its root node for use with either function. `render` returns a Matplotlib `Figure`. Set `figure_size=(width, height)` in inches to fit a particular page or expand space for long labels.
|
|
118
|
+
|
|
119
|
+
`evaluate` returns an `Evaluation` with `expected_value`, `chosen_edges` (zero-based indices for optimal decision branches), `children` (nested evaluations in branch order), and `branch_values` (child expected values including their incoming increments). Decision ties use absolute and relative tolerances of 0.000000000001. The result retains every tied choice and every child, including zero-probability outcomes.
|
|
120
|
+
|
|
121
|
+
## Weighted decision tables
|
|
122
|
+
|
|
123
|
+
When several options are judged against the same criteria, assign a weight to each criterion and a rating from 0 to 100 to each option. Higher ratings are better. For a criterion such as cost, map lower costs to higher ratings before passing them in.
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
from decision_tree_diagrams import Alternative, Criterion, render_table, score_table
|
|
127
|
+
|
|
128
|
+
criteria = [Criterion("Reliability", 3), Criterion("Convenience", 1)]
|
|
129
|
+
alternatives = [
|
|
130
|
+
Alternative("Standard", [60, 100]),
|
|
131
|
+
Alternative("Premium", [80, 80]),
|
|
132
|
+
Alternative("Unavailable", [95, 95], feasible=False),
|
|
133
|
+
]
|
|
134
|
+
for row in score_table(criteria, alternatives):
|
|
135
|
+
print(row.alternative.label, row.score, row.rank)
|
|
136
|
+
# Premium 80.0 1
|
|
137
|
+
# Standard 70.0 2
|
|
138
|
+
# Unavailable 95.0 None
|
|
139
|
+
|
|
140
|
+
render_table(criteria, alternatives, title="Compare suppliers").savefig("suppliers.pdf")
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Weights are relative and must be nonnegative, with at least one positive weight. Give each alternative one rating per criterion, in criterion order. The weighted score is the sum of each rating multiplied by its normalized weight. Feasible options are ranked by score. Scores within 0.000000001 points of the highest score in the tie group share a rank and retain input order. For example, two options tied for first are followed by third place. Infeasible options keep their scores, have a rank of `None`, and follow the ranked options in input order.
|
|
144
|
+
|
|
145
|
+
The table prints each rating, the normalized criterion weights, the weighted scores, ranks, and feasibility. Run `python examples/supplier-selection.py` to rank the suppliers below and save a PDF.
|
|
146
|
+
|
|
147
|
+

|
|
148
|
+
|
|
149
|
+
## Development
|
|
150
|
+
|
|
151
|
+
```sh
|
|
152
|
+
python -m pip install -e .
|
|
153
|
+
python -m unittest discover -s tests
|
|
154
|
+
```
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# Decision Tree Diagrams
|
|
2
|
+
|
|
3
|
+
Draw decision trees and calculate expected costs or payoffs by backward induction. Use weighted decision tables to rank alternatives against a common set of criteria.
|
|
4
|
+
|
|
5
|
+
Squares mark decisions, circles mark chance nodes, and dots mark outcomes. A node's value summarizes the costs or payoffs from that point onward.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
With Python 3.10 or later, install the GitHub release with:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
python -m pip install "git+https://github.com/ManavA/decision-tree-diagrams.git@v0.2.0"
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
To run the examples, clone the repository and run the commands below from its root.
|
|
16
|
+
|
|
17
|
+
## Command line
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
decision-tree-diagrams evaluate examples/delivery-cost.json
|
|
21
|
+
decision-tree-diagrams render examples/delivery-cost.json delivery.svg --units USD
|
|
22
|
+
decision-tree-diagrams render examples/market-payoff.json market.pdf --objective max --style modern
|
|
23
|
+
decision-tree-diagrams render examples/inspect-or-replace.json pump.png --style top-down
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The CLI writes a diagram in the format given by the output extension. Use SVG or PDF for vector output or PNG for raster output. Use `--title` to add a heading and `--units` to label numeric values. Units beginning with `$` use a dollar prefix and up to two decimal places, so `--units '$M'` displays amounts such as `$9.71M`. The `evaluate` command emits structured JSON. Both commands take `--objective min` (the default) or `--objective max`.
|
|
27
|
+
|
|
28
|
+
## Examples and styles
|
|
29
|
+
|
|
30
|
+
### Delivery cost · classic
|
|
31
|
+
|
|
32
|
+
A courier charges a flat fee. In-house delivery adds a dispatch fee and incurs costs based on traffic. The minimum expected cost is 86.50 USD. Classic uses orthogonal branches and marks unselected actions with double slashes. All styles show optimal actions with thicker lines, draw zero-probability outcomes with dashes, and align terminal values for comparison.
|
|
33
|
+
|
|
34
|
+

|
|
35
|
+
|
|
36
|
+
### Market payoff · modern
|
|
37
|
+
|
|
38
|
+
A reserved sales booth brings in a fixed amount. The outdoor stall pays a setup fee and sales depend on the weather. The maximum expected payoff is 208.50 USD. Modern uses diagonal branches and a green accent.
|
|
39
|
+
|
|
40
|
+

|
|
41
|
+
|
|
42
|
+
### Inspect or replace · top-down
|
|
43
|
+
|
|
44
|
+
A pump can be replaced immediately or inspected first. If inspection finds only wear, a second decision compares repair and replacement. The minimum expected cost is 131 USD. Top-down uses a vertical layout with the initial decision at the top.
|
|
45
|
+
|
|
46
|
+

|
|
47
|
+
|
|
48
|
+
## JSON format
|
|
49
|
+
|
|
50
|
+
Start with any JSON example under `examples/` and edit its labels, values, and branches. The file contains one root node, with child nodes nested inside branches.
|
|
51
|
+
|
|
52
|
+
| Node type | Required fields |
|
|
53
|
+
| --- | --- |
|
|
54
|
+
| `decision` | `type`, `label`, `branches` |
|
|
55
|
+
| `chance` | `type`, `label`, `branches` |
|
|
56
|
+
| `terminal` | `type`, `label`, `value` |
|
|
57
|
+
|
|
58
|
+
Each decision or chance node has a nonempty `branches` array. Each branch has a `child` node and may have a string `label` or numeric `value` (default zero). Chance branches also require `probability`, a number between zero and one. Probabilities from the same chance node must sum to one, within an absolute tolerance of 0.000000001. Probabilities can be zero. Unknown fields and nonfinite numeric values are rejected.
|
|
59
|
+
|
|
60
|
+
A terminal value is the cost or payoff at that outcome. A branch value is a one-time increment on that edge, added to its child’s expected value. Enter each cost or payoff once, on the edge where it is incurred or at a terminal outcome. Use positive costs when minimizing and negative expenses when maximizing net payoff.
|
|
61
|
+
|
|
62
|
+
## Python
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from decision_tree_diagrams import Branch, Chance, Decision, Terminal, evaluate, render
|
|
66
|
+
|
|
67
|
+
tree = Decision("Ship a parcel", [
|
|
68
|
+
Branch(Terminal("Courier quote", 95), label="Courier"),
|
|
69
|
+
Branch(Chance("Traffic", [
|
|
70
|
+
Branch(Terminal("Normal", 55), probability=0.7),
|
|
71
|
+
Branch(Terminal("Heavy", 120), probability=0.3),
|
|
72
|
+
]), value=12, label="In-house"),
|
|
73
|
+
])
|
|
74
|
+
|
|
75
|
+
result = evaluate(tree)
|
|
76
|
+
print(result.expected_value) # 86.5
|
|
77
|
+
print(result.chosen_edges) # (1,)
|
|
78
|
+
|
|
79
|
+
fig = render(tree, style="classic", units="USD", title="Delivery cost")
|
|
80
|
+
fig.savefig("delivery.pdf")
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
`load_json(path)` validates a JSON file and returns its root node for use with either function. `render` returns a Matplotlib `Figure`. Set `figure_size=(width, height)` in inches to fit a particular page or expand space for long labels.
|
|
84
|
+
|
|
85
|
+
`evaluate` returns an `Evaluation` with `expected_value`, `chosen_edges` (zero-based indices for optimal decision branches), `children` (nested evaluations in branch order), and `branch_values` (child expected values including their incoming increments). Decision ties use absolute and relative tolerances of 0.000000000001. The result retains every tied choice and every child, including zero-probability outcomes.
|
|
86
|
+
|
|
87
|
+
## Weighted decision tables
|
|
88
|
+
|
|
89
|
+
When several options are judged against the same criteria, assign a weight to each criterion and a rating from 0 to 100 to each option. Higher ratings are better. For a criterion such as cost, map lower costs to higher ratings before passing them in.
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from decision_tree_diagrams import Alternative, Criterion, render_table, score_table
|
|
93
|
+
|
|
94
|
+
criteria = [Criterion("Reliability", 3), Criterion("Convenience", 1)]
|
|
95
|
+
alternatives = [
|
|
96
|
+
Alternative("Standard", [60, 100]),
|
|
97
|
+
Alternative("Premium", [80, 80]),
|
|
98
|
+
Alternative("Unavailable", [95, 95], feasible=False),
|
|
99
|
+
]
|
|
100
|
+
for row in score_table(criteria, alternatives):
|
|
101
|
+
print(row.alternative.label, row.score, row.rank)
|
|
102
|
+
# Premium 80.0 1
|
|
103
|
+
# Standard 70.0 2
|
|
104
|
+
# Unavailable 95.0 None
|
|
105
|
+
|
|
106
|
+
render_table(criteria, alternatives, title="Compare suppliers").savefig("suppliers.pdf")
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Weights are relative and must be nonnegative, with at least one positive weight. Give each alternative one rating per criterion, in criterion order. The weighted score is the sum of each rating multiplied by its normalized weight. Feasible options are ranked by score. Scores within 0.000000001 points of the highest score in the tie group share a rank and retain input order. For example, two options tied for first are followed by third place. Infeasible options keep their scores, have a rank of `None`, and follow the ranked options in input order.
|
|
110
|
+
|
|
111
|
+
The table prints each rating, the normalized criterion weights, the weighted scores, ranks, and feasibility. Run `python examples/supplier-selection.py` to rank the suppliers below and save a PDF.
|
|
112
|
+
|
|
113
|
+

|
|
114
|
+
|
|
115
|
+
## Development
|
|
116
|
+
|
|
117
|
+
```sh
|
|
118
|
+
python -m pip install -e .
|
|
119
|
+
python -m unittest discover -s tests
|
|
120
|
+
```
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "decision",
|
|
3
|
+
"label": "Choose delivery method",
|
|
4
|
+
"branches": [
|
|
5
|
+
{
|
|
6
|
+
"label": "Courier",
|
|
7
|
+
"child": {"type": "terminal", "label": "Quoted fee", "value": 95}
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"label": "In-house",
|
|
11
|
+
"value": 12,
|
|
12
|
+
"child": {
|
|
13
|
+
"type": "chance",
|
|
14
|
+
"label": "Traffic",
|
|
15
|
+
"branches": [
|
|
16
|
+
{"probability": 0.7, "child": {"type": "terminal", "label": "Normal traffic", "value": 55}},
|
|
17
|
+
{"probability": 0.3, "child": {"type": "terminal", "label": "Heavy traffic", "value": 120}}
|
|
18
|
+
]
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
]
|
|
22
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "decision",
|
|
3
|
+
"label": "Service a pump",
|
|
4
|
+
"branches": [
|
|
5
|
+
{
|
|
6
|
+
"label": "Replace now",
|
|
7
|
+
"child": {"type": "terminal", "label": "New pump", "value": 220}
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"label": "Inspect",
|
|
11
|
+
"value": 35,
|
|
12
|
+
"child": {
|
|
13
|
+
"type": "chance",
|
|
14
|
+
"label": "Inspection result",
|
|
15
|
+
"branches": [
|
|
16
|
+
{
|
|
17
|
+
"label": "Wear only",
|
|
18
|
+
"probability": 0.8,
|
|
19
|
+
"child": {
|
|
20
|
+
"type": "decision",
|
|
21
|
+
"label": "Choose service",
|
|
22
|
+
"branches": [
|
|
23
|
+
{"label": "Repair", "child": {"type": "terminal", "label": "Seal kit", "value": 65}},
|
|
24
|
+
{"label": "Replace", "child": {"type": "terminal", "label": "New pump", "value": 220}}
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"label": "Cracked housing",
|
|
30
|
+
"probability": 0.2,
|
|
31
|
+
"child": {"type": "terminal", "label": "New pump", "value": 220}
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "decision",
|
|
3
|
+
"label": "Choose a sales format",
|
|
4
|
+
"branches": [
|
|
5
|
+
{
|
|
6
|
+
"label": "Reserved booth",
|
|
7
|
+
"child": {"type": "terminal", "label": "Guaranteed sales", "value": 180}
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"label": "Outdoor stall",
|
|
11
|
+
"value": -40,
|
|
12
|
+
"child": {
|
|
13
|
+
"type": "chance",
|
|
14
|
+
"label": "Weather",
|
|
15
|
+
"branches": [
|
|
16
|
+
{"probability": 0.65, "child": {"type": "terminal", "label": "Clear", "value": 350}},
|
|
17
|
+
{"probability": 0.35, "child": {"type": "terminal", "label": "Rain", "value": 60}}
|
|
18
|
+
]
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
]
|
|
22
|
+
}
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Rank suppliers on a 0–100 scale and save a printable decision table."""
|
|
2
|
+
|
|
3
|
+
from decision_tree_diagrams import Alternative, Criterion, render_table, score_table
|
|
4
|
+
|
|
5
|
+
criteria = [
|
|
6
|
+
Criterion("Delivery reliability", 3),
|
|
7
|
+
Criterion("Service coverage", 2),
|
|
8
|
+
Criterion("Unit cost rating", 1),
|
|
9
|
+
]
|
|
10
|
+
suppliers = [
|
|
11
|
+
Alternative("Regional supplier", [90, 75, 60]),
|
|
12
|
+
Alternative("National distributor", [85, 80, 65]),
|
|
13
|
+
Alternative("Local wholesaler", [70, 85, 85]),
|
|
14
|
+
Alternative("Direct import", [95, 95, 90], feasible=False),
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
if __name__ == "__main__":
|
|
18
|
+
for result in score_table(criteria, suppliers):
|
|
19
|
+
rank = result.rank if result.rank is not None else "infeasible"
|
|
20
|
+
print(f"{result.alternative.label}: {result.score:.2f} ({rank})")
|
|
21
|
+
figure = render_table(criteria, suppliers, title="Supplier selection")
|
|
22
|
+
figure.savefig("supplier-selection.pdf")
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "decision-tree-diagrams"
|
|
7
|
+
version = "0.2.0"
|
|
8
|
+
description = "Evaluate and draw decision trees and weighted decision tables with Matplotlib."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
dependencies = ["matplotlib>=3.7"]
|
|
12
|
+
license = {file = "LICENSE"}
|
|
13
|
+
classifiers = ["License :: OSI Approved :: MIT License"]
|
|
14
|
+
|
|
15
|
+
[project.scripts]
|
|
16
|
+
decision-tree-diagrams = "decision_tree_diagrams.cli:main"
|
|
17
|
+
|
|
18
|
+
[project.urls]
|
|
19
|
+
Repository = "https://github.com/ManavA/decision-tree-diagrams"
|
|
20
|
+
|
|
21
|
+
[tool.setuptools.packages.find]
|
|
22
|
+
where = ["src"]
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""Evaluate and draw decision trees and weighted decision tables."""
|
|
2
|
+
|
|
3
|
+
from .drawing import render
|
|
4
|
+
from .jsonio import load_json
|
|
5
|
+
from .model import Branch, Chance, Decision, Evaluation, Node, Terminal, evaluate
|
|
6
|
+
|
|
7
|
+
from .table import Alternative, Criterion, ScoredAlternative, render_table, score_table
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"Branch", "Chance", "Decision", "Evaluation", "Node", "Terminal",
|
|
11
|
+
"Alternative", "Criterion", "ScoredAlternative", "render_table", "score_table",
|
|
12
|
+
"evaluate", "load_json", "render",
|
|
13
|
+
]
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""Command-line entry point."""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
from dataclasses import asdict
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from .drawing import render
|
|
9
|
+
from .jsonio import load_json
|
|
10
|
+
from .model import evaluate
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def main() -> None:
|
|
14
|
+
parser = argparse.ArgumentParser(description="Evaluate and draw decision trees.")
|
|
15
|
+
commands = parser.add_subparsers(dest="command", required=True)
|
|
16
|
+
inspection = commands.add_parser("evaluate", help="print rollback results as JSON")
|
|
17
|
+
drawing = commands.add_parser("render", help="write an SVG, PDF, or PNG")
|
|
18
|
+
for command in (inspection, drawing):
|
|
19
|
+
command.add_argument("input", type=Path, help="JSON file containing the tree")
|
|
20
|
+
command.add_argument("--objective", choices=("min", "max"), default="min")
|
|
21
|
+
drawing.add_argument("output", type=Path, help="output path ending in .svg, .pdf, or .png")
|
|
22
|
+
drawing.add_argument("--style", choices=("classic", "modern", "top-down"), default="classic")
|
|
23
|
+
drawing.add_argument("--title")
|
|
24
|
+
drawing.add_argument("--units")
|
|
25
|
+
args = parser.parse_args()
|
|
26
|
+
|
|
27
|
+
try:
|
|
28
|
+
root = load_json(args.input)
|
|
29
|
+
if args.command == "evaluate":
|
|
30
|
+
print(json.dumps(asdict(evaluate(root, args.objective)), indent=2, allow_nan=False))
|
|
31
|
+
return
|
|
32
|
+
if args.output.suffix.lower() not in (".svg", ".pdf", ".png"):
|
|
33
|
+
raise ValueError("output must have an .svg, .pdf, or .png extension")
|
|
34
|
+
figure = render(root, objective=args.objective, style=args.style,
|
|
35
|
+
title=args.title, units=args.units)
|
|
36
|
+
figure.savefig(args.output, dpi=180)
|
|
37
|
+
except (ValueError, OSError, RecursionError) as exc:
|
|
38
|
+
parser.error(str(exc))
|