### fourdiffs finds the number of steps to zero. ### steps includes the last step to zero, so N means (N-1) nonzero. fourdiffs <- function (a=1,b=2,c=3,d=4, printf=FALSE) { #fourdiffs #A function to compute the number of steps need to get all zeroes #from absolute value of successive differences. #Written for EMAT 6680, Fall 2011, Adam Molnar. #a, b, c, and d are the four starting values. #printf is FALSE for no printing, TRUE for printing. #diffvec is the updating vector. I do the first step outside # the loop so I can use an easy test in the while loop. #In absolute value sum(diffvec) = 0 only when all = 0. diffvec <- c(abs(b-a), abs(c-b), abs(d-c), abs(a-d)) steps <- 1 if(printf) {print(diffvec)} while(sum(diffvec) != 0) { diffvec <- c(abs(diffvec[2]-diffvec[1]), abs(diffvec[3]-diffvec[2]), abs(diffvec[4]-diffvec[3]), abs(diffvec[1]-diffvec[4])) if(printf) {print(diffvec)} steps <- steps + 1 if (steps > 100) {break} #just in case } return(steps) } ### diffloop is the base grid search. diffloop <- function (maxd=5, mind=1) { #diffloop #Test different values of a, b, c, d in Assignment 12. #Written for EMAT 6680, Fall 2011, Adam Molnar. #I believe I can use integers and not decimals. #Irrationals might be different. #maxd is the maximum d, and mind is the minimum for partial eval. best <- 0 bestlist <- c(0,0,0,0) #Because of translation, a doesn't need a loop. a = zero. #Also, the set 0, b, c, d is equivalent to 0, d, c, b. # This means I can cap b at d on each loop. # It's d^3 / 2 checks, down fron d^4. Much better! for(d in mind:maxd) { for(c in 0:maxd) { for(b in 0:d) { results <- fourdiffs(0,b,c,d) #print(results) if(results > best) { #maxa <- max(alist*(results==max(results))) coverb <- ifelse(b>0, c/b, 0) doverc <- ifelse(c>0, d/c, 0) cat("Trials:", results, "A: 0 B:", b, "C:", c, "D:",d, "C/B:", coverb, "D/C:", doverc, "\n") best <- results bestlist <- c(0,b,c,d) } } } } return(c(best, bestlist)) } #### tightdiffs uses C/B and D/C rations, floating points, and move size. tightdiffs <- function (mincb=2.80,maxcb=2.85,bycb=0.01,mindc=2.15, maxdc=2.20,bydc=0.01) { #tightdiffs #Test different values of a, b, c, d in Assignment 12. #Written for EMAT 6680, Fall 2011, Adam Molnar. #This version is after identifying the range of high trial counts. #The inputs are for seq commands. a = 0 and b = 1. cb <- seq(from=mincb,to=maxcb,by=bycb) dc <- seq(from=mindc,to=maxdc,by=bydc) best <- 0 bestlist <- c(0,0,0,0) for(d in dc) { for(c in cb) { results <- fourdiffs(0,1,c,d*c) #cat(results, c, d, "\n") if(results > best) { cat("Trials:", results, "A: 0 B: 1 C:", c, "D:", d*c, "D/C:", d, "\n") best <- results bestlist <- c(0,1,c,d*c) } } } return(c(best, bestlist, d)) } #### tightdiffN uses integers and move size 1. tightdiffN <- function (minc=280, maxc=285, mind=620, maxd=625, b=1) { #tightdiffN #Test different values of c, d in Assignment 12. #Written for EMAT 6680, Fall 2011, Adam Molnar. #This version is after identifying the range of high trial counts. #The inputs are for seq commands. a = 0. cb <- seq(from=minc,to=maxc,by=1) dc <- seq(from=mind,to=maxd,by=1) best <- 0 bestlist <- c(0,0,0,0) for(d in dc) { for(c in cb) { results <- fourdiffs(0,b,c,d) #cat(results, c, d, "\n") if(results > best) { cat("Trials:", results, "A: 0 B:", b, "C:", c, "D:", d, "\n") best <- results bestlist <- c(0,b,c,d) } } } return(c(best, bestlist)) }