Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# add heuristic to interrupt solve: the methods we wanna test can only be called in solving stage
heuristic = MyHeur()
m.includeHeur(heuristic, "PyHeur", "heur to interrupt", "Y", timingmask=SCIP_HEURTIMING.BEFORENODE)
# create variables
x = m.addVar(name="x", lb=-3, ub=3, obj=-1)
y = m.addVar(name="y", lb=-3, ub=3, obj=-1)
# create constraints
m.addCons(1*x + 2*y + 3 * x**2 + 4*y**2 + 5*x*y <= 6)
m.addCons(7*x**2 + 8*y**2 == 9)
m.addCons(10*x + 11*y <= 12)
# optimize without presolving
m.setPresolve(SCIP_PARAMSETTING.OFF)
m.optimize()
# check whether NLP has been constructed and there are 3 nonlinear rows that match the above constraints
assert m.isNLPConstructed()
assert m.getNNlRows() == 3
# collect nonlinear rows
nlrows = m.getNlRows()
# check first nonlinear row
assert nlrows[0].getLhs() == -m.infinity()
assert nlrows[0].getRhs() == 6
linterms = nlrows[0].getLinearTerms()
assert len(linterms) == 2
assert str(linterms[0][0]) == "t_x"
def test_heur():
# create solver instance
s = Model()
heuristic = MyHeur()
s.includeHeur(heuristic, "PyHeur", "custom heuristic implemented in python", "Y", timingmask=SCIP_HEURTIMING.BEFORENODE)
s.setPresolve(SCIP_PARAMSETTING.OFF)
# 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
sol = s.getBestSol()
assert sol != None
assert round(sol[x]) == 5.0
assert round(sol[y]) == 0.0
def model():
# create solver instance
s = Model()
# include separator
sepa = GMI()
s.includeSepa(sepa, "python_gmi", "generate GMI", 1000, 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/cmir/freq", -1)
s.setIntParam("separating/gomory/freq", -1)
s.setIntParam("separating/flowcover/freq", -1)
s.setIntParam("separating/mcf/freq", -1)
s.setIntParam("separating/closecuts/freq", -1)
s.setIntParam("separating/clique/freq", -1)
# iteration limits over root lp: TODO: this doesn't seem to work. I wanted to use it to write some asserts regarding
# cuts, but probably I should find a better way of testing.
def test_flp():
'''
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, c=c, monolithic=True)
# initializing the default Benders' decomposition with the subproblem
master.setPresolve(SCIP_PARAMSETTING.OFF)
# optimizing the monolithic problem
master.optimize()
EPS = 1.e-6
y = master.data
facilities = [j for j in y if master.getVal(y[j]) > EPS]
print("Optimal value:", master.getObjVal())
print("Facilities at nodes:", facilities)
master.printBestSol()
master.printStatistics()
return master.getObjVal()
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)
return s
maximize 15x + 18y + 30z
subject to 2x + y + z <= 60
x + 2y + z <= 60
z <= 30
x,y,z >= 0
Variables correspond to the production of three types of wine blends,
made from pure-grape wines.
Constraints correspond to the inventory of pure-grape wines.
Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
"""
from pyscipopt import Model, quicksum, SCIP_PARAMSETTING
#Initialize model
model = Model("Wine blending")
model.setPresolve(SCIP_PARAMSETTING.OFF)
Inventory = {"Alfrocheiro":60, "Baga":60, "Castelao":30}
Grapes = Inventory.keys()
Profit = {"Dry":15, "Medium":18, "Sweet":30}
Blends = Profit.keys()
Use = {
("Alfrocheiro","Dry"):2,
("Alfrocheiro","Medium"):1,
("Alfrocheiro","Sweet"):1,
("Baga","Dry"):1,
("Baga","Medium"):2,
("Baga","Sweet"):1,
("Castelao","Dry"):0,
("Castelao","Medium"):0,