Skip to content

Commit 156d741

Browse files
committed
refactoring part 2
1 parent 23eb133 commit 156d741

14 files changed

+271
-161
lines changed

README.md

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# PythonVRFT Library
1+
# PythonVRFT Library - Version 0.0.3
22
VRFT Adaptive Control Library written in Python.
33

44
**Author**: Alessio Russo (alessior.wordpress.com - [email protected])
@@ -13,13 +13,13 @@ Run the following command from root folder:
1313
```sh
1414
pip install .
1515
```
16-
Dependencies: numpy, scipy, control
16+
Dependencies: numpy, scipy
1717

1818
Tests
1919
------
2020
To execute tests run the following command
2121
```sh
22-
python setup.py test
22+
python -m unittest
2323
```
2424

2525
Examples
@@ -28,14 +28,9 @@ Examples are located in the examples/ folder. At the moment only 1 example is av
2828

2929
Objectives
3030
------
31-
- [**DONE**, 26.03.2017] Implement the basic VRFT algorithm (1 DOF. offline, linear controller, controller expressed as scalar product theta*f(z))
31+
- [**DONE - V0.0.2**][26.03.2017] Implement the basic VRFT algorithm (1 DOF. offline, linear controller, controller expressed as scalar product theta*f(z))
32+
- [**DONE - V0.0.3**][05.01.2020] Code refactoring and conversion to Python 3; Removed support for Python Control library.
3233
- [**TODO**] Add Documentation and Latex formulas
33-
- [**TODO**] Calculate best coefficients (based on H2 norm, with weights) given controller structure, reference system and actual system model. Show results.
34-
- [**TODO**] Modifications to LS (forgetting factor, covariance, ...)
35-
- [**TODO**] Implement multiple DOF, sensitivity analysis
36-
- [**TODO**] Implement online version
37-
- [**TODO**] Generalize to other kind of controllers
38-
- [**TODO**] Advanced work (non linear systems ?)
39-
40-
41-
34+
- [**TODO**] Add MIMO Support
35+
- [**TODO**] Add IV Support
36+
- [**TODO**] Generalize to other kind of controllers (e.g., neural nets)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 4
6+
}

examples/2_example.png

-48.5 KB
Binary file not shown.

examples/2_example.py

Lines changed: 0 additions & 94 deletions
This file was deleted.

examples/notebook_example_1.ipynb

