diff --git a/tests/test_alldiff.py b/tests/test_alldiff.py index efe9bec47..c14dd1bff 100644 --- a/tests/test_alldiff.py +++ b/tests/test_alldiff.py @@ -298,7 +298,4 @@ def test_main(): out = '' for col in range(9): out += str(round(scip.getVal(x[row,col]))) + ' ' - print(out) - -if __name__ == "__main__": - test_main() + print(out) \ No newline at end of file diff --git a/tests/test_benders.py b/tests/test_benders.py index c4a72df51..2273b5d48 100644 --- a/tests/test_benders.py +++ b/tests/test_benders.py @@ -106,8 +106,4 @@ def test_flpbenders(): # the solution will be lost master.freeBendersSubproblems() - assert master.getObjVal() == 5.61e+03 - - -if __name__ == "__main__": - test_flpbenders() + assert master.getObjVal() == 5.61e+03 \ No newline at end of file diff --git a/tests/test_branch_probing_lp.py b/tests/test_branch_probing_lp.py index 782cdab14..b8bde8aa9 100644 --- a/tests/test_branch_probing_lp.py +++ b/tests/test_branch_probing_lp.py @@ -93,7 +93,4 @@ def test_branching(): print("t", m.getVal(t)) assert my_branchrule.was_called_val - assert my_branchrule.was_called_int - -if __name__ == "__main__": - test_branching() + assert my_branchrule.was_called_int \ No newline at end of file diff --git a/tests/test_cons.py b/tests/test_cons.py index 57300f887..0192cec82 100644 --- a/tests/test_cons.py +++ b/tests/test_cons.py @@ -24,4 +24,19 @@ def test_getConsVars(): c = m.addCons(quicksum(x[i] for i in x) <= 1) assert m.getConsVars(c) == [x[i] for i in x] - \ No newline at end of file + +def test_constraint_option_setting(): + m = Model() + x = m.addVar() + c = m.addCons(x >= 3) + + for option in [True, False]: + m.setCheck(c, option) + m.setEnforced(c, option) + m.setRemovable(c, option) + m.setInitial(c, option) + + assert c.isChecked() == option + assert c.isEnforced() == option + assert c.isRemovable() == option + assert c.isInitial() == option \ No newline at end of file diff --git a/tests/test_conshdlr.py b/tests/test_conshdlr.py index 45c42852f..9b27a8c2d 100644 --- a/tests/test_conshdlr.py +++ b/tests/test_conshdlr.py @@ -249,7 +249,4 @@ def create_model(): assert "consdisable" in calls #assert "consdelvars" in calls #assert "consprint" in calls - assert "consgetnvars" in calls - -if __name__ == "__main__": - test_conshdlr() + assert "consgetnvars" in calls \ No newline at end of file diff --git a/tests/test_copy.py b/tests/test_copy.py index b256452f2..b518d0a13 100644 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -18,6 +18,3 @@ def test_copy(): s2.optimize() assert s.getObjVal() == s2.getObjVal() - -if __name__ == "__main__": - test_copy() diff --git a/tests/test_customizedbenders.py b/tests/test_customizedbenders.py index d72c7942c..146d80279 100644 --- a/tests/test_customizedbenders.py +++ b/tests/test_customizedbenders.py @@ -188,7 +188,7 @@ def make_data(): return I,J,d,M,f,c -def test_flpbenders_defcuts(): +def flpbenders_defcuts_test(): ''' test the Benders' decomposition plugins with the facility location problem. ''' @@ -235,7 +235,7 @@ def test_flpbenders_defcuts(): return master.getObjVal() -def test_flpbenders_customcuts(): +def flpbenders_customcuts_test(): ''' test the Benders' decomposition plugins with the facility location problem. ''' @@ -285,7 +285,7 @@ def test_flpbenders_customcuts(): return master.getObjVal() -def test_flp(): +def flp_test(): ''' test the Benders' decomposition plugins with the facility location problem. ''' @@ -309,10 +309,11 @@ def test_flp(): return master.getObjVal() -if __name__ == "__main__": - defcutsobj = test_flpbenders_defcuts() - customcutsobj = test_flpbenders_customcuts() - monolithicobj = test_flp() + +def test_customized_benders(): + defcutsobj = flpbenders_defcuts_test() + customcutsobj = flpbenders_customcuts_test() + monolithicobj = flp_test() assert defcutsobj == customcutsobj assert defcutsobj == monolithicobj diff --git a/tests/test_cutsel.py b/tests/test_cutsel.py index a0c193031..22d5ac45d 100644 --- a/tests/test_cutsel.py +++ b/tests/test_cutsel.py @@ -79,7 +79,3 @@ def test_cut_selector(): scip.setObjective(quicksum(c[i] * e[i] for i in range(len(A))), sense='minimize') scip.optimize() - - -if __name__ == "__main__": - test_cut_selector() diff --git a/tests/test_event.py b/tests/test_event.py index b595945b7..72c518073 100644 --- a/tests/test_event.py +++ b/tests/test_event.py @@ -48,6 +48,3 @@ def test_event(): assert 'eventexit' in calls assert 'eventexec' in calls assert len(calls) == 3 - -if __name__ == "__main__": - test_event() diff --git a/tests/test_gomory.py b/tests/test_gomory.py index b6f209b3c..b95537045 100644 --- a/tests/test_gomory.py +++ b/tests/test_gomory.py @@ -340,6 +340,3 @@ def test_CKS(): # solve problem s.optimize() s.printStatistics() - -if __name__ == "__main__": - test_CKS() diff --git a/tests/test_heur.py b/tests/test_heur.py index 1f1ade8fe..3ec3d62ba 100644 --- a/tests/test_heur.py +++ b/tests/test_heur.py @@ -64,6 +64,3 @@ def inner(): heur_prox.name assert is_memory_freed() - -if __name__ == "__main__": - test_heur() diff --git a/tests/test_knapsack.py b/tests/test_knapsack.py index 359f3256b..2634a4a24 100644 --- a/tests/test_knapsack.py +++ b/tests/test_knapsack.py @@ -45,7 +45,4 @@ def test_knapsack(): print ("\tIncluded Weight:", weights[i]*solValue, "\tItem Cost:", costs[i]*solValue) includedWeight = sum([weights[i]*varSolutions[i] for i in range(len(weights))]) - assert includedWeight > 0 and includedWeight <= knapsackSize - -if __name__ == "__main__": - test_knapsack() + assert includedWeight > 0 and includedWeight <= knapsackSize \ No newline at end of file diff --git a/tests/test_lp.py b/tests/test_lp.py index 98314e487..299ce122c 100644 --- a/tests/test_lp.py +++ b/tests/test_lp.py @@ -20,6 +20,3 @@ def test_lp(): solval = myLP.solve() assert round(5.0 == solval) - -if __name__ == "__main__": - test_lp() diff --git a/tests/test_memory.py b/tests/test_memory.py index b0b824713..c599f72df 100644 --- a/tests/test_memory.py +++ b/tests/test_memory.py @@ -16,8 +16,4 @@ def test_freed(): assert is_memory_freed() def test_print_memory_in_use(): - print_memory_in_use() - -if __name__ == "__main__": - test_not_freed() - test_freed() \ No newline at end of file + print_memory_in_use() \ No newline at end of file diff --git a/tests/test_model.py b/tests/test_model.py index 8223627d3..991d95e1b 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -141,6 +141,8 @@ def test_multiple_cons_names(): assert len(conss) == 5 assert all([c.name.startswith(name + "_") for c in conss]) + assert conss == m.getConss() + assert m.getNConss() == 5 def test_multiple_cons_params(): @@ -170,6 +172,38 @@ def assert_conss_neq(a, b): assert_conss_neq(conss[0], conss[1]) +def test_addCoefLinear(): + m = Model() + x = m.addVar(obj=1) + y = m.addVar(obj=0) + c = m.addCons(x >= 1) + + m.addCoefLinear(c, y, 1) + + m.optimize() + assert m.getVal(x) == 0 + +def test_delCoefLinear(): + m = Model() + x = m.addVar(obj=1) + y = m.addVar(obj=0) + c = m.addCons(x + y >= 1) + + m.delCoefLinear(c,y) + + m.optimize() + assert m.getVal(x) == 1 + +def test_chgCoefLinear(): + m = Model() + x = m.addVar(obj=10) + y = m.addVar(obj=1) + c = m.addCons(x + y >= 1) + + m.chgCoefLinear(c, y, 0.001) + + m.optimize() + assert m.getObjVal() == 10 def test_model_ptr(): model1 = Model() diff --git a/tests/test_nlrow.py b/tests/test_nlrow.py index 63e12d68e..64bed80ce 100644 --- a/tests/test_nlrow.py +++ b/tests/test_nlrow.py @@ -35,6 +35,13 @@ def test_nlrow(): # collect nonlinear rows nlrows = m.getNlRows() + + # test getNNlRows + assert len(nlrows) == m.getNNlRows() + + # to test printing of NLRows + for row in nlrows: + m.printNlRow(row) # the nlrow that corresponds to the linear (third) constraint is added before the nonlinear rows, # because Initsol of the linear conshdlr gets called first @@ -65,6 +72,3 @@ def test_nlrow(): linterms = nlrows[2].getLinearTerms() assert len(linterms) == 0 - -if __name__ == "__main__": - test_nlrow() diff --git a/tests/test_nodesel.py b/tests/test_nodesel.py index 665ac07ee..2fa4254e9 100644 --- a/tests/test_nodesel.py +++ b/tests/test_nodesel.py @@ -41,8 +41,4 @@ def test_nodesel(): m.addCons(x1 * x2 >= x0) m.setObjective(x1 + x0) - m.optimize() - - -if __name__ == "__main__": - test_nodesel() + m.optimize() \ No newline at end of file diff --git a/tests/test_nonlinear.py b/tests/test_nonlinear.py index 35df5a14f..528ed383c 100644 --- a/tests/test_nonlinear.py +++ b/tests/test_nonlinear.py @@ -284,11 +284,4 @@ def test_quad_coeffs(): assert quadterms[0][1] == 0.5 assert linterms[0][0].name == z.name - assert linterms[0][1] == 4 - -if __name__ == "__main__": - test_string_poly() - test_string() - test_circle() - test_gastrans() - test_quad_coeffs() + assert linterms[0][1] == 4 \ No newline at end of file diff --git a/tests/test_pricer.py b/tests/test_pricer.py index 111b0452e..ebae4eb46 100644 --- a/tests/test_pricer.py +++ b/tests/test_pricer.py @@ -38,6 +38,7 @@ def pricerredcost(self): objval = 1 + subMIP.getObjVal() + # Adding the column to the master problem if objval < -1e-08: currentNumVar = len(self.data['var']) @@ -53,6 +54,9 @@ def pricerredcost(self): newPattern.append(coeff) + # Testing getVarRedcost + assert round(self.model.getVarRedcost(newVar),6) == round(objval,6) + # Storing the new variable in the pricer data. self.data['patterns'].append(newPattern) self.data['var'].append(newVar) @@ -112,6 +116,7 @@ def test_cuttingstock(): pricer.data['demand'] = demand pricer.data['rollLength'] = rollLength pricer.data['patterns'] = patterns + pricer.data['redcosts'] = [] # solve problem s.optimize() @@ -142,7 +147,7 @@ def test_cuttingstock(): print(outline) print('\t\t\tTotal Output:\t', '\t'.join(str(e) for e in widthOutput)) - + assert s.getObjVal() == 452.25 def test_incomplete_pricer(): @@ -155,9 +160,4 @@ class IncompletePricer(Pricer): model.includePricer(pricer, "", "") with pytest.raises(Exception): - model.optimize() - - -if __name__ == '__main__': - test_cuttingstock() - test_incomplete_pricer() + model.optimize() \ No newline at end of file diff --git a/tests/test_quadcons.py b/tests/test_quadcons.py index 6170a7375..3c378c189 100644 --- a/tests/test_quadcons.py +++ b/tests/test_quadcons.py @@ -25,8 +25,4 @@ def test_niceqcqp(): s.optimize() assert round(s.getVal(x)) == 1.0 - assert round(s.getVal(y)) == 1.0 - -if __name__ == "__main__": - test_niceqp() - test_niceqcqp() + assert round(s.getVal(y)) == 1.0 \ No newline at end of file diff --git a/tests/test_quickprod.py b/tests/test_quickprod.py index 04b26e2c6..70e767047 100644 --- a/tests/test_quickprod.py +++ b/tests/test_quickprod.py @@ -35,8 +35,3 @@ def test_largequadratic(): assert cons.expr.degree() == 2*dim*dim m.addCons(cons) # TODO: what can we test beyond the lack of crashes? - -if __name__ == "__main__": - test_quickprod() - test_quickprod_model() - test_largequadratic() diff --git a/tests/test_quicksum.py b/tests/test_quicksum.py index 83c5de11a..3ac8f26ae 100644 --- a/tests/test_quicksum.py +++ b/tests/test_quicksum.py @@ -33,8 +33,3 @@ def test_largequadratic(): assert len(cons.expr.terms) == dim * (dim-1) / 2 + dim m.addCons(cons) # TODO: what can we test beyond the lack of crashes? - -if __name__ == "__main__": - test_quicksum() - test_quicksum_model() - test_largequadratic() diff --git a/tests/test_reader.py b/tests/test_reader.py index 6debe458e..2f4712271 100644 --- a/tests/test_reader.py +++ b/tests/test_reader.py @@ -59,13 +59,13 @@ def createFile(filename): def deleteFile(filename): os.remove(filename) -def test(): +def test_sudoku_reader(): createFile("tmp.sod") - m = Model("soduko") + m = Model("sudoku") reader = SudokuReader() - m.includeReader(reader, "sodreader", "PyReader for soduko problem", "sod") + m.includeReader(reader, "sudreader", "PyReader for sudoku problem", "sod") m.readProblem("tmp.sod") @@ -76,10 +76,6 @@ def test(): m.writeProblem("model.sod") with open("model.sod", "r") as f: input = f.readline() - assert input == "soduko" + assert input == "sudoku" - deleteFile("model.sod") - - -if __name__ == "__main__": - test() + deleteFile("model.sod") \ No newline at end of file diff --git a/tests/test_relax.py b/tests/test_relax.py index 480cd9040..5ecdfed23 100644 --- a/tests/test_relax.py +++ b/tests/test_relax.py @@ -29,8 +29,4 @@ def test_relax(): m.optimize() assert 'relaxexec' in calls - assert len(calls) >= 1 - - -if __name__ == "__main__": - test_relax() + assert len(calls) >= 1 \ No newline at end of file diff --git a/tests/test_solution.py b/tests/test_solution.py index cb6fd9030..33c654c39 100644 --- a/tests/test_solution.py +++ b/tests/test_solution.py @@ -1,5 +1,5 @@ -from pyscipopt import Model, scip, SCIP_PARAMSETTING import pytest +from pyscipopt import Model, scip, SCIP_PARAMSETTING, quicksum, quickprod def test_solution_getbest(): m = Model() @@ -62,6 +62,23 @@ def test_solution_evaluation(): assert sol[expr] == m.getVal(expr) assert sol[expr2] == m.getVal(expr2) +def test_getSolTime(): + m = Model() + m.setPresolve(SCIP_PARAMSETTING.OFF) + + x = {} + for i in range(20): + x[i] = m.addVar(ub=i) + + for i in range(1,6): + m.addCons(quicksum(x[j] for j in range(20) if j%i==0) >= i) + m.addCons(quickprod(x[j] for j in range(20) if j%i==0) <= i**3) + + m.setObjective(quicksum(x[i] for i in range(20))) + m.optimize() + for s in m.getSols(): + assert m.getSolTime(s) >= 0 + def test_hasPrimalRay(): m = Model() x = m.addVar() @@ -103,7 +120,6 @@ def test_getPrimalRay(): assert m.getPrimalRay() == [1,0] - def test_create_solution(): with pytest.raises(ValueError): scip.Solution() @@ -116,7 +132,6 @@ def test_print_soltion(): assert str(m.getBestSol()) == "{'x1': -0.0}" - def test_getSols(): m = Model() @@ -124,7 +139,4 @@ def test_getSols(): m.optimize() assert len(m.getSols()) >= 1 - assert any(sol[x] == 0.0 for sol in m.getSols()) - -if __name__ == "__main__": - test_getPrimalRayVal() \ No newline at end of file + assert any(sol[x] == 0.0 for sol in m.getSols()) \ No newline at end of file diff --git a/tests/test_tree.py b/tests/test_tree.py index 74f0c7e69..58b121508 100644 --- a/tests/test_tree.py +++ b/tests/test_tree.py @@ -60,7 +60,4 @@ def test_tree(): del s - assert len(node_eventhdlr.calls) > 3 - -if __name__ == "__main__": - test_tree() + assert len(node_eventhdlr.calls) > 3 \ No newline at end of file diff --git a/tests/test_tsp.py b/tests/test_tsp.py index c6fb86dad..127fc267c 100644 --- a/tests/test_tsp.py +++ b/tests/test_tsp.py @@ -111,8 +111,4 @@ def test_main(): objective_value, edges = solve_tsp(vertices, distance) print("Optimal tour:", edges) - print("Optimal cost:", objective_value) - - -if __name__ == "__main__": - test_main() + print("Optimal cost:", objective_value) \ No newline at end of file diff --git a/tests/test_vars.py b/tests/test_vars.py index b55366a80..d8c92ff40 100644 --- a/tests/test_vars.py +++ b/tests/test_vars.py @@ -64,8 +64,4 @@ def test_vtype(): assert x.vtype() == "INTEGER" m.chgVarType(y, 'M') - assert y.vtype() == "IMPLINT" - -if __name__ == "__main__": - test_variablebounds() - test_vtype() + assert y.vtype() == "IMPLINT" \ No newline at end of file