Esempio n. 1
0
def solve(filename, method):

    s = CyClpSimplex()
    s.readMps(filename)

    s.preSolve(feasibilityTolerance=10 ** -8)
    #s.useCustomPrimal(1)

    if method == 'd':
        pivot = DantzigPivot(s)
    elif method == 'l':
        pivot = LIFOPivot(s)
    elif method == 'm':
        pivot = MostFrequentPivot(s)
    elif method == 'p':
        pivot = PositiveEdgePivot(s)
    else:
        print('Unkown solution method.')
        sys.exit(1)

    s.setPivotMethod(pivot)

    #s.setPerturbation(50)

    start = clock()
    s.primal()
    print('Problem solved in %g seconds.' % (clock() - start))
    return s.objectiveValue
Esempio n. 2
0
def solve(filename, method):

    s = CyClpSimplex()
    s.readMps(filename)

    s.preSolve(feasibilityTolerance=10 ** -8)
    #s.useCustomPrimal(1)

    if method == 'd':
        pivot = DantzigPivot(s)
    elif method == 'l':
        pivot = LIFOPivot(s)
    elif method == 'm':
        pivot = MostFrequentPivot(s)
    elif method == 'p':
        pivot = PositiveEdgePivot(s)
    else:
        print 'Unkown solution method.'
        sys.exit(1)

    s.setPivotMethod(pivot)

    #s.setPerturbation(50)

    start = clock()
    s.primal()
    print 'Problem solved in %g seconds.' % (clock() - start)
    return s.objectiveValue
Esempio n. 3
0
    def test_1(self):
        """simplest QP test"""
        s = CyClpSimplex()
        s.readMps(join(currentFilePath, '../input/hs35.qps'))
        #self.assertTrue(abs(cbcModel.objectiveValue - 3089.0) < 10 ** -6)

        #print s.Hessian.todense()

        p = WolfePivot(s)
        s.setPivotMethod(p)

        s.primal()
        print s.primalVariableSolution
        print s.objectiveValue
Esempio n. 4
0
    def test_1(self):
        """simplest QP test"""
        s = CyClpSimplex()
        s.readMps(join(currentFilePath, '../input/hs35.qps'))
        #self.assertTrue(abs(cbcModel.objectiveValue - 3089.0) < 10 ** -6)
        
        #print s.Hessian.todense()

        p = WolfePivot(s)
        s.setPivotMethod(p)

        s.primal()
        print s.primalVariableSolution
        print s.objectiveValue
Esempio n. 5
0
class TestCyClpSimplex(unittest.TestCase):

    def setUp(self):
        self.s = CyClpSimplex()
        self.s.readMps(join(currentFilePath, '../input/p0033.mps'))

    def test_PE(self):
        #pivot = PositiveEdgePivot(self.s)
        self.s.setPivotMethod(PositiveEdgePivot(self.s))
        self.s.primal()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_Dantzig(self):
        #pivot = DantzigPivot(self.s)
        self.s.setPivotMethod(DantzigPivot(self.s))
        self.s.primal()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_LIFO(self):
        #pivot = LIFOPivot(self.s)
        self.s.setPivotMethod(LIFOPivot(self.s))
        self.s.primal()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_MostFrequent(self):
        #pivot = MostFrequentPivot(self.s)
        self.s.setPivotMethod(MostFrequentPivot(self.s))
        self.s.primal()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_initialSolve(self):
        self.s.initialSolve()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_initialPrimalSolve(self):
        self.s.initialPrimalSolve()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_initialDualSolve(self):
        self.s.initialDualSolve()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_direction(self):
        self.assertEqual(self.s.optimizationDirection, 'min')
        self.s.optimizationDirection = 'max'
        self.assertEqual(self.s.optimizationDirection, 'max')
