带有遗传算法的神经网络并没有变得更好

问题描述

我正在尝试使用遗传算法创建一个神经网络来学习如何玩俄罗斯方块,但看起来有些不对劲,因为即使经过 20 代,我也看不到任何改进。

神经网络的代码是这样的:

import numpy

import configuration


def soft_max(z):
    return numpy.exp(z.T) / numpy.sum(numpy.exp(z.T),axis=1).reshape(-1,1)


def relu(z):
    return numpy.maximum(0,z)


class NeuralNetwork:
    """Class that represent the Neural Network."""

    def __init__(self,weights):
        self.__input_values = numpy.empty(shape=(1,configuration.INPUT))
        self.__weights = weights
        self.__W1_shape = (configuration.NEURONS_HIDDEN_1,configuration.INPUT)
        self.__W2_shape = (configuration.OUTPUT,configuration.NEURONS_HIDDEN_1)
        self.__W1 = self.__weights[0:self.__W1_shape[0] * self.__W1_shape[1]].reshape(self.__W1_shape[0],self.__W1_shape[1])
        self.__W2 = self.__weights[self.__W1_shape[0] * self.__W1_shape[1]:].reshape(self.__W2_shape[0],self.__W2_shape[1])

    def update_parameters(self,vision):
        """Update all the input values of the Neural Network."""
        self.__input_values = vision.reshape(-1,configuration.INPUT)

    def forward_propagation(self):
        """Propagates the initial information to the hidden units at each layer and finally produce the output."""
        z1 = numpy.matmul(self.__W1,self.__input_values.T)
        a1 = relu(z1)
        z2 = numpy.matmul(self.__W2,a1)
        a2 = soft_max(z2)
        return a2

    def get_action(self):
        """Based on the output of the forward_propagation produce an Action."""
        return numpy.argmax(numpy.array(self.forward_propagation()))

那些参数的配置是那些:

# NEURAL NETWORK
INPUT = 234
NEURONS_HIDDEN_1 = 144
OUTPUT = 5
NUMBER_WEIGHTS = INPUT * NEURONS_HIDDEN_1 + NEURONS_HIDDEN_1 * OUTPUT

输入是:

所有棋盘,其中 0 表示块空闲,1 表示块已被占用。电路板是 22x10,所以这些已经是 220 个输入。 7 个输入用于 AI 实际使用的片段,因为您可以拥有 7 个不同的片段。 4 用于工件的旋转 2 为 X 坐标和 Y 坐标。

输出为 5

action = self.get_action_from_nn()
        if action == 0:
            self.shape.move_piece(1)
        if action == 1:
            self.shape.move_piece(-1)
        if action == 2:
            self.shape.drop()
            fast_piece_multiplier = configuration.points['fast_piece_multiplier']
        if action == 3:
            self.shape.rotate()

最后的输出是“什么都不做”

遗传算法参数如下:

# genetic ALGORITHM
NUMBER_OF_POPULATION = 1000
NUMBER_OF_GENERATION = 200
NUMBER_PARENTS_CROSSOVER = 50
MUTATION_PERCENTAGE = 0.05

适应度函数是这样的:

alfa = -0.510066
beta = 0.760666
charlie = -0.35663
delta = -0.184483

score = alfa * (self.aggregate_height()) + beta * self.total_cleared + charlie * self.holes() + delta * self.bumpiness()
return score

其中self.aggregate_height()计算板内每列的总高度,self.total_cleared是AI清除了多少行,self.holes()计算板内有多少个孔,{{1 }} 计算每对列之间的高度差。

这不是我的健身功能,我尝试了很多不同的功能,并在讨论另一个俄罗斯方块项目的博客中找到了这个,这就是他使用的健身功能

信用:Tetris Project

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)