Skip to content

Commit

Permalink
Update CHANGELOG
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao-Dionisio committed Jan 24, 2024
1 parent 995796b commit 0bc5b27
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 48 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased
### Added
- Added method for adding piecewise linear constraints
- Added possibility for nonlinear objective functions
- Added methods for getting the names of the current stage and of an event
### Fixed
- Fixed outdated time.clock call in gcp.py
Expand Down
34 changes: 0 additions & 34 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -2841,40 +2841,6 @@ cdef class Model:
PY_SCIP_CALL(SCIPreleaseCons(self._scip, &scip_cons))

return pyCons

def addPiecewiseLinearCons(self, X, Y, a, b):
"""add constraint of the form y = f(x), where f is a piecewise linear function
:param X: x variable
:param Y: y variable
:param a: array with x-coordinates of the points in the piecewise linear relation
:param b: array with y-coordinate of the points in the piecewise linear relation
Disclaimer: For the moment, can only model 2d piecewise linear functions
Adapted from https://github.com/scipopt/PySCIPOpt/blob/master/examples/finished/piecewise.py
"""
assert len(a) == len(b), "Must have the same number of x and y-coordinates"

K = len(a)-1
w,z = {},{}
for k in range(K):
w[k] = self.addVar(lb=-self.infinity())
z[k] = self.addVar(vtype="B")

for k in range(K):
self.addCons(w[k] >= a[k]*z[k])
self.addCons(w[k] <= a[k+1]*z[k])

self.addCons(quicksum(z[k] for k in range(K)) == 1)

self.addCons(X == quicksum(w[k] for k in range(K)))

c = [float(b[k+1]-b[k]) / (a[k+1]-a[k]) for k in range(K)]
d = [b[k] - c[k]*a[k] for k in range(K)]

new_cons = self.addCons(Y == quicksum(d[k]*z[k] + c[k]*w[k] for k in range(K)))

return new_cons

def getSlackVarIndicator(self, Constraint cons):
"""Get slack variable of an indicator constraint.
Expand Down
13 changes: 0 additions & 13 deletions tests/test_cons.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,6 @@ def test_printCons():

m.printCons(c)


def test_addPiecewiseLinearCons():
m = Model()

xpoints = [1,3,5]
ypoints = [1,2,4]
x = m.addVar(lb=xpoints[0], ub=xpoints[-1], obj=2)
y = m.addVar(lb=-m.infinity(), obj=-3)
m.addPiecewiseLinearCons(x,y,xpoints,ypoints)

m.optimize()
assert m.isEQ(m.getObjVal(), -2)

@pytest.mark.skip(reason="TODO: test getValsLinear()")
def test_getValsLinear():
assert True
Expand Down

0 comments on commit 0bc5b27

Please sign in to comment.