Skip to content

Commit

Permalink
rename sigma to slack
Browse files Browse the repository at this point in the history
  • Loading branch information
rssalessio committed Oct 16, 2022
1 parent 61661e8 commit a9db63c
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ _License_: MIT

_Other contributors_:
- Many thanks to [Edgar W.](https://github.com/techniccontroller) for spotting out a bug that
let the slack variables `sigma_u` and `sigma_y` assume any value when the value of the
let the slack variables `slack_u` and `slack_y` assume any value when the value of the
corresponding regularizer was `0`.

![Closed loop results](examples/images/example_siso_pulley.png "Pulley system")
Expand Down
4 changes: 2 additions & 2 deletions examples/example_mimo_twotank.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def constraints_callback(u: cp.Variable, y: cp.Variable) -> List[Constraint]:

# Plot curve
data = sys.get_all_samples()
ax[0].plot(data.y, label=f'$s={s}, T={T}, T_i={T_INI}, N={HORIZON}$')
ax[1].plot(data.u, label=f'$s={s}, T={T}, T_i={T_INI}, N={HORIZON}$')
ax[0].plot(data.y[T_INI:], label=f'$s={s}, T={T}, T_i={T_INI}, N={HORIZON}$')
ax[1].plot(data.u[T_INI:], label=f'$s={s}, T={T}, T_i={T_INI}, N={HORIZON}$')

ax[0].set_ylim(0, 1.5)
ax[1].set_ylim(-1.2, 1.2)
Expand Down
4 changes: 2 additions & 2 deletions examples/example_siso_pulley.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def constraints_callback(u: cp.Variable, y: cp.Variable) -> List[Constraint]:

# Plot curve
data = sys.get_all_samples()
ax[0].plot(data.y, label=f'$s={s}, T={T}, T_i={T_INI}, N={HORIZON}$')
ax[1].plot(data.u, label=f'$s={s}, T={T}, T_i={T_INI}, N={HORIZON}$')
ax[0].plot(data.y[T_INI:], label=f'$s={s}, T={T}, T_i={T_INI}, N={HORIZON}$')
ax[1].plot(data.u[T_INI:], label=f'$s={s}, T={T}, T_i={T_INI}, N={HORIZON}$')

ax[0].set_ylim(0, 2)
ax[1].set_ylim(-4, 4)
Expand Down
2 changes: 1 addition & 1 deletion pydeepc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .utils import *

__author__ = 'Alessio Russo - [email protected]'
__version__ = '0.3.4'
__version__ = '0.3.5'
__url__ = 'https://github.com/rssalessio/PyDeePC'
__info__ = {
'version': __version__,
Expand Down
17 changes: 9 additions & 8 deletions pydeepc/deepc.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ def build_problem(self,
u = cp.Variable(shape=(self.M * self.horizon), name='u')
y = cp.Variable(shape=(self.P * self.horizon), name='y')
g = cp.Variable(shape=(self.T - self.Tini - self.horizon + 1), name='g')
sigma_y = cp.Variable(shape=(self.Tini * self.P), name='sigma_y')
sigma_u = cp.Variable(shape=(self.Tini * self.M), name='sigma_u')
slack_y = cp.Variable(shape=(self.Tini * self.P), name='slack_y')
slack_u = cp.Variable(shape=(self.Tini * self.M), name='slack_u')

Up, Yp, Uf, Yf = self.Up, self.Yp, self.Uf, self.Yf

Expand All @@ -125,15 +125,15 @@ def build_problem(self,
I_min_P = I - (ZpInv@ Zp)

A = np.vstack([Up, Yp, Uf, Yf])
b = cp.hstack([uini + sigma_u, yini + sigma_y, u, y])
b = cp.hstack([uini + slack_u, yini + slack_y, u, y])

# Build constraints
constraints = [A @ g == b]

if math.isclose(lambda_y, 0):
constraints.append(cp.norm(sigma_y, 2) <= DeePC._SMALL_NUMBER)
constraints.append(cp.norm(slack_y, 2) <= DeePC._SMALL_NUMBER)
if math.isclose(lambda_u, 0):
constraints.append(cp.norm(sigma_u, 2) <= DeePC._SMALL_NUMBER)
constraints.append(cp.norm(slack_u, 2) <= DeePC._SMALL_NUMBER)

# u, y = self.Uf @ g, self.Yf @ g
u = cp.reshape(u, (self.horizon, self.M))
Expand All @@ -155,9 +155,9 @@ def build_problem(self,

# Add regularizers
_regularizers = lambda_g * cp.norm(g, p=1) if lambda_g > DeePC._SMALL_NUMBER else 0
_regularizers += lambda_y * cp.norm(sigma_y, p=1) if lambda_y > DeePC._SMALL_NUMBER else 0
_regularizers += lambda_y * cp.norm(slack_y, p=1) if lambda_y > DeePC._SMALL_NUMBER else 0
_regularizers += lambda_proj * cp.norm(I_min_P @ g) if lambda_proj > DeePC._SMALL_NUMBER else 0
_regularizers += lambda_u * cp.norm(sigma_u, p=1) if lambda_u > DeePC._SMALL_NUMBER else 0
_regularizers += lambda_u * cp.norm(slack_u, p=1) if lambda_u > DeePC._SMALL_NUMBER else 0

problem_loss = _loss + _regularizers

Expand All @@ -170,7 +170,8 @@ def build_problem(self,
raise Exception(f'Error while constructing the DeePC problem. Details: {e}')

self.optimization_problem = OptimizationProblem(
variables = OptimizationProblemVariables(u_ini = uini, y_ini = yini, u = u, y = y, g = g, sigma_y = sigma_y),
variables = OptimizationProblemVariables(
u_ini = uini, y_ini = yini, u = u, y = y, g = g, slack_y = slack_y, slack_u = slack_u),
constraints = constraints,
objective_function = problem_loss,
problem = problem
Expand Down
4 changes: 2 additions & 2 deletions pydeepc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class OptimizationProblemVariables(NamedTuple):
u: Union[Variable, Parameter]
y: Union[Variable, Parameter]
g: Union[Variable, Parameter]
sigma_y: Union[Variable, Parameter]
sigma_u: Union[Variable, Parameter]
slack_y: Union[Variable, Parameter]
slack_u: Union[Variable, Parameter]


class OptimizationProblem(NamedTuple):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(name = 'PyDeePC',
packages=find_packages(),
version = '0.3.4',
version = '0.3.5',
description = 'Python library for Data-Enabled Predictive Control',
url = 'https://github.com/rssalessio/PyDeePC',
author = 'Alessio Russo',
Expand Down

0 comments on commit a9db63c

Please sign in to comment.