Lines changed: 212 additions & 0 deletions
Large diffs are not rendered by default.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
author = 'Alessio Russo',
1010
author_email = '[email protected]',
1111
license='GPL3',
12-
packages=['vrft', 'test', 'vrft.utilities', 'vrft.vrft'],
12+
packages=['vrft', 'test'],
1313
zip_safe=False,
1414
install_requires = [
1515
'scipy',

test/test_iddata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from unittest import TestCase
2-
from vrft.utilities.iddata import iddata
2+
from vrft.iddata import iddata
33
import numpy as np
44

55
class TestIDData(TestCase):

test/test_reference.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest import TestCase
2-
from vrft.utilities.iddata import *
3-
from vrft.utilities.utils import *
4-
from vrft.vrft.vrft_algo import *
2+
from vrft.iddata import *
3+
from vrft.utils import *
4+
from vrft.vrft_algo import *
55
import numpy as np
66
import scipy.signal as scipysig
77

test/test_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from unittest import TestCase
2-
from vrft.utilities.utils import *
3-
from vrft.vrft.vrft_algo import virtualReference
2+
from vrft.utils import *
3+
from vrft.extended_tf import ExtendedTF
4+
from vrft.vrft_algo import virtualReference
45
import numpy as np
56
import scipy.signal as scipysig
67

test/test_vrft.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest import TestCase
2-
from vrft.utilities.iddata import *
3-
from vrft.vrft.vrft_algo import *
4-
from vrft.utilities.utils import ExtendedTF
2+
from vrft.iddata import *
3+
from vrft.vrft_algo import *
4+
from vrft.extended_tf import ExtendedTF
55
import numpy as np
66
import scipy.signal as scipysig
77

vrft/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
2-
from .utilities import *
3-
from .vrft import *
1+
from .iddata import *
2+
from .extended_tf import *
3+
from .utils import *
4+
from .vrft_algo import *

vrft/extended_tf.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,56 +9,56 @@
99

1010

1111
class ExtendedTF(scipysig.ltisys.TransferFunctionDiscrete):
12-
def __init__(self, num, den, dt):
12+
def __init__(self, num: np.ndarray, den: np.ndarray, dt: float):
1313
self._dt = dt
1414
super().__init__(num, den, dt=dt)
1515

1616
def __neg__(self):
1717
return ExtendedTF(-self.num, self.den, dt=self._dt)
1818

19-
def __floordiv__(self,other):
19+
def __floordiv__(self, other):
2020
# can't make sense of integer division right now
2121
return NotImplemented
2222

23-
def __mul__(self,other):
23+
def __mul__(self, other):
2424
if type(other) in [int, float]:
2525
return ExtendedTF(self.num*other, self.den, dt=self._dt)
2626
elif type(other) in [TransFun, ExtendedTF]:
27-
numer = polymul(self.num,other.num)
28-
denom = polymul(self.den,other.den)
29-
return ExtendedTF(numer,denom, dt=self._dt)
27+
numer = polymul(self.num, other.num)
28+
denom = polymul(self.den, other.den)
29+
return ExtendedTF(numer, denom, dt=self._dt)
3030

31-
def __truediv__(self,other):
31+
def __truediv__(self, other):
3232
if type(other) in [int, float]:
3333
return ExtendedTF(self.num,self.den*other, dt=self._dt)
3434
if type(other) in [TransFun, ExtendedTF]:
35-
numer = polymul(self.num,other.den)
36-
denom = polymul(self.den,other.num)
37-
return ExtendedTF(numer,denom, dt=self._dt)
35+
numer = polymul(self.num, other.den)
36+
denom = polymul(self.den, other.num)
37+
return ExtendedTF(numer, denom, dt=self._dt)
3838

39-
def __rtruediv__(self,other):
39+
def __rtruediv__(self, other):
4040
if type(other) in [int, float]:
41-
return ExtendedTF(other*self.den,self.num, dt=self._dt)
41+
return ExtendedTF(other*self.den, self.num, dt=self._dt)
4242
if type(other) in [TransFun, ExtendedTF]:
43-
numer = polymul(self.den,other.num)
44-
denom = polymul(self.num,other.den)
45-
return ExtendedTF(numer,denom, dt=self._dt)
43+
numer = polymul(self.den, other.num)
44+
denom = polymul(self.num, other.den)
45+
return ExtendedTF(numer, denom, dt=self._dt)
4646

4747
def __add__(self,other):
4848
if type(other) in [int, float]:
49-
return ExtendedTF(polyadd(self.num,self.den*other), self.den, dt=self._dt)
49+
return ExtendedTF(polyadd(self.num, self.den*other), self.den, dt=self._dt)
5050
if type(other) in [TransFun, type(self)]:
5151
if np.all(self.den == other.den):
5252
numer = polyadd(self.num, other.num)
5353
denom = self.den
5454
else:
55-
numer = polyadd(polymul(self.num,other.den),polymul(self.den,other.num))
56-
denom = polymul(self.den,other.den)
57-
return ExtendedTF(numer,denom, dt=self._dt)
55+
numer = polyadd(polymul(self.num,other.den), polymul(self.den,other.num))
56+
denom = polymul(self.den, other.den)
57+
return ExtendedTF(numer, denom, dt=self._dt)
5858

59-
def __sub__(self,other):
59+
def __sub__(self, other):
6060
if type(other) in [int, float]:
61-
return ExtendedTF(polyadd(self.num,-self.den*other),self.den, dt=self._dt)
61+
return ExtendedTF(polyadd(self.num, -self.den*other), self.den, dt=self._dt)
6262
if type(other) in [TransFun, type(self)]:
6363
if np.all(self.den == other.den):
6464
numer = polyadd(self.num, -other.num)
@@ -68,17 +68,17 @@ def __sub__(self,other):
6868
denom = polymul(self.den,other.den)
6969
return ExtendedTF(numer,denom, dt=self._dt)
7070

71-
def __rsub__(self,other):
71+
def __rsub__(self, other):
7272
if type(other) in [int, float]:
73-
return ExtendedTF(polyadd(-self.num,self.den*other),self.den, dt=self._dt)
73+
return ExtendedTF(polyadd(-self.num, self.den*other), self.den, dt=self._dt)
7474
if type(other) in [TransFun, type(self)]:
7575
if np.all(self.den == other.den):
7676
numer = polyadd(self.num, -other.num)
7777
denom = self.den
7878
else:
79-
numer = polyadd(polymul(self.num,other.den),-polymul(self.den,other.num))
79+
numer = polyadd(polymul(self.num,other.den), -polymul(self.den,other.num))
8080
denom = polymul(self.den,other.den)
81-
return ExtendedTF(numer,denom, dt=self._dt)
81+
return ExtendedTF(numer, denom, dt=self._dt)
8282

8383
def feedback(self):
8484
num = self.num

vrft/utils.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
from __future__ import division
2-
31
import numpy as np
42
import scipy.signal as scipysig
53
from .iddata import iddata
64

7-
from scipy.signal.ltisys import TransferFunction as TransFun
8-
from numpy import polymul, polyadd
9-
105
def Doperator(p: int, q: int, x: float) -> np.ndarray:
116
D = np.zeros((p * q, q))
127
for i in range(q):

vrft/vrft_algo.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from vrft.utilities.iddata import iddata
2-
from vrft.utilities.utils import systemOrder, checkSystem, filter_iddata, deconvolve_signal
1+
from vrft.iddata import iddata
2+
from vrft.utils import systemOrder, checkSystem, \
3+
filter_iddata
34
import numpy as np
45
import scipy.signal as scipysig
56

@@ -94,22 +95,15 @@ def control_response(data: iddata, error: np.ndarray, control: list):
9495
return phi
9596

9697
def compute_vrft(data: iddata, refModel: scipysig.dlti, control: list, L: scipysig.dlti):
97-
# import pdb
98-
# import matplotlib.pyplot as plt
99-
# pdb.set_trace()
98+
10099
data = filter_iddata(data, L)
101100
r, n = virtualReference(data,
102101
refModel.num,
103102
refModel.den)
104-
# r2=deconvolve_signal(refModel, data.y, data.ts)
105-
# plt.plot(r)
106-
# plt.plot(r2)
107-
# plt.show()
108103

109104
phi = control_response(data, np.subtract(r, data.y[:n]), control)
110105
theta, phi = calc_minimum(data, phi)
111106
loss = compute_vrft_loss(data, phi, theta)
112-
print(phi)
113-
#theta[0] = -theta[0]
107+
114108
final_control = np.dot(theta, control)
115109
return theta, r, phi, loss, final_control

0 commit comments

Comments
 (0)