Search code examples
pythonmathmathematical-optimizationgurobi

Gurobi: cannot add constraint


Following code raises error

File "model.pxi", line 2469, in gurobipy.Model.addConstr (../../src/python/gurobipy.c:77443) TypeError: unsupported operand type(s) for -: 'bool' and 'NoneType'

on marked line

def get_incenter_and_radius(facets):
    n = len(facets[0])
    m = Model()
    m.setParam('OutputFlag', False)
    r = m.addVar(lb = -100.0, ub = 100.0, vtype = GRB.CONTINUOUS)
    center = [m.addVar(lb = -1.0, ub = 1.0, vtype = GRB.CONTINUOUS, name = "x" + str(i)) for i in range(n)]
    m.update()
    for facet in facets:
        norm = sqrt(sum(facet[i]**2 for i in nums(facet)))
        m.addConstr(r * norm <= get_sp(facet, center))
    m.addConstr(r > 0) # ________________ ERROR ___________________
    for coord_num in range(n):
        m.addConstr(center[coord_num] + r <= 1)
    for coord_num in range(n):
        m.addConstr(center[coord_num] - r >= -1)
    m.setObjective(r, GRB.MAXIMIZE)
    m.optimize()
    if m.sol_count == 0:
        return None, None
    center_val = tuple(elt.X for elt in center)
    return center_val, r.X

I have specified type and bounds for r, so what is the source of error?


Solution

  • Linear programming does not allow strict inequalities (>, <). You should use >= or <= operators.