{"id":9797,"student_id":1470,"content":"setBackdrop(\"#ffffff\");\n\nvar numOfPeople = 300,\n mutationRate = 0.3,\n population = [],\n geneLen = 5,\n isMating = false;\n/*\n0 UP\n1 RIGHT\n2 DOWN\n3 LEFT\n*/\n\nvar __creature = {\n canMove: true,\n score: 0,\n x: 100,\n y: 240,\n genes: [],\n initGenes: function () {\n for (var j = 0; j \u003c geneLen; j++) {\n this.genes[j] = Math.floor(Math.random() * 4);\n }\n }\n };\n\n\nfunction initPopulation() {\n\n for (var i = 0; i \u003c numOfPeople; i++) {\n var creature = {\n canMove: true,\n score: 0,\n x: 100,\n y: 240,\n genes: [],\n initGenes: function () {\n for (var j = 0; j \u003c geneLen; j++) {\n this.genes[j] = Math.floor(Math.random() * 4);\n }\n }\n };\n\n creature.initGenes();\n population[i] = creature;\n\n\n }\n}\n\ninitPopulation();\n\nfunction drawPopulation() {\n for (var i = 0; i \u003c numOfPeople; i++) {\n if(population[i]){\n pen.drawCircle(population[i].x, population[i].y, 2);\n }\n \n }\n}\n\nfunction evalFitness() {\n for (var i = 0; i \u003c numOfPeople; i++) {\n //console.log(\"I: \" + i + \" \" + population[i]);\n population[i].score = Math.pow(population[i].x, 1.5);\n }\n}\n\nfunction getRandElement(arr) {\n return arr[Math.floor(Math.random() * arr.length)];\n}\n\nfunction mate() {\n var matingPool = [];\n var totalFitness = 0;\n for (var i = 0; i \u003c numOfPeople; i++) {\n totalFitness += population[i].score;\n\n }\n for (var i = 0; i \u003c numOfPeople; i++) {\n\n var percentFit = Math.ceil(population[i].score*1000/totalFitness);\n //console.log(population[i].score*1000/totalFitness + \"%\");\n for (var j = 0; j \u003c percentFit; j++) {\n matingPool.push(population[i].genes);\n }\n\n }\n // console.log(matingPool);\n // stop();\n // //Mating\n var newPopulation = [];\n for (var i = 0; i \u003c numOfPeople; i++) {\n //console.log(getRandElement(matingPool));\n var a = getRandElement(matingPool);\n var b = getRandElement(matingPool);\n \n var child = __creature;\n //console.log(child);\n\n child.canMove = true;\n var newGenes = [];\n //console.log(child.genes);\n for (var j = 0; j \u003c geneLen; j++) {\n var _rand = Math.random();\n //console.log(j);\n if (_rand \u003c 0.5) {\n child.genes[j] = a[j];\n } else {\n child.genes[j] = b[j];\n }\n if (_rand \u003c mutationRate) {\n child.genes[j] = Math.floor(Math.random() * 4); //random gene\n }\n }\n //child.genes = newGenes;\n newPopulation[i] = child;\n \n }\n population = newPopulation;\n \n \n return newPopulation;\n\n\n\n}\n\nfunction move(step) {\n for (var i = 0; i \u003c numOfPeople; i++) {\n \n if (population[i].canMove) {\n if (population[i].genes[step] == 0) {\n population[i].y -= 1;\n } else if (population[i].genes[step] == 1) {\n population[i].x += 1;\n } else if (population[i].genes[step] == 2) {\n population[i].y += 1;\n } else if (population[i].genes[step] == 3) {\n population[i].x -= 1;\n }\n } else if(population[i].x \u003c 0 || population[i].y \u003c 0 || population[i].x \u003e 640 || population[i].y \u003e 480 ){\n population[i].canMove = false;\n }\n\n \n }\n\n\n}\n\n\nvar step = 0;\nvar generation = 1;\nvar flag = true;\n\n\n\nforever( function(){\nprint(\"Step: \" + step + \", Generation: \" + generation);\n drawPopulation(); \n //pen.drawCircle(0,0,100);\n});\n\nsetInterval(function(){\n if (step \u003c= geneLen \u0026\u0026 !isMating) {\n //console.log(\"MoVING\")\n\n if(flag)console.log(population[0].genes[step]+\" (\"+population[0].x+\",\"+population[0].x+\")\");\n move(step);\n if(flag)console.log(\"(\"+population[0].x+\",\"+population[0].x+\")\");\n //if(generation\u003e1){stop();flag = false;}\n step++;\n \n \n } else {\n generation++;\n evalFitness();\n isMating = true;\n population = mate();\n //console.log(population);\n //console.log(population.length);\n generation++;\n if(flag)console.log(\"Mated\");\n\n step = 0;\n isMating = false;\n }\n}, 100);","created_at":"2017-03-01T20:52:04.729+08:00","updated_at":"2019-10-17T15:55:18.941+08:00","name":"Genetic Algorithm","language":"javascript","screenshot":{"url":"https://cdn2.koding.school/uploads/project/screenshot/9797/f14f7cd43f3ef17bea57bca69a5ca3f1.jpg"},"parent_id":2,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":455,"hashid":"jzmseezp","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":217554,"file_name":"0.png","project_id":9797,"asset_id":6,"created_at":"2017-08-22T14:40:59.096+08:00","updated_at":"2017-08-22T14:40:59.096+08:00"},{"id":217555,"file_name":"1.svg","project_id":9797,"asset_id":7,"created_at":"2017-08-22T14:40:59.100+08:00","updated_at":"2017-08-22T14:40:59.100+08:00"},{"id":217556,"file_name":"2.png","project_id":9797,"asset_id":8,"created_at":"2017-08-22T14:40:59.104+08:00","updated_at":"2017-08-22T14:40:59.104+08:00"},{"id":217557,"file_name":"0.wav","project_id":9797,"asset_id":9,"created_at":"2017-08-22T14:40:59.107+08:00","updated_at":"2017-08-22T14:40:59.107+08:00"},{"id":217558,"file_name":"1.wav","project_id":9797,"asset_id":10,"created_at":"2017-08-22T14:40:59.111+08:00","updated_at":"2017-08-22T14:40:59.111+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