コード例 #1
0
def disp_polyhedron(A=None,
                    b=None,
                    points=None,
                    rays=None,
                    c=None,
                    obj_val=None,
                    opt=None,
                    loc=None):
    if loc is None and opt is not None:
        loc = opt
    f = Figure()
    f.add_polyhedron(p, label='Polyhedron $P$', color='red')
    f.set_xlim([p.xlim[0], p.xlim[1] + 1])
    f.set_ylim([p.ylim[0], p.ylim[1] + 2])
    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()
コード例 #2
0
def disp_polyhedron(A = None, b = None, points = None, rays = None, c = None, obj_val = None,
                    opt = None, loc = None):
    if loc is None and opt is not None:
        loc = opt
    f = Figure()
    f.add_polyhedron(p, label = 'Polyhedron $P$', color = 'red')
    f.set_xlim([p.xlim[0], p.xlim[1]+1])
    f.set_ylim([p.ylim[0], p.ylim[1]+2])
    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()
コード例 #3
0
def disp_relaxation(A, b, cuts = [], sol = None):
    #TODO: Check sense of inequalities by looking explicitly at
    #      lp.constraintsUpper and lp.constraintsLower
    p = Polyhedron2D(A = A, b = b)
    f = Figure()
    f.add_polyhedron(p, label = 'Polyhedron $P$')
    f.set_xlim([p.xlim[0], p.xlim[1]])
    f.set_ylim([p.ylim[0], p.ylim[1]])
    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')
    if sol is not None:
        f.add_point(sol, radius = .05)
    f.show()
コード例 #4
0
ファイル: cuttingPlanes.py プロジェクト: waldstein1983/CuPPy
def disp_relaxation(A, b, cuts = [], sol = None, disj = []):
    #TODO: Check sense of inequalities by looking explicitly at
    #      lp.constraintsUpper and lp.constraintsLower
    p = Polyhedron2D(A = A, b = b)
    f = Figure()
    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')
    if sol is not None:
        f.add_point(sol, radius = .05)
    f.show()
コード例 #5
0
f.add_polyhedron(
    sR,
    label=
    '$\operatorname{conv}(\mathcal{P}^{\,\prime\prime} \cap \mathbb{Z}^{\,2})$',
    color='red',
    linestyle='dashed')
f.set_xlim(p2.xlim + [0, 1])
f.set_ylim(p2.ylim + [0, 1])
opt = [2, 3.5]
f.add_point(opt, .05, 'red')
f.add_text([opt[0] - 0.25, opt[1]], r'$x^*$')
if c is not None and obj_val is not None:
    f.add_line(c,
               obj_val,
               p2.xlim,
               p2.ylim + [0.5, -1.5],
               linestyle='dashed',
               color='green',
               label="Objective Function")
f.show()
cuts = [[-3, 1]]
rhs = [-5]
opt = [[2.42, 2.25]]
obj_val = [2.42]

for i in range(len(cuts)):
    A1.append(cuts[i])
    b1.append(rhs[i])
    p1 = Polyhedron2D(A=A1, b=b1)
    f = Figure()
    f.add_polyhedron(p1,
コード例 #6
0
ファイル: DecompVis.py プロジェクト: tkralphs/GrUMPy
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 = '$\operatorname{conv}(\mathcal{P}^{\,\prime\prime} \cap \mathbb{Z}^{\,2})$', color = 'red', linestyle = 'dashed')
f.set_xlim(p2.xlim + [0, 1])
f.set_ylim(p2.ylim + [0, 1])
opt = [2, 3.5]
f.add_point(opt, .05, 'red')
f.add_text([opt[0]-0.25, opt[1]], r'$x^*$')
if c is not None and obj_val is not None:
    f.add_line(c, obj_val, p2.xlim, p2.ylim + [0.5, - 1.5], 
               linestyle = 'dashed', color = 'green', label = "Objective Function")
f.show()
cuts = [[-3, 1]]
rhs = [-5]
opt = [[2.42, 2.25]]
obj_val = [2.42]

for i in range(len(cuts)):
    A1.append(cuts[i])
    b1.append(rhs[i])
    p1 = Polyhedron2D(A = A1, b = b1)
    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 = '$\operatorname{conv}(\mathcal{P}^{\,\prime\prime} \cap \mathbb{Z}^{\,2})$', color = 'red', linestyle = 'dashed')
    f.set_xlim(p2.xlim + [0, 1])
