昨日の授業、はじめの高木・菅野型ファジイモデルの話のときは起きてたんだけど・・・
・・うう、GA(遺伝的アルゴリズム)になったら眠くなった・・・
1点交叉が夢の中・・・
みゅーてーしょんがヘビーローテーション
ってかんじ・・
・・・ってかんじだったんだけど・・・
発表者が
「RのGAのパッケージで、巡回セールスマンの問題が書いてあったので、
やってみま~す」
(@_@!)おいおい、まじ、急におきた・・・
おもしろそう~
っていうので、その発表者が発表終わったら、
すぐに、「いまのRのプログラム、ほしいほしいほしいほしいほしい・・・・」
と言って、もうパソコン閉まったのに、むりやりつけさせて、
げっとお~
したプログラムです。
ごたんの~くださいませ。
# RのGAで巡回セールスマン問題をとくプログラム
# どこかに公開されているらしい
# 下記のパッケージGAをあらかじめインストールしてください。
# GA Package by Luca Scrucca (2013-08-19)
library("GA")
data("eurodist", package = "datasets")
D <- as.matrix(eurodist)
D
tourLength <- function(tour, distMatrix) {
our <- c(tour, tour[1])
route <- embed(tour, 2)[,2:1]
sum(distMatrix[route])
}
tspFitness <- function(tour, ...) 1/tourLength(tour, ...)
GA <- ga(type = "permutation", fitness = tspFitness, distMatrix = D,min = 1,
max = attr(eurodist, "Size"), popSize = 50, maxiter = 5000,
run = 500, pmutation = 0.2)
summary(GA)
mds <- cmdscale(eurodist)
x <- mds[, 1]
y <- -mds[, 2]
plot(x, y, type = "n", asp = 1, xlab = "", ylab = "")
abline(h = pretty(range(x), 10), v = pretty(range(y), 10),col = "light gray")
tour <- GA@solution[1, ]
tour <- c(tour, tour[1])
n <- length(tour)
arrows(x[tour[-n]], y[tour[-n]], x[tour[-1]], y[tour[-1]],
length = 0.15, angle = 25, col = "steelblue", lwd = 2)
text(x, y, labels(eurodist), cex=0.8)
#結果を図示すると、こんなかんじ
・・うう、GA(遺伝的アルゴリズム)になったら眠くなった・・・
1点交叉が夢の中・・・
みゅーてーしょんがヘビーローテーション
ってかんじ・・
・・・ってかんじだったんだけど・・・
発表者が
「RのGAのパッケージで、巡回セールスマンの問題が書いてあったので、
やってみま~す」
(@_@!)おいおい、まじ、急におきた・・・
おもしろそう~
っていうので、その発表者が発表終わったら、
すぐに、「いまのRのプログラム、ほしいほしいほしいほしいほしい・・・・」
と言って、もうパソコン閉まったのに、むりやりつけさせて、
げっとお~
したプログラムです。
ごたんの~くださいませ。
# RのGAで巡回セールスマン問題をとくプログラム
# どこかに公開されているらしい
# 下記のパッケージGAをあらかじめインストールしてください。
# GA Package by Luca Scrucca (2013-08-19)
library("GA")
data("eurodist", package = "datasets")
D <- as.matrix(eurodist)
D
tourLength <- function(tour, distMatrix) {
our <- c(tour, tour[1])
route <- embed(tour, 2)[,2:1]
sum(distMatrix[route])
}
tspFitness <- function(tour, ...) 1/tourLength(tour, ...)
GA <- ga(type = "permutation", fitness = tspFitness, distMatrix = D,min = 1,
max = attr(eurodist, "Size"), popSize = 50, maxiter = 5000,
run = 500, pmutation = 0.2)
summary(GA)
mds <- cmdscale(eurodist)
x <- mds[, 1]
y <- -mds[, 2]
plot(x, y, type = "n", asp = 1, xlab = "", ylab = "")
abline(h = pretty(range(x), 10), v = pretty(range(y), 10),col = "light gray")
tour <- GA@solution[1, ]
tour <- c(tour, tour[1])
n <- length(tour)
arrows(x[tour[-n]], y[tour[-n]], x[tour[-1]], y[tour[-1]],
length = 0.15, angle = 25, col = "steelblue", lwd = 2)
text(x, y, labels(eurodist), cex=0.8)
#結果を図示すると、こんなかんじ
