bvls.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. """Bounded-Variable Least-Squares algorithm."""
  2. from __future__ import division, print_function, absolute_import
  3. import numpy as np
  4. from numpy.linalg import norm, lstsq
  5. from scipy.optimize import OptimizeResult
  6. from .common import print_header_linear, print_iteration_linear
  7. def compute_kkt_optimality(g, on_bound):
  8. """Compute the maximum violation of KKT conditions."""
  9. g_kkt = g * on_bound
  10. free_set = on_bound == 0
  11. g_kkt[free_set] = np.abs(g[free_set])
  12. return np.max(g_kkt)
  13. def bvls(A, b, x_lsq, lb, ub, tol, max_iter, verbose):
  14. m, n = A.shape
  15. x = x_lsq.copy()
  16. on_bound = np.zeros(n)
  17. mask = x < lb
  18. x[mask] = lb[mask]
  19. on_bound[mask] = -1
  20. mask = x > ub
  21. x[mask] = ub[mask]
  22. on_bound[mask] = 1
  23. free_set = on_bound == 0
  24. active_set = ~free_set
  25. free_set, = np.nonzero(free_set)
  26. r = A.dot(x) - b
  27. cost = 0.5 * np.dot(r, r)
  28. initial_cost = cost
  29. g = A.T.dot(r)
  30. cost_change = None
  31. step_norm = None
  32. iteration = 0
  33. if verbose == 2:
  34. print_header_linear()
  35. # This is the initialization loop. The requirement is that the
  36. # least-squares solution on free variables is feasible before BVLS starts.
  37. # One possible initialization is to set all variables to lower or upper
  38. # bounds, but many iterations may be required from this state later on.
  39. # The implemented ad-hoc procedure which intuitively should give a better
  40. # initial state: find the least-squares solution on current free variables,
  41. # if its feasible then stop, otherwise set violating variables to
  42. # corresponding bounds and continue on the reduced set of free variables.
  43. while free_set.size > 0:
  44. if verbose == 2:
  45. optimality = compute_kkt_optimality(g, on_bound)
  46. print_iteration_linear(iteration, cost, cost_change, step_norm,
  47. optimality)
  48. iteration += 1
  49. x_free_old = x[free_set].copy()
  50. A_free = A[:, free_set]
  51. b_free = b - A.dot(x * active_set)
  52. z = lstsq(A_free, b_free, rcond=-1)[0]
  53. lbv = z < lb[free_set]
  54. ubv = z > ub[free_set]
  55. v = lbv | ubv
  56. if np.any(lbv):
  57. ind = free_set[lbv]
  58. x[ind] = lb[ind]
  59. active_set[ind] = True
  60. on_bound[ind] = -1
  61. if np.any(ubv):
  62. ind = free_set[ubv]
  63. x[ind] = ub[ind]
  64. active_set[ind] = True
  65. on_bound[ind] = 1
  66. ind = free_set[~v]
  67. x[ind] = z[~v]
  68. r = A.dot(x) - b
  69. cost_new = 0.5 * np.dot(r, r)
  70. cost_change = cost - cost_new
  71. cost = cost_new
  72. g = A.T.dot(r)
  73. step_norm = norm(x[free_set] - x_free_old)
  74. if np.any(v):
  75. free_set = free_set[~v]
  76. else:
  77. break
  78. if max_iter is None:
  79. max_iter = n
  80. max_iter += iteration
  81. termination_status = None
  82. # Main BVLS loop.
  83. optimality = compute_kkt_optimality(g, on_bound)
  84. for iteration in range(iteration, max_iter):
  85. if verbose == 2:
  86. print_iteration_linear(iteration, cost, cost_change,
  87. step_norm, optimality)
  88. if optimality < tol:
  89. termination_status = 1
  90. if termination_status is not None:
  91. break
  92. move_to_free = np.argmax(g * on_bound)
  93. on_bound[move_to_free] = 0
  94. free_set = on_bound == 0
  95. active_set = ~free_set
  96. free_set, = np.nonzero(free_set)
  97. x_free = x[free_set]
  98. x_free_old = x_free.copy()
  99. lb_free = lb[free_set]
  100. ub_free = ub[free_set]
  101. A_free = A[:, free_set]
  102. b_free = b - A.dot(x * active_set)
  103. z = lstsq(A_free, b_free, rcond=-1)[0]
  104. lbv, = np.nonzero(z < lb_free)
  105. ubv, = np.nonzero(z > ub_free)
  106. v = np.hstack((lbv, ubv))
  107. if v.size > 0:
  108. alphas = np.hstack((
  109. lb_free[lbv] - x_free[lbv],
  110. ub_free[ubv] - x_free[ubv])) / (z[v] - x_free[v])
  111. i = np.argmin(alphas)
  112. i_free = v[i]
  113. alpha = alphas[i]
  114. x_free *= 1 - alpha
  115. x_free += alpha * z
  116. if i < lbv.size:
  117. on_bound[free_set[i_free]] = -1
  118. else:
  119. on_bound[free_set[i_free]] = 1
  120. else:
  121. x_free = z
  122. x[free_set] = x_free
  123. step_norm = norm(x_free - x_free_old)
  124. r = A.dot(x) - b
  125. cost_new = 0.5 * np.dot(r, r)
  126. cost_change = cost - cost_new
  127. if cost_change < tol * cost:
  128. termination_status = 2
  129. cost = cost_new
  130. g = A.T.dot(r)
  131. optimality = compute_kkt_optimality(g, on_bound)
  132. if termination_status is None:
  133. termination_status = 0
  134. return OptimizeResult(
  135. x=x, fun=r, cost=cost, optimality=optimality, active_mask=on_bound,
  136. nit=iteration + 1, status=termination_status,
  137. initial_cost=initial_cost)