与敌友共舞:鸟群算法的群体智能
Dancing with friends and enemies: boids' swarm intelligence

原始链接: https://community.wolfram.com/groups/-/m/t/122095

此代码展示了一个名为“与友敌共舞”的 Wolfram Mathematica `Manipulate` 交互式演示,现已增加了 2D 和 3D 选项卡。 该模拟可视化了一个包含 1,000 个点的群体,这些点根据一个方程动态演变:每个点通过平衡对“朋友”($p$) 的吸引力和对“敌人”($q$) 的排斥力,并受收缩因子 ($c$) 的影响而移动。用户可以实时调整参数,包括吸引力和排斥力的步长、点大小、绘图范围、不透明度和配色方案。界面清晰地展示了控制方程,并为 2D 和 3D 渲染提供了高度的自定义选项,其中 3D 模式包含边界框的切换开关。此次更新采纳了 Andre 和 Vitaliy 的建议,提升了易用性和视觉风格。

```Hacker News 新闻 | 过往 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 与友敌共舞:博伊德(Boids)群集智能 (wolfram.com) 5 分 | surprisetalk 发布于 2 小时前 | 隐藏 | 过往 | 收藏 | 1 条评论 帮助 Avicebron 0 分钟前 [–] (2012) 回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索: ```
相关文章

原文
Improved Manipulate, 2D/3D Tab, Color Styles, Equation Labels ... Enjoy!

 TabView[{
   "2D" -> Manipulate[n = 1000;
     r := RandomInteger[{1, n}];
     f := (#/(.01 + Sqrt[#.#])) & /@ (x[[#]] - x) &;
     s := With[{r1 = r}, p[[r1]] = r; q[[r1]] = r];
     x = RandomReal[{-1, 1}, {n, 2}];
     {p, q} = RandomInteger[{1, n}, {2, n}];
     Graphics[{Opacity[opacity], PointSize[size],
       Dynamic[If[r < 200, s];
       Point[y = x; x = c*x + ps*f[p] - qs*f[q],
        VertexColors -> (ColorData[col][color*Norm[#]] & /@ (x - y))]]}, PlotRange -> Range, ImageSize -> {700, 600},
     Background -> Black], Delimiter, Style["   2D Dancing with
        friends and enemies", Bold, Large], Delimiter, Style["Equation
                       x = c x + ps f[p] - qs f[q]
          where
       \!\(\*FormBox[\(\(\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \
\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \
\\\ \)\(f[a] = \*FractionBox[\((a - x)\), \(0.01 + \*SqrtBox[\(\((a - \
x)\) . \((a - x)\)\)]\)]\)\),
     TraditionalForm]\)
       ", Bold, Medium], Delimiter,
    Dynamic[Graphics[{Style[
        Text@TraditionalForm@
          Style[Row[{"x = ", c "x + ", ps " f[p] -", qs "f[q]"}]],
        15]}, ImageSize -> {240, 50}]], Delimiter,
    Style["Step towards their friend", Bold,
     Medium], {{ps, 0.02, "Step Size ps"}, 0, 1,Appearance -> "Open"}, Delimiter,
    Style["Step away from their enemy", Bold,
     Medium], {{qs, 0.01, "Step Size qs"}, 0, 1, Appearance -> "Open"},
    Delimiter, {{c, 0.995, "Contraction c"}, 0.5, 1.1,  Appearance -> "Open"},
    Delimiter, {{size, 0.015, "Point Size"}, 0.001, 0.05}, {{Range, 1, "Plot Range"}, 0.1, 5}, {{opacity, 1, "Opacity"}, 0.1, 1}, {{color, 62, "Color Scale"}, 0, 100},
    Control[{{col, "SolarColors", "Color Style"},
      (# -> Row[{Show[ColorData[#, "Image"], ImageSize -> 100], Spacer[10], #}]) & /@ ColorData["Gradients"], PopupMenu}],
     ControlPlacement -> Left],
 
  "3D" -> Manipulate[n = 1000;
    r := RandomInteger[{1, n}];
    f3d := (#/(.01 + Sqrt[#.#])) & /@ (x3d[[#]] - x3d) &;
    s := With[{r1 = r}, p[[r1]] = r; q[[r1]] = r];
    x3d = RandomReal[{-1, 1}, {n, 3}];
    {p, q} = RandomInteger[{1, n}, {2, n}];
    Graphics3D[{Opacity[opacity], PointSize[size],
      Dynamic[If[r < 200, s];
       Point[y = x3d; x3d = c*x3d + ps*f3d[p] - qs*f3d[q],
        VertexColors -> (ColorData[col][color*Norm[#]] & /@ (x3d - y))]]}, PlotRange -> Range, ImageSize -> {700, 600},
     Background -> Black, Boxed -> boxed], Delimiter,
    Style["   3D Dancing with
        friends and enemies", Bold, Large], Delimiter, Style["Equation
                       x = c x + ps f[p] - qs f[q]
          where
       \!\(\*FormBox[\(\(\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \
\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \
\\\ \)\(f[a] = \*FractionBox[\((a - x)\), \(0.01 + \*SqrtBox[\(\((a - \
x)\) . \((a - x)\)\)]\)]\)\),
     TraditionalForm]\)
       ", Bold, Medium], Delimiter,
    Dynamic[Graphics[{Style[
        Text@TraditionalForm@
          Style[Row[{"x = ", c "x + ", ps " f[p] -", qs "f[q]"}]],
        15]}, ImageSize -> {240, 50}]], Delimiter,
    Style["Step towards their friend", Bold,
     Medium], {{ps, 0.02, "Step Size ps"}, 0, 1, Appearance -> "Open"}, Delimiter,
    Style["Step away from their enemy", Bold,
     Medium], {{qs, 0.01, "Step Size qs"}, 0, 1, Appearance -> "Open"},
    Delimiter, {{c, 0.995, "Contraction c"}, 0.5, 1.1, Appearance -> "Open"},
    Delimiter, {{size, 0.015, "Point Size"}, 0.001, 0.05}, {{Range, 1, "Plot Range"}, 0.1, 5}, {{opacity, 1, "Opacity"}, 0.1, 1}, {{color, 62, "Color Scale"}, 0, 100},
    Control[{{col, "SolarColors", "Color Style"},
      (# ->Row[{Show[ColorData[#, "Image"], ImageSize -> 100],  Spacer[10], #}]) & /@ ColorData["Gradients"],
      PopupMenu}], {boxed, {True, False}}, ControlPlacement -> Left]},
  ControlPlacement -> Left]

Thanks to Andre and Vitaliy for suggesting some of these improvements.
联系我们 contact @ memedata.com