From b21c8f8ffc4ad233f638e7cd8d81a9c975334246 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Tue, 7 May 2024 02:59:15 +0200 Subject: Rename polymatrix.optvariable to just polymatrix.variable --- sumofsquares/optvariable.py | 72 --------------------------------------------- sumofsquares/variable.py | 72 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 72 deletions(-) delete mode 100644 sumofsquares/optvariable.py create mode 100644 sumofsquares/variable.py diff --git a/sumofsquares/optvariable.py b/sumofsquares/optvariable.py deleted file mode 100644 index 12df4df..0000000 --- a/sumofsquares/optvariable.py +++ /dev/null @@ -1,72 +0,0 @@ -""" -This module is an extension of the polymatrix package that adds a new type of -variable, a decision variable, to formulate optimization problems. Speficically, -this extends the following parts of polymatrix: - - - polymatrix.variable, by adding a new OptVariable type - - polymatrix.expression, by adding an OptVariableMixin, so that there can - be expression with underlying objects that are OptVariables. -""" - -from __future__ import annotations - -from itertools import product -from typing_extensions import override -from dataclassabc import dataclassabc - -from polymatrix.expression.expression import VariableExpression, init_variable_expression -from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin -from polymatrix.expressionstate.abc import ExpressionState -from polymatrix.polymatrix.abc import PolyMatrix -from polymatrix.polymatrix.init import init_poly_matrix -from polymatrix.polymatrix.index import PolyMatrixDict, PolyDict, MonomialIndex, VariableIndex -from polymatrix.variable.abc import Variable - - -class OptVariable(Variable): - """ Optimization (decision) variable. """ - - -class OptVariableMixin(ExpressionBaseMixin, OptVariable): - """ Optimization (decision) variable mixin for expression object. """ - - @override - def apply(self, state: ExpressionState) -> tuple[ExpressionState, PolyMatrix]: - state = state.register(self) - indices = state.get_indices(self) - p = PolyMatrixDict() - - rows, cols = self.shape - for (row, col), index in zip(product(range(rows), range(cols)), indices): - p[row, col] = PolyDict({ - # Create monomial with variable to the first power - # with coefficient of one - MonomialIndex((VariableIndex(index, power=1),)): 1. - }) - - return state, init_poly_matrix(p, self.shape) - - -@dataclassabc(frozen=True) -class OptVariableImpl(OptVariableMixin): - name: str - shape: tuple[int, int] - - def __str__(self): - return self.name - - -def init_opt_variable_expr(name, shape): - return OptVariableImpl(name, shape) - - -def from_names(names: str, shape: tuple[int, int] = (1, 1)) -> tuple[VariableExpression] | VariableExpression: - """ Construct one or multiple variables from comma separated a list of names. """ - variables = tuple(init_variable_expression( - underlying=init_opt_variable_expr(name.strip(), shape)) - for name in names.split(",")) - - if len(variables) == 1: - return variables[0] - - return variables diff --git a/sumofsquares/variable.py b/sumofsquares/variable.py new file mode 100644 index 0000000..12df4df --- /dev/null +++ b/sumofsquares/variable.py @@ -0,0 +1,72 @@ +""" +This module is an extension of the polymatrix package that adds a new type of +variable, a decision variable, to formulate optimization problems. Speficically, +this extends the following parts of polymatrix: + + - polymatrix.variable, by adding a new OptVariable type + - polymatrix.expression, by adding an OptVariableMixin, so that there can + be expression with underlying objects that are OptVariables. +""" + +from __future__ import annotations + +from itertools import product +from typing_extensions import override +from dataclassabc import dataclassabc + +from polymatrix.expression.expression import VariableExpression, init_variable_expression +from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin +from polymatrix.expressionstate.abc import ExpressionState +from polymatrix.polymatrix.abc import PolyMatrix +from polymatrix.polymatrix.init import init_poly_matrix +from polymatrix.polymatrix.index import PolyMatrixDict, PolyDict, MonomialIndex, VariableIndex +from polymatrix.variable.abc import Variable + + +class OptVariable(Variable): + """ Optimization (decision) variable. """ + + +class OptVariableMixin(ExpressionBaseMixin, OptVariable): + """ Optimization (decision) variable mixin for expression object. """ + + @override + def apply(self, state: ExpressionState) -> tuple[ExpressionState, PolyMatrix]: + state = state.register(self) + indices = state.get_indices(self) + p = PolyMatrixDict() + + rows, cols = self.shape + for (row, col), index in zip(product(range(rows), range(cols)), indices): + p[row, col] = PolyDict({ + # Create monomial with variable to the first power + # with coefficient of one + MonomialIndex((VariableIndex(index, power=1),)): 1. + }) + + return state, init_poly_matrix(p, self.shape) + + +@dataclassabc(frozen=True) +class OptVariableImpl(OptVariableMixin): + name: str + shape: tuple[int, int] + + def __str__(self): + return self.name + + +def init_opt_variable_expr(name, shape): + return OptVariableImpl(name, shape) + + +def from_names(names: str, shape: tuple[int, int] = (1, 1)) -> tuple[VariableExpression] | VariableExpression: + """ Construct one or multiple variables from comma separated a list of names. """ + variables = tuple(init_variable_expression( + underlying=init_opt_variable_expr(name.strip(), shape)) + for name in names.split(",")) + + if len(variables) == 1: + return variables[0] + + return variables -- cgit v1.2.1