帮助在 TikZ 中重现图表

帮助在 TikZ 中重现图表

这就是我想要实现的目标:

甲基安培

这是我目前所做的:

MDPI1型

这是重现我的图像的代码。我受到了这个的启发回答

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,shadows,positioning}

\tikzset{
  frame/.style={
    rectangle, draw,
    text width=6em, text centered,
    minimum height=4em,drop shadow,fill=white,
    rounded corners,
  },
  line/.style={
    draw, -latex',rounded corners=3mm,
  }
}

\begin{document}
\begin{tikzpicture}[font=\small\sffamily\bfseries,very thick,node distance = 4cm]
\node [frame] (agent) {Agent};
\node [below right of=agent] (aer) {};
\node [below left of=agent] (ael) {};
\node [frame, below left of=aer] (environment) {Environment};

\path [line] (agent)
  -| node[right, pos=1, align=left] {action\\ $A_t$} (aer)
  |- (environment);
\path [line] (environment.170)
  -| node[right, pos=1, align=left] {reward\\ $R_t$} (ael)
  |- (agent.190);
\path [line] (environment.190)
  -| node[left, pos=1, align=right] {state\\ $s_t$} (ael)
  |- (agent.170);
\end{tikzpicture}
\end{document}

我不想要完美的结果。我感兴趣的是保持状态奖励和奖励分离,我不希望每条路径的中心都有空白。也许有一种更好的开始/结束路径的方法会受到赞赏:目前,它是用一个角度完成的,但我不知道线之间的实际距离是多少。


这就是我修改土拨鼠的方法回答使状态线和奖励线在水平和垂直方向上的距离相同。

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,shadows,positioning}

\tikzset{
  frame/.style={
    rectangle, draw,
    text width=6em, text centered,
    minimum height=4em,drop shadow,fill=white,
    rounded corners,
  },
  line/.style={
    draw, -{Latex},rounded corners=3mm,
  }
}

\begin{document}
\begin{tikzpicture}[font=\small\sffamily\bfseries,very thick,node distance = 4cm]
\node [frame] (agent) {Agent};
\node [frame, below=1.2cm of agent] (environment) {Environment};
\draw[line] (agent) -- ++ (3.5,0) |- (environment)
node[right,pos=0.25,align=left] {action\\ $A_t$};
\coordinate[left=8mm of environment] (P);
\coordinate[above=3mm of environment.west] (ENW);
\coordinate[below=3mm of environment.west] (ESW);
\coordinate[above=3mm of agent.west] (ANW);
\coordinate[below=3mm of agent.west] (ASW);
\draw[thin,dashed] (P|-environment.north) -- (P|-environment.south);
\draw[line] (ESW) -- (P |- ESW)
node[midway,above]{$S_{i+1}$};
\draw[line,thick] (ENW) -- (P |- ENW)
node[midway,above]{$R_{i+1}$};
\draw[line] (P |- ESW) -- ++ (-1.4,0) |- (ANW)
node[left, pos=0.25, align=right] {state\\ $s_t$};
\draw[line,thick] (P |- ENW) -- ++ (-0.8,0) |- (ASW)
node[right,pos=0.25,align=left] {reward\\ $R_t$};
\end{tikzpicture}
\end{document}

这就是结果。

MDDP_3

这已经是我想要达到的视觉效果了。问题是我是否可以避免定义四个新坐标(ASW、ANW、ESW、ENW)。

答案1

您正在加载但未使用定位库,也就是说right of=,请使用right=of或来代替right=5mm of。并且可以踢出几个辅助节点/坐标,例如(ael)(aer),它们根本不需要。

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,shadows,positioning}

\tikzset{
  frame/.style={
    rectangle, draw,
    text width=6em, text centered,
    minimum height=4em,drop shadow,fill=white,
    rounded corners,
  },
  line/.style={
    draw, -{Latex},rounded corners=3mm,
  }
}

