Constrain opmtimization parameters

Hi there:

I have been trying using this function provided for free in the exchange. I don't know how to program in R. The function works fine when installed in 6.5 but I get results of the 'Hyp.b' value lower than 0 and higher than 1 which is not desirable for this analysis. I have been reading that there is way to constraint the optimization using an alternate methods. Can somebody help me to how to implement that Hyp.b only vary from zero to 1 in this script?

 

#Simple DCA - Hyperbolic fitting

#Technique used: Simple DCA - Hyperbolic fitting

#Programing Language: TERR V4.1

#Required Packages: None

#Description:This Data Function calculates a Hyperbolic DCA using your production data.# [TERR] Basic DCA

 

# Peter Shaw

# "Fri Jul 08 14:44:24 2016"

 

# Inputs

#   col.Date        # [Analysis Data].[${well.date}]

#   col.Production  # [Analysis Data].[${well.production}]

 

# Outputs

#   Hyp.qi

#   Hyp.b 

#   Hyp.Di

 

#TIBCO Component Exchange License

#Copyright (c) 2016 TIBCO Software Inc. All Rights Reserved.

#Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

#1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

#2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

#3. Neither the name of TIBCO Software Inc.  nor the names of any contributors may  be used to endorse or promote products derived from this software without specific prior written permission.

#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT OWNER AND CONTRIBUTORS  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE

 

TimeStamp=paste(date(),Sys.timezone())

tdir = 'C:/Demo' # place to store diagnostics if it exists (otherwise do nothing)

if(file.exists(tdir) && file.info(tdir)$isdir) suppressWarnings(try(save(list=ls(), file=paste(tdir,'/BasicDCA.in.RData',sep=''), RFormat=T )))

 

# remove(list=ls()); load(file='C:/Demo/BasicDCA.in.RData'); print(TimeStamp)

 

#------------------------------------------------------------------------------

Hyp.q.forward.fun = function( time, Hyp.qi, Hyp.b, Hyp.Di ){ # time in days

  # Peter Shaw

  # e.g. SPE 98042 (4)

  Hyp.q.theo = Hyp.qi*(1 + Hyp.b*Hyp.Di*time)^(-1/Hyp.b)

  return(Hyp.q.theo)

}

#------------------------------------------------------------------------------

residfun = function(x,x.days,y.prod){   

  Hyp.qi = x[1]

  Hyp.b  = x[2]

  Hyp.Di = x[3]

  q.theo = (365.25/12)*Hyp.q.forward.fun(

    time=x.days,

    Hyp.qi=Hyp.qi,

    Hyp.b=Hyp.b,

    Hyp.Di=Hyp.Di)

  #plot(x.days,y.prod); lines(x.days,q.theo)

  residual = sqrt(sum((q.theo-y.prod)^2))

  return(residual)

}

#------------------------------------------------------------------------------

min.data = 3

 

result = data.frame(

  time.days =  numeric(0),

  time.months = numeric(0),

  production = numeric(0),

  theo = numeric(0)

)

Hyp.qi       =as.numeric(NA)

Hyp.b        =as.numeric(NA)

Hyp.Di.daily =as.numeric(NA)

Hyp.Di.annual=as.numeric(NA)

 

ok = length(col.Production)>0

if(ok) ok = length(col.Date) == length(col.Production)

 

if(ok){

  u.order = order(col.Date)

 

  col.Date       = col.Date[u.order]

  col.Production = col.Production[u.order]

 

  t.days = as.numeric(difftime(col.Date,min(col.Date),units="days"))

  t.months = round(t.days*12/365.25)

 

  u0 = which.max(col.Production)

  u = u0:length(col.Production)

 

  x.months = t.months[u]

  x.days = t.days[u]

  y.prod = col.Production[u]

 

  x0 = c(y.prod[1]*12/365.25,1.00,0.005)

 

  optim.result = optim(

    par = x0,

    fn=residfun,

    x.days=x.days, y.prod=y.prod

  )

 

  Hyp.qi = optim.result$par[1]*(365.25/12) # Monthly

  Hyp.b  = optim.result$par[2]

  Hyp.Di.daily  = optim.result$par[3]

  Hyp.Di.annual = optim.result$par[3]*365.25 # Annualized

 

  result = data.frame(

    time.days   = x.days,

    time.months = x.months,

    production = y.prod

  )

}

(2) Answers

Login