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

Docplex utils methods to generate square matrix variables #56

Merged
merged 5 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ Filtering
NoDimRed
NaiveDimRed

Docplex
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. _docplex_api:
.. currentmodule:: pyriemann_qiskit.utils.docplex

.. autosummary::
:toctree: generated/

square_cont_mat_var
square_int_mat_var
square_bin_mat_var

Datasets
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. _datasets_api:
Expand Down
1 change: 1 addition & 0 deletions doc/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ qiskit-ibmq-provider==0.19.1
scipy==1.7.3
moabb>=0.4.6
git+https://github.com/pyRiemann/pyRiemann#egg=pyriemann
docplex
8 changes: 7 additions & 1 deletion pyriemann_qiskit/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from . import hyper_params_factory, filtering
from .docplex import (square_cont_mat_var,
square_int_mat_var,
square_bin_mat_var)

__all__ = [
'hyper_params_factory',
'filtering'
'filtering',
'square_cont_mat_var',
'square_int_mat_var',
'square_bin_mat_var'
]
100 changes: 100 additions & 0 deletions pyriemann_qiskit/utils/docplex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from docplex.mp.vartype import ContinuousVarType, IntegerVarType, BinaryVarType


def square_cont_mat_var(prob, channels,
name='cont_covmat'):
"""Creates a 2-dimensional dictionary of continuous decision variables,
indexed by pairs of key objects.
The dictionary represents a square matrix of size
len(channels) x len(channels).
A key can be any Python object, with the exception of None.

Parameters
----------
prob : Model
An instance of the docplex model [1]_
channels : list
The list of channels. A channel can be any Python object,
such as channels'name or number but None.

Returns
-------
square_mat : dict
A square matrix of continuous decision variables.
Access element (i, j) with square_mat[(i, j)].
Indices start with 0.

References
----------
.. [1] \
http://ibmdecisionoptimization.github.io/docplex-doc/mp/_modules/docplex/mp/model.html#Model
"""
ContinuousVarType.one_letter_symbol = lambda _: 'C'
return prob.continuous_var_matrix(keys1=channels, keys2=channels,
name=name, lb=-prob.infinity)


def square_int_mat_var(prob, channels,
name='int_covmat'):
"""Creates a 2-dimensional dictionary of integer decision variables,
indexed by pairs of key objects.
The dictionary represents a square matrix of size
len(channels) x len(channels).
A key can be any Python object, with the exception of None.

Parameters
----------
prob : Model
An instance of the docplex model [1]_
channels : list
The list of channels. A channel can be any Python object,
such as channels'name or number but None.

Returns
-------
square_mat : dict
A square matrix of integer decision variables.
Access element (i, j) with square_mat[(i, j)].
Indices start with 0.

References
----------
.. [1] \
http://ibmdecisionoptimization.github.io/docplex-doc/mp/_modules/docplex/mp/model.html#Model
"""
IntegerVarType.one_letter_symbol = lambda _: 'I'
return prob.integer_var_matrix(keys1=channels, keys2=channels,
name=name, lb=-prob.infinity)


def square_bin_mat_var(prob, channels,
name='bin_covmat'):
"""Creates a 2-dimensional dictionary of binary decision variables,
indexed by pairs of key objects.
The dictionary represents a square matrix of size
len(channels) x len(channels).
A key can be any Python object, with the exception of None.

Parameters
----------
prob : Model
An instance of the docplex model [1]_
channels : list
The list of channels. A channel can be any Python object,
such as channels'name or number but None.

Returns
-------
square_mat : dict
A square matrix of binary decision variables.
Access element (i, j) with square_mat[(i, j)].
Indices start with 0.

References
----------
.. [1] \
http://ibmdecisionoptimization.github.io/docplex-doc/mp/_modules/docplex/mp/model.html#Model
"""
BinaryVarType.one_letter_symbol = lambda _: 'B'
return prob.binary_var_matrix(keys1=channels, keys2=channels,
name=name)
25 changes: 18 additions & 7 deletions tests/test_docplex.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
from docplex.mp.vartype import ContinuousVarType
import pytest
from docplex.mp.model import Model
from docplex.mp.vartype import ContinuousVarType, IntegerVarType, BinaryVarType
from pyriemann_qiskit.utils import (square_cont_mat_var,
square_int_mat_var,
square_bin_mat_var)


def test_get_square_cont_var():
channels = range(4)
@pytest.mark.parametrize('square_mat_var',
[(square_cont_mat_var, ContinuousVarType),
(square_int_mat_var, IntegerVarType),
(square_bin_mat_var, BinaryVarType)])
def test_get_square_cont_var(square_mat_var):
n = 4
channels = range(n)
prob = Model()
ContinuousVarType.one_letter_symbol = lambda _: 'C'
mat = prob.continuous_var_matrix(keys1=channels, keys2=channels,
name='test', lb=-prob.infinity)
assert mat is not None
handle = square_mat_var[0]
expected_result_type = square_mat_var[1]
mat = handle(prob, channels, name='test')
first_element = mat[(0, 0)]
assert len(mat) == n * n
assert type(first_element.vartype) is expected_result_type