最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Writing an independent routine to add a minimization constraint in solnl() - Stack Overflow

programmeradmin2浏览0评论

I need to find a way to write an independent routine to add a particular constraint to a minimization problem in solnl()

With a concrete example, one has this

##### for optimization
library(MASS)
library(NlcOptim)

##############################

###objective function to minimize

objfun=function(x){
return(x[1]^2+x[2]^2+1)  
}

### our initial constraint to be improved

constraint = function(x){
f = NULL
f = rbind(f, x[2] - 2)

return(list(ceq = f, c= NULL))

}

#### solve now
x0 = c(1,1)
solnl(x0,objfun=objfun,confun=constraint)

Say that we want to add the new constraint

h(x) = x[1] - 1

which makes the problem easy to solve.

I would like to have a function named add_constraint(h) that would generate the same usable output as

constraint = function(x){
 f = NULL
 f = rbind(f, x[2] - 2)
 f = rbind(f,x[1] - 1 )
 return(list(ceq = f, c= NULL))

}

I have tried the following without success

f = paste0("x[1] - 1")
h = paste0("x[2] - 2")

local = rbind(f,h)

# then the routine
add_constraint = function(local){

 function(x){
  f= NULL
  for( i in 1:2)
  f = rbind(f, local[i])
  return(list(ceq=f, c= NULL))
  }
 }

 constraint = add_constraint(local)

I need to find a way to write an independent routine to add a particular constraint to a minimization problem in solnl()

With a concrete example, one has this

##### for optimization
library(MASS)
library(NlcOptim)

##############################

###objective function to minimize

objfun=function(x){
return(x[1]^2+x[2]^2+1)  
}

### our initial constraint to be improved

constraint = function(x){
f = NULL
f = rbind(f, x[2] - 2)

return(list(ceq = f, c= NULL))

}

#### solve now
x0 = c(1,1)
solnl(x0,objfun=objfun,confun=constraint)

Say that we want to add the new constraint

h(x) = x[1] - 1

which makes the problem easy to solve.

I would like to have a function named add_constraint(h) that would generate the same usable output as

constraint = function(x){
 f = NULL
 f = rbind(f, x[2] - 2)
 f = rbind(f,x[1] - 1 )
 return(list(ceq = f, c= NULL))

}

I have tried the following without success

f = paste0("x[1] - 1")
h = paste0("x[2] - 2")

local = rbind(f,h)

# then the routine
add_constraint = function(local){

 function(x){
  f= NULL
  for( i in 1:2)
  f = rbind(f, local[i])
  return(list(ceq=f, c= NULL))
  }
 }

 constraint = add_constraint(local)
Share Improve this question edited Mar 18 at 18:09 SuperLeo asked Mar 18 at 15:30 SuperLeoSuperLeo 831 silver badge6 bronze badges 2
  • I do not understand where you want to go – SuperLeo Commented Mar 18 at 19:23
  • Doesn't matter, it is closely related to what is given in an answer now. Have deleted it therefore. – Friede Commented Mar 19 at 0:23
Add a comment  | 

1 Answer 1

Reset to default 1

You could add sth in the body, using str2lang() since you have strings, and move the return one forward.

> constraint  ## old FUN
function(x) {
  f <- NULL
  f <- rbind(f, x[2] - 2)
  return(list(ceq=f, c=NULL))
}
> add_constraint <- \(x) {
+   bd <- body(constraint)
+   rt <- bd[[length(bd)]]
+   bd[[length(bd)]] <- str2lang(x)
+   bd[[length(bd) + 1]] <- rt
+   body(constraint) <- bd
+   constraint
+ }
> constraint <- add_constraint('f <- rbind(f, x[1] - 1)')
> constraint  ## new FUN
function (x) 
{
    f <- NULL
    f <- rbind(f, x[2] - 2)
    f <- rbind(f, x[1] - 1)
    return(list(ceq = f, c = NULL))
}
发布评论

评论列表(0)

  1. 暂无评论