状态操作和过渡操作*是分别写在状态或过渡中的指令,用于在模拟过程中定义图表的行为。
考虑一个有限自动机模型,该模型对科拉茨假设 的一个变体进行经验测试。对于给定的数字输入$u$ ,图通过重复规则计算科拉茨序列$n_0=u,n_1,n_2,...$ :
- 如果$n_i$ 是偶数,那么$n_{i+1}=n_i/2$
- 如果$n_i$ 是奇数,那么$n_{i+1}=3n_i+1$
科拉茨假设指出,对于每个自然数,都有一个最终归一的数列。
图由三个状态组成。在模拟开始时,初始状态通过设置来初始化图表数据:
- 本地数据 n 与输入值 u 相对应。
- 本地数据 n2 除以 2 时转换为余数。
- 输出数据 y 设置为 false
根据输入数据的奇偶校验,图表切换到偶或奇状态。当状态活动在偶和奇状态之间切换时,图表会计算序列中的数字。当数字网格序列达到值 1 时,y 输出变为true,模拟停止。
状态操作定义了状态图在状态激活时的操作。最常见的状态操作类型是进入、持续和退出:
** 进入** - 状态激活时发生的动作。
during - 当状态已处于活动状态,而图没有脱离该状态时,在某一时间步发生动作。
您可以使用完整关键字(entry、during、exit)或缩写(en、du、ex)来指定状态动作类型。也可以使用逗号组合状态操作类型。例如,组合类型为进入、持续的操作会在状态激活的时间步发生,只要状态保持激活状态,就会在随后的每个时间步发生。
梯度图包含状态中的动作:
Init - 当模拟开始时该状态处于活动状态,entry动作决定n的奇偶性,并将y设为false。当图表在一个时间步后离开初始状态时,退出动作将确定n是否等于 1。
Even - 当该状态激活时,以及在该状态激活期间的每个后续时间步,entry、during 组合动作会计算冰雹序列中下一个数字 n/2 的值和奇偶校验。
Odd - 当该状态激活时,以及在该状态激活后的每个时间步长内,entry、during的组合动作会检查n是否大于1,如果大于1,则计算冰雹序列中下一个数字3*n+1的数值和奇偶校验。
过渡标签定义了当活动状态发生变化时状态图会做什么。最常见的过渡标签类型是条件和条件动作:[条件]{动作}。
梯度图包含转场动作:
Init 中的默认转换 - 在模拟开始时,条件操作$n = u$ 将输入值 u 分配给本地数据 n。
从 Init 到 Even 的过渡 -$n2 == 0$ 条件指定当 n 为偶数时发生过渡。此过渡源中的数字 1 表示此过渡在 Init 到 Odd 的过渡之前进行评估。
从 Odd 到 Even 的过渡 -$n2 == 0$ 条件指定当 n 为偶数时发生过渡。
从偶数过渡到奇数--条件$n2 ~= 0$ 规定,当 n 为奇数时,过渡发生。在这种情况下,$y = isequal(n,1)$ 条件的作用决定了 n 是否等于 1。
让我们模拟运行一个从数值 9 开始的冰雹序列。为此,请使用控制指令加载并运行模型collatz.engee
。
Out[0]:
SimulationResult(
"n" => WorkspaceArray{Float64}("collatz/n")
,
"y" => WorkspaceArray{Float64}("collatz/y")
)
状态图

