Skip to content

Commit

Permalink
Merge branch 'master' into 243-fix-write-methods-dont-work-after-redi…
Browse files Browse the repository at this point in the history
…rectoutput
  • Loading branch information
Joao-Dionisio authored Dec 3, 2024
2 parents ec5a346 + cfbd832 commit a4a5f70
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 41 deletions.
32 changes: 6 additions & 26 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Integration test

env:
version: 9.1.0
version: 9.2.0

# runs on branches and pull requests; doesn't run on tags.
on:
Expand Down Expand Up @@ -57,7 +57,7 @@ jobs:

- name: Download dependencies (SCIPOptSuite)
shell: powershell
run: wget https://github.com/scipopt/scip/releases/download/$(echo "v${{env.version}}" | tr -d '.')/SCIPOptSuite-${{ env.version }}-win64-VS15.exe -outfile scipopt-installer.exe
run: wget https://github.com/scipopt/scip/releases/download/$(echo "v${{env.version}}" | tr -d '.')/SCIPOptSuite-${{ env.version }}-win64.exe -outfile scipopt-installer.exe

- name: Install dependencies (SCIPOptSuite)
shell: cmd
Expand Down Expand Up @@ -93,33 +93,13 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Cache dependencies (SCIPOptSuite)
id: cache-scip
uses: actions/cache@v2
with:
path: |
${{ runner.workspace }}/scipoptsuite
~/Library/Caches/Homebrew/tbb--*
/usr/local/opt/tbb*
~/Library/Caches/Homebrew/downloads/*--tbb-*
~/Library/Caches/Homebrew/boost--*
/usr/local/opt/boost*
~/Library/Caches/Homebrew/downloads/*--boost-*
key: ${{ runner.os }}-scipopt-${{ env.version }}-${{ hashFiles('**/lockfiles') }}
restore-keys: |
${{ runner.os }}-scipopt-${{ env.version }}-
- name: Install dependencies (SCIPOptSuite)
if: steps.cache-scip.outputs.cache-hit != 'true'
run: |
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 }}
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=${{ runner.workspace }}/scipoptsuite -DIPOPT=off -DSYM=none -DTPI=tny -DREADLINE=off
make install -j
wget --quiet --no-check-certificate https://github.com/scipopt/scip/releases/download/$(echo "v${{env.version}}" | tr -d '.')/SCIPOptSuite-${{ env.version }}-Darwin.sh
chmod +x SCIPOptSuite-${{ env.version }}-Darwin.sh
./SCIPOptSuite-${{ env.version }}-Darwin.sh --skip-license --include-subdir
mv SCIPOptSuite-${{ env.version }}-Darwin ${{ runner.workspace }}/scipoptsuite
- name: Setup python ${{ matrix.python-version }}
uses: actions/setup-python@v4
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

## Unreleased
### Added
- Added categorical data example
- Added printProblem to print problem to stdout
- Added stage checks to presolve, freereoptsolve, freetransform
- Added primal_dual_evolution recipe and a plot recipe
### Fixed
- Only redirect stdout and stderr in redirectOutput() so that file output still works afterwards
### Changed
- GitHub actions using Mac now use precompiled SCIP from latest release
### Removed

