我将如何编写这个遗传算法?

发布于 2024-11-29 10:26:06 字数 756 浏览 2 评论 0原文

我想编写一个遗传算法来学习玩类似于俄罗斯方块的游戏。 游戏本身比较简单;我在下面写了它的完整行为。

游戏:

  • 基于网格,12x16。
  • 您必须清除网格中的方块。
  • 每 5 个刻度就会添加一行新块到底部,将块向上推。
  • 您只能清除相同类型块的簇。
  • 随着游戏的进行,方块类型的数量会增加。
  • 您只能清除 3 个或以上的簇。
  • 对于每个清除的簇,(CLUSTER_SIZE - 3)^2 都会添加到 BLOCK_SCORE。
  • 从网格中删除簇后,上面的块会向下滑动以填充间隙,如果此后有任何水平间隙(在底行),则间隙的左侧会移动以填充它。
  • 这个游戏的目标是尽可能长时间地生存。时间以刻度或您已移动的次数来衡量。
  • 您的得分(或健康状况)由(TIME_ALIVE * BLOCK_SCORE)决定。
  • 一旦方块到达网格顶部,游戏就结束了。

本场比赛的得分兼顾了寿命和效率。您清除的簇越大,适合度就越高。

我现在已经编写了一些 GA,但它们是基于本地竞争,例如收集目标等,与其他个人相比。 我的问题是我不知道如何解决这个问题。这个新 GA 的每个不同个体应该只使用当前网格作为输入。 (至少,这是我认为需要的)

我如何开始为此编写 GA 代码?我一辈子都无法解决这个问题。

谢谢大家,

Steffan 'Ruirize' James

I would like to code a genetic algorithm that learns to play a game similar to Tetris.
The game itself is relatively simple; I have written the full behaviour of it below.

The game:

  • Grid based, 12x16.
  • You must clear blocks from the grid.
  • A row of new blocks is added every 5 ticks, to the bottom, pushing the blocks up.
  • You can only clear clusters of the same type block.
  • The number of types of block increases as the game goes on.
  • You can only clear clusters of 3 or above.
  • For every cluster cleared, (CLUSTER_SIZE - 3)^2 is added to BLOCK_SCORE.
  • After a cluster has been removed from the grid, blocks above slide down to fill the gaps, and if there are any horizontal gaps (On the bottom row) after this, the left side of the gap moves over to fill it.
  • The goal of this game is to survive as long as possible. Time is measured in ticks, or the number of moves that you have made.
  • Your score (or Fitness) is determined by (TIME_ALIVE * BLOCK_SCORE)
  • The game is over once a block reaches the top of the grid.

The score of this game incorporates both longevity and efficiency. The larger the clusters that you clear, the higher the fitness is.

I have coded a few GAs now, but they have been based on local competition, things like collection goals and the like, VS other individuals.
My problem is that I don't know how to approach this problem. Each different individual of this new GA should have only the current grid to work on as input. (At least, that's what I think would be needed)

How can I begin to code the GA for this? I cannot for the life of me work it out.

.

Thanks all,

Steffan 'Ruirize' James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彼岸花似海 2024-12-06 10:26:06

您群体中的每个人都代表一场已完成的游戏。每个人的属性都是定义放置方块的给定策略所必需的参数。我假设您有几种不同的启发式方法来放置块。策略的一个示例是从可用策略中随机选择启发式,因此您的属性将是选择给定启发式的一组概率。您能否提供有关您拥有的块 k 放置启发式的更多信息?

Each individual in your population would represent a game played to completion. The attributes of each individual would be parameters necessary to define a given strategy for placing the blocks down. I'm assuming you have a couple of different heuristics for placing a block. One example of a strategy would be to select a heuristic at random from the available strategies so your attributes would be a set of probabilities that a given heuristic is chosen. Can you provide more information on the block k placement heuristic you have?

青丝拂面 2024-12-06 10:26:06

另一种编码可能涉及:

for each possible move
  set phenotypeBehavior to 0
  calculate the post-move position 
  foreach block cleared add a perBlockClearedEmphasis value to phenotypeBehavior
  foreach column add a perColumnHeightEmphasis value to your phenotypeBehavior
  foreach cluster of size x, add a clusterSizeXEmphasis value to your phenotypeBehavior
choose the move that produces the highest phenotypeBehavior

对各种 _foo_Emphasis 值进行遗传编码并进化它们。例如,大概 perBlockClearedEmphasis 将趋向高值,而您的启发式“高度不好”将导致 perColumnHeightEmphasis 为负,而 clusterSizeXEmphasis 对于小 X 将为负,对于较大 X 将为正。

从最一般的意义上来说,这表明你的基因结构描述了一个声明性的、但高度参数化的程序。

An alternative encoding might involve:

for each possible move
  set phenotypeBehavior to 0
  calculate the post-move position 
  foreach block cleared add a perBlockClearedEmphasis value to phenotypeBehavior
  foreach column add a perColumnHeightEmphasis value to your phenotypeBehavior
  foreach cluster of size x, add a clusterSizeXEmphasis value to your phenotypeBehavior
choose the move that produces the highest phenotypeBehavior

Encode the various _foo_Emphasis values genetically and evolve them. Presumably, for instance, perBlockClearedEmphasis will drive towards high values, while your heuristic "height is bad" will drive perColumnHeightEmphasis to be negative, and clusterSizeXEmphasis will be negative for small X and positive for larger X.

In the most general sense, this is suggesting that your genetic structure describe a declarative, but highly-parameterized, program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文