# Implementing a Perceptron in R - where am I going wrong?

I am trying to build a perception in R. I have generated some test data that is clearly linearly separable. I have tried a variety of learning rates, but the classification rate just seems to bounce back and forth between around .2 and .8. I think the problem must be in the updateWeights function, but when I test the function with simple examples, it seems to update the weights to produce the correct output. I am using [1,0] and [0,1] for my two classes so that I can eventually try to expand using three classes of ([1,0,0], [0,1,0], [0,0,1]).

updateWeights <- function(x, w, a, target){
act <- x %*% w
out <- ifelse(act > 0.5, 1, 0)
e <- target - out
update <- (t(t(w) + as.numeric(e*a)) * x)
w <- ifelse(update == 0, w, update)
return(w)
}

trainANN <- function(train, w, a, train_classes){
ind <- sample(1:nrow(train), nrow(train), FALSE)
for(i in ind){
w <- updateWeights(train[i , ], w, a, train_classes[i, ])
}
return(w)
}

testANN <- function(test, w, test_classes){
r <- matrix(0, nrow(test))

for(i in 1:nrow(test)){
x <- test[i, ]
target <- test_classes[i, ]
out <- ifelse(x %*% w > 0.5, 1, 0)
r[i] <- ifelse(all(target == out), 1, 0)
}
return(sum(r) / length(r))
}

d <- matrix(c(runif(100, 0, 0.4), runif(100, 0.5, 1)), 100, 2, TRUE)
plot(d, col = rep(c("blue", "green"), each = 50))

classes <- matrix(rep(c(1,0,0,1), each = 50), 100, 2)

ind <- sample(1:nrow(d), nrow(d)*.9, FALSE)
train <- d[ind, ]
test <- d[-(ind), ]
train_classes <- classes[ind, ]
test_classes <- classes[-(ind), ]

nEpochs <- 500
results <- rep(0, nEpochs)
a <- 0.2

w <- matrix(runif(ncol(train)*ncol(classes), 0, 1), ncol(train), ncol(classes))

for(i in 1:nEpochs){
w <- trainANN(train, w, a, train_classes)
results[i] <- testANN(test, w, test_classes)
}

plot(results)
lines(results)
mean(results)