{"id":2762,"date":"2013-04-23T17:54:31","date_gmt":"2013-04-23T08:54:31","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=2762"},"modified":"2014-02-03T17:29:54","modified_gmt":"2014-02-03T08:29:54","slug":"post-2762","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/javascript\/2762","title":{"rendered":"\u30a8\u30f3\u30b8\u30cb\u30a2\u304c\u6b63\u3057\u304f\u300cI love you\u300d\u3068\u4f1d\u3048\u308b\u305f\u3081\u306e\u907a\u4f1d\u7684\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0(\u6bba\u4f10)"},"content":{"rendered":"<p><a href=\"http:\/\/www.naturalselection2.com\/\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/04\/natural-selection-2-e1366706722976.jpg\" alt=\"natural-selection-2\" width=\"590\" height=\"295\" class=\"alignnone size-full wp-image-2763\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/04\/natural-selection-2-e1366706722976.jpg 590w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/04\/natural-selection-2-e1366706722976-500x250.jpg 500w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/04\/natural-selection-2-e1366706722976-300x150.jpg 300w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a><\/p>\n<p>\u300c<a href=\"http:\/\/www.atmarkit.co.jp\/ait\/articles\/1304\/19\/news114.html\">\u30a8\u30f3\u30b8\u30cb\u30a2\u304c\u6b63\u3057\u304f\u300c\u597d\u304d\u300d\u3068\u4f1d\u3048\u308b\u305f\u3081\u306e\u5b9f\u88c5\u6cd5<\/a>\u300d\u3068\u3044\u3046\u8a18\u4e8b\u304c\u8a71\u984c\u3060\u3063\u305f\u306e\u3067\u3001\u4e57\u3063\u304b\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u5185\u6c17\u306a\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u305f\u3081\u306b\u300cI love you\u300d\u3068\u8a00\u3063\u3066\u304f\u308c\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u907a\u4f1d\u7684\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<p>\u3010\u53c2\u8003\u3011<br \/>\n<a href=\"http:\/\/bit.ly\/10a6YPc\">Machine Learning: Introduction to Genetic Algorithms<\/a><\/p>\n<h2>\u907a\u4f1d\u7684\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u306f\uff1f<\/h2>\n<blockquote><p>\u3053\u306e\u56fd\u306f\u3059\u3063\u304b\u308a\u30c0\u30e1\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002<br \/>\n \u3060\u304b\u3089\u3001\u5049\u3044\u4eba\u9054\u306f\u76f8\u8ac7\u3057\u3066\u3001\u3053\u306e\u6cd5\u5f8b\u3092\u3064\u304f\u308a\u307e\u3057\u305f\u3002<br \/>\n \u300c\u30d0\u30c8\u30eb\u30fb\u30ed\u30ef\u30a4\u30a2\u30eb\u300d<br \/>\n \u305d\u3053\u3067\u4eca\u65e5\u306f\u307f\u306a\u3055\u3093\u306b\u3001\u3061\u3087\u3063\u3068\u6bba\u3057\u5408\u3044\u3092\u3057\u3066\u3082\u3089\u3044\u307e\u3059\u3002<br \/>\n \u6700\u5f8c\u306e\u4e00\u4eba\u306b\u306a\u308b\u307e\u3067\u3067\u3059\u3002<\/p><\/blockquote>\n<p>\u2191\u3053\u308c\u306b&#8221;\u4ea4\u914d&#8221;\u3068&#8221;\u7a81\u7136\u5909\u7570&#8221;\u306e\u4ed5\u7d44\u307f\u304c\u542b\u307e\u308c\u308c\u3070\u907a\u4f1d\u7684\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002<br \/>\n\u30cb\u30b3\u30cb\u30b3\u5927\u767e\u79d1\u306b\u3055\u3089\u306b<a href=\"http:\/\/dic.nicovideo.jp\/a\/%E9%81%BA%E4%BC%9D%E7%9A%84%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0\">\u308f\u304b\u308a\u3084\u3059\u3044\u4f8b<\/a>\u304c\u63b2\u8f09\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<blockquote><p>\u5c11\u5e74\u5c11\u5973\u3092100\u4eba\u305a\u3064\u7528\u610f\u3057\u307e\u3059\u3002<br \/>\n\u4e00\u751f\u61f8\u547d\u6b4c\u3063\u3066\u3044\u305f\u3060\u304d\u307e\u3059\u3002<br \/>\n\u6b4c\u306e\u4e0a\u624b\u3044\u4e0a\u4f4d5\u4eba\u305a\u3064\u3092\u6b8b\u3057\u3066\u6b8b\u308a\u306f\u62b9\u6bba\u3057\u307e\u3059\u3002<br \/>\n\u4e92\u3044\u306b\u4ea4\u914d\u3057\u3066\u5f7c\u3089\u306e\u5b50\u4f9b\u3092\u7537\u5973100\u4eba\u305a\u3064\u7528\u610f\u3057\u307e\u3059\u3002<br \/>\n2\u301c4\u3092\u4f55\u56de\u3082\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002<br \/>\n\u3069\u3063\u304b\u3067\u505c\u6b62\u3057\u3001\u305d\u306e\u6642\u4e00\u756a\u3046\u307e\u304b\u3063\u305f\u4e00\u7d44\u3092\u6b8b\u3057\u3066\u62b9\u6bba\u3057\u307e\u3059\u3002<br \/>\n\u6b8b\u3063\u305f2\u4eba\u304c\u93e1\u97f3\u30ea\u30f3\u30fb\u30ec\u30f3\u3067\u3059\u3002<br \/>\n\u306a\u3093\u3060\u3063\u3066\u30fc\uff01\uff1f<br \/>\n&#8211; <a href=\"http:\/\/dic.nicovideo.jp\/b\/a\/%E9%81%BA%E4%BC%9D%E7%9A%84%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0\/1-#3\">\u672c\u63b2\u793a\u677f>>3<\/a>\u3088\u308a<\/p><\/blockquote>\n<p>\u3068\u308a\u3042\u3048\u305a\u3001\u306a\u3093\u3068\u306a\u304f\u4ed5\u7d44\u307f\u304c\u5206\u304b\u3063\u305f\u3068\u3053\u308d\u3067\u3084\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>\u300cI love you\u300d\u306b\u8fbf\u308a\u3064\u304f\u307e\u3067\u306e\u6d41\u308c<\/h2>\n<p>\u307e\u305a\u3001\uff12\uff10\u4eba\u306e\u500b\u4f53\u3092\u7528\u610f\u3057\u307e\u3059\u3002<\/p>\n<ol>\n<li>\u500b\u4f53\u305d\u308c\u305e\u308c\u306b\u6587\u5b57\u3092\u66f8\u304b\u305b\u307e\u3059\u3002<\/li>\n<li>\u6587\u5b57\u306e\u5185\u5bb9\u304c\u300cI love you\u300d\u306b\u8fd1\u3044\u9806\u306b\u4e26\u3079\u307e\u3059\u3002<\/li>\n<li>\u4e0a\u4f4d\uff12\u540d\u304c\u5b50\u4f5c\u308a\u3057\u3066\u3001\u5b50\u4f9b\u3092\u4e8c\u4eba\u751f\u307f\u307e\u3059\u3002<\/li>\n<li>\u4e0b\u4f4d\uff12\u540d\u3092\u51e6\u5206\u3057\u3066\u3001\u751f\u307e\u308c\u305f\u5b50\u4f9b\u3068\u5165\u308c\u66ff\u3048\u307e\u3059\u3002<\/li>\n<li>\u30e9\u30f3\u30c0\u30e0\u306b\u9078\u3093\u3060\u500b\u4f53\u306e\u6587\u5b57\u3092\u4e00\u90e8\u5909\u5316\u3055\u305b\u307e\u3059(\u7a81\u7136\u5909\u7570)<\/li>\n<\/ol>\n<p>\u4e0a\u8a18\uff11\u301c\uff15\u3092\u300cI love you\u300d\u304c\u3061\u3083\u3093\u3068\u66f8\u3051\u308b\u500b\u4f53\u304c\u751f\u307e\u308c\u308b\u307e\u3067\u7d9a\u3051\u307e\u3059\u3002<\/p>\n<h2>\u30b5\u30f3\u30d7\u30eb<\/h2>\n<p><script type=\"text\/javascript\" src=\"http:\/\/jsdo.it\/blogparts\/tt03\/js?width=465&#038;height=496&#038;view=screenshot\"><\/script><\/p>\n<h2>\u30b3\u30fc\u30c9<\/h2>\n<p><a href=\"http:\/\/burakkanber.com\/blog\/machine-learning-genetic-algorithms-part-1-javascript\/\" target=\"_blank\" rel=\"noopener noreferrer\">Machine Learning: Introduction to Genetic Algorithms<\/a>\u306b\u63b2\u8f09\u3055\u308c\u3066\u3044\u308b\u30b3\u30fc\u30c9\u306b\u30b3\u30e1\u30f3\u30c8\u4ed8\u3051\u52a0\u3048\u305f\u3060\u3051\u306a\u306e\u3067\u3001\u8a73\u3057\u304f\u306f\u5148\u306e\u8a18\u4e8b\u3092\u8aad\u3093\u3067\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"lang:js decode:true \" >var Gene = function(code) { \/\/\u500b\u4f53\r\n    if (code) this.code = code;\r\n    this.cost = 9999;\r\n};\r\nGene.prototype.code = ''; \/\/ \u907a\u4f1d\u60c5\u5831\r\n\r\nGene.prototype.calcCost = function(compareTo) { \/\/\u30b3\u30b9\u30c8\u8a08\u7b97\r\n    var total = 0;\r\n    for (i = 0; i &lt; this.code.length; i++) {\r\n        total += (this.code.charCodeAt(i) - compareTo.charCodeAt(i)) * (this.code.charCodeAt(i) - compareTo.charCodeAt(i)); \/\/\u4e00\u6587\u5b57\u305a\u3064Unicode\u30b3\u30fc\u30c9\u5024\u306e\u5dee\u3092\u8a08\u7b97\r\n    }\r\n    this.cost = total;\r\n};\r\n\r\nGene.prototype.random = function(length) { \/\/\u30e9\u30f3\u30c0\u30e0\u306a\u907a\u4f1d\u5b50\u3092\u4f5c\u6210\u3059\u308b\r\n    while (length--) {\r\n        this.code += String.fromCharCode(Math.floor(Math.random() * 255));\r\n        \/\/fromCharCode\u30e1\u30bd\u30c3\u30c9\u306f0x41, 0x42, 0x43\u306e\u3088\u3046\u306a\u6587\u5b57\u30b3\u30fc\u30c9\u5217\u3092 &quot;ABC&quot; \u306e\u3088\u3046\u306a\u6587\u5b57\u5217\u306b\u5909\u63db\u3057\u3066\u8fd4\u3057\u307e\u3059\r\n    }\r\n};\r\n\r\nGene.prototype.mate = function(gene) { \/\/\u4ea4\u53c9\r\n    var pivot = Math.round(this.code.length \/ 2) - 1; \/\/\u4ea4\u53c9\u70b9(\u4e2d\u5fc3)\u53d6\u5f97\r\n    \r\n    \/\/\u5b50\u4f5c\u308a\uff08\u4e00\u70b9\u4ea4\u53c9\uff09\r\n    var child1 = this.code.substr(0, pivot) + gene.code.substr(pivot); \r\n    var child2 = gene.code.substr(0, pivot) + this.code.substr(pivot);\r\n\r\n    return [new Gene(child1), new Gene(child2)]; \/\/\u751f\u8a95\r\n};\r\n\r\nGene.prototype.mutate = function(chance) { \/\/\u7a81\u7136\u5909\u7570\r\n    if (Math.random() &gt; chance) return; \/\/\u5909\u7570\u3067\u304d\u305a\u306b\u30d0\u30a4\u30d0\u30a4\r\n\r\n    var index = Math.floor(Math.random() * this.code.length); \/\/\u5909\u7570\u3059\u308b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u6c7a\u5b9a(\u30e9\u30f3\u30c0\u30e0)\r\n    var upOrDown = Math.random() &lt;= 0.5 ? -1 : 1; \/\/1 or -1  \/\/\u5897\u3084\u3059\u304b\u6e1b\u3089\u3059\u304b\u6c7a\u5b9a\r\n    var newChar = String.fromCharCode(this.code.charCodeAt(index) + upOrDown); \/\/\u30ad\u30e3\u30e9\u30af\u30bf\u30fc\u30b3\u30fc\u30c9\u3092\u5897\u6e1b\r\n    var newString = ''; \/\/\u65b0\u3057\u3044\u907a\u4f1d\u60c5\u5831\r\n    for (i = 0; i &lt; this.code.length; i++) {\r\n        if (i == index) newString += newChar; \/\/\u5909\u7570\u3055\u305b\u305f\u30ad\u30e3\u30e9\u30af\u30bf\u30fc\u30b3\u30fc\u30c9\u3092\u633f\u5165\r\n        else newString += this.code[i];\r\n    }\r\n\r\n    this.code = newString; \/\/\u907a\u4f1d\u60c5\u5831\u3092\u4e0a\u66f8\u304d\r\n\r\n};\r\n\r\n\r\n\r\n\r\n\r\n\r\nvar Population = function(goal, size) { \/\/\u7fa4\u308c\u3000\r\n    this.members = []; \/\/\u500b\u4f53\u3092\u5165\u308c\u3068\u304f\u7bb1\r\n    this.goal = goal; \/\/\u76ee\u6307\u3059\u30b4\u30fc\u30eb\r\n    this.generationNumber = 0; \/\/\u4e16\u4ee3\u6570\r\n    while (size--) {\r\n        var gene = new Gene(); \/\/size\u306e\u6570\u3060\u3051\u500b\u4f53\u3092\u7528\u610f\u3059\u308b\r\n        gene.random(this.goal.length); \/\/\u30b4\u30fc\u30eb\u306e\u9577\u3055\u3092\u5143\u306b\u67d3\u8272\u4f53\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u4f5c\u6210\r\n        this.members.push(gene); \/\/\u7fa4\u308c\u306b\u8ffd\u52a0\r\n    }\r\n};\r\n\r\nPopulation.prototype.sort = function() { \/\/\u30b3\u30b9\u30c8\u3092\u57fa\u6e96\u306b\u30bd\u30fc\u30c8\r\n    this.members.sort(function(a, b) {\r\n        return a.cost - b.cost;\r\n    });\r\n}\r\n\r\nPopulation.prototype.generation = function() { \/\/\u4e16\u4ee3\u51e6\u7406\r\n    for (var i = 0; i &lt; this.members.length; i++) {\r\n        this.members[i].calcCost(this.goal); \/\/\u30b3\u30b9\u30c8\u8a08\u7b97\r\n\r\n    }\r\n\r\n    this.sort(); \/\/\u7fa4\u308c\u3092\u30bd\u30fc\u30c8\r\n    this.display(); \/\/\u753b\u9762\u51fa\u529b\r\n    \r\n    var children = this.members[0].mate(this.members[1]); \/\/\u4e0a\u4f4d\uff12\u540d\u3067\u4ea4\u53c9\r\n    \r\n    \/\/\u53d6\u6368\u9078\u629e\r\n    this.members.splice(this.members.length - 2, 2, children[0], children[1]); \/\/\u7fa4\u308c\u306e\u306a\u304b\u304b\u3089\u9806\u4f4d\u306e\u4f4e\u3044\uff12\u540d\u304c\u6b7b\u4ea1\u3002\u5b50\u4f9b\uff12\u4eba\u3068\u5165\u308c\u66ff\u3048\u3002\r\n\r\n    \r\n    for (var i = 0; i &lt; this.members.length; i++) {\r\n        this.members[i].mutate(0.5); \/\/\u7a81\u7136\u5909\u7570\r\n        this.members[i].calcCost(this.goal); \/\/\u30b3\u30b9\u30c8\u8a08\u7b97\r\n        if (this.members[i].code == this.goal) { \/\/\u30b4\u30fc\u30eb\u306b\u8fbf\u308a\u3064\u3044\u305f\u3089\u7d42\u4e86\r\n            this.sort();\r\n            this.display();\r\n            return true;\r\n        }\r\n    }\r\n    \r\n    this.generationNumber++; \/\/\u4e16\u4ee3\u30ab\u30a6\u30f3\u30c8\r\n\r\n    var scope = this;\r\n    \r\n    setTimeout(function() {\r\n        scope.generation(); \/\/\u6b21\u306e\u4e16\u4ee3\u3078\r\n    }, 20);\r\n    \r\n};\r\n\r\nPopulation.prototype.display = function() { \/\/\u753b\u9762\u51fa\u529b\r\n    document.body.innerHTML = '';\r\n    document.body.innerHTML += (&quot;&lt;h2&gt;Generation: &quot; + this.generationNumber + &quot;&lt;\/h2&gt;&quot;);\r\n    document.body.innerHTML += (&quot;&lt;ul&gt;&quot;);\r\n    for (var i = 0; i &lt; this.members.length; i++) {\r\n        document.body.innerHTML += (&quot;&lt;li&gt;&quot; + this.members[i].code + &quot; (&quot; + this.members[i].cost + &quot;)&quot;);\r\n    }\r\n    document.body.innerHTML += (&quot;&lt;\/ul&gt;&quot;);\r\n};\r\n\r\nvar population = new Population(&quot;I love you&quot;, 20);\r\npopulation.generation();<\/pre>\n<p>\u591a\u5927\u306a\u72a0\u7272\u3092\u6255\u3063\u3066\u4f5c\u6210\u3057\u305f\u300cI love you\u300d\u306a\u3089\u3001\u304d\u3063\u3068\u601d\u3044\u3082\u4f1d\u308f\u308b\u306f\u305a\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u300c\u30a8\u30f3\u30b8\u30cb\u30a2\u304c\u6b63\u3057\u304f\u300c\u597d\u304d\u300d\u3068\u4f1d\u3048\u308b\u305f\u3081\u306e\u5b9f\u88c5\u6cd5\u300d&hellip;<\/p>\n","protected":false},"author":1,"featured_media":2763,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-2762","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","has-post-thumbnail-archive"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2762","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=2762"}],"version-history":[{"count":0,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2762\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media\/2763"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=2762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=2762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=2762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}