def _asphericity_iteration(xk, l, m, p, A, a, B, b, verbose=0, solver='GLPK'): # find better way to share all those parameters, maybe a class #alphak = asphericity(xk) R_xk = max(A.row(i) * vector(xk) + a[i] for i in range(m)) r_xk = min(B.row(j) * vector(xk) + b[j] for j in range(l)) alphak = R_xk / r_xk a_LP = MixedIntegerLinearProgram(maximization=False, solver=solver) x = a_LP.new_variable(integer=False, nonnegative=False) z = a_LP.new_variable(integer=False, nonnegative=False) for i in range(m): for j in range(l): aux = sum( (A.row(i)[k] - alphak * B.row(j)[k]) * x[k] for k in range(p)) a_LP.add_constraint(aux + a[i] - alphak * b[j] <= z[0]) #solve LP a_LP.set_objective(z[0]) if (verbose): print '**** Solve LP ****' a_LP.show() opt_val = a_LP.solve() x_opt = a_LP.get_values(x) z_opt = a_LP.get_values(z) if (verbose): print 'Objective Value:', opt_val for i, v in x_opt.iteritems(): print 'x_%s = %f' % (i, v) for i, v in z_opt.iteritems(): print 'z = %f' % (v) print '\n' return [opt_val, x_opt, z_opt]
def support_function(P, d, verbose=0, return_xopt=False, solver='GLPK'): r"""Compute support function of a convex polytope. It is defined as `\max_{x in P} \langle x, d \rangle` , where `d` is an input vector. INPUT: * ``P`` - an object of class Polyhedron. It can also be given as ``[A, b]`` meaning `Ax \leq b`. * ``d`` - a vector (or list) where the support function is evaluated. * ``verbose`` - (default: 0) If 1, print the status of the LP. * ``solver`` - (default: ``'GLPK'``) the LP solver used. * ``return_xopt`` - (default: False) If True, the optimal point is returned, and ``sf = [oval, opt]``. OUTPUT: * ``sf`` - the value of the support function. EXAMPLES:: sage: from polyhedron_tools.misc import BoxInfty, support_function sage: P = BoxInfty([1,2,3], 1); P A 3-dimensional polyhedron in QQ^3 defined as the convex hull of 8 vertices sage: support_function(P, [1,1,1], return_xopt=True) (9.0, {0: 2.0, 1: 3.0, 2: 4.0}) NOTES: - The possibility of giving the input polytope as `[A, b]` instead of a polyhedron is useful in cases when the dimensions are high (in practice, above or around 20, but it depends on the particular system -number of constraints- as well). - If a different solver (e.g. ``guurobi``) is given, it should be installed properly. """ from sage.numerical.mip import MixedIntegerLinearProgram if (type(P) == list): A = P[0] b = P[1] elif P.is_empty(): # avoid formulating the LP if P = [] return 0 else: #assuming some form of Polyhedra base_ring = P.base_ring() # extract the constraints from P m = len(P.Hrepresentation()) n = len(vector(P.Hrepresentation()[0])) - 1 b = vector(base_ring, m) A = matrix(base_ring, m, n) P_gen = P.Hrep_generator() i = 0 for pigen in P_gen: pi_vec = pigen.vector() A.set_row(i, -pi_vec[1:len(pi_vec)]) b[i] = pi_vec[0] i += 1 s_LP = MixedIntegerLinearProgram(maximization=True, solver=solver) x = s_LP.new_variable(integer=False, nonnegative=False) # objective function obj = sum(d[i] * x[i] for i in range(len(d))) s_LP.set_objective(obj) s_LP.add_constraint(A * x <= b) if (verbose): print '**** Solve LP ****' s_LP.show() oval = s_LP.solve() xopt = s_LP.get_values(x) if (verbose): print 'Objective Value:', oval for i, v in xopt.iteritems(): print 'x_%s = %f' % (i, v) print '\n' if (return_xopt == True): return oval, xopt else: return oval