From 3d785460e17acfd6adcbc6e18b465bb9f63a9b78 Mon Sep 17 00:00:00 2001 From: Mohammed Ghannam Date: Tue, 2 Apr 2024 21:18:42 +0200 Subject: [PATCH 1/5] Use backwards compatible type hint --- src/pyscipopt/recipes/piecewise.py | 41 ++++++++++++++++-------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/pyscipopt/recipes/piecewise.py b/src/pyscipopt/recipes/piecewise.py index 7540414da..05029a574 100644 --- a/src/pyscipopt/recipes/piecewise.py +++ b/src/pyscipopt/recipes/piecewise.py @@ -1,9 +1,12 @@ +from typing import List from pyscipopt import Model, quicksum, Variable, Constraint -def add_piecewise_linear_cons(model: Model, X: Variable, Y: Variable, a: list[float], b: list[float]) -> Constraint: - """add constraint of the form y = f(x), where f is a piecewise linear function +def add_piecewise_linear_cons(model: Model, X: Variable, Y: Variable, a: List[float], b: List[float]) -> Constraint: + """add constraint of the form y = f(x), where f is a piecewise linear function + + :param model: pyscipopt model to add the constraint to :param X: x variable :param Y: y variable :param a: array with x-coordinates of the points in the piecewise linear relation @@ -12,25 +15,25 @@ def add_piecewise_linear_cons(model: Model, X: Variable, Y: Variable, a: list[fl 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" + 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] = model.addVar(lb=-model.infinity()) + z[k] = model.addVar(vtype="B") + + for k in range(K): + model.addCons(w[k] >= a[k] * z[k]) + model.addCons(w[k] <= a[k + 1] * z[k]) - K = len(a)-1 - w,z = {},{} - for k in range(K): - w[k] = model.addVar(lb=-model.infinity()) - z[k] = model.addVar(vtype="B") + model.addCons(quicksum(z[k] for k in range(K)) == 1) - for k in range(K): - model.addCons(w[k] >= a[k]*z[k]) - model.addCons(w[k] <= a[k+1]*z[k]) + model.addCons(X == quicksum(w[k] for k in range(K))) - model.addCons(quicksum(z[k] for k in range(K)) == 1) + 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)] - model.addCons(X == quicksum(w[k] for k in range(K))) + new_cons = model.addCons(Y == quicksum(d[k] * z[k] + c[k] * 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 = model.addCons(Y == quicksum(d[k]*z[k] + c[k]*w[k] for k in range(K))) - - return new_cons + return new_cons From 452cd5c3b4f11c8ce5e650b355f224b375f20fd3 Mon Sep 17 00:00:00 2001 From: Mohammed Ghannam Date: Tue, 2 Apr 2024 21:19:00 +0200 Subject: [PATCH 2/5] Install bison in macos pipeline --- .github/workflows/integration-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml index 6e9d1e95a..dd9b378d6 100644 --- a/.github/workflows/integration-test.yml +++ b/.github/workflows/integration-test.yml @@ -109,7 +109,7 @@ jobs: - name: Install dependencies (SCIPOptSuite) if: steps.cache-scip.outputs.cache-hit != 'true' run: | - brew install tbb boost + brew install tbb boost bison wget --quiet --no-check-certificate https://github.com/scipopt/scip/releases/download/$(echo "v${{env.version}}" | tr -d '.')/scipoptsuite-${{ env.version }}.tgz tar xfz scipoptsuite-${{ env.version }}.tgz cd scipoptsuite-${{ env.version }} From 7cc41f1e52154bfb432749e1ce42bd74cbf6bc55 Mon Sep 17 00:00:00 2001 From: Mohammed Ghannam Date: Tue, 2 Apr 2024 21:20:56 +0200 Subject: [PATCH 3/5] Enable integration test for pull requests for now --- .github/workflows/integration-test.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml index dd9b378d6..32d26fcb1 100644 --- a/.github/workflows/integration-test.yml +++ b/.github/workflows/integration-test.yml @@ -8,6 +8,9 @@ on: push: branches: - 'master' + pull_request: + branches: + - 'master' jobs: From e1514bbd87081cfcff63d1792a49a452d2f76d3a Mon Sep 17 00:00:00 2001 From: Mohammed Ghannam Date: Tue, 2 Apr 2024 21:30:36 +0200 Subject: [PATCH 4/5] Install locales needed by tests in ubuntu integration test --- .github/workflows/integration-test.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml index 32d26fcb1..c79acf42e 100644 --- a/.github/workflows/integration-test.yml +++ b/.github/workflows/integration-test.yml @@ -28,6 +28,9 @@ jobs: wget --quiet --no-check-certificate https://github.com/scipopt/scip/releases/download/$(echo "v${{env.version}}" | tr -d '.')/SCIPOptSuite-${{ env.version }}-Linux-ubuntu20.deb sudo apt-get update && sudo apt install -y ./SCIPOptSuite-${{ env.version }}-Linux-ubuntu20.deb + - name: Install locales for tests + run: sudo apt-get install tzdata locales -y && sudo locale-gen pt_PT && sudo update-locale # add pt_PT locale that is used in tests + - name: Setup python ${{ matrix.python-version }} uses: actions/setup-python@v4 with: From b2bec120ab63e1d6b53119a0399f2c78eebf7355 Mon Sep 17 00:00:00 2001 From: Mohammed Ghannam Date: Tue, 2 Apr 2024 21:49:05 +0200 Subject: [PATCH 5/5] Turn off again integration tests for PRs --- .github/workflows/integration-test.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml index c79acf42e..b56129a34 100644 --- a/.github/workflows/integration-test.yml +++ b/.github/workflows/integration-test.yml @@ -8,9 +8,6 @@ on: push: branches: - 'master' - pull_request: - branches: - - 'master' jobs: