当前位置: 首页>>代码示例>>Python>>正文


Python FeedForwardNetwork.mutate方法代码示例

本文整理汇总了Python中pybrain.structure.FeedForwardNetwork.mutate方法的典型用法代码示例。如果您正苦于以下问题:Python FeedForwardNetwork.mutate方法的具体用法?Python FeedForwardNetwork.mutate怎么用?Python FeedForwardNetwork.mutate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pybrain.structure.FeedForwardNetwork的用法示例。


在下文中一共展示了FeedForwardNetwork.mutate方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from pybrain.structure import FeedForwardNetwork [as 别名]
# 或者: from pybrain.structure.FeedForwardNetwork import mutate [as 别名]
class BlondieBrain:
    def __init__(self, datadir, insize=None, outsize=None, paramfile=None):
        self.datadir = datadir
        if insize == None:
            g = runner.Game()
            ip = self._game2input(g)
            self.insize = len(ip)
        else:
            self.insize = insize
        if outsize == None:
            self.outsize = 1
        else:
            self.outsize = outsize
        if paramfile:
            f = os.path.join(self.datadir, paramfile)
            self.nn = NetworkReader.readFrom(f)
            try:
                self.name = re.search("(.*)-bestof-(.*)", paramfile).group(1)
            except AttributeError:
                self.name = "blondie-%s" % (datetime.datetime.now())
        else:
            self.nn = FeedForwardNetwork()
            tmpname = "blondie-%s" % (datetime.datetime.now())
            self.name = re.sub("[.: ]", "-", tmpname)

            inLayer = LinearLayer(self.insize)
            hiddenLayer1 = SigmoidLayer(self.insize)
            hiddenLayer2 = SigmoidLayer(self.insize)
            outLayer = LinearLayer(self.outsize)

            self.nn.addInputModule(inLayer)
            self.nn.addModule(hiddenLayer1)
            self.nn.addModule(hiddenLayer2)
            self.nn.addOutputModule(outLayer)

            in_to_hidden1 = FullConnection(inLayer, hiddenLayer1)
            hidden1_to_hidden2 = FullConnection(hiddenLayer1, hiddenLayer2)
            hidden2_to_out = FullConnection(hiddenLayer2, outLayer)

            self.nn.addConnection(in_to_hidden1)
            self.nn.addConnection(hidden1_to_hidden2)
            self.nn.addConnection(hidden2_to_out)

            self.nn.sortModules()

    def nextmove(self, game):
        inputdata = self._game2input(game)
        if self.outsize == 1:
            op = int(self.nn.activate(inputdata))
        else:
            r = self.nn.activate(inputdata)
            op = r.argmax()
        return op

    def save(self, suffix=""):
        f = os.path.join(self.datadir, self.name + suffix + ".xml")
        NetworkWriter.writeToFile(self.nn, f)

    def mutate(self):
        self.nn.mutate()

    def randomize(self):
        self.nn.randomize()

    def copy(self):
        x = copy.deepcopy(self)
        x.nn = x.nn.copy()
        return x

    @classmethod
    def _game2input(cls, game):
        mysymbol = len(game.moves) % 2
        cells = [cls._trsymb(x, mysymbol) for x in itertools.chain.from_iterable(game.grid_columns)]
        cols = [cls._trsum(c, mysymbol) for c in game.grid_columns]
        rows = [cls._trsum(r, mysymbol) for r in game.grid_rows]
        diags = [cls._trsum(r, mysymbol) for r in game.diags]
        l = itertools.chain.from_iterable([cells, cols, rows, diags])
        return list(l)

    @classmethod
    def _trsymb(cls, piece, mysymbol):
        # Transform symbol
        if piece == None:
            return 0
        elif piece == mysymbol:
            return 1
        else:
            return -1

    @classmethod
    def _trsum(cls, l, mysymbol):
        # Transform symbol and sum list
        s = 0
        for ll in l:
            s += cls._trsymb(ll, mysymbol)
        return s
开发者ID:kirubakaran,项目名称:blondie18,代码行数:98,代码来源:blondiebrain.py


注:本文中的pybrain.structure.FeedForwardNetwork.mutate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。