示例#1
0
    def test_removeVar2(self):
        s = CyClpSimplex()
        fp = os.path.join(currentFilePath, '../../input/p0033.mps')
        s.extractCyLPModel(fp)
        y = s.addVariable('y', 3)
        s.primal()

        x = s.getVarByName('x')
        s.addConstraint(x[1] +  y[1] >= 1.2)
        #s.primal()
        s.removeVariable('x')
        s.primal()
        s = s.primalVariableSolution
        self.assertTrue((s['y'] - np.array([0, 1.2, 0]) <= 10**-6).all())
示例#2
0
def read_instance(module_name = None, file_name = None):

    if module_name is not None:
        lp = CyClpSimplex()

        mip = ilib.import_module(module_name)
            
        A = np.matrix(mip.A)
        #print np.linalg.cond(A)
        b = CyLPArray(mip.b)
        
        #Warning: At the moment, you must put bound constraints in explicitly for split cuts
        x_l = CyLPArray([0 for _ in range(mip.numVars)])
            
        x = lp.addVariable('x', mip.numVars)
        
        lp += x >= x_l
        try:
            x_u = CyLPArray(getattr(mip, 'x_u'))
            lp += x <= x_u
        except:
            pass
        
        lp += (A * x <= b if mip.sense[1] == '<=' else
               A * x >= b)
        c = CyLPArray(mip.c)
        lp.objective = -c * x if mip.sense[0] == 'Max' else c * x
        return lp, x, mip.A, mip.b, mip.sense[1], mip.integerIndices
    elif file_name is not None:
        lp = CyClpSimplex()
        m = lp.extractCyLPModel(file_name)
        x = m.getVarByName('x')
        integerIndices = [i for (i, j) in enumerate(lp.integerInformation) if j == True]
        infinity = lp.getCoinInfinity()
        sense = None
        for i in range(lp.nRows):
            if lp.constraintsLower[i] > -infinity:
                if sense == '<=':
                    print "Function does not support mixed constraint..."
                    break
                else: 
                    sense = '>='
                    b = lp.constraintsLower
            if lp.constraintsUpper[i] < infinity: 
                if sense == '>=':
                    print "Function does not support mixed constraint..."
                    break
                else: 
                    sense = '<='
                    b = lp.constraintsUpper
        return lp, x, lp.coefMatrix, b, sense, integerIndices
    else:
        print "No file or module name specified..."
        return None, None, None, None, None, None
示例#3
0
def read_instance(module_name = True, file_name = None):

    if module_name:
        lp = CyClpSimplex()

        mip = ilib.import_module(module_name)
            
        A = np.matrix(mip.A)
        #print np.linalg.cond(A)
        b = CyLPArray(mip.b)
        
        #We assume variables have zero lower bounds
        x_l = CyLPArray([0 for _ in range(mip.numVars)])
            
        x = lp.addVariable('x', mip.numVars)
        
        lp += x >= x_l
        try:
            x_u = CyLPArray(getattr(mip, 'x_u'))
            lp += x <= x_u
        except:
            pass
        
        lp += (A * x <= b if mip.sense[1] == '<=' else
               A * x >= b)
        c = CyLPArray(mip.c)
        lp.objective = -c * x if mip.sense[0] == 'Max' else c * x
        
        return lp, x, mip.A, mip.b, mip.sense, mip.integerIndices
    else:
        #TODO Change sense of inequalities so they are all the same
        #     by explicitly checking lp.constraintsUpper and lp.constraintsLower
        #Warning: Reading MP not well tested 
        lp.extractCyLPModel(file_name)
        x = lp.cyLPModel.getVarByName('x')
        sense = ('Min', '>=')
        return lp, x, None, None, sense, integerIndices
示例#4
0
    if (firstExample):
        x = m.addVariable('x', 2, isInt=True)

        A = np.matrix([[7., -2.], [0., 1], [2., -2]])
        b = CyLPArray([14, 3, 3])

        m += A * x <= b
        m += x >= 0

        c = CyLPArray([-4, 1])
        m.objective = c * x
        s = CyClpSimplex(m)
    else:
        s = CyClpSimplex()
        #cylpDir = os.environ['CYLP_SOURCE_DIR']
        inputFile = os.path.join('..', '..', 'input', 'p0033.mps')
        m = s.extractCyLPModel(inputFile)
        x = m.getVarByName('x')
        s.setInteger(x)

    cbcModel = s.getCbcModel()

    gc = GomoryCutGenerator(m)
    #cbcModel.addPythonCutGenerator(gc, name='PyGomory')

    #cbcModel.branchAndBound()
    cbcModel.solve()

    print(cbcModel.primalVariableSolution)
示例#5
0
    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
示例#6
0
    if (firstExample):
        x = m.addVariable('x', 2, isInt=True)

        A = np.matrix([[7., -2.],[0., 1], [2., -2]])
        b = CyLPArray([14, 3, 3])

        m += A * x <= b
        m += x >= 0

        c = CyLPArray([-4, 1])
        m.objective = c * x
        s = CyClpSimplex(m)
    else:
        s = CyClpSimplex()
        cylpDir = os.environ['CYLP_SOURCE_DIR']
        inputFile = os.path.join(cylpDir, 'cylp', 'input', 'p0033.mps')
        m = s.extractCyLPModel(inputFile)
        x = m.getVarByName('x')
        s.setInteger(x)

    cbcModel = s.getCbcModel()

    gc = GomoryCutGenerator(m)
    cbcModel.addPythonCutGenerator(gc, name='PyGomory')

    cbcModel.branchAndBound()

    print cbcModel.primalVariableSolution

示例#7
0
 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