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

Particle filter initialization error #44

Open
qtomcatq opened this issue Feb 22, 2024 · 0 comments
Open

Particle filter initialization error #44

qtomcatq opened this issue Feb 22, 2024 · 0 comments
Assignees
Labels
help wanted Extra attention is needed

Comments

@qtomcatq
Copy link

I'm trying to initialize the particle filter in the following way:

x0 = [0, 0, 0]
dt = 0.001
model=initialize(x0,dt)
observer= PF(model, roughening=True, plot_backend='bokeh')
observer.setup()

my model is Lorenz attractor initialized in the following way:

def initialize(x0,dt):

    model = Model(plot_backend='bokeh')

    states = model.set_dynamical_states(['x1', 'x2', 'x3'])
    inputs = model.set_inputs(['u1','u2','u3'])
    model.set_measurements(['o1', 'o2', 'o3'])
    model.set_measurement_equations(states)

    # Unwrap states
    x1 = states[0]
    x2 = states[1]
    x3 = states[2]

    # Unwrap states
    u1 = inputs[0]
    u2 = inputs[1]
    u3 = inputs[2]

    # Parameters
    c = np.array([10., 28., 8/3])

    # ODE
    dx1 = c[0]*(x2-x1) + u1
    dx2 = c[1] *x1 - x2 - x1*x3 + u2
    dx3 = x1*x2 - c[2]*x3 + u3


    model.set_dynamical_equations([dx1, dx2, dx3])

    # Initial conditions
    x0 = [0, 0, 0]

    # Create model and run simulation
    dt = 0.001
    model.setup(dt=dt)
    model.set_initial_conditions(x0=x0)
    return model

my attempt to initialize the particle filter ends in the following error:

18 observer= PF(model, roughening=True, plot_backend='bokeh')
---> 19 observer.setup()
20

C:\ProgramData\Anaconda3\lib\site-packages\hilo_mpc\modules\estimator\pf.py in setup(self, **kwargs)
290
291 self._propagate_particles(n_s)
--> 292 self._evaluate_likelihood(n_s)
293
294 n_x = self._model.n_x

C:\ProgramData\Anaconda3\lib\site-packages\hilo_mpc\modules\estimator\pf.py in _evaluate_likelihood(self, n_samples)
156 R = ca.SX.sym('R', (n_y, n_y))
157
--> 158 q = self._normpdf(Y, y, ca.sqrt(R)) # Since R is usually a diagonal matrix, we can use ca.sqrt() here. We need
159 # to change this, if we have non-diagonal matrices, i.e. covariance entries.
160 q /= ca.sum2(q)

C:\ProgramData\Anaconda3\lib\site-packages\casadi\casadi.py in call(self, *args, **kwargs)
23379 if len(args)>0:
23380 # Ordered inputs -> return tuple

23381 ret = self.call(args)
23382 if len(ret)==0:
23383 return None

C:\ProgramData\Anaconda3\lib\site-packages\casadi\casadi.py in call(self, *args)
20037
20038 """

20039 return _casadi.Function_call(self, *args)
20040
20041

RuntimeError: Error in Function::call for 'normpdf' [SXFunction] at .../casadi/core/function.cpp:339:
.../casadi/core/function_internal.hpp:1644: Input 0 (i0) has mismatching shape. Got 3-by-15. Allowed dimensions, in general, are:

  • The input dimension N-by-M (here 1-by-1)
  • A scalar, i.e. 1-by-1
  • M-by-N if N=1 or M=1 (i.e. a transposed vector)
  • N-by-M1 if K*M1=M for some K (argument repeated horizontally)
  • N-by-P*M, indicating evaluation with multiple arguments (P must be a multiple of 1 for consistency with previous inputs)

Some problem with initialization of probability distribution. I don't understand casadi, so I can't fix it myself.
The code works perfectly with EKF, UKF and MHE filters.
Thanks for help in advance.

@brunomorampc brunomorampc self-assigned this Feb 27, 2024
@brunomorampc brunomorampc added the help wanted Extra attention is needed label Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants