{"id":26101,"student_id":10,"content":"setBackdrop('bg.png');\nlet SIZE = 20;\nlet SPEED = 3;\nlet GRAVITY = 0.25;\nlet pipe1 = createSprite('pipe.png');\npipe1.direction = 90;\nlet pipe2 = createSprite('pipe.png');\npipe2.direction = -90;\nlet pipe3 = createSprite('pipe.png');\npipe3.direction = 90;\nlet pipe4 = createSprite('pipe.png');\npipe4.direction = -90;\nlet pipe5 = createSprite('pipe.png');\npipe5.direction = 90;\nlet pipe6 = createSprite('pipe.png');\npipe6.direction = -90;\n\nlet pairA = { x: 213 * 1 + 150, y: Math.random() * 240 + 90 };\nlet pairB = { x: 213 * 2 + 150, y: Math.random() * 240 + 90 };\nlet pairC = { x: 213 * 3 + 150, y: Math.random() * 240 + 90 };\nlet pairs = [pairA, pairB, pairC];\n\nlet birds = [];\nfor (let i = 0; i \u003c SIZE; i++) {\n let bird = createSprite('bird.png');\n bird.vy = 0;\n bird.x = 100;\n bird.jump = () =\u003e { bird.vy = -6; };\n birds.push(bird);\n}\n\nlet count = 1;\nlet best = 0;\nlet population = [];\nfor (let i = 0; i \u003c SIZE; i++) {\n population.push(randomDNA());\n}\n\nforever(() =\u003e {\n pipe1.moveTo(pairA);\n pipe1.y -= 255;\n pipe2.moveTo(pairA);\n pipe2.y += 255;\n pipe3.moveTo(pairB);\n pipe3.y -= 255;\n pipe4.moveTo(pairB);\n pipe4.y += 255;\n pipe5.moveTo(pairC);\n pipe5.y -= 255;\n pipe6.moveTo(pairC);\n pipe6.y += 255;\n\n birds.forEach((b) =\u003e {\n b.vy += GRAVITY;\n b.y += b.vy;\n if (b.touched([pipe1, pipe2, pipe3, pipe4, pipe5, pipe6]) || b.y \u003c -50 || b.y \u003e 530) {\n b.hidden = true;\n }\n });\n\n pairs.forEach((p) =\u003e {\n p.x -= SPEED;\n if (p.x \u003c 0) {\n p.x += 640;\n p.y = Math.random() * 240 + 90;\n }\n });\n\n let alive = false;\n let target = getPipes();\n for (let i = 0; i \u003c SIZE; i++) {\n let b = birds[i];\n if (b.hidden == false) {\n let x = Math.floor((b.x - target.x) / 15);\n let y = Math.floor((b.y - target.y) / 15);\n let outputs = NN([x, y], population[i]);\n if (outputs \u003e 0) b.jump();\n population[i].fitness++;\n alive = true;\n }\n };\n if (!alive) {\n nextGeneration();\n resetAll();\n count++;\n }\n\n for (let i = 0; i \u003c SIZE; i++) {\n print(population[i].fitness, 10, 10 + 20 * i);\n }\n print('generation:' + count, 10, 420);\n print('best:' + best, 10, 440);\n});\n\nfunction resetAll() {\n pairA.x = 213 * 1 + 150;\n pairA.y = Math.random() * 240 + 90;\n pairB.x = 213 * 2 + 150;\n pairB.y = Math.random() * 240 + 90;\n pairC.x = 213 * 3 + 150;\n pairC.y = Math.random() * 240 + 90;\n birds.forEach((b) =\u003e {\n b.y = 240;\n b.vy = 0;\n b.hidden = false;\n });\n}\n\nfunction getPipes() {\n return pairs.filter(p =\u003e p.x \u003e= 100).sort((a, b) =\u003e a.x - b.x)[0];\n}\n\nfunction sigmoid(x) {\n return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));\n}\n\n// input*2 hidden*6 output*1\nfunction NN(inputs, weights) {\n let w = weights;\n let i = inputs;\n let a = i[0] * w[0] + i[1] * w[1];\n let b = i[0] * w[2] + i[1] * w[3];\n let c = i[0] * w[4] + i[1] * w[5];\n let d = i[0] * w[6] + i[1] * w[7];\n let e = i[0] * w[8] + i[1] * w[9];\n let f = i[0] * w[10] + i[1] * w[11];\n a = sigmoid(a);\n b = sigmoid(b);\n c = sigmoid(c);\n d = sigmoid(d);\n e = sigmoid(e);\n f = sigmoid(f);\n let g = a * w[12] + b * w[13] + c * w[14] + d * w[15] + e * w[16] + f * w[17];\n return sigmoid(g);\n}\n\nfunction randomDNA() {\n let dna = [];\n dna.fitness = 0;\n for (let i = 0; i \u003c 18; i++) dna.push(Math.random() * 0.2 - 0.1);\n return dna;\n}\n\nfunction nextGeneration() {\n let newPops = [randomDNA()];\n population.sort((a, b) =\u003e a.fitness - b.fitness);\n best = population[SIZE -1].fitness \u003e best ? population[SIZE -1].fitness : best;\n\n for (let i = 0; i \u003c SIZE - 4; i++) {\n let a = Math.floor(Math.sqrt(Math.random() * SIZE*SIZE));\n let b = Math.floor(Math.sqrt(Math.random() * SIZE*SIZE));\n let newDNA = crossover(population[a], population[b]);\n newPops.push(newDNA);\n }\n newPops = newPops.concat(population.slice(SIZE - 3));\n population = newPops;\n population.forEach(b =\u003e b.fitness = 0);\n}\n\nfunction crossover(a, b) {\n let dna = [];\n for (let i = 0; i \u003c a.length; i++) {\n dna[i] = Math.random() \u003e 0.5 ? a[i] : b[i];\n if (Math.random() \u003c 0.02) dna[i] = Math.random() * 0.2 - 0.1; // mutation\n }\n return dna;\n}","created_at":"2018-04-11T20:46:22.051+08:00","updated_at":"2022-11-03T18:31:16.286+08:00","name":"NN + GA","language":"javascript","screenshot":{"url":"https://cdn1.koding.school/uploads/project/screenshot/26101/2543a0dd3698025ca39e8a6173f79271.jpg"},"parent_id":24426,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":306,"hashid":"rdvs4wvq","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":482851,"file_name":"pipe.png","project_id":26101,"asset_id":57152,"created_at":"2018-04-11T20:46:22.059+08:00","updated_at":"2018-04-11T20:46:22.059+08:00"},{"id":482852,"file_name":"ground.png","project_id":26101,"asset_id":57151,"created_at":"2018-04-11T20:46:22.061+08:00","updated_at":"2018-04-11T20:46:22.061+08:00"},{"id":482853,"file_name":"bird.png","project_id":26101,"asset_id":57150,"created_at":"2018-04-11T20:46:22.062+08:00","updated_at":"2018-04-11T20:46:22.062+08:00"},{"id":482854,"file_name":"bg.png","project_id":26101,"asset_id":57149,"created_at":"2018-04-11T20:46:22.064+08:00","updated_at":"2018-04-11T20:46:22.064+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