\begin{document}
\begin{tikzpicture}[font=\small\sffamily\bfseries,very thick,node distance = 4cm]
\node [frame] (agent) {Agent};
\node [frame, below=1.2cm of agent] (environment) {Environment};
\draw[line] (environment) -- ++ (3.5,0) |- (agent) 
node[right,pos=0.25,align=left] {action\\ $A_t$};
\coordinate[left=8mm of environment] (P);
\draw[thin,dashed] (P|-environment.north) -- (P|-environment.south);
\draw[line] (environment.200) -- (P |- environment.200)
node[midway,above]{$S_{i+1}$};
\draw[line,thick] (environment.160) -- (P |- environment.160)
node[midway,above]{$R_{i+1}$};
\draw[line] (P |- environment.200) -- ++ (-1.4,0) |- (agent.160)
node[left, pos=0.25, align=right] {state\\ $s_t$};
\draw[line,thick] (P |- environment.160) -- ++ (-0.8,0) |- (agent.200)
node[right,pos=0.25,align=left] {reward\\ $R_t$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

附录:如果您想调整两条线的距离,则无需引入新的坐标(不过,我认为,您使用附加坐标实现此目的的方式非常优雅。就在原始问题中,坐标(ael)(aer)不一定是个好选择,因为它们会中断路径。原则上,可以P通过切换到来避免在下文中引入decorations.markings,但这可能是一种过度的行为。)

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,shadows,positioning}

\tikzset{
  frame/.style={
    rectangle, draw,
    text width=6em, text centered,
    minimum height=4em,drop shadow,fill=white,
    rounded corners,
  },
  line/.style={
    draw, -{Latex},rounded corners=3mm,
  }
}

\begin{document}
\begin{tikzpicture}[font=\small\sffamily\bfseries,very thick,node distance = 4cm]
\node [frame] (agent) {Agent};
\node [frame, below=1.2cm of agent] (environment) {Environment};
\draw[line] (environment) -- ++ (3.5,0) |- (agent) 
node[right,pos=0.25,align=left] {action\\ $A_t$};
\coordinate[left=12mm of environment] (P);
\draw[thin,dashed] (P|-environment.north) -- (P|-environment.south);
\pgfmathsetmacro{\Ldist}{4mm}
\draw[line] ([yshift=-\Ldist]environment.west) -- 
([yshift=-\Ldist]environment.west -| P) node[midway,above]{$S_{i+1}$};
\draw[line,thick] ([yshift=\Ldist]environment.west) -- ([yshift=\Ldist]environment.west
-|P) node[midway,above]{$R_{i+1}$};
\draw[line] ([yshift=-\Ldist]environment.west -| P) -- ++ (-12mm-\Ldist,0) |- 
([yshift=\Ldist]agent.west) node[left, pos=0.25, align=right] {state\\ $S_t$};
\draw[line,thick] ([yshift=\Ldist]environment.west -| P) -- ++ (-12mm+\Ldist,0) 
|- ([yshift=-\Ldist]agent.west) node[right,pos=0.25,align=left] {reward\\ $R_t$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

(ael)您可以通过将和声明(aer)为 来删除中间的空格\coordinate

目前,奖励和状态是问题中的同一节点。将状态节点向左移动以将它们分开。这可以使用positioning库来完成:

\coordinate [left=1cm of ael] (aell) {}

请注意,我没有使用left=1cm of aelleft of=正如@marmot提到的,写入left of=不是使用已加载的库positioning。这也可以减少所需的节点数。

在此处输入图片描述

\documentclass[border=3mm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,shadows,positioning}

\tikzset{
  frame/.style={
    rectangle, draw,
    text width=6em, text centered,
    minimum height=4em,drop shadow,fill=white,
    rounded corners,
  },
  line/.style={
    draw, -latex',rounded corners=3mm,
  }
}

\begin{document}
\begin{tikzpicture}[font=\small\sffamily\bfseries,very thick,node distance = 2cm]
\node [frame] (agent) {Agent};
\coordinate [below right=of agent] (aer) {};
\coordinate [below left=of agent] (ael) {};
\coordinate [left=0.9 cm of ael] (aell) {};
\node [frame, below left=of aer] (environment) {Environment};

\path [line] (agent) -| node[right, pos=1,align=left] {action\\ $A_t$} (aer) |- (environment);
\path [line] (environment.160) -| node[right,pos=1,align=left] {reward\\ $R_t$} (ael) |- (agent.200);
\path [line] (environment.200) -| node[left,pos=1,align=right]  {state\\ $S_t$} (aell) |- (agent.160);

\draw[-latex] (environment.160) -- ++(-1,0) node[above,midway]{$R_{i+1}$} coordinate(c1);
\draw[-latex] (environment.200) -- ++(-1,0) node[above,midway]{$S_{i+1}$} coordinate(c2);
\draw[dashed,shorten >=-3mm,shorten <=-3mm] (c1) -- (c2);
\end{tikzpicture}
\end{document}

相关内容