TikZ - 游戏树 - 帮助绘图

TikZ - 游戏树 - 帮助绘图

我正在尝试使用 Tikz 绘制游戏树,但失败了。我想绘制的游戏具有以下结构:

在此处输入图片描述

关于如何在 TikZ 中绘制这个,有什么建议吗?

答案1

这是使用istgame包裹:

在此处输入图片描述

\documentclass[tikz]{standalone}
    
\usepackage{istgame}

\begin{document}

\begin{istgame}
% nodes
\tikzset{p1/.style={oval node,minimum size=3pt}}
\tikzset{p2/.style={oval node,fill=black,minimum size=3pt}}
% tree
\xtdistance{20mm}{45mm}
\cntmdistance{10mm}{20mm}
\istroot(1)[p1] \istb \istb \endist
\istrootcntmA(2a)(1-1)[p2] \istbA[draw=none] \endist
\istrootcntmA(2b)(1-2)[p2] \istbA[draw=none] \endist
\xtdistance{15mm}{30mm}
\istroot(1a)(2a-1)[p1] \istb \istb \endist
\istroot(1b)(2b-1)[p1] \istb \endist
\istrootcntmA(2c)(1a-1)[p2] \istbA[draw=none] \endist
\istrootcntmA(2d)(1a-2)[p2] \istbA[draw=none] \endist
\istrootcntmA(2e)(1b-1)[p2] \istbA[draw=none] \endist
\istroot(1c)(2c-1)[p1] \istb \endist
\istroot(1d)(2d-1)[p1] \istb \endist
\istroot(1e)(2e-1)[p1] \istb \endist
% information sets
\xtInfosetO[solid,thin,fill=blue!10](2a)(2b)
\xtInfosetO[solid,thin,fill=blue!10](2d)(2e)
\end{istgame}

\end{document}

答案2

以下是一个简单的选项。我制作了两个宏,分别用于扇区“分支”和信息集。我使用库calc来定位一些点,并添加了层(可能不是必需的,但看起来更好)。

\documentclass[border=2mm]{standalone}
\usepackage    {tikz}
\usetikzlibrary{calc}

\def\r{2} % branch radius

% layers
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers   {background,main,foreground}

\newcommand{\branch}[2] % position (top), name
{% 
  \begin{scope}[shift={#1}]
    \coordinate (N#2) at (0,0);   % north
    \coordinate (S#2) at (0,-\r); % south
    \draw    (0,0) -- (225:\r) arc (225:315:\r) -- cycle;
    \draw[fill]       (0,0)    circle (1mm);
    \begin{pgfonlayer}{foreground}
      \draw[fill=white] (0,-\r)  circle (1mm);
     \end{pgfonlayer}
  \end{scope}
}

\newcommand{\infset}[2] % two points
{%
  \begin{pgfonlayer}{background}
    \draw[rounded corners=0.25 cm,fill=green!30] ($#1-(0.25,0.25)$) rectangle ($#2+(0.25,0.25)$);
  \end{pgfonlayer}
}

\begin{document}
\begin{tikzpicture}[line join=round,line cap=round]
  \branch{(0,0)}{1}
  \branch{(4,0)}{2}
  \branch{(8,0)}{3}
  \branch{(2,4)}{4}
  \branch{(8,4)}{5}
  \infset{(N2)}{(N3)}
  \infset{(N4)}{(N5)}
  \coordinate (O) at ($(N4)!0.5!(N5)+(0,2.5)$);
  \draw (N1) -- (S4) -- (N2); 
  \draw (N3) -- (S5);
  \draw (N4) -- (O) -- (N5);
  \foreach \i in {1,2,3}
  {%
    \draw (S\i) --++ (0,-\r);
  }
  \draw[fill=white] (O) circle (1mm);
\end{tikzpicture} 
\end{document}

在此处输入图片描述

相关内容