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

Add example of parameter estimation from multiple measurement trials #230

Closed

Conversation

Peter230655
Copy link
Contributor

@Peter230655 Peter230655 commented Sep 13, 2024

In a simple spring / damper model the spring constant and the dampening coefficient are to be estimated using noisy measurements.
The model is set up four times corresponding to four noisy measurements.
I create these noisy measurements by integrating the four systems and adding random numbers to each point of the solutions and a random shift.
As the method plot_trajectories does not work if no input trajectories are given, I plot them 'manually'.
(I hope I understood the idea correctly)

u1.subs({t: t0}) - x0[4],
u2.subs({t: t0}) - x0[5],
u3.subs({t: t0}) - x0[6],
u4.subs({t: t0}) - x0[7],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You generally don't want instance constraints in parameter estimation problems. This overly constrains the solution.

Copy link
Contributor Author

@Peter230655 Peter230655 Sep 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You generally don't want instance constraints in parameter estimation problems. This overly constrains the solution.

Sorry, I missed this in the PR I just started. Will correct it.
Question: Would you not know the initial conditionsd of the experiment, like im my case you would know how much you extended the mas, and at what initial speed it started?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bet is that if you remove these, you'll get better parameter estimates.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Would you not know the initial conditionsd of the experiment, like im my case you would know how much you extended the mas, and at what initial speed it started?

You know the initial conditions only as good as your error filled measurement might say. If you force the initial condition to the value of a measurement that has error, then you are not allowing for solutions that minimize the overall error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense!
When I give reasonable bounds it finds good values.
I will try to incorporate all you suggested tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: does it make sense to estimate continuous parameters this way? What I mean: say, I apply a time varying force to this sytem and try to estimate what it looks like?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand you, I think you can.

@moorepants
Copy link
Member

This is the right idea and very cool to see that it works!

@Peter230655
Copy link
Contributor Author

Peter230655 commented Sep 14, 2024

This is the right idea and very cool to see that it works!

Removing the instance control did two things:

  • it made the estimates worse (I know their true value :-) )
  • it gives a mistake in plot_constraint_violations
    I will fix the error in the method over the weekend and fix it.
    I think, no much use merging this simulation until the method plot_constraint_violation is fixed?

are to be estimated.

The idea is to set up four sets of eoms, one for each of the measurements, with
identical parameters, and let opty estimate the parameters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add an explanation of why we have to do such a thing. Something like:

For parameter identification, it is common to collect measurements of a system's trajectories from distinct expeirments. For example, if you are identifying the parameters of a mass-spring-damper system you will excite the system with different initial conditions multiple times. The data cannot simply be stacked and the identification run because the measurement data would be discontinuous between trials. A work around in opty is to create a set of differential equations with unique state variables for each measurement trial that all share the same constant parameters. You can then identify the parameters from all measurement trials simultaneously by passing the uncoupled differential equations to opty.

