Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type to loop variables only #947

Merged
merged 12 commits into from
Feb 1, 2025
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
2 changes: 2 additions & 0 deletions src/pyscipopt/benders.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ cdef SCIP_RETCODE PyBendersPostsolve (SCIP* scip, SCIP_BENDERS* benders, SCIP_SO
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
cdef int i

bendersdata = SCIPbendersGetData(benders)
PyBenders = <Benders>bendersdata
if sol == NULL:
Expand Down
32 changes: 32 additions & 0 deletions src/pyscipopt/conshdlr.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ 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 = []
DominikKamp marked this conversation as resolved.
Show resolved Hide resolved
for i in range(nconss):
Expand All @@ -176,6 +178,8 @@ cdef SCIP_RETCODE PyConsInit (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** c
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 = []
for i in range(nconss):
Expand All @@ -184,6 +188,8 @@ cdef SCIP_RETCODE PyConsExit (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** c
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 = []
for i in range(nconss):
Expand All @@ -192,6 +198,8 @@ cdef SCIP_RETCODE PyConsInitpre (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS*
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 = []
for i in range(nconss):
Expand All @@ -200,6 +208,8 @@ cdef SCIP_RETCODE PyConsExitpre (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS*
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 = []
for i in range(nconss):
Expand All @@ -208,6 +218,8 @@ cdef SCIP_RETCODE PyConsInitsol (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS*
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 = []
for i in range(nconss):
Expand Down Expand Up @@ -244,6 +256,8 @@ 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 = []
for i in range(nconss):
Expand All @@ -253,6 +267,8 @@ cdef SCIP_RETCODE PyConsInitlp (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS**
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 = []
for i in range(nconss):
Expand All @@ -263,6 +279,8 @@ 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 = []
for i in range(nconss):
Expand All @@ -274,6 +292,8 @@ 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 = []
for i in range(nconss):
Expand All @@ -283,6 +303,8 @@ cdef SCIP_RETCODE PyConsEnfolp (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS**
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 = []
for i in range(nconss):
Expand All @@ -294,6 +316,8 @@ 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 = []
for i in range(nconss):
Expand All @@ -304,6 +328,8 @@ 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 = []
for i in range(nconss):
Expand All @@ -315,6 +341,8 @@ 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 = []
for i in range(nconss):
Expand All @@ -328,6 +356,8 @@ 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 = []
for i in range(nconss):
Expand Down Expand Up @@ -401,6 +431,8 @@ 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 = []
for i in range(nconss):
Expand Down
2 changes: 2 additions & 0 deletions src/pyscipopt/cutsel.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ cdef SCIP_RETCODE PyCutselSelect (SCIP* scip, SCIP_CUTSEL* cutsel, SCIP_ROW** cu
int* nselectedcuts, SCIP_RESULT* result) noexcept with gil:
cdef SCIP_CUTSELDATA* cutseldata
cdef SCIP_ROW* scip_row
cdef int i

cutseldata = SCIPcutselGetData(cutsel)
PyCutsel = <Cutsel>cutseldata

Expand Down
33 changes: 29 additions & 4 deletions src/pyscipopt/lp.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ cdef class LP:
lb -- lower bound (default 0.0)
ub -- upper bound (default infinity)
"""
nnonz = len(entries)
cdef int nnonz = len(entries)

Joao-Dionisio marked this conversation as resolved.
Show resolved Hide resolved
cdef SCIP_Real* c_coefs = <SCIP_Real*> malloc(nnonz * sizeof(SCIP_Real))
cdef int* c_inds = <int*>malloc(nnonz * sizeof(int))
cdef SCIP_Real c_obj
cdef SCIP_Real c_lb
cdef SCIP_Real c_ub
cdef int c_beg
cdef int i

c_obj = obj
c_lb = lb
Expand All @@ -95,9 +96,10 @@ cdef class LP:
lbs -- lower bounds (default 0.0)
ubs -- upper bounds (default infinity)
"""

ncols = len(entrieslist)
nnonz = sum(len(entries) for entries in entrieslist)

cdef ncols = len(entrieslist)
Joao-Dionisio marked this conversation as resolved.
Show resolved Hide resolved
cdef nnonz = sum(len(entries) for entries in entrieslist)
cdef int i

Joao-Dionisio marked this conversation as resolved.
Show resolved Hide resolved
cdef SCIP_Real* c_objs = <SCIP_Real*> malloc(ncols * sizeof(SCIP_Real))
cdef SCIP_Real* c_lbs = <SCIP_Real*> malloc(ncols * sizeof(SCIP_Real))
Expand Down Expand Up @@ -166,6 +168,7 @@ cdef class LP:
cdef SCIP_Real c_lhs
cdef SCIP_Real c_rhs
cdef int c_beg
cdef int i

c_lhs = lhs
c_rhs = rhs if rhs != None else self.infinity()
Expand Down Expand Up @@ -196,6 +199,7 @@ cdef class LP:
cdef SCIP_Real* c_coefs = <SCIP_Real*> malloc(nnonz * sizeof(SCIP_Real))
cdef int* c_inds = <int*>malloc(nnonz * sizeof(int))
cdef int* c_beg = <int*>malloc(nrows * sizeof(int))
cdef int i

tmp = 0
for i,entries in enumerate(entrieslist):
Expand Down Expand Up @@ -232,6 +236,8 @@ cdef class LP:
firstcol -- first column (default 0)
lastcol -- last column (default ncols - 1)
"""
cdef int i

lastcol = lastcol if lastcol != None else self.ncols() - 1

if firstcol > lastcol:
Expand Down Expand Up @@ -261,6 +267,8 @@ cdef class LP:
firstrow -- first row (default 0)
lastrow -- last row (default nrows - 1)
"""
cdef int i

lastrow = lastrow if lastrow != None else self.nrows() - 1

if firstrow > lastrow:
Expand Down Expand Up @@ -292,6 +300,7 @@ cdef class LP:
"""
cdef int c_col = col
cdef SCIP_Real c_obj = obj

PY_SCIP_CALL(SCIPlpiChgObj(self.lpi, 1, &c_col, &c_obj))

def chgCoef(self, row, col, newval):
Expand All @@ -315,6 +324,7 @@ cdef class LP:
cdef int c_col = col
cdef SCIP_Real c_lb = lb
cdef SCIP_Real c_ub = ub

PY_SCIP_CALL(SCIPlpiChgBounds(self.lpi, 1, &c_col, &c_lb, &c_ub))

def chgSide(self, row, lhs, rhs):
Expand All @@ -328,6 +338,7 @@ cdef class LP:
cdef int c_row = row
cdef SCIP_Real c_lhs = lhs
cdef SCIP_Real c_rhs = rhs

PY_SCIP_CALL(SCIPlpiChgSides(self.lpi, 1, &c_row, &c_lhs, &c_rhs))

def clear(self):
Expand All @@ -337,12 +348,14 @@ cdef class LP:
def nrows(self):
"""Returns the number of rows."""
cdef int nrows

PY_SCIP_CALL(SCIPlpiGetNRows(self.lpi, &nrows))
return nrows

def ncols(self):
"""Returns the number of columns."""
cdef int ncols

PY_SCIP_CALL(SCIPlpiGetNCols(self.lpi, &ncols))
return ncols

Expand All @@ -363,6 +376,8 @@ cdef class LP:

def getPrimal(self):
"""Returns the primal solution of the last LP solve."""
cdef int i

ncols = self.ncols()
cdef SCIP_Real* c_primalsol = <SCIP_Real*> malloc(ncols * sizeof(SCIP_Real))
PY_SCIP_CALL(SCIPlpiGetSol(self.lpi, NULL, c_primalsol, NULL, NULL, NULL))
Expand All @@ -379,6 +394,8 @@ cdef class LP:

def getDual(self):
"""Returns the dual solution of the last LP solve."""
cdef int i

nrows = self.nrows()
cdef SCIP_Real* c_dualsol = <SCIP_Real*> malloc(nrows * sizeof(SCIP_Real))
PY_SCIP_CALL(SCIPlpiGetSol(self.lpi, NULL, NULL, c_dualsol, NULL, NULL))
Expand All @@ -395,6 +412,8 @@ cdef class LP:

def getPrimalRay(self):
"""Returns a primal ray if possible, None otherwise."""
cdef int i

if not SCIPlpiHasPrimalRay(self.lpi):
return None
ncols = self.ncols()
Expand All @@ -409,6 +428,8 @@ cdef class LP:

def getDualRay(self):
"""Returns a dual ray if possible, None otherwise."""
cdef int i

if not SCIPlpiHasDualRay(self.lpi):
return None
nrows = self.nrows()
Expand All @@ -424,11 +445,14 @@ cdef class LP:
def getNIterations(self):
"""Returns the number of LP iterations of the last LP solve."""
cdef int niters

PY_SCIP_CALL(SCIPlpiGetIterations(self.lpi, &niters))
return niters

def getRedcost(self):
"""Returns the reduced cost vector of the last LP solve."""
cdef int i

ncols = self.ncols()

cdef SCIP_Real* c_redcost = <SCIP_Real*> malloc(ncols * sizeof(SCIP_Real))
Expand All @@ -445,6 +469,7 @@ cdef class LP:
"""Returns the indices of the basic columns and rows; index i >= 0 corresponds to column i, index i < 0 to row -i-1"""
nrows = self.nrows()
cdef int* c_binds = <int*> malloc(nrows * sizeof(int))
cdef int i

PY_SCIP_CALL(SCIPlpiGetBasisInd(self.lpi, c_binds))

Expand Down
3 changes: 3 additions & 0 deletions src/pyscipopt/reader.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ cdef SCIP_RETCODE PyReaderWrite (SCIP* scip, SCIP_READER* reader, FILE* file,
SCIP_CONS** conss, int nconss, int maxnconss, int startnconss,
SCIP_Bool genericnames, SCIP_RESULT* result) noexcept with gil:
cdef SCIP_READERDATA* readerdata
cdef int i

readerdata = SCIPreaderGetData(reader)
cdef int fd = fileno(file)

Joao-Dionisio marked this conversation as resolved.
Show resolved Hide resolved
PyFile = os.fdopen(fd, "w", closefd=False)
PyName = name.decode('utf-8')
PyBinVars = [Variable.create(vars[i]) for i in range(nbinvars)]
Expand Down
Loading