Esempio n. 6
0
class TestCyClpSimplex(unittest.TestCase):
    def setUp(self):
        self.s = CyClpSimplex()
        self.s.readMps(join(currentFilePath, '../input/p0033.mps'))

    def test_PE(self):
        #pivot = PositiveEdgePivot(self.s)
        self.s.setPivotMethod(PositiveEdgePivot(self.s))
        self.s.primal()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_Dantzig(self):
        #pivot = DantzigPivot(self.s)
        self.s.setPivotMethod(DantzigPivot(self.s))
        self.s.primal()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_LIFO(self):
        #pivot = LIFOPivot(self.s)
        self.s.setPivotMethod(LIFOPivot(self.s))
        self.s.primal()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_MostFrequent(self):
        #pivot = MostFrequentPivot(self.s)
        self.s.setPivotMethod(MostFrequentPivot(self.s))
        self.s.primal()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_initialSolve(self):
        self.s.initialSolve()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_initialPrimalSolve(self):
        self.s.initialPrimalSolve()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_initialDualSolve(self):
        self.s.initialDualSolve()
        self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)

    def test_direction(self):
        self.assertEqual(self.s.optimizationDirection, 'min')
        self.s.optimizationDirection = 'max'
        self.assertEqual(self.s.optimizationDirection, 'max')
Esempio n. 7
0
    def test_NodeCompare(self):
        s = CyClpSimplex()
        s.readMps(join(currentFilePath, '../input/p0033.mps'))

        s.copyInIntegerInformation(np.array(s.nCols * [True], np.uint8))

        print("Solving relaxation")
        cbcModel = s.getCbcModel()
        n = SimpleNodeCompare()
        cbcModel.setNodeCompare(n)

        gom = CyCglGomory(limit=150)
        #gom.limit = 150
        cbcModel.addCutGenerator(gom, name="Gomory")

        #clq = CyCglClique()
        #cbcModel.addCutGenerator(clq, name="Clique")

        knap = CyCglKnapsackCover(maxInKnapsack=50)
        cbcModel.addCutGenerator(knap, name="Knapsack")

        cbcModel.branchAndBound()
        self.assertTrue(abs(cbcModel.objectiveValue - 3089.0) < 10**-6)
Esempio n. 8
0
    def test_NodeCompare(self):
        s = CyClpSimplex()
        s.readMps(join(currentFilePath, '../input/p0033.mps'))

        s.copyInIntegerInformation(np.array(s.nCols * [True], np.uint8))

        print "Solving relaxation"
        cbcModel = s.getCbcModel()
        n = SimpleNodeCompare()
        cbcModel.setNodeCompare(n)

        gom = CyCglGomory(limit=150)
        #gom.limit = 150
        cbcModel.addCutGenerator(gom, name="Gomory")

        #clq = CyCglClique()
        #cbcModel.addCutGenerator(clq, name="Clique")

        knap = CyCglKnapsackCover(maxInKnapsack=50)
        cbcModel.addCutGenerator(knap, name="Knapsack")


        cbcModel.branchAndBound()
        self.assertTrue(abs(cbcModel.objectiveValue - 3089.0) < 10 ** -6)
Esempio n. 9
0
            #del rc2
            return  indicesToConsider[ind]
        return -1

    def saveWeights(self, model, mode):
        self.clpModel = model

    def isPivotAcceptable(self):
        return True


def getMpsExample():
    import os
    import inspect
    cylpDir = os.environ['CYLP_SOURCE_DIR']
    return os.path.join(cylpDir, 'cylp', 'input', 'p0033.mps')


if __name__ == "__main__":
    if len(sys.argv) == 1:
        import doctest
        doctest.testmod()
    else:
        from cylp.cy import CyClpSimplex
        from cylp.py.pivots import DantzigPivot
        s = CyClpSimplex()
        s.readMps(sys.argv[1])
        pivot = DantzigPivot(s)
        s.setPivotMethod(pivot)
        s.primal()
Esempio n. 10
0
            #del rc2
            return  indicesToConsider[ind]
        return -1

    def saveWeights(self, model, mode):
        self.clpModel = model

    def isPivotAcceptable(self):
        return True


def getMpsExample():
    import os
    import inspect
    cylpDir = os.environ['CYLP_SOURCE_DIR']
    return os.path.join(cylpDir, 'cylp', 'input', 'p0033.mps')


if __name__ == "__main__":
    if len(sys.argv) == 1:
        import doctest
        doctest.testmod()
    else:
        from cylp.cy import CyClpSimplex
        from cylp.py.pivots import DantzigPivot
        s = CyClpSimplex()
        s.readMps(sys.argv[1])
        pivot = DantzigPivot(s)
        s.setPivotMethod(pivot)
        s.primal()
