Skip to content

Commit

Permalink
removed | operator to support python < 10
Browse files Browse the repository at this point in the history
  • Loading branch information
krichelj committed Dec 25, 2024
1 parent 649e629 commit 05766f6
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/PyDiffGame/ContinuousPyDiffGame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
from scipy.integrate import odeint
from numpy.linalg import eigvals, inv
from typing import Sequence, Optional
from typing import Sequence, Optional, Union

from PyDiffGame.PyDiffGame import PyDiffGame
from PyDiffGame.Objective import Objective
Expand All @@ -30,7 +30,7 @@ def __init__(self,
x_0: Optional[np.array] = None,
x_T: Optional[np.array] = None,
T_f: Optional[float] = None,
P_f: Optional[Sequence[np.array] | np.array] = None,
P_f: Optional[Union[Sequence[np.array], np.array]] = None,
show_legend: Optional[bool] = True,
state_variables_names: Optional[Sequence[str]] = None,
epsilon_x: Optional[float] = PyDiffGame._epsilon_x_default,
Expand Down
15 changes: 8 additions & 7 deletions src/PyDiffGame/DiscretePyDiffGame.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import scipy as sp
# import quadpy
from typing import Sequence, Optional

from typing import Sequence, Optional, Union

from PyDiffGame.PyDiffGame import PyDiffGame
from PyDiffGame.Objective import Objective
Expand Down Expand Up @@ -37,7 +37,7 @@ def __init__(self,
x_0: Optional[np.array] = None,
x_T: Optional[np.array] = None,
T_f: Optional[float] = None,
P_f: Optional[Sequence[np.array] | np.array] = None,
P_f: Optional[Union[Sequence[np.array], np.array]] = None,
show_legend: Optional[bool] = True,
state_variables_names: Optional[Sequence] = None,
epsilon_x: Optional[float] = PyDiffGame._epsilon_x_default,
Expand Down Expand Up @@ -132,12 +132,13 @@ def __discretize_game(self):
"""

A_tilda = np.exp(self._A * self._delta)
e_AT = quadpy.quad(f=lambda T: np.array([np.exp(t * self._A) for t in T]).swapaxes(0, 2).swapaxes(0, 1),
a=0,
b=self._delta)[0]
e_AT = sp.integrate.quad(lambda T: np.array([
np.exp(t * self._A) for t in T]).swapaxes(0, 2).swapaxes(0, 1),
a=0,
b=self._delta)[0]

self._A = A_tilda
B_tilda = e_AT
B_tilda = np.array(e_AT)
self._Bs = [B_tilda @ B_i for B_i in self._Bs]
self._Q = [Q_i * self._delta for Q_i in self._Q]
self._R = [R_i / self._delta for R_i in self._Rs]
Expand Down
10 changes: 5 additions & 5 deletions src/PyDiffGame/PyDiffGame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import scipy as sp
import sympy
import warnings
from typing import Callable, Final, ClassVar, Optional, Sequence
from typing import Callable, Final, ClassVar, Optional, Sequence, Union
from abc import ABC, abstractmethod

from PyDiffGame.Objective import Objective, GameObjective, LQRObjective
Expand Down Expand Up @@ -122,7 +122,7 @@ def __init__(self,
x_0: Optional[np.array] = None,
x_T: Optional[np.array] = None,
T_f: Optional[float] = None,
P_f: Optional[Sequence[np.array] | np.array] = None,
P_f: Optional[Union[Sequence[np.array], np.array]] = None,
show_legend: Optional[bool] = True,
state_variables_names: Optional[Sequence[str]] = None,
epsilon_x: Optional[float] = _epsilon_x_default,
Expand Down Expand Up @@ -550,7 +550,7 @@ def plot_y(self,
C: np.array,
output_variables_names: Optional[Sequence[str]] = None,
save_figure: Optional[bool] = False,
figure_path: Optional[str | Path] = _default_figures_filename,
figure_path: Optional[Union[str, Path]] = _default_figures_filename,
figure_filename: Optional[str] = _default_figures_filename):
"""
Plots an output vector y = C x^T wth respect to time
Expand Down Expand Up @@ -579,7 +579,7 @@ def plot_y(self,

@_post_convergence
def __save_figure(self,
figure_path: Optional[str | Path] = _default_figures_filename,
figure_path: Optional[Union[str, Path]] = _default_figures_filename,
figure_filename: Optional[str] = _default_figures_filename):
"""
Saves the current figure
Expand Down Expand Up @@ -1157,7 +1157,7 @@ def __call__(self,
M: Optional[np.array] = None,
output_variables_names: Optional[Sequence[str]] = None,
save_figure: Optional[bool] = False,
figure_path: Optional[str | Path] = _default_figures_path,
figure_path: Optional[Union[str, Path]] = _default_figures_path,
figure_filename: Optional[str] = _default_figures_filename,
print_characteristic_polynomials: Optional[bool] = False,
print_eigenvalues: Optional[bool] = False):
Expand Down
15 changes: 8 additions & 7 deletions src/PyDiffGame/PyDiffGameLQRComparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from concurrent.futures import ProcessPoolExecutor

import inspect
from typing import Sequence, Any, Optional, Callable
from typing import Sequence, Any, Optional, Callable, Union
from abc import ABC

from PyDiffGame.PyDiffGame import PyDiffGame
Expand Down Expand Up @@ -34,9 +34,9 @@ class PyDiffGameLQRComparison(ABC, Callable, Sequence):
def __init__(self,
args: dict[str, Any],
games_objectives: Sequence[Sequence[GameObjective]],
M: np.array,
M: np.array = None,
continuous: bool = True):
GameClass = ContinuousPyDiffGame if continuous else DiscretePyDiffGame
game_class = ContinuousPyDiffGame if continuous else DiscretePyDiffGame

self.__args = args
self.__verify_input()
Expand All @@ -46,7 +46,7 @@ def __init__(self,
self.__M = M

for i, game_i_objectives in enumerate(games_objectives):
game_i = GameClass(**self.__args | {'objectives': [o for o in game_i_objectives]})
game_i = game_class(**self.__args | {'objectives': [o for o in game_i_objectives]})
self._games[i] = game_i

if game_i.is_LQR():
Expand Down Expand Up @@ -109,8 +109,9 @@ def __call__(self,
plot_Mx: Optional[bool] = False,
output_variables_names: Optional[Sequence[str]] = None,
save_figure: Optional[bool] = False,
figure_path: Optional[str | Path] = PyDiffGame.default_figures_path,
figure_filename: Optional[str | Callable[[PyDiffGame], str]] = PyDiffGame.default_figures_filename,
figure_path: Optional[Union[str, Path]] = PyDiffGame.default_figures_path,
figure_filename: Optional[Union[str, Callable[[PyDiffGame], str]]] =
PyDiffGame.default_figures_filename,
run_animations: Optional[bool] = True,
print_characteristic_polynomials: Optional[bool] = False,
print_eigenvalues: Optional[bool] = False):
Expand All @@ -133,7 +134,7 @@ def __call__(self,
self.__run_animation(i=i)

@staticmethod
def run_multiprocess(multiprocess_worker_function: Callable[[Any], None],
def run_multiprocess(multiprocess_worker_function: Callable,
values: Sequence[Sequence]):
t_start = time()
combos = list(itertools.product(*values))
Expand Down
5 changes: 2 additions & 3 deletions src/PyDiffGame/examples/InvertedPendulumComparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,8 @@ def multiprocess_worker_function(x_T: float,
epsilon_P=epsilon_P) # game class
is_max_lqr = \
inverted_pendulum_comparison(plot_state_spaces=False,
run_animations=False,
non_linear_costs=True,
agnostic_costs=True)
run_animations=False
)

# inverted_pendulum_comparison.plot_two_state_spaces(non_linear=True)
return int(is_max_lqr)
Expand Down
4 changes: 2 additions & 2 deletions src/PyDiffGame/examples/MassesWithSpringsComparison.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
from typing import Sequence, Optional
from typing import Sequence, Optional, Union

from PyDiffGame.PyDiffGame import PyDiffGame
from PyDiffGame.PyDiffGameLQRComparison import PyDiffGameLQRComparison
Expand All @@ -11,7 +11,7 @@ def __init__(self,
N: int,
m: float,
k: float,
q: float | Sequence[float] | Sequence[Sequence[float]],
q: Union[float, Sequence[float], Sequence[Sequence[float]]],
r: float,
Ms: Optional[Sequence[np.array]] = None,
x_0: Optional[np.array] = None,
Expand Down

0 comments on commit 05766f6

Please sign in to comment.