tensorflow.js实现一个人工智能神经网络进化学习自主打游戏代码
代码语言:html
所属分类:游戏
代码描述:tensorflow.js实现一个人工智能神经网络进化学习自主打游戏代码,用随机神经网络生成一个agent群体,代理人的数量在不断增加,探员们试图穿过这些空隙,在所有特工都死后(灰色描绘),选出前两名,以较小的突变概率,通过杂交繁殖前两个种群,产生新种群进行循环。
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> @import url('https://fonts.googleapis.com/css?family=Roboto:400,700'); body { background-color: #444f56; color: #dde1e4; font-family: 'Roboto'; line-height: 1.6; } canvas { display: block; height: auto; max-width: 100%; background-color: #f8fafc; box-shadow: 0 0 0.5rem rgba(0, 0, 0, 0.125); } dt { display: inline-block; font-weight: 700; } dt::after { content: ':'; } dd { display: inline-block; padding: 0; margin: 0; } h1, h2, h3, h4, h5, h6 { font-weight: 700; line-height: 1.2; } .container { max-width: 512px; padding-left: 1rem; padding-right: 1rem; margin-left: auto; margin-right: auto; } </style> </head> <bod> <div class="container"> <h1>神经进化</h1> <p>见证AI如何学习打游戏通过缝隙</p> × <canvas id="canvas"></canvas> <dl> <div> <dt>Generation</dt> <dd id="param-generation">0</dd> </div> <div> <dt>Highscore</dt> <dd id="param-highscore">0</dd> </div> <div> <dt>Agents (alive)</dt> <dd id="param-agents-alive">0</dd> </div> </dl> <ol> <li> 用随机神经网络生成一个agent群体</li> <li>代理人的数量在不断增加</li> <li> 探员们试图穿过这些空隙 <ol> <li>在所有特工都死后(灰色描绘),选出前两名</li> <li>以较小的突变概率,通过杂交繁殖前两个种群,产生新种群</li> <li>转至步骤2</li> </ol> </li> </ol> </div> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/tf.2.0.js"></script> <script> const MathUtils = { lerp(a, b, t) { return a + t * (b - a); } }; class Agent { constructor( brain = new NeuralNetwork( null, new NeuralNetworkLayer(2), new NeuralNetworkLayer(2), new NeuralNetworkLayer(1))) { this.boundingBox = new Rectangle(); this.brain = brain; this.isAlive = true; this.position = new Vector2(); this.score = 0; } dispose() { this.brain.dispose(); } kill() { this.isAlive = false; } render(context) { context.save(); context.globalCompositeOperation = 'darken'; context.fillStyle = this.isAlive ? '#ea2d2d' : '#dde1e4'; context.fillRect( this.boundingBox.left, this.boundingBox.top, this.boundingBox.width, this.boundingBox.height); context.restore(); } update(context, target) { const { width } = context.canvas; const sizeHalf = 0.5 * Agent.SIZE; const output = this.brain.predict([this.position.x, target]). dataSync(); this.position.x += Agent.SPEED * output[0]; this.position.x = Math.max(sizeHalf, Math.min(width - sizeHalf, this.position.x)); this.updateBoundingBox(); } updateBoundingBox() { const sizeHalf = 0.5 * Agent.SIZE; this.boundingBox.left = this.position.x - sizeHalf; this.boundingBox.right = this.position.x + sizeHalf; this.boundingBox.top = this.position.y - sizeHalf; this.boundingBox.bottom = this.position.y + sizeHalf; } static get SIZE() { return 8; } static get SPEED() { return 64; } static fromParents(a, b) { return new Agent(NeuralNetwork.fromParents(a.brain, b.brain)); }} class NeuralNetwork { constructor(weights, ...layers) { this.inputLayerSize = layers[0].size; this.layers = layers; this.weights = weights || layers. slice(0, layers.length - 1). map((layer, index) => { const layerSizeNext = layers[index + 1].size; return tf.randomUniform([layer.size, layerSizeNext], -1, 1); }); } dispose() { this.weights. forEach(w => w.dispose()); } getWeights() { return this.weights. map(weights => weights.dataSync()); } predict(input) { return tf.tidy(() => { const inputLayer = tf.tensor(input, [1, this.inputLayerSize]); return this.weights.reduce((layer, weights, index) => { const fn = this.layers[index].fn; const result = layer.matMul(weights); return result[fn](). sub(tf.scalar(0.5)); }, inputLayer); }); } static get MUTATION_PROBABILITY() { return 0.05; } static fromParents(a, b) { const weightsA = a.getWeights(); const weightsB = b.getWeights(); const weightsC = new Float32Array(weightsA.length).fill(). map(_ => Math.random() * 2 - 1); const weights = new Array(weightsA.length).fill(). map((_, index) => { const a = weightsA[index]; const b = weightsB[index]; return new Float32Array(a.length).fill(). map((_, weightIndex) => { if (Math.random() < NeuralNetwork.MUTATION_PROBABILITY) { return Math.random() * 2 - 1; } return Math.random() < 0.5 ? a[weightIndex] : b[weightIndex]; }); }). map((arr, index) => tf.tensor(arr, a.weights[index].shape)); return new NeuralNetwork(weights, ...a.layers); }} class NeuralNetworkLayer { constructor(size, fn = 'sigmoid') { this.fn = fn; this.size = size; }} class Obstacle { constructor(gapSize) { this.gapSize = gapSize; this.gapSizeHalf = 0.5 * gapSize; this.position = new Vector2(); } .........完整代码请登录后点击上方下载按钮下载查看
网友评论0