## 5.2.1 - 2024.10.29
Expand Down
73 changes: 73 additions & 0 deletions examples/finished/categorical_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
This example shows how one can optimize a model with categorical data by converting it into integers.
There are three employees (Alice, Bob, Charlie) and three shifts. Each shift is assigned an integer:
Morning - 0
Afternoon - 1
Night - 2
The employees have availabilities (e.g. Alice can only work in the Morning and Afternoon), and different
salary demands. These constraints, and an additional one stipulating that every shift must be covered,
allows us to model a MIP with the objective of minimizing the money spent on salary.
"""

from pyscipopt import Model

# Define categorical data
shift_to_int = {"Morning": 0, "Afternoon": 1, "Night": 2}
employees = ["Alice", "Bob", "Charlie"]

# Employee availability
availability = {
"Alice": ["Morning", "Afternoon"],
"Bob": ["Afternoon", "Night"],
"Charlie": ["Morning", "Night"]
}

# Transform availability into integer values
availability_int = {}
for emp, available_shifts in availability.items():
availability_int[emp] = [shift_to_int[shift] for shift in available_shifts]


# Employees have different salary demands
cost = {
"Alice": [2,4,1],
"Bob": [3,2,7],
"Charlie": [3,3,3]
}

# Create the model
model = Model("Shift Assignment")

# x[e, s] = 1 if employee e is assigned to shift s
x = {}
for e in employees:
for s in shift_to_int.values():
x[e, s] = model.addVar(vtype="B", name=f"x({e},{s})")

# Each shift must be assigned to exactly one employee
for s in shift_to_int.values():
model.addCons(sum(x[e, s] for e in employees) == 1)

# Employees can only work shifts they are available for
for e in employees:
for s in shift_to_int.values():
if s not in availability_int[e]:
model.addCons(x[e, s] == 0)

# Minimize shift assignment cost
model.setObjective(
sum(cost[e][s]*x[e, s] for e in employees for s in shift_to_int.values()), "minimize"
)

# Solve the problem
model.optimize()

# Display the results
print("\nOptimal Shift Assignment:")
for e in employees:
for s, s_id in shift_to_int.items():
if model.getVal(x[e, s_id]) > 0.5:
print("%s is assigned to %s" % (e, s))
61 changes: 46 additions & 15 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -2901,6 +2901,32 @@ cdef class Model:
if not onlyroot:
self.setIntParam("propagating/maxrounds", 0)

def printProblem(self, ext='.cip', trans=False, genericnames=False):
"""
Write current model/problem to standard output.
Parameters
----------
ext : str, optional
the extension to be used (Default value = '.cip').
Should have an extension corresponding to one of the readable file formats,
described in https://www.scipopt.org/doc/html/group__FILEREADERS.php.
trans : bool, optional
indicates whether the transformed problem is written to file (Default value = False)
genericnames : bool, optional
indicates whether the problem should be written with generic variable
and constraint names (Default value = False)
"""
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

if trans:
PY_SCIP_CALL(SCIPwriteTransProblem(self._scip, NULL, str_conversion(ext)[1:], genericnames))
else:
PY_SCIP_CALL(SCIPwriteOrigProblem(self._scip, NULL, str_conversion(ext)[1:], genericnames))

locale.setlocale(locale.LC_NUMERIC,user_locale)

def writeProblem(self, filename='model.cip', trans=False, genericnames=False, verbose=True):
"""
Write current model/problem to a file.
Expand All @@ -2923,22 +2949,27 @@ cdef class Model:
user_locale = locale.getlocale(category=locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, "C")

str_absfile = abspath(filename)
absfile = str_conversion(str_absfile)
fn, ext = splitext(absfile)

if len(ext) == 0:
ext = str_conversion('.cip')
fn = fn + ext
ext = ext[1:]

if trans:
PY_SCIP_CALL(SCIPwriteTransProblem(self._scip, fn, ext, genericnames))
if filename:
str_absfile = abspath(filename)
absfile = str_conversion(str_absfile)
fn, ext = splitext(absfile)
if len(ext) == 0:
ext = str_conversion('.cip')
fn = fn + ext
ext = ext[1:]

if trans:
PY_SCIP_CALL(SCIPwriteTransProblem(self._scip, fn, ext, genericnames))
else:
PY_SCIP_CALL(SCIPwriteOrigProblem(self._scip, fn, ext, genericnames))

if verbose:
print('wrote problem to file ' + str_absfile)
else:
PY_SCIP_CALL(SCIPwriteOrigProblem(self._scip, fn, ext, genericnames))

if verbose:
print('wrote problem to file ' + str_absfile)
if trans:
PY_SCIP_CALL(SCIPwriteTransProblem(self._scip, NULL, str_conversion('.cip')[1:], genericnames))
else:
PY_SCIP_CALL(SCIPwriteOrigProblem(self._scip, NULL, str_conversion('.cip')[1:], genericnames))

locale.setlocale(locale.LC_NUMERIC,user_locale)

Expand Down
1 change: 1 addition & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def test_model():

s.writeProblem('model')
s.writeProblem('model.lp')
s.printProblem()

s.freeProb()
s = Model()
Expand Down

0 comments on commit a4a5f70

Please sign in to comment.