有人知道如何防止决策树的分支相互收敛吗?这是使用 Tikz 时的情况 - 我尝试调整兄弟距离等,但末端的两个分支收敛,导致最终值难以辨认。
其次,有人知道如何在两个方形“选择”兄弟之间添加虚线吗?以指示信息集。
\documentclass{article}
\usepackage{tikz}
\tikzset{
choice/.style = {shape=rectangle, draw, align=center, color=black, fill=black, font=\normalsize},
chance/.style = {shape=circle, draw, align=center, color=black, fill=black, font=\normalsize},
root/.style = {choice, font=\normalsize, color=black},
outcome/.style = {shape=rectangle, draw=white, align=center, font=\tiny, parent anchor=left},
}
\begin{figure}[H]
\centering
\begin{tikzpicture}
[
grow = right,
sibling distance = 6em,
level distance = 8em,
edge from parent/.style = {draw, -latex},
every node/.style = {font=\normalsize},
sloped
]
\node [root] {}
child {node [outcome, label=right:{\$30}] {}
edge from parent node [below] {}}
child {node [choice] {}
child {node [chance] {}
child {node [choice] {}
child {node [outcome, label=right:{\$10}]{}}
child {node[outcome, label=right:{\$100}]{}}
edge from parent node [below] {.5}}
child {node [choice]{}
child {node [outcome, label=right:{\$45}]{}}
child {node [outcome, label=right:{\$55}]{}}
edge from parent node [above] {.5}}
edge from parent node [below] {}}
child {node [outcome, label=right:{\$25}]{}
edge from parent node [above] {}}
edge from parent node [above] {}};
\end{tikzpicture}
\caption{Sample Decision Tree} \label{fig: Sample}
\end{figure}
答案1
我使用yshift
选项来改变节点的位置,以避免重叠。
我给两个choice
节点一些名字(a
和b
),并像往常一样画一条虚线。
代码:
\documentclass[tikz]{standalone}
\tikzset{
choice/.style = {shape=rectangle, draw, align=center, color=black, fill=black, font=\normalsize},
chance/.style = {shape=circle, draw, align=center, color=black, fill=black, font=\normalsize},
root/.style = {choice, font=\normalsize, color=black},
outcome/.style = {shape=rectangle, draw=white, align=center, font=\tiny, parent anchor=left},
}
\begin{document}
\begin{tikzpicture}
[
grow = right,
sibling distance = 6em,
level distance = 8em,
edge from parent/.style = {draw, -latex},
every node/.style = {font=\normalsize},
sloped
]
\node [root] {}
child {node [outcome, label=right:{\$30}] {}
edge from parent node [below] {}}
child {node [choice] {}
child {node [chance] {}
child {node [choice] (a) {}
child {node [outcome, label=right:{\$10}]{}}
child {node[yshift=-2em,outcome, label=right:{\$100}]{}}
edge from parent node [below] {.5}}
child {node [choice] (b) {}
child {node [yshift=2em,outcome, label=right:{\$45}]{}}
child {node [outcome, label=right:{\$55}]{}}
edge from parent node [above] {.5}}
edge from parent node [below] {}}
child {node [outcome, label=right:{\$25}]{}
edge from parent node [above] {}}
edge from parent node [above] {}};
\draw[dashed] (a)--(b);
\end{tikzpicture}
\end{document}
答案2
我推荐 forest,它的语法更简单,你不必担心这些事情。
\documentclass{article}
\usepackage[edges]{forest}
\tikzset{
choice/.style = {shape=rectangle, draw, align=center, color=black, fill=black},
chance/.style = {shape=circle, draw, align=center, color=black, fill=black},
root/.style = {choice, font=\normalsize, color=black},
outcome/.style = {shape=rectangle, draw=white, align=center, font=\tiny, parent anchor=left},
}
\begin{document}
\begin{forest}
for tree={grow'=east,edge={-latex},l+=1cm}
[,root
[,choice
[\$25]
[,chance
[,choice,edge label={node[midway,above,sloped]{.5}}
[\$55]
[\$45]
]
[,choice,edge label={node[midway,below,sloped]{.5}}
[\$100]
[\$10]
]
]
]
[\$30]
]
\end{forest}
\end{document}