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(100) + 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, 500)
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')
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.3206
##
## Centers of the molecules:
## x
## molecule1 -0.6863629
## molecule2 -0.1316634
## molecule3 -0.1305425
## molecule4 -0.4445938
## molecule5 0.2160453
##
## Molecules:
## Molecule 1:
## x
## C1 -0.259
## H11 2.164
## H12 1.696
## H13 0.510
##
## Molecule 2:
## x
## C2 0.051
## H21 3.596
## H22 3.337
##
## Molecule 3:
## x
## C3 0.025
## H31 3.168
## H32 1.385
##
## Molecule 4:
## x
## C4 -0.104
## H41 2.600
## H42 1.704
##
## Molecule 5:
## x
## C5 -1.020
## H51 14.852
## H52 -31.136
## H53 16.293
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
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, 500)
# Simulate
ysim <- SimAHNnD(ahn, X)
plot(seq_along(Y$y), Y$y, type = 'l', col = 'black')
lines(seq_along(ysim), ysim, col = 'red')
summary(ahn)
##
## Artificial Hydrocarbon Network trained:
##
## Number of molecules:
## 5
##
## Learning factor:
## 0.01
##
## Overall error:
## 0.3523
##
## Centers of the molecules:
## x1 x2
## molecule1 -0.01279928 11.31135355
## molecule2 0.40496425 11.37478953
## molecule3 -0.53182701 0.46498680
## molecule4 -0.06948056 2.12529653
## molecule5 -0.51136596 8.93287402
##
## Molecules:
## Molecule 1:
## x1 x2
## C1 -0.459 -0.459
## H11 -0.872 -2.714
## H12 3.478 0.412
## H13 4.622 -0.015
##
## Molecule 2:
## x1 x2
## C2 -0.159 -0.159
## H21 1.056 -0.837
## H22 -0.653 0.066
##
## Molecule 3:
## x1 x2
## C3 -1.677 -1.677
## H31 4.262 1.013
## H32 -0.908 1.145
##
## Molecule 4:
## x1 x2
## C4 6.325 6.325
## H41 -4.521 -9.724
## H42 -0.931 1.448
##
## Molecule 5:
## x1 x2
## C5 -0.835 -0.835
## H51 0.286 -1.907
## H52 -0.270 0.578
## H53 0.039 -0.037
plot(ahn)