【发布时间】:2020-01-21 15:10:58
【问题描述】:
我正在为遗传算法制作 GUI 可视化,并希望能够在此函数上绘制最大化函数和每一代个体的点。对于每一代,我只希望删除点,并将下一个点绘制在已经存在的函数图上。
使用函数plot/dc,我正在实现为每一代个体设置动画,绘制坐标列表中的所有点。但是这个方法每次运行时都会重绘画布。所以我不能在点后面绘制函数本身。我可以为每一代做一个功能和点的列表,但这会浪费资源。
使用此代码,您应该能够模拟我的实际开发状态。
#lang racket
(require racket/gui plot)
(define main-window (new frame% [label "FUNCTION AND POINTS"] [width 200] [height 600]))
(define canvas-panel (new panel% [parent main-window]))
(define function-canvas (new canvas% [parent canvas-panel]))
(define (plot-points list-of-points)
(for-each
(λ (population)
(plot/dc (points population
#:x-min 0
#:x-max 3
#:y-min 0
#:y-max 9
#:color 'red)
(send function-canvas get-dc)
0 0
(- (send canvas-panel get-width) 40)
(- (send canvas-panel get-height) 40))
(sleep/yield 1))
list-of-points))
(send main-window show #t)
(plot-points '(((1 8) (2 5) (2.5 2))
((2 5) (1.5 6.5) (2 3))
((1.5 3) (2 2) (1.5 3.5))
((2 7) (0.5 1) (2 0.5))
((0.5 9) (0 5) (0.5 0))
((0 1) (1 4.5) (0 8.5))))
注意:上面的点是随机生成的,与遗传算法输出不对应,所以没有一个函数可以匹配这个坐标。
我希望在这些点后面绘制函数图形,以便能够看到发生的最大化。
【问题讨论】:
-
您找到Interactive Overlays for 2D plots 文档了吗?特别是
set-overlay-renderers方法。 Blog Post by Alex Harsányi 有一些例子,如果你想从头开始的话,虽然这些是更少的动画和更多的鼠标交互 -
当你在这些点后面绘制函数图形时,点变化时函数会固定吗?
-
我已阅读文档,但很难适应点绘图,而不是鼠标交互。此外,在未来的阶段,我想绘制一个 3D 函数,其中的点覆盖它,而文档只说 2D。关于第二条评论,这正是我想要的。如有必要,该功能甚至可以是背景上的图像。
标签: user-interface plot racket