Esempio n. 1
0
 def compute_alpha(self, problem, expr, ctx):
     xs = self._collect_expr_variables(expr)
     x_bounds = []
     for x in xs:
         x_view = problem.variable_view(x)
         x_l = x_view.lower_bound()
         x_u = x_view.upper_bound()
         if x_l is None or x_u is None:
             raise ValueError(
                 'Variables must be bounded: name={}, lb={}, ub={}'.format(
                     x.name, x_l, x_u))
         x_bounds.append(Interval(x_l, x_u))
     tree_data = expr.expression_tree_data()
     f = tree_data.eval(x_bounds)
     H = f.hessian(x_bounds, [1.0])
     n = len(xs)
     min_ = 0
     H = [Interval.zero() + i for i in H]
     for i in range(n):
         h = H[i * n:(i + 1) * n]
         v = h[i].lower_bound - sum(
             max(abs(h[j].lower_bound), abs(h[j].upper_bound))
             for j in range(n) if j != i)
         if v < min_:
             min_ = v
     alpha = max(0, -0.5 * min_)
     return alpha
Esempio n. 2
0
def bound_description_to_bound(bound_str):
    if isinstance(bound_str, str):
        return {
            'zero': Interval.zero(),
            'nonpositive': Interval(None, 0),
            'nonnegative': Interval(0, None),
            'positive': Interval(1, None),
            'negative': Interval(None, -1),
            'unbounded': Interval(None, None),
        }[bound_str]
    elif isinstance(bound_str, Interval):
        return bound_str
    else:
        return Interval(bound_str, bound_str)
Esempio n. 3
0
 def test_addition_with_zero(self, bound):
     zero_bound = Interval.zero()
     assert bound + zero_bound == bound