scratch/OldScratch/test_against_mleGP.R

rm(list = ls())

borehole <- function(x) {
  rw <- x[, 1] * (0.15 - 0.05) + 0.05
  r <-  x[, 2] * (50000 - 100) + 100
  Tu <- x[, 3] * (115600 - 63070) + 63070
  Hu <- x[, 4] * (1110 - 990) + 990
  Tl <- x[, 5] * (116 - 63.1) + 63.1
  Hl <- x[, 6] * (820 - 700) + 700
  L <-  x[, 7] * (1680 - 1120) + 1120
  Kw <- x[, 8] * (12045 - 9855) + 9855
  
  m1 <- 2 * pi * Tu * (Hu - Hl)
  m2 <- log(r / rw)
  m3 <- 1 + 2 * L * Tu / (m2 * rw ^ 2 * Kw) + Tu / Tl
  return(m1 / m2 / m3)
}

piston <- function(xx)
{
  M  <- xx[,1]*30 + 30
  S  <- xx[,2]*0.015 + 0.005
  V0 <- xx[,3]*0.008 + 0.002
  k  <- xx[,4]*4000 + 1000
  P0 <- xx[,5]*20000+90000
  Ta <- xx[,6]*6 + 290
  T0 <- xx[,7]*20 + 340
  
  Aterm1 <- P0 * S
  Aterm2 <- 19.62 * M
  Aterm3 <- -k*V0 / S
  A <- Aterm1 + Aterm2 + Aterm3
  
  Vfact1 <- S / (2*k)
  Vfact2 <- sqrt(A^2 + 4*k*(P0*V0/T0)*Ta)
  V <- Vfact1 * (Vfact2 - A)
  
  fact1 <- M
  fact2 <- k + (S^2)*(P0*V0/T0)*(Ta/(V^2))
  
  C <- 2 * pi * sqrt(fact1/fact2)
  return(C)
}


wingweight <- function(xx)
{
  Sw      <- xx[,1]*50+150
  Wfw     <- xx[,2]*80+220
  A       <- xx[,3]*4 + 6
  LamCaps <- (xx[,4]*20-10)*pi/180
  q       <- xx[,5]*(45-16)+16
  lam     <- xx[,6]*0.5+0.5
  tc      <- xx[,7]*0.1+0.08
  Nz      <- xx[,8]*3.5 + 2.5
  Wdg     <- xx[,9]*800+1700
  Wp      <- xx[,10]*(0.08-0.025)+0.025
  
  fact1 <- 0.036 * Sw^0.758 * Wfw^0.0035
  fact2 <- (A / ((cos(LamCaps))^2))^0.6
  fact3 <- q^0.006 * lam^0.04
  fact4 <- (100*tc / cos(LamCaps))^(-0.3)
  fact5 <- (Nz*Wdg)^0.49
  
  term1 <- Sw * Wp
  
  y <- fact1*fact2*fact3*fact4*fact5 + term1
  return(y)
}

otlcircuit <- function(xx)
{
  Rb1  <- xx[,1]*100+50
  Rb2  <- xx[,2]*45 + 25
  Rf   <- xx[,3]*2.5 + 0.5
  Rc1  <- xx[,4]*1.3 + 1.2
  Rc2  <- xx[,5]*.95 + .25
  beta <- xx[,6]*250+50
  
  Vb1 <- 12*Rb2 / (Rb1+Rb2)
  term1a <- (Vb1+0.74) * beta * (Rc2+9)
  term1b <- beta*(Rc2+9) + Rf
  term1 <- term1a / term1b
  
  term2a <- 11.35 * Rf
  term2b <- beta*(Rc2+9) + Rf
  term2 <- term2a / term2b
  
  term3a <- 0.74 * Rf * beta * (Rc2+9)
  term3b <- (beta*(Rc2+9)+Rf) * Rc1
  term3 <- term3a / term3b
  
  Vm <- term1 + term2 + term3
  return(Vm)
}

#d = 10
#testf<-function (x) {  return(wingweight(x))} 

#d = 8
#testf<-function (x) {  return(borehole(x))} 

#d = 7
# testf<-function (x) {  return(piston(x))} 

d = 6
testf<-function (x) {  return(otlcircuit(x))} 

Npred <- 1000
library("lhs")
Xp = randomLHS(Npred, d)
Yp = testf(Xp)


SGGP = SGGPcreate(d,100) #create the design.  it has so many entries because i am sloppy
Y = testf(SGGP$design) #the design is $design, simple enough, right?
SGGP = SGGPfit(SGGP,Y)
SGGPTS=SGGPappend(SGGP,200,selectionmethod="TS")
YTS = testf(SGGPTS$design) #the design is $design, simple enough, right?
SGGPTS = SGGPfit(SGGPTS,YTS)
SGGPTS=SGGPappend(SGGPTS,300,selectionmethod="TS")
YTS = testf(SGGPTS$design) #the design is $design, simple enough, right?
SGGPTS = SGGPfit(SGGPTS,YTS)
SGGPTS=SGGPappend(SGGPTS,600,selectionmethod="TS")
YTS = testf(SGGPTS$design) #the design is $design, simple enough, right?
SGGPTS = SGGPfit(SGGPTS,YTS)
PredTS = SGGPpred(Xp,SGGPTS)
mean(abs(Yp-PredTS$mean)^2)  #prediction should be much better
mean(abs(Yp-PredTS$mean)^2/PredTS$var+log(PredTS$var)) #score should be much better


library(mlegp) #this is gonna be slow, trying to reduce predictions
Xmlegp = randomLHS(200, d)
Ymlegp = testf(Xmlegp)
fitMulti = mlegp(Xmlegp, Ymlegp,constantMean = 1, simplex.ntries = 1)

Vprime = predict(fitMulti,Xp, se.fit = TRUE)
predmlegp = Vprime$fit
varmlegp = (10^(-8)+Vprime$se.fit)^2
mean(abs(Yp-predmlegp)^2)  #prediction should be much better
mean(abs(Yp-predmlegp)^2/varmlegp+log(varmlegp)) #score should be much better
CollinErickson/CGGP documentation built on Feb. 6, 2024, 2:24 a.m.