コード例 #7
0
class GenericSeparation(object):

    def __init__(self, origProb, x0):
        self.prob = origProb
        self.iter = 1
        self.x0 = x0
        self.var = origProb.variablesDict()
        self.dim = len(self.var)
        self.currentXtremePoint = {}
        self.solver = None
        self.c = dict(list((v.name,c) for v,c in origProb.objective.iteritems()))
        for v in self.var:
            if v in self.c:
                continue
            else:
                self.c[v] = 0.0
        self.generate_separation_problem()
        self.piCurrent = dict([(v.name, 0) for v in self.var.values()]) 
        self.piPrevious = dict([(v.name, 0) for v in self.var.values()]) 
        self.extremePoints = []
        self.f = Figure()
        
    def generate_separation_problem(self):
        self.sepProb = LpProblem (name='separation '+self.prob.name, sense=LpMinimize)
        self.pi = dict(list((v,LpVariable('pi'+v, None, None, LpContinuous,
                                         None))
                                         for v in self.var)) 
        self.sepProb += lpSum(self.pi[v]*self.x0[v] for v in self.var)

    def generate_xtreme_point(self):
        obj = LpConstraintVar()
        for v in self.prob.variables():
            obj.addVariable(v, self.piCurrent[v.name])
        self.prob.setObjective(obj)
        self.prob.solve()
        #solvers.COIN_CMD.solve(self.prob)
        for v in self.var:
            self.currentXtremePoint[v] = self.var[v].value()
        if self.output == 1:
            currentXtremePointList = self.currentXtremePoint.items()
            currentXtremePointList.sort()
            for v in currentXtremePointList:
                print v[0]+'\t', v[1]
        self.extremePoints.append(self.currentXtremePoint.values())
        return self.prob.objective.value()

    def add_inequality(self):
        # change this, you should not access sense directly call a method
        self.sepProb += lpSum (self.currentXtremePoint[v]*self.pi[v] for v in self.var) >= 1

    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.plot_min[0], p.plot_max[0])
                self.f.set_ylim(p.plot_min[1], p.plot_max[1])
                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.plot_max, p.plot_min, color = 'green', 
                                linestyle = 'dashed', label = 'Separating Hyperplane')
                self.f.show()
            if self.output:
                print self.sepProb.objective.value()            
コード例 #8
0
class GenericSeparation(object):
    def __init__(self, origProb, x0, pi_init=None):
        if pi_init == None:
            pi_init = [(v.name, 0) for v in self.var.values()]
        self.prob = origProb
        self.iter = 1
        self.x0 = x0
        self.var = origProb.variablesDict()
        self.dim = len(self.var)
        self.currentXtremePoint = {}
        self.solver = None
        self.c = dict(
            list((v.name, c) for v, c in origProb.objective.iteritems()))
        for v in self.var:
            if v in self.c:
                continue
            else:
                self.c[v] = 0.0
        self.generate_separation_problem()
        self.piCurrent = dict([(v.name, pi_init[v.name])
                               for v in self.var.values()])
        self.piPrevious = dict([(v.name, 0) for v in self.var.values()])
        self.extremePoints = []
        self.f = Figure()

    def generate_separation_problem(self):
        self.sepProb = LpProblem(name='separation ' + self.prob.name,
                                 sense=LpMinimize)
        self.pi = dict(
            list((v, LpVariable('pi' + v, None, None, LpContinuous, None))
                 for v in self.var))
        self.sepProb += lpSum(self.pi[v] * self.x0[v] for v in self.var)

    def generate_xtreme_point(self):
        obj = LpConstraintVar()
        for v in self.prob.variables():
            obj.addVariable(v, self.piCurrent[v.name])
        self.prob.setObjective(obj)
        self.prob.solve()
        #solvers.COIN_CMD.solve(self.prob)
        for v in self.var:
            self.currentXtremePoint[v] = self.var[v].value()
        if self.output == 1:
            currentXtremePointList = self.currentXtremePoint.items()
            currentXtremePointList.sort()
            for v in currentXtremePointList:
                print v[0] + '\t', v[1]
        self.extremePoints.append(self.currentXtremePoint.values())
        return self.prob.objective.value()

    def add_inequality(self):
        # change this, you should not access sense directly call a method
        self.sepProb += lpSum(self.currentXtremePoint[v] * self.pi[v]
                              for v in self.var) >= 1

    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()