Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_event():
# create solver instance
s = Model()
s.hideOutput()
s.setPresolve(SCIP_PARAMSETTING.OFF)
eventhdlr = MyEvent()
s.includeEventhdlr(eventhdlr, "TestFirstLPevent", "python event handler to catch FIRSTLPEVENT")
# add some variables
x = s.addVar("x", obj=1.0)
y = s.addVar("y", obj=2.0)
# add some constraint
s.addCons(x + 2*y >= 5)
# solve problem
s.optimize()
# print solution
assert round(s.getVal(x)) == 5.0
assert round(s.getVal(y)) == 0.0
def test_main():
scip, x = create_sudoku()
#scip.setBoolParam("misc/allowdualreds", False)
scip.setBoolParam("misc/allowdualreds", False)
scip.setEmphasis(SCIP_PARAMEMPHASIS.CPSOLVER)
scip.setPresolve(SCIP_PARAMSETTING.OFF)
scip.optimize()
if scip.getStatus() != 'optimal':
print('Sudoku is not feasible!')
else:
print('\nSudoku solution:\n')
for row in range(9):
out = ''
for col in range(9):
out += str(round(scip.getVal(x[row,col]))) + ' '
print(out)
def pricerredcost(self):
# Retreiving the dual solutions
dualSolutions = []
for i, c in enumerate(self.data['cons']):
dualSolutions.append(self.model.getDualsolLinear(c))
# Building a MIP to solve the subproblem
subMIP = Model("CuttingStock-Sub")
# Turning off presolve
subMIP.setPresolve(SCIP_PARAMSETTING.OFF)
# Setting the verbosity level to 0
subMIP.hideOutput()
cutWidthVars = []
varNames = []
varBaseName = "CutWidth"
# Variables for the subMIP
for i in range(len(dualSolutions)):
varNames.append(varBaseName + "_" + str(i))
cutWidthVars.append(subMIP.addVar(varNames[i], vtype = "I", obj = -1.0 * dualSolutions[i]))
# Adding the knapsack constraint
knapsackCons = subMIP.addCons(
quicksum(w*v for (w,v) in zip(self.data['widths'], cutWidthVars)) <= self.data['rollLength'])
def model():
# create solver instance
s = Model()
# include separator
sepa = GMI()
s.includeSepa(sepa, "python_gmi", "generates gomory mixed integer cuts", priority = 1000, freq = 1)
# turn off presolve
s.setPresolve(SCIP_PARAMSETTING.OFF)
# turn off heuristics
s.setHeuristics(SCIP_PARAMSETTING.OFF)
# turn off propagation
s.setIntParam("propagating/maxrounds", 0)
s.setIntParam("propagating/maxroundsroot", 0)
# turn off some cuts
s.setIntParam("separating/strongcg/freq", -1)
s.setIntParam("separating/gomory/freq", -1)
s.setIntParam("separating/aggregation/freq", -1)
s.setIntParam("separating/mcf/freq", -1)
s.setIntParam("separating/closecuts/freq", -1)
s.setIntParam("separating/clique/freq", -1)
s.setIntParam("separating/zerohalf/freq", -1)
# only two rounds of cuts
s.setIntParam("separating/maxroundsroot", 2)
def test_flpbenders_defcuts():
'''
test the Benders' decomposition plugins with the facility location problem.
'''
I,J,d,M,f,c = make_data()
master = flp(I, J, M, d, f)
# initializing the default Benders' decomposition with the subproblem
master.setPresolve(SCIP_PARAMSETTING.OFF)
master.setBoolParam("misc/allowstrongdualreds", False)
master.setBoolParam("misc/allowweakdualreds", False)
master.setBoolParam("benders/copybenders", False)
bendersName = "testBenders"
testbd = testBenders(master.data, I, J, M, c, d, bendersName)
master.includeBenders(testbd, bendersName, "benders plugin")
master.includeBendersDefaultCuts(testbd)
master.activateBenders(testbd, 1)
master.setBoolParam("constraints/benders/active", True)
master.setBoolParam("constraints/benderslp/active", True)
master.setBoolParam("benders/testBenders/updateauxvarbound", False)
# optimizing the problem using Benders' decomposition
master.optimize()
# since custom solving functions are defined, we need to manually solve the
# Benders' decomposition subproblems to get the best solution
def test_tree():
# create solver instance
s = Model()
s.setMaximize()
s.hideOutput()
s.setPresolve(SCIP_PARAMSETTING.OFF)
node_eventhdlr = NodeEventHandler()
s.includeEventhdlr(node_eventhdlr, "NodeEventHandler", "python event handler to catch NODEFOCUSED")
# add some variables
n = 121
x = [s.addVar("x{}".format(i), obj=1.0, vtype="INTEGER") for i in range(n)]
# add some constraints
for i in range(n):
for j in range(i):
dist = min(abs(i - j), abs(n - i - j))
if dist in (1, 3, 4):
s.addCons(x[i] + x[j] <= 1)
# solve problem
s.optimize()
def test_flpbenders_customcuts():
'''
test the Benders' decomposition plugins with the facility location problem.
'''
I,J,d,M,f,c = make_data()
master = flp(I, J, M, d, f)
# initializing the default Benders' decomposition with the subproblem
master.setPresolve(SCIP_PARAMSETTING.OFF)
master.setBoolParam("misc/allowdualreds", False)
master.setBoolParam("benders/copybenders", False)
bendersName = "testBenders"
benderscutName = "testBenderscut"
testbd = testBenders(master.data, I, J, M, c, d, bendersName)
testbdc = testBenderscut(I, J, M, d)
master.includeBenders(testbd, bendersName, "benders plugin")
master.includeBenderscut(testbd, testbdc, benderscutName,
"benderscut plugin", priority=1000000)
master.activateBenders(testbd, 1)
master.setBoolParam("constraints/benders/active", True)
master.setBoolParam("constraints/benderslp/active", True)
master.setBoolParam("benders/testBenders/updateauxvarbound", False)
# optimizing the problem using Benders' decomposition
master.optimize()
def test_flpbenders():
'''
test the Benders' decomposition plugins with the facility location problem.
'''
I,J,d,M,f,c = make_data()
master, subprob = flp(I,J,d,M,f,c)
# initializing the default Benders' decomposition with the subproblem
master.setPresolve(SCIP_PARAMSETTING.OFF)
master.setBoolParam("misc/allowstrongdualreds", False)
master.setBoolParam("benders/copybenders", False)
master.initBendersDefault(subprob)
# optimizing the problem using Benders' decomposition
master.optimize()
# solving the subproblems to get the best solution
master.computeBestSolSubproblems()
EPS = 1.e-6
y = master.data
facilities = [j for j in y if master.getVal(y[j]) > EPS]
x, suby = subprob.data
edges = [(i,j) for (i,j) in x if subprob.getVal(x[i,j]) > EPS]
J,M,f = multidict({1:[500,1000], 2:[500,1000], 3:[500,1000]}) # capacity, fixed costs
c = {(1,1):4, (1,2):6, (1,3):9, # transportation costs
(2,1):5, (2,2):4, (2,3):7,
(3,1):6, (3,2):3, (3,3):4,
(4,1):8, (4,2):5, (4,3):3,
(5,1):10, (5,2):8, (5,3):4,
}
return I,J,d,M,f,c
if __name__ == "__main__":
I,J,d,M,f,c = make_data()
master, subprob = flp(I,J,d,M,f,c)
# initializing the default Benders' decomposition with the subproblem
master.setPresolve(SCIP_PARAMSETTING.OFF)
master.setBoolParam("misc/allowdualreds", False)
master.setBoolParam("benders/copybenders", False)
master.initBendersDefault(subprob)
# optimizing the problem using Benders' decomposition
master.optimize()
# solving the subproblems to get the best solution
master.computeBestSolSubproblems()
EPS = 1.e-6
y = master.data
facilities = [j for j in y if master.getVal(y[j]) > EPS]
x, suby = subprob.data
edges = [(i,j) for (i,j) in x if subprob.getVal(x[i,j]) > EPS]
# if asked, disable presolving
if not presolving:
model.setIntParam('presolving/maxrounds', 0)
model.setIntParam('presolving/maxrestarts', 0)
# if asked, disable separating (cuts)
if not separating:
model.setIntParam('separating/maxroundsroot', 0)
# if asked, disable conflict analysis (more cuts)
if not conflict:
model.setBoolParam('conflict/enable', False)
# if asked, disable primal heuristics
if not heuristics:
model.setHeuristics(scip.SCIP_PARAMSETTING.OFF)