00001 import numpy as np
00002 import pdb
00003
00004 def max_wrt_constrain(a, b, constraint_f, toler):
00005 return find_split(a, None, b, None, constraint_f, toler)
00006
00007 def min_wrt_constrain(a, b, constraint_f, toler):
00008 return find_split(b, None, a, None, constraint_f, toler)
00009
00010 def find_split(a, qa, b, qb, constraint_f, toler):
00011
00012 print 'a', a, qa, 'b', b, qb
00013
00014 if abs(b - a) < toler:
00015 if qb == True:
00016 return b
00017 elif qa == True:
00018 return a
00019 else:
00020 raise RuntimeError('min interval reached without finding a point that returns success')
00021 else:
00022 nqb = constraint_f(b)
00023
00024 if nqb:
00025 return b
00026
00027 else:
00028 mid = (a + b) / 2.0
00029 nmid = constraint_f(mid)
00030 if not nmid:
00031 return find_split(a, qa, mid, nmid, constraint_f, toler)
00032 else:
00033 return find_split(mid, True, b, nqb, constraint_f, toler)
00034
00035 def my_func(input):
00036 if input > 5.5:
00037 return True
00038 else:
00039 return False
00040
00041
00042 print 'returned', find_split(20.0, None, -10., None, my_func, .1)