ahnr
is a package that implements the artificial hydrocarbon networks developed by Hiram Ponce. Most of the work is based on the book Artificial Organic Networks.
Here are some quick examples to get you started.
The following code let’s you create the data to train an artificial hydrocarbon network.
library(ahnr)
# Create data
set.seed(12321)
x <- 2 * runif(1000) - 1;
x <- sort(x)
y <- (x < 0.1) * (0.05 * runif(1000) + atan(pi*x)) +
(x >= 0.1 & x < 0.6) * (0.05 * runif(1000) + sin(pi*x)) +
(x >= 0.6) * (0.05 * runif(1000) + cos(pi*x))
plot(x, y, type = 'l')
# Create the Sigma dataset
Sigma <- list(X = data.frame(x = x), Y = data.frame(y = y))
# Create network
ahn <- AHNnD(Sigma, 5, 0.01, 100)
The trained network can be used to visualize its performance.
# Create test data
X <- data.frame(x = x)
# Simulate
ysim <- SimAHNnD(ahn, X)
plot(x, y, type = 'l')
lines(x, ysim, type = 'l', col = 'red')
legend(-1, 1, c('Original', 'Simulation'), col = c(1,2), lty = c(1,3), cex = 0.8)
A summary of the network can be obtained with the summary
command.
summary(ahn)
##
## Artificial Hydrocarbon Network trained:
##
## Number of molecules:
## 5
##
## Learning factor:
## 0.01
##
## Overall error:
## 0.2875
##
## Centers of the molecules:
## x
## molecule1 0.36691689
## molecule2 0.06369617
## molecule3 -0.96254244
## molecule4 -0.68667076
## molecule5 -0.94715030
##
## Molecules:
## Molecule 1:
## x
## C1 -4.092
## H11 32.303
## H12 -60.912
## H13 31.995
##
## Molecule 2:
## x
## C2 0.014
## H21 2.909
## H22 1.057
##
## Molecule 3:
## x
## C3 11.446
## H31 25.479
## H32 12.796
##
## Molecule 4:
## x
## C4 -0.228
## H41 2.049
## H42 1.101
##
## Molecule 5:
## x
## C5 -28.744
## H51 -94.902
## H52 -108.461
## H53 -41.124
Finally, the network itself can be plotted with the plot
command. The text of the carbon of the first molecule is in red.
plot(ahn)
# Create data
set.seed(12321)
t <- seq(0, 15, 0.01)
X <- data.frame(x1 = cos(t), x2 = t)
Y <- data.frame(y = sin(t))
# Create the Sigma dataset
Sigma <- list(X = X, Y = Y)
# Create network
ahn <- AHNnD(Sigma, 5, 0.01, 100)
# Simulate
ysim <- SimAHNnD(ahn, X)
plot(t, Y$y, type = 'l', col = 'black', xlab = 't', ylab = 'output')
lines(t, ysim, col = 'red')
legend(0, -0.5, c('Original', 'Simulation'), col = c(1,2), lty = c(1,3), cex = 0.6)
summary(ahn)
##
## Artificial Hydrocarbon Network trained:
##
## Number of molecules:
## 5
##
## Learning factor:
## 0.01
##
## Overall error:
## 0.2475
##
## Centers of the molecules:
## x1 x2
## molecule1 0.6871690 6.4083024
## molecule2 0.9325409 0.9380700
## molecule3 0.2068092 6.3405795
## molecule4 -0.8726707 3.4672676
## molecule5 0.8153233 14.7093439
##
## Molecules:
## Molecule 1:
## x1 x2
## C1 -1.619 -1.619
## H11 -3.170 -5.443
## H12 0.939 2.007
## H13 -0.226 -0.159
##
## Molecule 2:
## x1 x2
## C2 -0.013 -0.013
## H21 0.272 1.039
## H22 -0.248 -0.246
##
## Molecule 3:
## x1 x2
## C3 -6.842 -6.842
## H31 -0.364 3.826
## H32 -0.631 -0.249
##
## Molecule 4:
## x1 x2
## C4 11.201 11.201
## H41 -6.861 -17.287
## H42 -0.760 2.616
##
## Molecule 5:
## x1 x2
## C5 -0.077 -0.077
## H51 0.433 -0.352
## H52 -1.070 0.005
## H53 0.807 0.002
plot(ahn)