SHARE
TWEET

Untitled

a guest Aug 20th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using NLPModels
  2. using SolverTools
  3.  
  4.  
  5. function coordinate_search3(nlp :: AbstractNLPModel;
  6.                 tol :: Real = 1e-12,
  7.                 α :: Float64 = 1.0,
  8.                 x :: AbstractVector = nlp.meta.x0,
  9.                 max_time :: Float64  = 3.0,
  10.                 max_eval :: Int = -1,
  11.                 β :: Real = 2.0,
  12.                 greedy :: Bool = true)
  13.  
  14.   k = 0
  15.   el_time = 0.0
  16.   start_time = time()
  17.   tired = neval_obj(nlp) > max_eval >= 0 || el_time > max_time
  18.   optimal = α < tol
  19.   T = eltype(x)
  20.   status =:unknown
  21.   @info log_header([:iter, :f, :H],[Int, T, T], hdr_override=Dict(:f=>"f(x)", :H=>"α"))
  22.  
  23.   f = obj(nlp,x)
  24.   best_f = f
  25.   best_i = 0
  26.  
  27.   while !(optimal || tired)
  28.     @info log_row(Any[k, f, α])
  29.     xt = copy(x)
  30.     success = false
  31.     done = false
  32.     i = 1
  33.     while !(done || i > nlp.meta.nvar)
  34.       for s in [-1,1]
  35.     xt[i] = x[i] + α * s
  36.     f_xt = obj(nlp,xt)
  37.     if (f_xt < best_f)
  38.       success = true
  39.       best_f = f_xt
  40.       best_i = i * s
  41.       if greedy
  42.         done = true
  43.         break
  44.       end
  45.     end
  46.    
  47.     xt[i] = x[i]
  48.       end
  49.       i += 1
  50.     end
  51.  
  52.     if success
  53.       x[abs(best_i)] = x[abs(best_i)] + sign(best_i) *  α
  54.       f = obj(nlp,x)
  55.     else
  56.       α /= β
  57.     end
  58.  
  59.     k += 1
  60.     el_time = time() - start_time
  61.     tired = neval_obj(nlp) > max_eval >= 0 || el_time > max_time
  62.     optimal = α <= tol
  63.   end
  64.  
  65.   if (optimal)
  66.     status =:unknown
  67.   elseif (tired)
  68.     if (neval_obj(nlp) > max_eval && max_eval >= 0)
  69.       status =:max_eval
  70.     elseif (el_time >= max_time)
  71.       status =:max_time
  72.     end
  73.   end
  74.  
  75.   return GenericExecutionStats(status, nlp, solution=x, objective=f,
  76.                                 iter = k, elapsed_time = el_time)
  77.  
  78. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top