Guest User

Untitled

a guest
Apr 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. def cache(func):
  2. cached = {}
  3.  
  4. def inner_func(*args, **kwargs):
  5. if args not in cached:
  6. cached[args] = func(*args)
  7. return cached[args]
  8. return inner_func
  9.  
  10.  
  11. ##return the value, vcont, of the location n... does it need to be n,m? identify a value at a single square
  12. ##TODO n equals the total number of samples, h = the nummber of positive values from the total sample set
  13. @cache
  14. def values(total, n, h, reward, cost, continue_val=0):
  15. if out_of_range(total, n, h):
  16. raise Exception('Out of range, {} and {} must be less than or equal to {} and greater than equal to 0'.format(n,h,total))
  17. phit = h / (h + (n - h))
  18. u = max(phit, 1 - phit) * reward
  19. vstop = u - (n * cost)
  20. if is_base_case(total, n):
  21. ##TODO what is the formuala for the value of the outer edge, in relation to n and h
  22. #return (vstop, ((phit * continue_val) + ((1 - phit) * continue_val) - n * cost))
  23. return (vstop, reward - (total * cost))
  24. ##TODO vstop vs vcont, what are we returning? return a tuple for the value of stoping and the
  25. # value of continuing at each point in the grid?
  26. # the value of stoping seems to have nothing to do with the value of continuing? should we group them together?
  27. # worth putting this in ab obj? or return a tuple for each location? maybe return an object for each location?
  28. # object {x, y, vstop, vcont}??
  29. print('n={}, h={}, phit={}, qhit={}, u={} phit_u={}, EV={}'.format(n,h,phit,1-phit,u,phit*u,phit*u-n*cost))
  30. return (vstop, ((phit * values(total, n + 1, h + 1, reward, cost, continue_val)[1])
  31. + ((1 - phit) * values(total, n + 1, h, reward, cost, continue_val)[1])
  32. - cost))
  33.  
  34.  
  35. def out_of_range(total, n, h):
  36. """
  37. Identifies if the params passed through are out of range
  38. :param total:
  39. :param n:
  40. :param h:
  41. :return:
  42. """
  43. return n > total or h > total or n <= 0 or h < 0 or n < h
  44.  
  45.  
  46. def is_base_case(total, n):
  47. """
  48. Identifies if the locations are on the outer edge of the graph
  49. :param total:
  50. :param n:
  51. :return:
  52. """
  53. return n == total
  54.  
  55. def main():
  56. print(values(25, 20, 7, 5, .1))
  57. pass
  58.  
  59.  
  60. if __name__ == '__main__':
  61. main()
Add Comment
Please, Sign In to add comment