在$t = 0$ 时,默认过渡到初始状态。过渡操作将 n 的值设置为 9。初始状态变为活动状态。条目中的操作会将初始值 n2 设为 1,将 y 设为 false。
在时间$t = 1$ 时刻,n2 == 0 的条件未满足,因此图中准备过渡到奇数状态。Init 中的退出操作将 y 设置为 false。初始状态变为非活动状态,奇数状态变为活动状态。Odd 中的进入操作将 n 设置为 28,n2 设置为 0。
在$t = 2$ 时,条件 n2 == 0 为真,因此图表准备过渡到 Even 状态。奇数状态变为非活动状态,偶数状态变为活动状态。进入偶数状态的操作将 n 设为 14,n2 设为 0。
在$t = 3$ 时,条件 n2 ~= 0 未满足,因此图表没有过渡到其他状态。偶数状态保持激活。在偶数态期间的操作将 n 设为 7,n2 设为 1。
*$t = 4$ 期间条件 n2 ~= 0 为真,因此图表准备过渡到奇数状态。过渡操作将 y 设为 false。偶数状态变为非活动状态,奇数状态变为活动状态。进入奇数状态的动作将 n 设为 22,将 n2 设为 0。
您也可以从建模环境中运行模型模拟。为此,请打开模型collatz.engee
并运行。然后您可以在 "图表 "窗口或 "数据检查器 "应用程序中查看模拟结果。
1.在右上角的 "应用程序 "选项卡上,转到 "数据检查器 "部分。
2.要查看冰雹序列值,请在 "数据检查器 "中选择记录的信号 n。
冰雹 "序列在迭代 19 次后达到了 1 的值。你也可以用其他数字做实验。
{"id": "e3e647c3-4113-4591-8300-daf2fcd90679", "data": [{"showlegend": true, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [9, 0, 0, 9, 9, 9], "type": "scatter", "x": [-0.4, -0.4, 0.4, 0.4, -0.4, -0.4], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [28, 0, 0, 28, 28, 28], "type": "scatter", "x": [0.6, 0.6, 1.4, 1.4, 0.6, 0.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [14, 0, 0, 14, 14, 14], "type": "scatter", "x": [1.6, 1.6, 2.4, 2.4, 1.6, 1.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [7, 0, 0, 7, 7, 7], "type": "scatter", "x": [2.6, 2.6, 3.4, 3.4, 2.6, 2.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [22, 0, 0, 22, 22, 22], "type": "scatter", "x": [3.6, 3.6, 4.4, 4.4, 3.6, 3.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [11, 0, 0, 11, 11, 11], "type": "scatter", "x": [4.6, 4.6, 5.4, 5.4, 4.6, 4.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [34, 0, 0, 34, 34, 34], "type": "scatter", "x": [5.6, 5.6, 6.4, 6.4, 5.6, 5.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [17, 0, 0, 17, 17, 17], "type": "scatter", "x": [6.6, 6.6, 7.4, 7.4, 6.6, 6.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [52, 0, 0, 52, 52, 52], "type": "scatter", "x": [7.6, 7.6, 8.4, 8.4, 7.6, 7.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [26, 0, 0, 26, 26, 26], "type": "scatter", "x": [8.6, 8.6, 9.4, 9.4, 8.6, 8.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [13, 0, 0, 13, 13, 13], "type": "scatter", "x": [9.6, 9.6, 10.4, 10.4, 9.6, 9.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [40, 0, 0, 40, 40, 40], "type": "scatter", "x": [10.6, 10.6, 11.4, 11.4, 10.6, 10.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [20, 0, 0, 20, 20, 20], "type": "scatter", "x": [11.6, 11.6, 12.4, 12.4, 11.6, 11.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [10, 0, 0, 10, 10, 10], "type": "scatter", "x": [12.6, 12.6, 13.4, 13.4, 12.6, 12.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [5, 0, 0, 5, 5, 5], "type": "scatter", "x": [13.6, 13.6, 14.4, 14.4, 13.6, 13.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [16, 0, 0, 16, 16, 16], "type": "scatter", "x": [14.6, 14.6, 15.4, 15.4, 14.6, 14.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [8, 0, 0, 8, 8, 8], "type": "scatter", "x": [15.6, 15.6, 16.4, 16.4, 15.6, 15.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [4, 0, 0, 4, 4, 4], "type": "scatter", "x": [16.6, 16.6, 17.4, 17.4, 16.6, 16.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [2, 0, 0, 2, 2, 2], "type": "scatter", "x": [17.6, 17.6, 18.4, 18.4, 17.6, 17.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [1, 0, 0, 1, 1, 1], "type": "scatter", "x": [18.6, 18.6, 19.4, 19.4, 18.6, 18.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [1, 0, 0, 1, 1, 1], "type": "scatter", "x": [19.6, 19.6, 20.4, 20.4, 19.6, 19.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "markers", "xaxis": "x", "colorbar": {"y": 0.513888888888889, "title": {"text": ""}, "len": 0.9525371828521435, "x": 0.9934383202099738}, "name": "y1", "yaxis": "y", "legendgroup": "y1", "marker": {"symbol": "circle", "color": "rgba(0, 154, 250, 0.000)", "line": {"color": "rgba(0, 0, 0, 0)", "width": 1}, "size": 0}, "y": [9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 1], "type": "scatter", "x": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 21, "currentCount": 21}}}], "config": {"showlegend": true, "xaxis": {"showticklabels": true, "gridwidth": 0.5, "tickvals": [0, 5, 10, 15, 20], "range": [-1.6854400000000016, 21.68544], "domain": [0.03619130941965587, 0.9934383202099738], "mirror": false, "tickangle": 0, "showline": true, "ticktext": ["0", "5", "10", "15", "20"], "zeroline": false, "tickfont": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "zerolinecolor": "rgba(0, 0, 0, 1)", "anchor": "y", "visible": true, "ticks": "inside", "tickmode": "array", "linecolor": "rgba(0, 0, 0, 1)", "showgrid": true, "title": {"text": "", "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}}, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickcolor": "rgb(0, 0, 0)", "type": "linear"}, "paper_bgcolor": "rgba(255, 255, 255, 1.000)", "annotations": [], "height": 400, "margin": {"l": 0, "b": 20, "r": 0, "t": 20}, "plot_bgcolor": "rgba(255, 255, 255, 1.000)", "yaxis": {"showticklabels": true, "gridwidth": 0.5, "tickvals": [0, 10, 20, 30, 40, 50], "range": [0, 52], "domain": [0.03762029746281716, 0.9901574803149606], "mirror": false, "tickangle": 0, "showline": true, "ticktext": ["0", "10", "20", "30", "40", "50"], "zeroline": false, "tickfont": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "zerolinecolor": "rgba(0, 0, 0, 1)", "anchor": "x", "visible": true, "ticks": "inside", "tickmode": "array", "linecolor": "rgba(0, 0, 0, 1)", "showgrid": true, "title": {"text": "", "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}}, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickcolor": "rgb(0, 0, 0)", "type": "linear"}, "legend": {"yanchor": "auto", "xanchor": "auto", "bordercolor": "rgba(0, 0, 0, 1)", "bgcolor": "rgba(255, 255, 255, 1.000)", "borderwidth": 1, "tracegroupgap": 0, "y": 1, "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "title": {"font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}, "text": ""}, "traceorder": "normal", "x": 1}, "width": 526.390625}}