Esempio n. 11
0
            model.solution[basicVarInds[rowNumbers]] -= change

        changeObj = -np.dot(change, cost)
        primalUpdate.clear()
        objectiveChange[0] += changeObj

        return changeObj


def getMpsExample():
    import os
    import inspect
    import sys
    cylpDir = os.environ['CYLP_SOURCE_DIR']
    return os.path.join(cylpDir, 'cylp', 'input', 'p0033.mps')


if __name__ == "__main__":
    print(sys.argv)
    if len(sys.argv) == 1:
        import doctest
        doctest.testmod()
    else:
        from cylp.cy import CyClpSimplex
        from cylp.py.pivots import DualDantzigPivot
        s = CyClpSimplex()
        s.readMps(sys.argv[1])  # Returns 0 if OK
        pivot = DualDantzigPivot(s)
        s.setDualPivotMethod(pivot)
        s.dual()
Esempio n. 12
0
if __name__ == "__main__":
    mip_model = MIPModel(GurobiSolver())
    starttime1 = timeit.default_timer()
    load_mps_primitive(mip_model, path="../../examples/mps_files/10teams.mps")
    endtime1 = timeit.default_timer()
    mip_model.set_mip_sense(LPSense.MIN)
    starttime2 = timeit.default_timer()
    mip_model.lp_model.optimize()
    endtime2 = timeit.default_timer()
    first_value = mip_model.lp_model.get_objective_value()
    mip_model = MIPModel(GurobiSolver())
    starttime3 = timeit.default_timer()
    load_mps_advanced(mip_model, path="../../examples/mps_files/10teams.mps")
    endtime3 = timeit.default_timer()
    mip_model.set_mip_sense(LPSense.MIN)
    starttime4 = timeit.default_timer()
    mip_model.lp_model.optimize()
    endtime4 = timeit.default_timer()
    second_value = mip_model.lp_model.get_objective_value()
    print(
        "Primitive Load: {}s loading time, {}s optimization time, objective value: {}\nAdvanced Load: {}s loading time, {}s optimization time, objective value: {}"
        .format(endtime1 - starttime1, endtime2 - starttime2, first_value,
                endtime3 - starttime3, endtime4 - starttime4, second_value))

    from cylp.cy import CyClpSimplex
    s = CyClpSimplex()
    s.readMps("../../examples/mps_files/10teams.mps")
    s.initialSolve()
    print(s.objectiveValue)
    print(s.solution)
Esempio n. 13
0
            model.solution[basicVarInds[rowNumbers]] -= change

        changeObj = -np.dot(change, cost)
        primalUpdate.clear()
        objectiveChange[0] += changeObj

        return changeObj


def getMpsExample():
    import os
    import inspect
    import sys
    cylpDir = os.environ['CYLP_SOURCE_DIR']
    return os.path.join(cylpDir, 'cylp', 'input', 'p0033.mps')


if __name__ == "__main__":
    print sys.argv
    if len(sys.argv) == 1:
        import doctest
        doctest.testmod()
    else:
        from cylp.cy import CyClpSimplex
        from cylp.py.pivots import DualDantzigPivot
        s = CyClpSimplex()
        s.readMps(sys.argv[1])  # Returns 0 if OK
        pivot = DualDantzigPivot(s)
        s.setDualPivotMethod(pivot)
        s.dual()
Esempio n. 14
0
#!/usr/bin/env python3

import numpy as np
from cylp.cy import CyClpSimplex
from cylp.py.modeling.CyLPModel import CyLPArray

s = CyClpSimplex()

s.readMps("/Users/cassie/Dropbox/GA/Website/example.lp")
s.initialSolve()

# # Add variables
# x = s.addVariable('x', 3)
# y = s.addVariable('y', 2)

# # Create coefficients and bounds
# A = np.matrix([[1., 2., 0],[1., 0, 1.]])
# B = np.matrix([[1., 0, 0], [0, 0, 1.]])
# D = np.matrix([[1., 2.],[0, 1]])
# a = CyLPArray([5, 2.5])
# b = CyLPArray([4.2, 3])
# x_u= CyLPArray([2., 3.5])

# # Add constraints
# s += A * x <= a
# s += 2 <= B * x + D * y <= b
# s += y >= 0
# s += 1.1 <= x[1:3] <= x_u

# # Set the objective function
# c = CyLPArray([1., -2., 3.])