def disp_relaxation(f, A, b, cuts=[], sol=None, disj=[], filename=None): #TODO: Check sense of inequalities by looking explicitly at # lp.constraintsUpper and lp.constraintsLower p = Polyhedron2D(A=A, b=b) f.add_polyhedron(p, label='Polyhedron $P$') f.set_xlim(p.xlim) f.set_ylim(p.ylim) pI = p.make_integer_hull() f.add_polyhedron(pI, show_int_points=True, color='red', linestyle='dashed', label='Convex hull of integer points') for (coeff, r) in cuts: f.add_line(coeff, r, p.xlim, p.ylim, color='green', linestyle='dashed') for (coeff, r) in disj: f.add_line(coeff, r, p.xlim, p.ylim, color='red', linestyle='dashed') f.add_line(coeff, r + 1, p.xlim, p.ylim, color='red', linestyle='dashed') if sol is not None: f.add_point(sol, radius=.05) f.show(filename=filename)
numCons = 4 #points = [[0, 0], [3, 4], [8, 6], [6, 1]] points = None rays = [] A1 = [[1, 1], [4, -10], [-2, -2], [-6, -2], [-1, 4]] A2 = [[-7, 1], [0, -1], [1, -1], [4, 1], [0, 1], [-1, 5]] b1 = [8, -3, -9, -19, 12] b2 = [-13, -1, 3, 27, 5, 20] sense = ('Min', '<=') integerIndices = [0, 1] c = [1, 0] cuts = None rhs = None obj_val = 2 p1 = Polyhedron2D(A=A1, b=b1) p2 = Polyhedron2D(A=A2, b=b2) sR = p2.make_integer_hull() f = Figure() f.add_polyhedron(p1, label='$\mathcal{P}^{\,\prime}$', color='blue', show_int_points=True) f.add_polyhedron(p2, label='$\mathcal{P}^{\,\prime\prime}$', color='black', show_int_points=True) f.add_polyhedron( sR, label=
def __init__(self, module_name=None, file_name=None, A=None, b=None, c=None, points=None, rays=None, sense=None, integerIndices=None, numVars=None): if file_name is not None: # We got a file name, so ignore everything else and read in the instance lp = CyClpSimplex() lp.extractCyLPModel(file_name) self.integerIndices = [ i for (i, j) in enumerate(lp.integerInformation) if j == True ] infinity = lp.getCoinInfinity() A = lp.coefMatrix b = CyLPArray([0 for _ in range(lp.nRows)]) for i in range(lp.nRows): if lp.constraintsLower[i] > -infinity: if lp.constraintsUpper[i] < infinity: raise Exception('Cannot handle ranged constraints') b[i] = -lp.constraintsLower[i] for j in range(lp.nCols): A[i, j] = -A[i, j] elif lp.constraintsUpper[i] < infinity: b[i] = lp.constraintsUpper[i] else: raise Exception('Constraint with no bounds detected') x = lp.addVariable('x', lp.nCols) lp += A * x <= b lp += x <= lp.variablesUpper lp += x >= lp.variablesLower lp.objective = lp.objective self.sense = '<=' numVars = lp.nCols else: min_or_max = None if module_name is not None: # We got a module name, read the data from there mip = ilib.import_module(module_name) self.A = mip.A if hasattr(mip, 'A') else None self.b = mip.b if hasattr(mip, 'b') else None points = mip.points if hasattr(mip, 'points') else None rays = mip.rays if hasattr(mip, 'rays') else None self.c = mip.c if hasattr(mip, 'c') else None self.sense = mip.sense[1] if hasattr(mip, 'sense') else None min_or_max = mip.sense[0] if hasattr(mip, 'sense') else None self.integerIndices = mip.integerIndices if hasattr( mip, 'integerIndices') else None x_u = CyLPArray(mip.x_u) if hasattr(mip, 'x_u') else None numVars = mip.numVars if hasattr(mip, 'numVars') else None self.x_sep = mip.x_sep if hasattr(mip, 'x_sep') else None if numVars is None and mip.A is not None: numVars = len(mip.A) if numVars is None: raise "Must specify number of variables when problem is not" else: self.A = A self.b = b self.c = c self.points = points self.rays = rays if sense is not None: self.sense = sense[1] min_or_max = sense[0] self.integerIndices = integerIndices x_u = None lp = CyClpSimplex() if self.A is not None: A = np.matrix(self.A) b = CyLPArray(self.b) elif numVars <= 2 and GRUMPY_INSTALLED: p = Polyhedron2D(points=points, rays=rays) A = np.matrix(p.hrep.A) b = np.matrix(p.hrep.b) else: raise "Must specify problem in inequality form with more than two variables\n" #Warning: At the moment, you must put bound constraints in explicitly for split cuts x_l = CyLPArray([0 for _ in range(numVars)]) x = lp.addVariable('x', numVars) lp += x >= x_l if x_u is not None: lp += x <= x_u lp += (A * x <= b if self.sense == '<=' else A * x >= b) c = CyLPArray(self.c) if min_or_max == 'Max': lp.objective = -c * x else: lp.objective = c * x self.lp = lp self.x = x
def separate(self, output=False, p=None): self.output = output while True: print 'Iteration ', self.iter if self.generate_xtreme_point() >= 1 - EPS: break self.add_inequality() if self.output: print "Separation problem solution status:", LpStatus[ self.sepProb.solve()] for v in self.var: if self.pi[v].value() is not None: print self.pi[v].name + '\t\t', self.pi[v].value() else: print self.pi[v].name + '\t\t', 0 self.piPrevious = deepcopy(self.piCurrent) for v in self.var: if self.pi[v].value() is not None: self.piCurrent[v] = self.pi[v].value() else: self.piCurrent[v] = 0 self.iter += 1 if p is not None: self.f.initialize() self.f.add_polyhedron(p, label='Polyhedron P') xList = (self.x0.values()[0], self.x0.values()[1]) if len(self.extremePoints) > 2: pp = Polyhedron2D(points=self.extremePoints) self.f.add_polyhedron( pp, color='red', linestyle='dashed', label='Convex Hull of Generated Points') elif len(self.extremePoints) == 1: self.f.add_point(self.extremePoints[0], radius=0.05, color='green') self.f.add_text([ self.extremePoints[0][0] - 0.5, self.extremePoints[0][1] - 0.08 ], '$x^0$') else: self.f.add_line_segment( self.extremePoints[0], self.extremePoints[1], color='red', linestyle='dashed', label='Convex Hull of Generated Points') self.f.set_xlim(p.xlim) self.f.set_ylim(p.ylim) self.f.add_point(xList, radius=0.05, color='red') self.f.add_text([xList[0] - 0.5, xList[1] - 0.08], '$x^*$') dList = (self.piCurrent.values()[0], self.piCurrent.values()[1]) self.f.add_line(dList, 1, p.xlim, p.ylim, color='green', linestyle='dashed', label='Separating Hyperplane') self.f.show() if self.output: print self.sepProb.objective.value()
x.append(LpVariable('x_' + str(i), 0, None, LpInteger)) # add obj function prob += lpSum(mip.c[i] * x[i] for i in range(len(mip.A[0]))) # add constraints to the prob for i in range(len(mip.A)): prob += mip.A[i][0] * x[0] + mip.A[i][1] * x[1] <= mip.b[i] return prob, x, mip if __name__ == '__main__': display = True prob, vars, mip = read_instance(module_name='MIP8') if display: p = Polyhedron2D(A=mip.A, b=mip.b) f = Figure() f.add_polyhedron(p, show_int_points=True, label='Polyhedron P') f.set_xlim(p.xlim) f.set_ylim(p.ylim) f.add_point(mip.x_sep, radius=0.05, color='red') f.add_text([mip.x_sep[0] - 0.5, mip.x_sep[1] - 0.08], '$x^*$') f.show() # This is the point to be separated x0 = {} for index, value in enumerate(mip.x_sep): x0[vars[index].name] = value i = 0 ic = GenericSeparation(prob, x0, {'x_0': -1, 'x_1': 0}) ic.separate(output=True, p=p)
from src.grumpy.polyhedron2D import Polyhedron2D, add_line, add_point if __name__ == '__main__': #points = np.random.random ((20,2)) #p = make_hull(points) A = [[-5, -3], [-5, 5], [1, -2]] b = [-10, 1, 2] points = [[1.175, 1.375], [2, 0], [2.175, 2.375], [3, 1], [3.175, 2.375], [4, 1]] fig = plt.figure() ax = fig.add_subplot(111) ax.grid() p = Polyhedron2D(A=A, b=b) q = Polyhedron2D(points=points) p.draw(ax, color='blue', linestyle='solid', label='$P$') q.draw(ax, color='black', linestyle='solid', color_int_points=True, label='$Q$') ax.set_xlim(p.plot_min[0] - 0.5, p.plot_max[0] + 1) ax.set_ylim(p.plot_min[1], p.plot_max[1] + 1) pI = p.make_integer_hull() pI.draw(ax, color='red', linestyle='dashed', label='conv(S)') plt.text(-0.2, 1.3, r'$e_1 = (1.175, 1.375)$') plt.text(1.8, -0.2, r'$e_2 = (2, 0)$') plt.text(1.5, 2.5, r'$r_1 = (1, 1)$') plt.text(3.5, 0.5, r'$r_2 = (2, 1)$')
if c is not None and obj_val is not None: f.add_line(c, obj_val, p.xlim + [0.2, 0.8], p.ylim + [0.2, 1.8], linestyle='dashed', color='black', label="Objective Function") if opt is not None: f.add_point(opt, 0.04, 'red') f.add_text(loc, r'$x^* = %s$' % str(opt)) f.show() try: p = Polyhedron2D(A=LP.A, b=LP.b) except AttributeError: try: p = Polyhedron2D(points=LP.points, rays=LP.rays) except AttributeError: print 'Error: Must specify either A and b or points and rays' p = None if p is not None: if CYLP_INSTALLED: lp = CyClpSimplex() A = np.matrix(p.hrep.A) b = CyLPArray(p.hrep.b)