Hopsan / hopsan

Hopsan is a free multi-domain system simulation tool developed at the division of Fluid and mechatronic systems at Linköping university.
Apache License 2.0
150 stars 42 forks source link

Correlation and indexing vectors when evaluating objective functions #1970

Open mrejster opened 4 years ago

mrejster commented 4 years ago

Is your improvement request related to a problem? Please describe. When optimizing model with cyclical output, such as rockdrills, it is often useful to time-shift signals before comparing / matching. For example if I want to calculate an objective function based on the similarity of two piston location vectors. This could be done using Matlab etc by locating the time shfit using correlation, and then index the vectors appropriately. An (currently unsolvable?) issue arises when one wants to include such time shifts in an optimization in Hopsan.

Describe the solution you'd like Either HCOM functionality to enable indexing of vectors, generating new vector based on indeces (such as A= B[10342:82739]). Or possibly by re-enabling the Python scripting environment, which I believe had such capabilities?

Describe alternatives you've considered Currently, my best option is to add a time-shift parameter as an optimization parameter, letting it move the compared generation using a lookup table, and hoping for the best that Hopsan finds a good time shift value. This is much more computationally heavy than simply matching the signals before obj-function evaluation, and complicates the optimization.

Additional context

robbr48 commented 4 years ago

I think we can add both a subvector function and a time shift function to HCOM. Perhaps subvec(vector, start, end) and tshift(vector, dt)?

mrejster commented 4 years ago

If possible, stealing some well known syntaxt would be nice. Such as matlab A(13:25) or numpy B[13:25]. But I'm guessing that's much more work to implement?

peterNordin commented 4 years ago

Hmm, for time shifting, I think that this should already work, in plots you can add time shifts, that then affect all plot variables using that time vector in the same generation. But maybe its not possible to do so from HCOM, or maybe it only affects the plots.

The following (needlessly complicated) command would shift the time vector by 5, for all time-domain variables with that particular Time variable. But I am not sure if that what was desired, or only to shift time for one particular variable ? Time = Time + ones(size(Time)) * 5

@robbr48 oddly, the expression Time = Time + 5 * ones(size(Time)) does not evaluate. Did not debug, maybe a Symhop issue?

peterNordin commented 4 years ago

Implementing new operators such as () and [] will take some time, parenthesis are used in functions so brackets would be better. Quicker to do subvec solution first. In this case for time-domain and frequency-domain, a new subvec of the time or frequency vector would also have to be created.

@robbr48 When you say tshift(vector, dt) does that mean to shift the data left or right or change the underlying time data. I assume its the former, so that we do not affect all variables using that time vector?

mrejster commented 4 years ago

I think a time shift operator would need some consideration. When starting to change time sync for individual signals it is easy to screw things up, so I think manual indexing of vectors is sufficient for now. Time vectors can be created manually anyway.

Den tis 18 aug. 2020 kl 21:46 skrev Peter Nordin notifications@github.com:

Implementing new operators such as () and [] will take some time, parenthesis are used in functions so brackets would be better. Quicker to do subvec solution first. In this case for time-domain and frequency-domain, a new subvec of the time or frequency vector would also have to be created.

@robbr48 https://github.com/robbr48 When you say tshift(vector, dt) does that mean to shift the data left or right or change the underlying time data. I assume its the former, so that we do not affect all variables using that time vector?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Hopsan/hopsan/issues/1970#issuecomment-675677112, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABRS3W6RBECJI4UB77PBECTSBLK7RANCNFSM4QBQFM2A .

robbr48 commented 4 years ago

@peterNordin Yes, we must of course not modify the time vector variable. Only the specified variable must be changed. I think a positive value of dt means shift to the right, but I'm not sure.

@mrejster Operators would be nice, but is very non-HCOM. Compare e.g. with peek(vector,15) which equals vector[15].