{"id":661,"date":"2014-10-19T14:08:06","date_gmt":"2014-10-19T05:08:06","guid":{"rendered":"http:\/\/frsw.net\/blog\/?p=661"},"modified":"2014-10-28T23:16:47","modified_gmt":"2014-10-28T14:16:47","slug":"ruby%e3%81%a7%e5%b7%a1%e5%9b%9e%e3%82%bb%e3%83%bc%e3%83%ab%e3%82%b9%e3%83%9e%e3%83%b3%e5%95%8f%e9%a1%8c","status":"publish","type":"post","link":"http:\/\/frsw.net\/blog\/ruby%e3%81%a7%e5%b7%a1%e5%9b%9e%e3%82%bb%e3%83%bc%e3%83%ab%e3%82%b9%e3%83%9e%e3%83%b3%e5%95%8f%e9%a1%8c\/","title":{"rendered":"Ruby\u3067\u5de1\u56de\u30bb\u30fc\u30eb\u30b9\u30de\u30f3\u554f\u984c"},"content":{"rendered":"<p>Ruby\u3067\u5de1\u56de\u30bb\u30fc\u30eb\u30b9\u30de\u30f3\u554f\u984c\u3092\u89e3\u3053\u3046\u3068\u601d\u3044\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3092\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n\u3068\u308a\u3042\u3048\u305a\u3001\u96e3\u3057\u3044\u3053\u3068\u306f\u8003\u3048\u305a\u306b\u3001\u5358\u7d14\u306a\u65b9\u6cd5\u3067\u3084\u3063\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n\u4ee5\u4e0b\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u8a00\u3046\u306b\u306f\u3042\u307e\u308a\u306b\u5358\u7d14\u306a\u65b9\u6cd5\u3002<\/p>\n<p><!--more--><\/p>\n<ol>\n<li>\u3059\u3079\u3066\u306e\u7d4c\u8def\u3092\u30ea\u30b9\u30c8\u30a2\u30c3\u30d7<\/li>\n<li>\u305d\u308c\u305e\u308c\u306e\u7d4c\u8def\u306e\u30b3\u30b9\u30c8\u3092\u8a08\u7b97<\/li>\n<li>\u30b3\u30b9\u30c8\u304c\u4f4e\u3044\u9806\u306b\u30bd\u30fc\u30c8<\/li>\n<li>\u30b3\u30b9\u30c8\u304c\u4f4e\u3044\u9806\u306b\u7d50\u679c\u3092\u8868\u793a<\/li>\n<\/ol>\n<p>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u3059\u3002<br \/>\n\u3082\u3046\u5c11\u3057\u6d17\u7df4\u3055\u305b\u3066\u4ed6\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u304b\u3082\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3057\u3066\u3044\u304d\u305f\u3044\u3067\u3059\u3002<br \/>\n\u306a\u3093\u304b\u306a\u3055\u305d\u3046\u3060\u3063\u305f\u306e\u3067\u3001TSP(\u5de1\u56de\u30bb\u30fc\u30eb\u30b9\u30de\u30f3\u554f\u984c)\u306egem\u3068\u304b\u4f5c\u3063\u3066\u307f\u305f\u3044\u3067\u3059\u306d\uff01<\/p>\n<p>[ruby]<br \/>\n# encoding: UTF-8<\/p>\n<p># file name(distence matrix)<br \/>\nfilename = &quot;matrix.csv&quot;<\/p>\n<p># -(CSV\u30d5\u30a1\u30a4\u30eb\u306e\u30c6\u30ad\u30c8\u30a6\u306a\u4f8b)-<br \/>\n# ,tokyo,osaka,nagoya<br \/>\n# tokyo,0,300,500<br \/>\n# osaka,300,0,400<br \/>\n# nagoya,500,400,0<br \/>\n# &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p># import CSV as distance matrix<br \/>\ndef import_distance(filename)<br \/>\n  require &quot;csv&quot;<br \/>\n  city_list = Array.new<br \/>\n  distance = Array.new<br \/>\n  i = 0<br \/>\n  reader = CSV.open(filename, &quot;r:UTF-8&quot;)<br \/>\n  header = reader.take(1)[0]<br \/>\n  reader.each do |row|<br \/>\n    city_list.push(row.shift)<br \/>\n    distance[i] = row<br \/>\n    i = i + 1<br \/>\n  end<br \/>\n  return [distance, city_list]<br \/>\nend<\/p>\n<p># \u529b\u305a\u304f\u6cd5\u3067TSP\u3092\u89e3\u304f<br \/>\n# \u5f15\u6570\u306f\u8ddd\u96e2\u884c\u5217\uff08CSV\uff09\u3001\u7d50\u679c\u306f\u77ed\u3044\u9806\u306b\u30bd\u30fc\u30c8\u3055\u308c\u305f\u9806\u8def\u3068\u8ddd\u96e2\u306e\u5408\u8a08\uff08\u4e8c\u91cd\u914d\u5217\uff09<br \/>\ndef brute_force_method(filename)<br \/>\n  distance, city_list = import_distance(filename)<\/p>\n<p>  # list up all courses<br \/>\n  num_of_city = city_list.size &#8211; 1<br \/>\n  str_num = (0..num_of_city).to_a<br \/>\n  start = str_num.shift<br \/>\n  route_arr = str_num.permutation.to_a.map {|e| e.unshift(start).push(start)}<\/p>\n<p>  # calculate the cost<br \/>\n  route = Hash.new<br \/>\n  route_arr.each do |elment|<br \/>\n    first_place = 0<br \/>\n    second_place = 0<br \/>\n    distance_temp = 0<br \/>\n    num_of_city.times do |num|<br \/>\n      first_place = num<br \/>\n      second_place = num + 1<br \/>\n      distance_temp += distance[elment[first_place]][elment[second_place]].to_i<br \/>\n    end<br \/>\n    distance_temp += distance[elment[second_place]][elment[0]].to_i<br \/>\n    route[elment] = distance_temp<br \/>\n  end<\/p>\n<p>  # sort by the cost<br \/>\n  route_sort = route.sort {|(k1, v1), (k2, v2)| v1 &lt;=&gt; v2 }<\/p>\n<p>  # replace city number for city name<br \/>\n  return route_sort.each{|k| k[0].map!{|e| e = city_list[e]}}<br \/>\nend<\/p>\n<p># \u7d50\u679c\u306e\u8868\u793a\uff08\u4e0a\u4f4d10\u307e\u3067\uff09<br \/>\nroute_sorted = brute_force_method(filename)<br \/>\n10.times do |i|<br \/>\n  print(&quot;\u30eb\u30fc\u30c8\uff1a\u3000&quot;, route_sorted[i][0].join(&quot;\u2192&quot;), &quot;\\n&quot;)<br \/>\n  print(&quot;\u8ddd\u96e2\uff1a\u3000#{route_sorted[i][1]}\\n&quot;)<br \/>\nend<br \/>\n[\/ruby]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ruby\u3067\u5de1\u56de\u30bb\u30fc\u30eb\u30b9\u30de\u30f3\u554f\u984c\u3092\u89e3\u3053\u3046\u3068\u601d\u3044\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3092\u3057\u3066\u307f\u307e\u3057\u305f\u3002 \u3068\u308a\u3042\u3048\u305a\u3001\u96e3\u3057\u3044\u3053\u3068\u306f\u8003\u3048\u305a\u306b\u3001\u5358\u7d14\u306a\u65b9\u6cd5\u3067\u3084\u3063\u3066\u307f\u307e\u3057\u305f\u3002 \u4ee5\u4e0b\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u8a00\u3046\u306b\u306f\u3042\u307e\u308a\u306b\u5358\u7d14\u306a\u65b9\u6cd5\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,4,16],"tags":[22,23,24],"_links":{"self":[{"href":"http:\/\/frsw.net\/blog\/wp-json\/wp\/v2\/posts\/661"}],"collection":[{"href":"http:\/\/frsw.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/frsw.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/frsw.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/frsw.net\/blog\/wp-json\/wp\/v2\/comments?post=661"}],"version-history":[{"count":6,"href":"http:\/\/frsw.net\/blog\/wp-json\/wp\/v2\/posts\/661\/revisions"}],"predecessor-version":[{"id":669,"href":"http:\/\/frsw.net\/blog\/wp-json\/wp\/v2\/posts\/661\/revisions\/669"}],"wp:attachment":[{"href":"http:\/\/frsw.net\/blog\/wp-json\/wp\/v2\/media?parent=661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/frsw.net\/blog\/wp-json\/wp\/v2\/categories?post=661"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/frsw.net\/blog\/wp-json\/wp\/v2\/tags?post=661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}