我正在使用“森林”包创建决策树,并想在分支上添加短线以表明决策不是最优的。这可以在“森林”中完成吗?我附上了一本书中的样本图。
如果无法实现完全这种外观,我会对接近的替代解决方案感兴趣。
编辑:根据要求,这里是树的一般结构的示例代码(与扫描版本不太一样,并且省略了一些数字)。
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{trees,shapes,snakes}
\usepackage{forest}
\tikzset{
decision/.style={rectangle, minimum height=10pt, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
chance/.style={circle, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
leaf-chance/.style={isosceles triangle, minimum width=10pt, draw=black, thick, fill=white, inner sep=0pt, shape border rotate=180, outer sep=-\pgflinewidth}
}
\begin{document}
\begin{frame}[fragile]
\scalebox{0.85}{
\begin{forest}
my label/.style={
edge label={node[auto, sloped,pos=.75,anchor=south]{#1}}
},
for tree={
grow=0,
child anchor=west,
anchor=west,
text ragged,
inner sep=1mm,
edge={thick, draw=blue},
l sep+=30mm,
s sep+=5mm,
if n children=0{
before typesetting nodes={
label/.wrap pgfmath arg={right:#1}{content()},
content={},
leaf-chance,
},
}{},
edge path={
\noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) |- (.child anchor)\forestoption{edge label};
% alternatively, with angled lines
%\noexpand\path[\forestoption{edge}]
% (!u.parent anchor) -- ([xshift=-2.6cm].child anchor) --
% (.child anchor)\forestoption{edge label};
},
}
[,decision
[0.8351,chance,my label=Insure]
[,chance,my label=Don't Insure
[0.8394,chance,my label=No outbreak
]
[,chance,my label=Outbreak
[0.8334,chance,my label=Bans only
]
[0.6663,chance,my label=Slaughter
]
]
]
]
\end{forest}
}
\end{frame}
\end{document}
答案1
以下是基于我发布的代码回答您之前的一个问题。sub optimal
穿过树的相关分支。
\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest}
\usetikzlibrary{shapes}
\tikzset{
decision/.style={rectangle, minimum height=10pt, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
chance/.style={circle, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
leaf-chance/.style={isosceles triangle, minimum width=10pt, draw=black, thick, fill=white, inner sep=0pt, shape border rotate=180, outer sep=-\pgflinewidth}
}
\newsavebox\notgood
\sbox\notgood{\textbardbl}
\forestset{
declare toks={optimality}{},
sub optimal/.style={
optimality={
node [midway, sloped] {\usebox\notgood}
}
}
}
\begin{document}
\begin{forest}
my label/.style={
edge label={node[auto, pos=1, anchor=south east]{#1}}
},
for tree={
grow=0,
child anchor=west,
anchor=west,
text ragged,
inner sep=1mm,
edge={thick, draw=blue, rounded corners=2pt},
s sep+=5mm,
l sep+=30mm,
if n children=0{
if n=1{my label=Accident}{my label=No Accident},
before typesetting nodes={
label/.wrap pgfmath arg={[align=right, text width=25pt]right:#1}{content()},
content={},
leaf-chance,
},
}{
if level=2{
chance,
if n=1{my label=Fast}{my label=Slow}
}{decision}
},
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- ([xshift=-22.5mm].child anchor)\forestove{optimality} -- (.child anchor)\forestoption{edge label};
}
}
[
[,my label=Route 1, sub optimal
[
[-900]
[100]
]
[
[-700]
[50]
]
]
[,my label=Route 2
[
[-600]
[80]
]
[
[-500]
[70]
]
]
]
\end{forest}
\end{document}
答案2
这是一个如何划掉树枝的简单例子。
\documentclass{article}
\usepackage{forest}
\usepackage{mathtools}
\newcommand{\verteq}{\rotatebox{90}{$=$}}
\begin{document}
\begin{forest}
crossed/.style={edge label={node[midway, sloped]{$\verteq$}}}
[A [B [C,crossed ] [D ] ] [E,crossed ] [F,crossed ]]
\end{forest}
\end{document}
答案3
谢谢@cfr 和@AlanMunn。我已将@cfr 的答案改编为原始决策树。
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{trees,shapes,snakes}
\usepackage{forest}
\tikzset{
decision/.style={rectangle, minimum height=10pt, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
chance/.style={circle, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
leaf-chance/.style={isosceles triangle, minimum width=10pt, draw=black, thick, fill=white, inner sep=0pt, shape border rotate=180, outer sep=-\pgflinewidth}
}
\newsavebox\notgood
\sbox\notgood{\textbardbl}
\forestset{
declare toks={optimality}{},
sub optimal/.style={
optimality={
node [midway, sloped] {\usebox\notgood}
}
}
}
\begin{document}
\begin{frame}[fragile]
\scalebox{0.85}{
\begin{forest}
my label/.style={
edge label={node[auto, sloped,pos=.75,anchor=south]{#1}}
},
for tree={
grow=0,
child anchor=west,
anchor=west,
text ragged,
inner sep=1mm,
edge={thick, draw=blue},
l sep+=30mm,
s sep+=5mm,
if n children=0{
before typesetting nodes={
label/.wrap pgfmath arg={right:#1}{content()},
content={},
leaf-chance,
},
}{},
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- ([xshift=-22.5mm].child anchor)\forestove{optimality} -- (.child anchor)\forestoption{edge label};
},
}
[,decision
[0.8351,chance,my label=Insure]
[,chance,my label=Don't Insure, sub optimal
[0.8394,chance,my label=No outbreak
]
[,chance,my label=Outbreak
[0.8334,chance,my label=Bans only
]
[0.6663,chance,my label=Slaughter
]
]
]
]
\end{forest}
}
\end{frame}
\end{document}