在 R 中将二进制解码为十进制
In R decoding binary to decimal
来自这篇文章,第 202 页:
https://journal.r-project.org/archive/2017/RJ-2017-008/RJ-2017-008.pdf,在遗传算法包里
library(GA)
decode <- function(string, bitOrders)
{
string <- split(string, rep.int(seq.int(bitOrders), times = bitOrders))
orders <- sapply(string, function(x) { binary2decimal(gray2binary(x)) })
return(unname(orders))
}
不明白为什么第一行是(3,1,1),第二行是(2,1,1),不应该反过来吗?
decode(c(0,1,0, 0,1, 0,0,1), bitOrders = c(3,2,3))
[1] 3 1 1
decode(c(0,1,1, 0,1, 0,0,1), bitOrders = c(3,2,3))
[1] 2 1 1
此函数不会将二进制转换为十进制,它会将 Grey 编码二进制 转换为十进制。
Note that the decode() function assumes that the input binary string is expressed using Gray encoding, which ensures that consecutive values have the same Hamming distance (Hamming, 1950).
如果我们看一下代码,向量被拆分,然后 运行 到 gray2binary()
,然后 binary2decimal()
。我以前没听说过它,但它显然是二进制编码的不同版本,其中将数字增加 1 只涉及更改单个数字。那么,什么是格雷编码?来自 ?gray2binary 帮助:
Gray encoding allows to obtain binary strings not affected by the well-known Hamming cliff problem. With Gray encoding the number of bit differences between any two consecutive values is one, whereas in binary strings this is not always true.
# Consider a five-bit encoding of values 15 and 16 using the standard
# binary coding
decimal2binary(15, 5)
[1] 0 1 1 1 1
decimal2binary(16, 5)
[1] 1 0 0 0 0
# Moving from 15 to 16 (or vice versa) all five bits need to be changed,
# but using Gray encoding the two binary strings differ by one bit.
binary2gray(decimal2binary(15, 5))
[1] 0 1 0 0 0
binary2gray(decimal2binary(16, 5))
[1] 1 1 0 0 0
我们可以使用一个简单的循环来查看 1:10 在二进制与灰色编码中的外观
# Grey encoding
sapply(1:10, function(x) paste(binary2gray(decimal2binary(x)), collapse = ''))
[1] "1" "11" "10" "110" "111" "101" "100" "1100" "1101" "1111"
# Binary
sapply(1:10, function(x) paste(decimal2binary(x), collapse = ''))
[1] "1" "10" "11" "100" "101" "110" "111" "1000" "1001" "1010"
来自这篇文章,第 202 页:
https://journal.r-project.org/archive/2017/RJ-2017-008/RJ-2017-008.pdf,在遗传算法包里
library(GA)
decode <- function(string, bitOrders)
{
string <- split(string, rep.int(seq.int(bitOrders), times = bitOrders))
orders <- sapply(string, function(x) { binary2decimal(gray2binary(x)) })
return(unname(orders))
}
不明白为什么第一行是(3,1,1),第二行是(2,1,1),不应该反过来吗?
decode(c(0,1,0, 0,1, 0,0,1), bitOrders = c(3,2,3))
[1] 3 1 1
decode(c(0,1,1, 0,1, 0,0,1), bitOrders = c(3,2,3))
[1] 2 1 1
此函数不会将二进制转换为十进制,它会将 Grey 编码二进制 转换为十进制。
Note that the decode() function assumes that the input binary string is expressed using Gray encoding, which ensures that consecutive values have the same Hamming distance (Hamming, 1950).
如果我们看一下代码,向量被拆分,然后 运行 到 gray2binary()
,然后 binary2decimal()
。我以前没听说过它,但它显然是二进制编码的不同版本,其中将数字增加 1 只涉及更改单个数字。那么,什么是格雷编码?来自 ?gray2binary 帮助:
Gray encoding allows to obtain binary strings not affected by the well-known Hamming cliff problem. With Gray encoding the number of bit differences between any two consecutive values is one, whereas in binary strings this is not always true.
# Consider a five-bit encoding of values 15 and 16 using the standard
# binary coding
decimal2binary(15, 5)
[1] 0 1 1 1 1
decimal2binary(16, 5)
[1] 1 0 0 0 0
# Moving from 15 to 16 (or vice versa) all five bits need to be changed,
# but using Gray encoding the two binary strings differ by one bit.
binary2gray(decimal2binary(15, 5))
[1] 0 1 0 0 0
binary2gray(decimal2binary(16, 5))
[1] 1 1 0 0 0
我们可以使用一个简单的循环来查看 1:10 在二进制与灰色编码中的外观
# Grey encoding
sapply(1:10, function(x) paste(binary2gray(decimal2binary(x)), collapse = ''))
[1] "1" "11" "10" "110" "111" "101" "100" "1100" "1101" "1111"
# Binary
sapply(1:10, function(x) paste(decimal2binary(x), collapse = ''))
[1] "1" "10" "11" "100" "101" "110" "111" "1000" "1001" "1010"