-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadaptconcept_sFFLHD.R
More file actions
133 lines (123 loc) · 5.01 KB
/
adaptconcept_sFFLHD.R
File metadata and controls
133 lines (123 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
adapt.concept.sFFLHD <- function(func,D=2,L=5,g=3,level=1,
lims=matrix(c(0,1),D,2,byrow=T),
dat=NULL,
lims.second=NULL,
mod=NULL) {
browser()
# Adapt concept without trees
print(paste('At level',level))
first.time <- is.null(dat)
if(first.time) {
lims.second <- list()
dat$X <- matrix(NA,0,D)
dat$Z <- c()
dat$s <- sFFLHD.seq$new(D=D,L=L)
dat$Xnotrun <- matrix(NA,0,D)
#mod <- UGP::UGP(package = "mlegp")
}
mod <- UGP::UGP(package = "GPfit")
#get sample
if(D==2 & !first.time) points(dat$Xnotrun,col='yellow')
notrun.torun <- which(apply(dat$Xnotrun,1,is.in.lims,lims))
if(length(notrun.torun)>L) {notrun.torun <- notrun.torun[1:L]}
Xnew <- dat$Xnotrun[notrun.torun,]
if(length(notrun.torun)==1) { # If only one row it will be numeric, not matrix, need to fix it
Xnew <- matrix(Xnew,nrow=1)
}
dat$Xnotrun <- dat$Xnotrun[-notrun.torun,]
while(nrow(Xnew)<L) {
Xadd <- dat$s$get.batch()
in.lims <- apply(Xadd,1,is.in.lims,lims)
if (D == 2) {if(!first.time) points(Xadd,col=in.lims+2)}
Xnew <- rbind(Xnew,Xadd[in.lims,])
dat$Xnotrun <- rbind(dat$Xnotrun,Xadd[!in.lims,])
}
Znew <- apply(Xnew,1,func)
dat$X <- rbind(dat$X,Xnew)
dat$Z <- c(dat$Z,Znew)
while (TRUE) {
# fit+plot
mod$update(Xall=dat$X,Zall=dat$Z)
if (D == 2) {par(mfrow=c(2,1))}
# Plot fitted values
if(anyDuplicated(dat$X)>0) browser()
if (D == 2) {
xlim <- lims[1,]
ylim <- lims[2,]
contourfilled.func(mod$predict,batchmax=500)
points(dat$X,pch=19)
rect(xlim[1],ylim[1],xlim[2],ylim[2],lwd=5)
abline(v=xlim[1] + 1:(g-1)/g * (xlim[2]-xlim[1]),h=ylim[1] + 1:(g-1)/g * (ylim[2]-ylim[1]))
# Plot s2 predictions
contourfilled.func(mod$predict.var,batchmax=500)
points(dat$X,pch=19)
rect(xlim[1],ylim[1],xlim[2],ylim[2],lwd=5)
abline(v=xlim[1] + 1:(g-1)/g * (xlim[2]-xlim[1]),h=ylim[1] + 1:(g-1)/g * (ylim[2]-ylim[1]))
}
browser()
#find lmse
mod.se.pred.func <- mod$predict.var
mses.grid <- outer.d1n(rep(g,D),
func=
(function(...){
ii <- c(...)
#msfunc(mod.se.pred.func, apply(lims,1,function(irow){irow[1]+()*c(ii)/g}))
msfunc(mod.se.pred.func,
lims=t(sapply(1:D,function(jj){
lims[jj,1]+(lims[jj,2]-lims[jj,1])*c(ii[jj]-1,ii[jj])/g
})),
pow=1L,
batch=T
)
})
)
mses <- sapply(1:D,function(d){apply(mses.grid,d,mean)})
maxmse <- max(mses)
maxmse.ind <- which(mses==maxmse,arr.ind=T)
# Refit maxmse.levelup???
if(level>1) {
maxmse.levelup <- sapply(1:(level-1),function(i){msfunc(mod.se.pred.func,lims.second[[i]])})
}
if ( level==1 ||
(level>=2 & maxmse > maxmse.levelup) ) {
secondmaxmse <- maxN(mses,2)
secondmaxmse.ind <- which(mses==secondmaxmse,arr.ind=T)
lims.next <- lims
lims.next[maxmse.ind[2],] <- lims[maxmse.ind[2],1]+(lims[maxmse.ind[2],2]-lims[maxmse.ind[2],1])*c(maxmse.ind[1]-1,maxmse.ind[1])/g
lims.nextsecond <- lims
lims.nextsecond[secondmaxmse.ind[2],] <- lims[secondmaxmse.ind[2],1]+(lims[secondmaxmse.ind[2],2]-lims[secondmaxmse.ind[2],1])*c(secondmaxmse.ind[1]-1,secondmaxmse.ind[1])/g
lims.second[[level]] <- lims.nextsecond
if(D==2) {
rect(lims.nextsecond[1,1],lims.nextsecond[2,1],lims.nextsecond[1,2],lims.nextsecond[2,2],lwd=2,border='black')
rect(lims.next[1,1],lims.next[2,1],lims.next[1,2],lims.next[2,2],lwd=5,border='red')
rect(lims.next[1,1],lims.next[2,1],lims.next[1,2],lims.next[2,2],col=1,angle=45,density=6+2*level^2)
print(paste('Diving to xlim, ylim:',lims.next[1,1],lims.next[1,2],lims.next[2,1],lims.next[2,2],collapse = ''))
}
# RECURSIVE STEP HERE
dat <- adapt.concept.sFFLHD(func=func,D=D,g=g,level=level+1,
lims=lims.next,
dat=dat,
lims.second=lims.second,
mod=mod
)
print(paste('Back at level',level))
} else {
print(paste('Jumping back up'))
return(dat)
}
}
}
if(F) {
source("adaptconcept_helpers.R")
source("sFFLHD.R")
require(mlegp)
require(GPfit)
require(contourfilled)
source('LHS.R')
source("adaptconcept.R")
gaussian1 <- function(xx) exp(-sum((xx-.5)^2)/2/.1)
adapt.concept.sFFLHD(gaussian1)
adapt.concept.sFFLHD(gaussian1,D=3)
sinumoid <- function(xx){sum(sin(2*pi*xx*3)) + 10/(1+exp(-80*(xx[[1]]-.5)))}; contourfilled.func(sinumoid)
adapt.concept.sFFLHD(sinumoid)
}