# %%
# Initial guess.
#
initial_guess = np.array(list(np.random.randn(8*num_nodes + 2)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In parameter identification problems you have the measured state values, so there is really no reason to not use the measurements as the initial guess for the state trajectories.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also typically have some reasonable guess for the parameters and the parameters needs to be bounded.

# Solve the Optimization Problem.
#
solution, info = problem.solve(initial_guess)
print(info['status_msg'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new cell before the plot command, basically a new cell should be after anything you print or plot

@moorepants moorepants changed the title Estimate parameters of a dynamic system Add example of parameter estimation from multiple measurement trials Sep 14, 2024
x0 = np.array([3, 3, 3, 3, 0, 0, 0, 0])
pL_vals = [1.0, 0.25, 1.0, 1.0]

resultat1 = solve_ivp(gradient, t_span, x0, t_eval = times, args=(pL_vals,))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more realistic measurement data set would be collected from trials that have different initial conditions. You often don't have control of the initial conditions in the experiments, you simply measure whatever the state happens to be.

@moorepants
Copy link
Member

I removed instance constraints, added bounds to the unknown parameters, set the initial guess of x to the measured states and set the initial guess for u's to zeros, and gave reasonable guesses for the unknown parameters and I get a consistent optimal solution. I think making those updates would make this more realistic and solve well. Additionally, we should use different initial conditions for each of the four measurement trials.

@Peter230655
Copy link
Contributor Author

I removed instance constraints, added bounds to the unknown parameters, set the initial guess of x to the measured states and set the initial guess for u's to zeros, and gave reasonable guesses for the unknown parameters and I get a consistent optimal solution. I think making those updates would make this more realistic and solve well. Additionally, we should use different initial conditions for each of the four measurement trials.

I will do this. But could you plot the constraint violations?

@moorepants
Copy link
Member

It would be helpful to make the final plot be a comparison of the optimal trajectories vs the measured trajectories (like done in the other parameter id examples). You can do this by first plotting the measurements on the same number of axes that the plot_trajectories() method produces and then pass that axes set into the plot method.

@moorepants
Copy link
Member

I will do this. But could you plot the constraint violations?

No there is a bug.

@moorepants
Copy link
Member

There should be a Problem.num_instance_constraints that returns 0 when there are none.

# %%
"""

Parameter Estimation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title and filename are too generic.

  1. We use the terms "parameter identification" in opty not "parameter estimation", we should be consistent.
  2. This is a special parameter id example, in that it is showing how to use multiple measurement trials that constitute a set of discontinuous data. So the title should reflect this uniqueness (relative to the other simpler parameter id examples)

@Peter230655
Copy link
Contributor Author

I just pushed this to let you know where I am. Not yet been able to incorporate all your suggestions.
NB: I assumed only the positions of the mass are measured, not the speeds. So the measurement has only four plots, the plot_trajectories has 8.
(I have a hell of a time setting up eoms without sympy mechanics! Got the signs wrong all the time!)

@Peter230655
Copy link
Contributor Author

I will do this. But could you plot the constraint violations?

No there is a bug.

I will try to fix the bug in the next few days. I might have caused it when I changed this method a bit.

@moorepants
Copy link
Member

Already fixed: #232

@moorepants
Copy link
Member

(I have a hell of a time setting up eoms without sympy mechanics! Got the signs wrong all the time!)

Equations are here: https://en.wikipedia.org/wiki/Mass-spring-damper_model

@Peter230655
Copy link
Contributor Author

(I have a hell of a time setting up eoms without sympy mechanics! Got the signs wrong all the time!)

Equations are here: https://en.wikipedia.org/wiki/Mass-spring-damper_model

Thanks!
I studied engineering 100 years ago! I should still be able to do it without Wikipedia! :-(

@Peter230655
Copy link
Contributor Author

Already fixed: #232

Unbelievable! I would have taken me 2 days!! :-(

"""

Parameter Identification of a Mass-Spring-Damper System.
========================================================
Copy link
Member

@moorepants moorepants Sep 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer "Parameter Identification from Non-Continuous Measurements" or something similar. The unique aspect of this example relative to what we already have is that we have these independent measurements of the system's motion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "non-contiguous" is the right word.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter Identification from Non-Contiguous Measurements

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer "Parameter Identification from Non-Continuous Measurements" of something similar. The unique aspect of this example relative to what we already have is that we have these independent measurements of the system's motion.

Will do so and push

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also change the filename to plot_non_contiguous_parameter_estimation.py

# %%
"""

Parameter Identification from Non-Continuous Measurements.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You didn't agree that "contiguous" was the correct word?

contiguous /kən-tĭg′yoo͞-əs/
adjective

Sharing an edge or boundary; touching.
Neighboring; adjacent.
Connecting without a break. 

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it can be either, but we have continuous measurements, they are just not contiguous in time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You didn't agree that "contiguous" was the correct word?

contiguous /kən-tĭg′yoo͞-əs/ adjective

Sharing an edge or boundary; touching.
Neighboring; adjacent.
Connecting without a break. 

:-) :-)
I thought is was a typo. Knowing you I should have known better! Will change and push.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't' really matter, I just get nit picky on wording. I'm working an journal article this morning doing the final editing and my mind is in the mode of being extremely precise with wording.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't' really matter, I just get nit picky on wording. I'm working an journal article this morning doing the final editing and my mind is in the mode of being extremely precise with wording.

Can I get a copy when you are done with it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Peter230655
Copy link
Contributor Author

Peter230655 commented Sep 16, 2024

I modified this example a bit: I added a force = F1 * sin(omega * t) and also estimate F1 and omega.
Also I made the number of measurements variable.
Would you prefer this 'enlarged' simulation?
If yes, would I simply push to this PR, or close this PR and make a new one?
Thanks!

@moorepants
Copy link
Member

moorepants commented Sep 16, 2024

I modified this example a bit: I added a force = F1 * sin(omega * t) and also estimate F1 and omega.

I'd prefer if we didn't add this to this example and simply demonstrate how to do parameter identification from multiple non-contiguous measurements. If you want it to be something different I can make an example that focuses on the parameter id approach.

@Peter230655
Copy link
Contributor Author

Peter230655 commented Sep 16, 2024

I modified this example a bit: I added a force = F1 * sin(omega * t) and also estimate F1 and omega.

I'd prefer if we didn't add this to this example and simply demonstrate how to do parameter identification from multiple non-contiguous measurements. If you want it to be something different I can make an example that focuses on the parameter id approach.

NO, not at all, I'd like for the one already pushed to be added!
I play around a lot, the come up with something else, which I feel is interesting, so I let you know.
I do have a tendency to complicate my programs, I want to see how far I can go - and this has a tendency to cover up what should be shown.
NB: I do not know, why it failed on Github test.

@moorepants
Copy link
Member

NB: I do not know, why it failed on Github test.

You have to read the error message.

@Peter230655
Copy link
Contributor Author

Peter230655 commented Sep 18, 2024

NB: I do not know, why it failed on Github test.

You have to read the error message.
Of course I tried to read it - problem is understanding :-(
image

What is title underline too short?
I checked, and the title had the right number of '=' under it.

@@ -0,0 +1,209 @@
# %%
"""

Copy link
Contributor

@tjstienstra tjstienstra Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/home/runner/work/opty/opty/docs/examples/plot_non_contiguous_parameter_estimation.rst:206:Title underline too short.

I guess that it is complaining about this empty line, as the underline ==== seems to be the right length.

My strategy in these cases is to look toward what part the error messages points me and in this case compare the code to similar examples. Those don't have this empty line ;)


# %%
# Set up the Identification Problem.
# --------------------------------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error is here and it is exactly what it says in the error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error is here and it is exactly what it says in the error message.

Fair enough! I only looked at the 'big' title. I will correct and push

@Peter230655
Copy link
Contributor Author

I close this PR and start a new one, as here are wrong files included

@Peter230655 Peter230655 deleted the plot_parameter_estimation branch September 19, 2024 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants