0

I have a problem with results of loop in loop function. It counts inside loop only once and choose the best solution for the first raw and then stop.

I would like to remember the best solution for every row of the matrix zmienne. What am I doing wrong?

schaffer <- function(xx)
{x1 <- xx[1]
 x2 <- xx[2]

 fact1 <- (sin(x1^2-x2^2))^2 - 0.5
 fact2 <- (1 + 0.001*(x1^2+x2^2))^2

 y <- 0.5 + fact1/fact2
 return(y)
}

gradient_descent <- function(func, step, niter) { 

  N <- 3 #N- number of random points
  zmienne <- matrix(runif(N*2, min = -100, max = 100), N, 2)
  print(zmienne)
  h = 0.001;
  iter_count = 0;

  for (i in 1:N) {
  x_0 <- zmienne[i,]
  x_n = x_0;

    for (j in 1:niter) {
      func_grad = (func(x_n+h) - func(x_n))/h;
      if (abs(func_grad) < 0.0001) { break; }
      x_n = x_n - step * func_grad;
      print(x_n)
      iter_count = iter_count + 1
    }
  }
return(list(iterations = niter, best_value = func_grad, best_state = x_n, x0=x_0))
    }

solution_m1 <- gradient_descent(schaffer, 0.1, 20)
solution_m1
user3463225
  • 401
  • 9
  • 19
  • Can you please provide your data (or a subset) se we can test the output? – Molx Mar 24 '15 at 22:01
  • Consider allocating `iterations`, `best_value`, `best_state` and `x0` with `NA` outside the first loop and add the best result into these variables for each row. Then return outside the outer loop a list made of the `iterations`, `best_value`, `best_state` and `x0`. – ConfusedMan Mar 24 '15 at 22:10
  • Can you explain me the modification by adding your advices into the code? should i add return after closing inside loop and then after closing the main loop? – user3463225 Mar 24 '15 at 22:13

2 Answers2

0

The return should not be inside the inside loop but at then end of the function.

cmbarbu
  • 4,354
  • 25
  • 45
  • 1
    @Pascal I'm sorry but it provides a clear and concise answer to the question "what am I doing wrong?", unless you consider we should systematically write code in an answer. I edited though to make clearer what should be done. – cmbarbu Mar 25 '15 at 01:52
  • Yes, we should provide code. Otherwise, such reply must be written as a comment. –  Mar 25 '15 at 01:54
  • @Pascal Any article in stackoverflow policy about that I would have missed? – cmbarbu Mar 25 '15 at 02:04
  • Your answer is still there. So please, stop to whine. –  Mar 25 '15 at 02:40
  • 1
    @Pascal I'm not whining I'm asking you for a reference about a comment I don't understand. – cmbarbu Mar 25 '15 at 02:41
0

I think this is what you want:

gradient_descent <- function(func, step, niter) { 

  N <- 3 #N- number of random points
  zmienne <- matrix(runif(N*2, min = -100, max = 100), N, 2)
  print(zmienne)
  h = 0.001;
  iter_count = 0;
  best.vals <- NULL
  for (i in 1:N) {
    x_0 <- zmienne[i,]
    x_n = x_0;

    for (j in 1:niter) {
      func_grad = (func(x_n+h) - func(x_n))/h;
      if (abs(func_grad) < 0.0001) { break; }
      x_n = x_n - step * func_grad;
      print(x_n)
      iter_count = iter_count + 1
    } 
    best.vals <- c(best.vals, func_grad)
  }

  return(list(iterations = iter_count, best_value = best.vals, best_state = x_n, x0=x_0))
}

solution_m1 <- gradient_descent(schaffer, 0.1, 20)
solution_m1
Molx
  • 6,816
  • 2
  • 31
  • 47
  • Thanks, but this code return $best_value=func_grad for the last row of zmienne. I want to return the best_value from all.vals - in my case its gonna be the smallest positive value to find global minimum – user3463225 Mar 24 '15 at 22:27
  • If I understood it correctly than it's an easy fix, I updated the post, try again. – Molx Mar 24 '15 at 22:48
  • not exactly, because now i have three best values and best_state is one - for the last value of best values. I want to return best_values for every point - so three best values and from those values choose the best one - the smallest positive - and then return best_state=x_n for this value of best value, not the last x_n – user3463225 Mar 24 '15 at 22:55