Skip to main content
2 of 9
added 36 characters in body
plannapus
  • 9k
  • 22
  • 47

Wolves with a Collective Memory

A Wolf pack in R

Yet untested (i am very unfamiliar with the java machinery).
The idea of this wolf pack is that it keeps in memory (in a file called collective_memory.txt) who's alive or dead, check what the dead wolves and the alive wolves used as attacks and change the choice probability accordingly.

Here is the R code:

input <- scan(file("stdin"))[1]
type <- substr(input,1,1)
id <- substr(input,2,3)
f <- "collective_memory.txt"
if(nchar(input)>3){info <- substr(input,4,nchar(input))}else{info <- NULL}
attack <- function(id,info,f){
    if(info%in%c("B","L")){choice <- "S"}
    if(info=="S"){choice <- "P"}
    if(info=="W"){
        if(file.exists(f)){
            memory <- read.table(f,sep="\t",header=TRUE)
            dead <- memory$ID[memory$Status=="Dead"]
            veteran <- memory[memory$Attack!="", ]
            if(length(veteran)>0){
                deadvet <- table(factor(veteran$Attack[veteran$ID%in%dead],levels=c("R","P","S","")))
                livevet <- table(factor(veteran$Attack[!veteran$ID%in%dead],levels=c("R","P","S","")))
                probR <- livevet['R']/(livevet['R']+deadvet['R'])
                probS <- livevet['S']/(livevet['S']+deadvet['S'])
                probP <- livevet['P']/(livevet['P']+deadvet['P'])
                choice <- sample(c("S","P","R"),1,prob=c(probS,probP,probR))
                memory <- rbind(memory, data.frame(ID=id, Status="Alive", Attack=choice))
                }else{
                    choice <- sample(c("S","P","R"),1)
                    memory <- rbind(memory, data.frame(ID=id, Status="Alive", Attack=choice))
                    }
            }else{
                choice <- sample(c("S","P","R"),1)
                memory <- data.frame(ID=id, Status="Alive", Attack=choice)
                }
            write.table(memory,file=f,sep="\t",row.names=FALSE, col.names=TRUE)
    }
    paste(choice,id,sep="")
}
move <- function(id,info){
    surroundings <- matrix(strsplit(info,"")[[1]],ncol=3,nrow=3,byrow=TRUE)
    stones <- which(surroundings=="S")
    bears <- which(surroundings=="B")
    killables <- sort(c(stones, bears))
    if(length(killables)>0){
        pos <- c("H","L","H","U","H","D","H","R")
        choice <- pos[killables]
    if(any(choice!="H")){choice <- choice[choice!="H"][1]}else{choice <- sample(c("H","U","L","R","D"),1,prob=c(4,1,1,1,1))}
}else{choice <- sample(c("H","U","L","R","D"),1,prob=c(4,1,1,1,1))}
    paste(choice,id,sep="")
}
initialize <- function(id,f){
    if(file.exists(f)){
        memory <- read.table(f,sep="\t",header=TRUE)
        memory <- rbind(memory,data.frame(ID=id,Status="Alive",Attack=""))
        }else{memory <- data.frame(ID=id,Status="Alive",Attack="")}
    confirmed_dead <- memory$ID[memory$Status=="Dead"]
    last_seen <- memory[!memory$ID%in%confirmed_dead,]
    last_seen <- last_seen[last_seen$Attack="",]
    lid <- table(last_seen$ID)
    turns <- max(lid)
    dead <- lid[lid<(turns-1)]
    if(length(dead)>0){
        dead_id <- names(dead)
        for(i in dead_id){
            memory <- rbind(memory, data.frame(ID=i, Status="Dead", Attack=""))
            }
        }
    write.table(memory,file=f,sep="\t",row.names=FALSE, col.names=TRUE)
    paste("K",id,sep="")
    }
result <- switch(type,"A"=attack(id,info,f),"M"= move(id,info),"S"=initialize(id,f))
cat(result,"\n",sep="")

It uses @ProgrammerDan wrapper (thank you!), with WolfCollectiveMemory as custom name and "Rscript WolfCollectiveMemory.R" as invocation.

plannapus
  • 9k
  • 22
  • 47