title: Rearrange rows to find maximum value/optimize function
body: <p>I'm trying to create a function that iterates over my data to find the highest value. The table below contains a dataframe with id and Revenues for each id. I'm trying to solve for the best arrangement of the id's so that I maximize the npv value. One major caveat being the following parameter. The mutation below finds if any previous id is within a delta of 5. If so the revenue is reduced by 40%.</p>
  <pre><code> df_fcn &lt;- df_fcn %&gt;% mutate(nearest = transform(., close_prev = random_sort[apply(`diag&lt;-`(m &lt;-
      as.matrix(dist(random_sort)), Inf) / upper.tri(m), 2, which.min)])) %&gt;% as_tibble()
    
    df_fcn &lt;- as_tibble(df_fcn$nearest)
    
    df_fcn &lt;-
      df_fcn %&gt;% mutate(Penalty =ifelse(id==1,1, if_else(abs(random_sort - close_prev) &gt; 5, 1, .6)))
  </code></pre>
  <pre><code>library(zoo)
  library(tidyverse)
  
  
  df&lt;-data.frame(id=c(1:500),Revenue=sample(500))
  
  dcf &lt;- function(x, r, t0=FALSE){
    # calculates discounted cash flows (DCF) given cash flow and discount rate
    #
    # x - cash flows vector
    # r - vector or discount rates, in decimals. Single values will be recycled
    # t0 - cash flow starts in year 0, default is FALSE, i.e. discount rate in first period is zero.
    if(length(r)==1){
      r &lt;- rep(r, length(x))
      if(t0==TRUE){r[1]&lt;-0}
    }
    x/cumprod(1+r)
  }
  
  npv &lt;- function(x, r, t0=FALSE){
    # calculates net present value (NPV) given cash flow and discount rate
    #
    # x - cash flows vector
    # r - discount rate, in decimals
    # t0 - cash flow starts in year 0, default is FALSE
    sum(dcf(x, r, t0))
    
  }  
    
  
  #Only reason I created this column is to have a way of changing the row arrangment. The final function n just iterates and randomly changes the numbers to find the highest value and arrangment.
  
  df&lt;- df%&gt;%mutate(random_sort= sample(nrow(df)))
  
  
  schedule_function=function(i){
    df_fcn&lt;-df%&gt;%mutate(random_sort=sample(random_sort))
    df_fcn &lt;- df_fcn %&gt;% mutate(nearest = transform(., close_prev = random_sort[apply(`diag&lt;-`(m &lt;-
      as.matrix(dist(random_sort)), Inf) / upper.tri(m), 2, which.min)])) %&gt;% as_tibble()
    
    df_fcn &lt;- as_tibble(df_fcn$nearest)
    
    df_fcn &lt;-
      df_fcn %&gt;% mutate(Penalty =ifelse(id==1,1, if_else(abs(random_sort - close_prev) &gt; 5, 1, .6))) %&gt;%
      mutate(Penalty = ifelse(is.na(Penalty), 1, Penalty))
    df_fcn&lt;- df_fcn%&gt;%mutate(Revenue_Penalized= Revenue*Penalty)
    df_fcn&lt;-df_fcn%&gt;%mutate(npv=npv(df_fcn$Revenue_Penalized,.2))
    max_npv&lt;&lt;-npv(df_fcn$Revenue_Penalized,.2)
    final_df&lt;&lt;-df_fcn
      
  }
  
  schedule_function(df)
  
  
  n &lt;- 1:10000
  MAX = df%&gt;%mutate(npv=-Inf)    ## initialize maximum
  for (i in 1:length(n)) {
    x &lt;- schedule_function(df)
    if (max(x$npv) &gt; max(MAX$npv)) MAX &lt;- x
  #unique(print(MAX$npv))  
  }
  </code></pre>