0

I am using R package Rsolnp to solve some constrained optimization problems. After solving the optimization problem, it shows the notification and optimization results automatically. Does anyone know how to silence it? I have to write a loop to run solnp in every iteration. Keep showing results is very annoying.

Thanks in advance!

AYY
  • 59
  • 2
  • 1
    Take a look at [How to suppress output](https://stackoverflow.com/q/2501895/4752675) – G5W Jan 21 '18 at 00:09

1 Answers1

4

You don't give much to go on, but you can set the trace control parameter via the control argument to solnp. For example, using the exmaple from ?solnp:

fn1 <- function(x) {
  exp(x[1]*x[2]*x[3]*x[4]*x[5])
}
eqn1 <- function(x) {
  z1 <- x[1]*x[1]+x[2]*x[2]+x[3]*x[3]+x[4]*x[4]+x[5]*x[5]
  z2 <- x[2]*x[3]-5*x[4]*x[5]
  z3 <- x[1]*x[1]*x[1]+x[2]*x[2]*x[2]
  return(c(z1,z2,z3))
}
x0 <- c(-2, 2, 2, -1, -1)

Then we have

> powell <- solnp(x0, fun = fn1, eqfun = eqn1, eqB = c(10, 0, -1),
+                 control = list(trace = 1)) # default

Iter: 1 fn: 0.03526  Pars:  -1.59385  1.51051  2.07795 -0.81769 -0.81769
Iter: 2 fn: 0.04847  Pars:  -1.74461  1.62029  1.80509 -0.77020 -0.77020
Iter: 3 fn: 0.05384  Pars:  -1.71648  1.59482  1.82900 -0.76390 -0.76390
Iter: 4 fn: 0.05395  Pars:  -1.71713  1.59570  1.82727 -0.76364 -0.76364
Iter: 5 fn: 0.05395  Pars:  -1.71714  1.59571  1.82725 -0.76364 -0.76364
Iter: 6 fn: 0.05395  Pars:  -1.71714  1.59571  1.82725 -0.76364 -0.76364
solnp--> Completed in 6 iterations

so set trace = 0 to suppress this:

> powell <- solnp(x0, fun = fn1, eqfun = eqn1, eqB = c(10, 0, -1),
+                 control = list(trace = 0))> 
> # look, no output

There's a similar argument in other functions in the package.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453