{"id":27200,"student_id":2591,"content":"// noprotect\n\nvar target = createSprite({\n costumes: \"target.png\",\n x: 600,\n y: 240\n});\nvar record = createSprite({\n costumes: \"ant.png\",\n x: 0,\n y: 240\n});\nvar walls = [];\nfunction createRandWall () {\n var wall = createSprite(\"rect3.png\");\n wall.x = Math.random()*640;\n wall.y = Math.random()*480;\n wall.direction = Math.random()*360;\n wall.scale = 0.7;\n walls.push(wall);\n}\nvar population = [];\nvar children = [];\nvar mutationRate = 0.01;\nvar size = 100;\nvar interval = 10;\nvar t = 0;\n\nfor (var i = 0; i \u003c 10; i++) createRandWall();\n\nwhile (population.length \u003c size) {\n randomDNA();\n}\n\nfunction randomDNA () {\n let ant = createSprite(\"ant.png\");\n ant.x = 0;\n ant.y = 240;\n ant['path'] = \"\";\n for (var k = 0; k \u003c 5000; k++) {\n ant.path += Math.floor(Math.random() * 3 + 1);\n }\n population.push(ant);\n}\n\nfunction fitness () {\n population.sort(function(a, b) {\n var da = a.distanceTo(target);\n var db = b.distanceTo(target);\n return da - db;\n });\n}\nfunction matePool () {\n fitness();\n while (children.length \u003c size) {\n crossover(population[0], population[0]);\n }\n for (var i = 0; i \u003c children.length; i++) {\n if (population[i].distanceTo(target) \u003c record.distanceTo(target)) {\n record.x = population[i].x;\n record.y = population[i].y;\n }\n if (record.distanceTo(target) \u003c 20) {\n stopRendering();\n }\n population[i].x = 0;\n population[i].y = 240;\n population[i].path = children[i];\n population[i].hidden = false;\n population[i].direction = 90;\n }\n children = [];\n}\n\nfunction crossover (a, b) {\n var c = \"\"\n var mid = Math.floor(Math.random()*key.length);\n for (var i = 0; i \u003c a.path.length; i++) {\n if (i \u003c mid) {\n c += a.path[i];\n } else {\n c += b.path[i];\n }\n if (Math.random() \u003c mutationRate) {\n c = c.substr(0, i) + Math.floor(Math.random() * 4 + 1) + c.substr(i + 1);\n }\n }\n children.push(c);\n}\nvar movement = function() {\n for (var i = 0; i \u003c population.length; i++) {\n if (population[i].hidden === false) {\n if (population[i].path[t] == 2) {\n population[i].direction += 5;\n } else if (population[i].path[t] == 3) {\n population[i].direction -= 5;\n }\n population[i].stepForward(2);\n if (population[i].hidden === false) {\n if (population[i].touched(walls)) {\n population[i].hidden = true;\n }\n }\n }\n }\n t++;\n if (t === 350) {\n t = 0;\n matePool();\n }\n setTimeout(movement, interval);\n};\nsetTimeout(movement, 0);","created_at":"2018-04-27T12:16:33.458+08:00","updated_at":"2019-10-17T09:59:29.010+08:00","name":"Genetic Algorithm 進階","language":"javascript","screenshot":{"url":"https://cdn7.koding.school/uploads/project/screenshot/27200/b0330e5cabaee0c36720ea8dd2902604.jpg"},"parent_id":2,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":117,"hashid":"9ygsnwjz","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":501788,"file_name":"0.png","project_id":27200,"asset_id":6,"created_at":"2018-04-27T12:16:33.465+08:00","updated_at":"2018-04-27T12:16:33.465+08:00"},{"id":501789,"file_name":"1.svg","project_id":27200,"asset_id":7,"created_at":"2018-04-27T12:16:33.467+08:00","updated_at":"2018-04-27T12:16:33.467+08:00"},{"id":501790,"file_name":"2.png","project_id":27200,"asset_id":8,"created_at":"2018-04-27T12:16:33.468+08:00","updated_at":"2018-04-27T12:16:33.468+08:00"},{"id":501791,"file_name":"0.wav","project_id":27200,"asset_id":9,"created_at":"2018-04-27T12:16:33.469+08:00","updated_at":"2018-04-27T12:16:33.469+08:00"},{"id":501792,"file_name":"1.wav","project_id":27200,"asset_id":10,"created_at":"2018-04-27T12:16:33.471+08:00","updated_at":"2018-04-27T12:16:33.471+08:00"},{"id":501793,"file_name":"ant.png","project_id":27200,"asset_id":61528,"created_at":"2018-04-27T12:39:47.889+08:00","updated_at":"2018-04-27T12:39:47.889+08:00"},{"id":501794,"file_name":"target.png","project_id":27200,"asset_id":61529,"created_at":"2018-04-27T12:39:47.891+08:00","updated_at":"2018-04-27T12:39:47.891+08:00"},{"id":511345,"file_name":"rect1.png","project_id":27200,"asset_id":62780,"created_at":"2018-05-04T10:20:36.511+08:00","updated_at":"2018-05-04T10:26:51.743+08:00"},{"id":511346,"file_name":"rect2.png","project_id":27200,"asset_id":62781,"created_at":"2018-05-04T10:26:32.638+08:00","updated_at":"2018-05-04T10:26:59.063+08:00"},{"id":511347,"file_name":"rect3.png","project_id":27200,"asset_id":62781,"created_at":"2018-05-04T10:26:36.071+08:00","updated_at":"2018-05-04T10:27:04.827+08:00"}]
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
繁中
简中
English
日本語
1:1:1
1:1
全寬
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