在 Tikz 上绘制游戏树

在 Tikz 上绘制游戏树

我需要在 上绘制以下游戏树tikz。这是一个贝叶斯游戏。抱歉,图片是我在 Paint 上画的。

所以基本上有 2 个玩家 G 和 T,N 是给每种类型的玩家机会的性质。G 的动作是连续的,这就是我画曲线的原因。

在此处输入图片描述

因此,我的主要难题是放置玩家 G 的连续动作集,并为同一类型的玩家放置相同的信息集。如下图所示:

 \documentclass[tikz,border=10pt,multi]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usetikzlibrary{trees}
\usetikzlibrary{calc}
\tikzset{
% Two node styles for game trees: solid and hollow
solid node/.style={circle,draw,inner sep=1.5,fill=black},
hollow node/.style={circle,draw,inner sep=1.5}
}
\begin{document}
\begin{tikzpicture}[font=\footnotesize,edge from parent/.style={draw,thick}]
% Two node styles: solid and hollow
\tikzstyle{solid node}=[circle,draw,inner sep=1.2,fill=black];
\tikzstyle{hollow node}=[circle,draw,inner sep=1.2];
% Specify spacing for each level of the tree
\tikzstyle{level 1}=[level distance=15mm,sibling distance=50mm]
\tikzstyle{level 2}=[level distance=15mm,sibling distance=25mm]
\tikzstyle{level 3}=[level distance=15mm,sibling distance=15mm]
\tikzstyle{level 4}=[level distance=15mm,sibling distance=10mm]
% The Tree
\node(0)[hollow node]{}
child{node[solid node]{}
child{node[solid node]{}
child{node[below]{$1,0$} edge from parent node[left]{$\gamma$}
child{node[below]{$2,3$} edge from parent node[right]{$\tal$}
edge from parent node[above left]{$P$}
}
%I need to put for options in the node
child{node[solid node]{}
edge from parent node[above right]{$N$}
}
edge from parent node[above left]{$\theta$}
}
child{node[solid node]{}
child{node[solid node]{}
child{node[below]{$1,0$} edge from parent node(m)[left]{$\gamma$}
child{node[below]{$2,3$} edge from parent node(n)[right]{$\tal$}
edge from parent node[above left]{$c$}
}
child{node[solid node]{}
edge from parent node[above right]{$N$}
}
edge from parent node[above right]{$1-\theta$}
}
% information sets
\drawloosely dotted,very thicktoout=-15,in=195;
% movers
\node[above,yshift=2]at(0){};
\foreach \x in {1,2} \node[above,yshift=2]at(0-\x){};
\node at($.5(m)+.5(n)$){1};
\end{tikzpicture}
\end{document}

我尝试在这里使用这段代码:

\begin{tikzpicture}
\tikzstyle{hollow node}=[circle,draw,inner sep=1.5]
\tikzstyle{solid node}=[circle,draw,inner sep=1.5,fill=black]
\tikzset{
red node/.style={circle,draw=red,fill=red,inner sep=1.2},
blue node/.style={rectangle,draw=blue,inner sep=2.5}
}
% Specify spacing for each level of the tree
\tikzstyle{level 1}=[level distance=15mm,sibling distance=40mm]
\tikzstyle{level 2}=[level distance=15mm,sibling distance=30mm]
\tikzstyle{level 3}=[level distance=15mm,sibling distance=15mm]
\tikzstyle{level 4}=[level distance=15mm,sibling distance=12mm]

\node[hollow node,label=above:{Natureza}]{}
child{node(1)[solid node]{}
child{node[solid node]{} edge from parent node[left]{$P$}}
child{node[solid node]{} edge from parent node[right]{$N$}}
edge from parent node[left,xshift=-3]{$\theta$}
}
child{node(2)[solid node]{}
child{node[solid node]{} edge from parent node[left]{$P$}}
child{node[solid node]{} edge from parent node[right]{$N$}}
edge from parent node[right,xshift=3]{$1-\theta$}
};
\node at ($(1)!.5!(2)$) {$Governo$};
\end{tikzpicture}

如果有人能帮助我,我会很高兴。

答案1

即使添加了代码,您仍然不完全清楚您到底想要改变什么以及您到底遇到什么困难。

就我个人而言,我会使用 Forest,因为我喜欢它,而且它很容易让你根据自己的需求简洁地、或多或少地自动化地完成任务。你也可以使用 Ti树中的 Z 代码,因为它基于 Ti然而,Z.Tree 规范更加简洁。

下面是一个实现拆分每个节点指定内容的配置的示例:

<label>:<edge label>

以及选项

my arc

可以绘制绿色小圆弧。希望这能帮助您入门。

然后我们可以写

  [N, label=above:Natureza
    [G:\theta, my arc
      [N:P, name=a
        [T:\gamma[:A][:R]]
        [T:\tau[:A][:R]]
        [T:\phi[:A][:R]]
        [T:1-\gamma-\tau-\phi[:A][:R]]
      ]
      [:N, name=b]
    ]
    [G:1-\theta, my arc, before typesetting nodes={prepend'={a}, append'={b}}
    ]
  ]

指定主树,然后手动添加“Governo”标签。

  \node [font=\itshape] at (!r |- !r1) {Governo};

结果如下:

有小绿色弧线的树

通过明智的定义,copy name template我们可以实现一种风格,copy connect将一个节点与另一侧的对应节点连接起来。

然后我们可以写

  [N, label=above:Natureza
    [G:\theta, my arc
      [N:P, name=a, for children=copy connect
        [T:\gamma[:A][:R]]
        [T:\tau[:A][:R]]
        [T:\phi[:A][:R]]
        [T:1-\gamma-\tau-\phi[:A][:R]]
      ]
      [:N, name=b]
    ]
    [G:1-\theta, my arc, before typesetting nodes={prepend'={a}, append'={b}}
    ]
  ]
  \node [font=\itshape] at (!r |- !r1) {Governo};

生产

还有大橙色弧线的树

这是有效的,因为右侧的节点是左侧节点的副本,并且我们通过附加-c名称系统地标记了我们的副本。

完整代码:

\documentclass[border=10pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{forest}
\usetikzlibrary{calc}
\tikzset{
  % Two node styles for game trees: solid and hollow
  solid node/.style={circle,draw,inner sep=1.5,fill=black},
  hollow node/.style={circle,draw,inner sep=1.5}
}
\begin{document}
\begin{forest}
  for tree={
    l sep'+=25pt,
    s sep'+=5pt,
  },
  before typesetting nodes={
    where content={}{}{
      split option={content}{:}{my label,my edge label},
      delay={content=},
    },
    delay={for descendants={if n children=0{}{solid node}}},
  },
  hollow node,
  my label/.style={
    if={isodd(n)}{label={[green!75!black, font=\small]left:#1}}{label={[green!75!black, font=\small]right:#1}}
  },
  my edge label/.style={
    if={isodd(n)}{edge label={node [midway, red, left, font=\scriptsize] {$#1$}}}{edge label={node [midway, red, right, font=\scriptsize] {$#1$}}}
  },
  my arc/.style={
    tikz+={
      \draw [green!75!black] ($()!0.3!(!l)$) [bend left] to ($()!0.3!(!1) + (-5pt,2.5pt)$);
    },
  },
  copy name template={#1-c},
  copy connect/.style={
    tikz+/.process={
      Ow {name}
      {
        \draw [orange] () [bend left=15] to (##1-c);
      }%
    },
  }
  [N, label=above:Natureza
    [G:\theta, my arc
      [N:P, name=a, for children=copy connect
        [T:\gamma[:A][:R]]
        [T:\tau[:A][:R]]
        [T:\phi[:A][:R]]
        [T:1-\gamma-\tau-\phi[:A][:R]]
      ]
      [:N, name=b]
    ]
    [G:1-\theta, my arc, before typesetting nodes={prepend'={a}, append'={b}}
    ]
  ]
  \node [font=\itshape] at (!r |- !r1) {Governo};
\end{forest}
\end{document}

答案2

您还可以使用istgame包裹:

编辑:istgame v2.0

使用 istgame v2.0,\istrootcntm(而不是 \istcntm)和\xtCInfoset(曲线信息集)可以稍微改进代码。(不鼓励使用过时的\istcntm

\begin{document}   

\begin{istgame}[font=\scriptsize]
\xtALPush{-2pt}{-2pt}
% top part
\xtdistance{15mm}{50mm}
\istroot(0)[chance node]{N}
  \istb{\theta}[al]
  \istb{1-\theta}[ar]
  \endist
% middle part: left
\cntmdistance*{15mm}{15mm}
\istrootcntm[-120](P1)(0-1)
  \istb{P}[l]  \endist
\istroot(N1)(0-1)<135>{G}
  \istb*<grow=-60>{N}[r]
  \endist
% middle part: right
\istrootcntm[-120](P2)(0-2)
  \istb{P}[l]
  \endist
\istroot(N2)(0-2)  
  \istb*<grow=-60>{N}[r]
  \endist
% lower part: left
\xtdistance{15mm}{10mm}
\istroot(PN1)(P1-1)[chance node]<180>{N}
  \istb{\gamma}[l]
  \istb{\tau}[l]
  \istb{\phi}[l]
  \istb{1-\gamma-\tau-\phi}[r]
  \endist
% lower part: right
\xtdistance{15mm}{10mm}
\istroot(PN2)(P2-1)[chance node]<180>{N}
  \istb{\gamma}[l]
  \istb{\tau}[l]
  \istb{\phi}[l]
  \istb{1-\gamma-\tau-\phi}[r]
  \endist
% bottom part
\xtdistance{10mm}{5mm}
\foreach \a in {1,2}
  {\foreach \b in {1,2,3,4}
     {\istroot(T\a\b)(PN\a-\b)<180>{T}
      \istb{A}[l]  \istb{B}[r]  \endist
     }
  }
% information sets
\foreach \c in {1,2,3,4}
  {\xtCInfoset[solid,thin,orange](T1\c)(T2\c)<1.2>
  }
\end{istgame}

\end{document}

在此处输入图片描述

原始答案(istgame v1.0)

%% full codes
\documentclass{standalone}

\usepackage{istgame}

\begin{document}   

\begin{istgame}[font=\scriptsize]
\xtALPush{-2pt}{-2pt}
% top part
\xtdistance{15mm}{50mm}
\istroot(0)[chance node]{N}
  \istb{\theta}[al]
  \istb{1-\theta}[ar]
  \endist
% middle part: left
\istcntm[-120](cntm)(0-1)[green]+15mm..15mm+
\istroot[-120](P1)(cntm)<135>{G}+15mm..15mm+
  \istb{P}[l]
  \endist
\istroot(N1)(cntm)  
  \istb*<grow=-60>{N}[r]
  \endist
% middle part: right
\istcntm[-120](cntm)(0-2)[green]+15mm..15mm+
\istroot[-120](P2)(cntm)<45>{G}+15mm..15mm+
  \istb{P}[l]
  \endist
\istroot(N2)(cntm)  
  \istb*<grow=-60>{N}[r]
  \endist
% lower part: left
\xtdistance{15mm}{10mm}
\istroot(PN1)(P1-1)[chance node]<180>{N}
  \istb{\gamma}[l]
  \istb{\tau}[l]
  \istb{\phi}[l]
  \istb{1-\gamma-\tau-\phi}[r]
  \endist
\xtdistance{10mm}{5mm}
\istroot(T11)(PN1-1)<180>{T}
  \istb{A}[l]  \istb{R}[r]  \endist
\istroot(T12)(PN1-2)<180>{T}
  \istb{A}[l]  \istb{R}[r]  \endist
\istroot(T13)(PN1-3)<180>{T}
  \istb{A}[l]  \istb{R}[r]  \endist
\istroot(T14)(PN1-4)<180>{T}
  \istb{A}[l]  \istb{R}[r]  \endist
% lower part: right
\xtdistance{15mm}{10mm}
\istroot(PN2)(P2-1)[chance node]<180>{N}
  \istb{\gamma}[l]
  \istb{\tau}[l]
  \istb{\phi}[l]
  \istb{1-\gamma-\tau-\phi}[r]
  \endist
\xtdistance{10mm}{5mm}
\istroot(T21)(PN2-1)<180>{T}
  \istb{A}[l]  \istb{R}[r]  \endist
\istroot(T22)(PN2-2)<180>{T}
  \istb{A}[l]  \istb{R}[r]  \endist
\istroot(T23)(PN2-3)<180>{T}
  \istb{A}[l]  \istb{R}[r]  \endist
\istroot(T24)(PN2-4)<180>{T}
  \istb{A}[l]  \istb{R}[r]  \endist
% information sets
\xtInfoset[solid,thin,orange,bend left=20](T11)(T21)
\xtInfoset[solid,thin,orange,bend left=20](T12)(T22)
\xtInfoset[solid,thin,orange,bend left=20](T13)(T23)
\xtInfoset[solid,thin,orange,bend left=20](T14)(T24)
\end{istgame}

\end{document}

环境istgame就像tikzpicture环境一样。对于这棵树的底部,您可以使用来\foreach重复事情。完整的示例如下:

%% shortened codes using \foreach
\documentclass{standalone}

\usepackage{istgame}

\begin{document}   

\begin{istgame}[font=\scriptsize]
\xtALPush{-2pt}{-2pt}
% top part
\xtdistance{15mm}{50mm}
\istroot(0)[chance node]{N}
  \istb{\theta}[al]
  \istb{1-\theta}[ar]
  \endist
% middle part: left
\istcntm[-120](cntm)(0-1)[green]+15mm..15mm+
\istroot[-120](P1)(cntm)<135>{G}+15mm..15mm+
  \istb{P}[l]
  \endist
\istroot(N1)(cntm)  
  \istb*<grow=-60>{N}[r]
  \endist
% middle part: right
\istcntm[-120](cntm)(0-2)[green]+15mm..15mm+
\istroot[-120](P2)(cntm)<45>{G}+15mm..15mm+
  \istb{P}[l]
  \endist
\istroot(N2)(cntm)  
  \istb*<grow=-60>{N}[r]
  \endist
% lower part: left
\xtdistance{15mm}{10mm}
\istroot(PN1)(P1-1)[chance node]<180>{N}
  \istb{\gamma}[l]
  \istb{\tau}[l]
  \istb{\phi}[l]
  \istb{1-\gamma-\tau-\phi}[r]
  \endist
% lower part: right
\xtdistance{15mm}{10mm}
\istroot(PN2)(P2-1)[chance node]<180>{N}
  \istb{\gamma}[l]
  \istb{\tau}[l]
  \istb{\phi}[l]
  \istb{1-\gamma-\tau-\phi}[r]
  \endist
% bottom part
\xtdistance{10mm}{5mm}
\foreach \a in {1,2}
  {\foreach \b in {1,2,3,4}
     {\istroot(T\a\b)(PN\a-\b)<180>{T}
      \istb{A}[l]  \istb{B}[r]  \endist
     }
  }
% information sets
\foreach \c in {1,2,3,4}
  {\xtInfoset[solid,thin,orange,bend left=20](T1\c)(T2\c)
  }
\end{istgame}

\end{document}

相关内容