Skip to content

Commit

Permalink
Add type to loop variables only (#947)
Browse files Browse the repository at this point in the history
* add missing type

* Typed loop variables

* remove forgotten stuff

* Update CHANGELOG.md

Co-authored-by: DominikKamp <[email protected]>

* Dominik's comments

* Also on conshdlr, even though it looks ugly

* fixing some more stuff

* Update src/pyscipopt/reader.pxi

Co-authored-by: DominikKamp <[email protected]>

* added some consistency to variable names

* Remove additional lines

* Reorder variable declarations

* Reformat variable declarations

---------

Co-authored-by: DominikKamp <[email protected]>
Co-authored-by: Dominik Kamp <[email protected]>
  • Loading branch information
3 people authored Feb 1, 2025
1 parent c473fd7 commit 12a76e9
Show file tree
Hide file tree
Showing 8 changed files with 341 additions and 248 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
### Added
- Added cdef type declaration of loop variables for slight speedup
- Added wrappers for setting and getting heuristic timing
- Added transformed option to getVarDict, updated test
- Added categorical data example
Expand Down
4 changes: 2 additions & 2 deletions src/pyscipopt/benders.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ cdef SCIP_RETCODE PyBendersSolvesub (SCIP* scip, SCIP_BENDERS* benders, SCIP_SOL
cdef SCIP_RETCODE PyBendersPostsolve (SCIP* scip, SCIP_BENDERS* benders, SCIP_SOL* sol,
SCIP_BENDERSENFOTYPE type, int* mergecands, int npriomergecands, int nmergecands, SCIP_Bool checkint,
SCIP_Bool infeasible, SCIP_Bool* merged) noexcept with gil:
cdef SCIP_BENDERSDATA* bendersdata
bendersdata = SCIPbendersGetData(benders)
cdef SCIP_BENDERSDATA* bendersdata = SCIPbendersGetData(benders)
cdef int i
PyBenders = <Benders>bendersdata
if sol == NULL:
solution = None
Expand Down
48 changes: 32 additions & 16 deletions src/pyscipopt/conshdlr.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -168,48 +168,54 @@ cdef SCIP_RETCODE PyConsFree (SCIP* scip, SCIP_CONSHDLR* conshdlr) noexcept with
return SCIP_OKAY

cdef SCIP_RETCODE PyConsInit (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss) noexcept with gil:
cdef int i
PyConshdlr = getPyConshdlr(conshdlr)
cdef constraints = []
constraints = []
for i in range(nconss):
constraints.append(getPyCons(conss[i]))
PyConshdlr.consinit(constraints)
return SCIP_OKAY

cdef SCIP_RETCODE PyConsExit (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss) noexcept with gil:
cdef int i
PyConshdlr = getPyConshdlr(conshdlr)
cdef constraints = []
constraints = []
for i in range(nconss):
constraints.append(getPyCons(conss[i]))
PyConshdlr.consexit(constraints)
return SCIP_OKAY

cdef SCIP_RETCODE PyConsInitpre (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss) noexcept with gil:
cdef int i
PyConshdlr = getPyConshdlr(conshdlr)
cdef constraints = []
constraints = []
for i in range(nconss):
constraints.append(getPyCons(conss[i]))
PyConshdlr.consinitpre(constraints)
return SCIP_OKAY

cdef SCIP_RETCODE PyConsExitpre (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss) noexcept with gil:
cdef int i
PyConshdlr = getPyConshdlr(conshdlr)
cdef constraints = []
constraints = []
for i in range(nconss):
constraints.append(getPyCons(conss[i]))
PyConshdlr.consexitpre(constraints)
return SCIP_OKAY

cdef SCIP_RETCODE PyConsInitsol (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss) noexcept with gil:
cdef int i
PyConshdlr = getPyConshdlr(conshdlr)
cdef constraints = []
constraints = []
for i in range(nconss):
constraints.append(getPyCons(conss[i]))
PyConshdlr.consinitsol(constraints)
return SCIP_OKAY

cdef SCIP_RETCODE PyConsExitsol (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss, SCIP_Bool restart) noexcept with gil:
cdef int i
PyConshdlr = getPyConshdlr(conshdlr)
cdef constraints = []
constraints = []
for i in range(nconss):
constraints.append(getPyCons(conss[i]))
PyConshdlr.consexitsol(constraints, restart)
Expand Down Expand Up @@ -244,17 +250,19 @@ cdef SCIP_RETCODE PyConsTrans (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS* s
return SCIP_OKAY

cdef SCIP_RETCODE PyConsInitlp (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss, SCIP_Bool* infeasible) noexcept with gil:
cdef int i
PyConshdlr = getPyConshdlr(conshdlr)
cdef constraints = []
constraints = []
for i in range(nconss):
constraints.append(getPyCons(conss[i]))
result_dict = PyConshdlr.consinitlp(constraints)
infeasible[0] = result_dict.get("infeasible", infeasible[0])
return SCIP_OKAY

cdef SCIP_RETCODE PyConsSepalp (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss, int nusefulconss, SCIP_RESULT* result) noexcept with gil:
cdef int i
PyConshdlr = getPyConshdlr(conshdlr)
cdef constraints = []
constraints = []
for i in range(nconss):
constraints.append(getPyCons(conss[i]))
result_dict = PyConshdlr.conssepalp(constraints, nusefulconss)
Expand All @@ -263,8 +271,9 @@ cdef SCIP_RETCODE PyConsSepalp (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS**

cdef SCIP_RETCODE PyConsSepasol (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss, int nusefulconss,
SCIP_SOL* sol, SCIP_RESULT* result) noexcept with gil:
cdef int i
PyConshdlr = getPyConshdlr(conshdlr)
cdef constraints = []
constraints = []
for i in range(nconss):
constraints.append(getPyCons(conss[i]))
solution = Solution.create(scip, sol)
Expand All @@ -274,17 +283,19 @@ cdef SCIP_RETCODE PyConsSepasol (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS*

cdef SCIP_RETCODE PyConsEnfolp (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss, int nusefulconss,
SCIP_Bool solinfeasible, SCIP_RESULT* result) noexcept with gil:
cdef int i
PyConshdlr = getPyConshdlr(conshdlr)
cdef constraints = []
constraints = []
for i in range(nconss):
constraints.append(getPyCons(conss[i]))
result_dict = PyConshdlr.consenfolp(constraints, nusefulconss, solinfeasible)
result[0] = result_dict.get("result", <SCIP_RESULT>result[0])
return SCIP_OKAY

cdef SCIP_RETCODE PyConsEnforelax (SCIP* scip, SCIP_SOL* sol, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss, int nusefulconss, SCIP_Bool solinfeasible, SCIP_RESULT* result) noexcept with gil:
cdef int i
PyConshdlr = getPyConshdlr(conshdlr)
cdef constraints = []
constraints = []
for i in range(nconss):
constraints.append(getPyCons(conss[i]))
solution = Solution.create(scip, sol)
Expand All @@ -294,8 +305,9 @@ cdef SCIP_RETCODE PyConsEnforelax (SCIP* scip, SCIP_SOL* sol, SCIP_CONSHDLR* con

cdef SCIP_RETCODE PyConsEnfops (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss, int nusefulconss,
SCIP_Bool solinfeasible, SCIP_Bool objinfeasible, SCIP_RESULT* result) noexcept with gil:
cdef int i
PyConshdlr = getPyConshdlr(conshdlr)
cdef constraints = []
constraints = []
for i in range(nconss):
constraints.append(getPyCons(conss[i]))
result_dict = PyConshdlr.consenfops(constraints, nusefulconss, solinfeasible, objinfeasible)
Expand All @@ -304,8 +316,9 @@ cdef SCIP_RETCODE PyConsEnfops (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS**

cdef SCIP_RETCODE PyConsCheck (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss, SCIP_SOL* sol, SCIP_Bool checkintegrality,
SCIP_Bool checklprows, SCIP_Bool printreason, SCIP_Bool completely, SCIP_RESULT* result) noexcept with gil:
cdef int i
PyConshdlr = getPyConshdlr(conshdlr)
cdef constraints = []
constraints = []
for i in range(nconss):
constraints.append(getPyCons(conss[i]))
solution = Solution.create(scip, sol)
Expand All @@ -315,8 +328,9 @@ cdef SCIP_RETCODE PyConsCheck (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS**

cdef SCIP_RETCODE PyConsProp (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss, int nusefulconss, int nmarkedconss,
SCIP_PROPTIMING proptiming, SCIP_RESULT* result) noexcept with gil:
cdef int i
PyConshdlr = getPyConshdlr(conshdlr)
cdef constraints = []
constraints = []
for i in range(nconss):
constraints.append(getPyCons(conss[i]))
result_dict = PyConshdlr.consprop(constraints, nusefulconss, nmarkedconss, proptiming)
Expand All @@ -328,8 +342,9 @@ cdef SCIP_RETCODE PyConsPresol (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS**
int nnewdelconss, int nnewaddconss, int nnewupgdconss, int nnewchgcoefs, int nnewchgsides,
int* nfixedvars, int* naggrvars, int* nchgvartypes, int* nchgbds, int* naddholes,
int* ndelconss, int* naddconss, int* nupgdconss, int* nchgcoefs, int* nchgsides, SCIP_RESULT* result) noexcept with gil:
cdef int i
PyConshdlr = getPyConshdlr(conshdlr)
cdef constraints = []
constraints = []
for i in range(nconss):
constraints.append(getPyCons(conss[i]))
# dictionary for input/output parameters
Expand Down Expand Up @@ -401,8 +416,9 @@ cdef SCIP_RETCODE PyConsDisable (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS*
return SCIP_OKAY

cdef SCIP_RETCODE PyConsDelvars (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss) noexcept with gil:
cdef int i
PyConshdlr = getPyConshdlr(conshdlr)
cdef constraints = []
constraints = []
for i in range(nconss):
constraints.append(getPyCons(conss[i]))
PyConshdlr.consdelvars(constraints)
Expand Down
5 changes: 3 additions & 2 deletions src/pyscipopt/cutsel.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ cdef SCIP_RETCODE PyCutselExitsol (SCIP* scip, SCIP_CUTSEL* cutsel) noexcept wit
cdef SCIP_RETCODE PyCutselSelect (SCIP* scip, SCIP_CUTSEL* cutsel, SCIP_ROW** cuts, int ncuts,
SCIP_ROW** forcedcuts, int nforcedcuts, SCIP_Bool root, int maxnselectedcuts,
int* nselectedcuts, SCIP_RESULT* result) noexcept with gil:
cdef SCIP_CUTSELDATA* cutseldata
cdef SCIP_CUTSELDATA* cutseldata = SCIPcutselGetData(cutsel)
cdef SCIP_ROW* scip_row
cutseldata = SCIPcutselGetData(cutsel)
cdef int i

PyCutsel = <Cutsel>cutseldata

# translate cuts to python
Expand Down
Loading

0 comments on commit 12a76e9

Please sign in to comment.