def penalize(x, upper, lower):
    """
	produces a penalty relative to how much x is above the upper or below lower bound. maximum value is 2
	:param x:
	:param upper:
	:param lower:
	:return:
	"""
    if lower is not None and upper is not None and lower > upper:
        lower, upper = upper, lower
    u = 0
    l = 0
    if upper is not None and x > upper:
        u += 1 - 1 / ((x - upper) ** 2 + 1)
    if lower is not None and x < lower:
        l += 1 - 1 / ((x - lower) ** 2 + 1)
    return u + l
