{"id":27775,"student_id":10,"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});\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}\n\nfor (var i=0;i\u003c10; i++) createRandWall();\n\nvar population = [];\nvar children = [];\nvar mutationRate = 0.1;\nvar size = 200;\nvar interval = 10;\nvar t = 0;\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() * 4 + 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[Math.floor(Math.random()*size/4)], population[Math.floor(Math.random()*size/4)]);\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 }\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] == 1) {\n population[i].y -= 10;\n } else if (population[i].path[t] == 2) {\n population[i].y += 10;\n } else if (population[i].path[t] == 3) {\n population[i].x += 10;\n } else if (population[i].path[t] == 4) {\n population[i].x -= 10;\n }\n }\n if (population[i].hidden == false) {\n if (population[i].touched(walls)) {\n population[i].hidden = true;\n }\n }\n }\n t++;\n if (t === 400) {\n t = 0;\n matePool();\n }\n setTimeout(movement, interval);\n};\nsetTimeout(movement, 0);","created_at":"2018-05-04T12:09:47.375+08:00","updated_at":"2019-10-12T06:31:09.801+08:00","name":"Genetic Algorithm 進階","language":"javascript","screenshot":{"url":null},"parent_id":27200,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":462,"hashid":"jzmskqqj","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":511381,"file_name":"rect3.png","project_id":27775,"asset_id":62781,"created_at":"2018-05-04T12:09:47.383+08:00","updated_at":"2018-05-04T12:09:47.383+08:00"},{"id":511382,"file_name":"rect2.png","project_id":27775,"asset_id":62781,"created_at":"2018-05-04T12:09:47.384+08:00","updated_at":"2018-05-04T12:09:47.384+08:00"},{"id":511383,"file_name":"rect1.png","project_id":27775,"asset_id":62780,"created_at":"2018-05-04T12:09:47.386+08:00","updated_at":"2018-05-04T12:09:47.386+08:00"},{"id":511384,"file_name":"target.png","project_id":27775,"asset_id":61529,"created_at":"2018-05-04T12:09:47.387+08:00","updated_at":"2018-05-04T12:09:47.387+08:00"},{"id":511385,"file_name":"ant.png","project_id":27775,"asset_id":61528,"created_at":"2018-05-04T12:09:47.389+08:00","updated_at":"2018-05-04T12:09:47.389+08:00"},{"id":511386,"file_name":"1.wav","project_id":27775,"asset_id":10,"created_at":"2018-05-04T12:09:47.390+08:00","updated_at":"2018-05-04T12:09:47.390+08:00"},{"id":511387,"file_name":"0.wav","project_id":27775,"asset_id":9,"created_at":"2018-05-04T12:09:47.392+08:00","updated_at":"2018-05-04T12:09:47.392+08:00"},{"id":511388,"file_name":"2.png","project_id":27775,"asset_id":8,"created_at":"2018-05-04T12:09:47.393+08:00","updated_at":"2018-05-04T12:09:47.393+08:00"},{"id":511389,"file_name":"1.svg","project_id":27775,"asset_id":7,"created_at":"2018-05-04T12:09:47.395+08:00","updated_at":"2018-05-04T12:09:47.395+08:00"},{"id":511390,"file_name":"0.png","project_id":27775,"asset_id":6,"created_at":"2018-05-04T12:09:47.396+08:00","updated_at":"2018-05-04T12:09:47.396+08:00"}]
Orange Apple Learning Platform
My Projects
View Project Page
Export
Remix
Import
Delete
Download Android APP (APK)
Screenshot
繁中
简中
English
日本語
1:1:1
1:1
Full Width
Scan the QRCode below with your phone to install
Or you can
download APK
to this computer
Scan the QRCode below with your phone to install
Or you can
download APK
to this computer