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

Make constraints as Model instead of the same as their parent model #287

Closed
wants to merge 4 commits into from

Conversation

pckroon
Copy link
Collaborator

@pckroon pckroon commented Dec 4, 2019

ODEModels make for poor constraints, since they require an initial argument, which is not passed by BaseModel.as_constraint. This does mean that if you want to use an ODEModel as constraint, you need to initialize your constraint model yourself, and pass it.
On top of that, this PR adds initial ODE parameters to the connectivity_mapping.

Fixes #282


constraints = [Ge(a0, c0)]

fit = Fit(ode_model, t=tdata, a=AA, c=AAB, d=BAAB)
Copy link
Owner

Choose a reason for hiding this comment

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

The constraints are not passed to Fit. It is probably better to use an equatilty or make the constraint the other way around, since initially the constraint is already true so we don't know if it was enforced properly.

@tBuLi
Copy link
Owner

tBuLi commented Dec 5, 2019

Nice one. But I think the original reason for using the same class is that when adding constraints to a CallableNumericalModel, the constraint also needs to be of that same type, in case the constraint reuses some of the components of the original model. This functionality is now broken.

E.g.

model = CallableNumericalModel({y: lambda x: a * x**2}, connectivity_mapping={y: {x, a}})
constraint = CallableNumericalModel.as_constraint(
    {z: lambda y: np.sum(y)}, 
    connectivity_mapping={z: {y}}, 
    model=model, constraint_type=Eq
)

I'm actually surprised the tests didn't fail for this, I guess that means I never added proper testing for the above functionality. Perhaps whoever of us has time first can check if this indeed works, because this is very nice functionality to have: it means that also for these kinds of models the constraints can depend on the output of the model, not just the parameters. I personally already used this type of construction to fix the integral of my solution to be 1.

So I think I'm more in favor of adding a simple check for ODEModels and only do your new fix there but keep the standard behavior otherwise.

@tBuLi
Copy link
Owner

tBuLi commented Dec 5, 2019

I also think that ultimately the solution would be to rework ODEModel into a subclass of CallableNumericalModel so we can also more easily mix component of ODE and non-ODE type like in the recent issue #284, but might be bigger than the current issue.

@pckroon
Copy link
Collaborator Author

pckroon commented Dec 5, 2019

I also think that ultimately the solution would be to rework ODEModel into a subclass of CallableNumericalModel so we can also more easily mix component of ODE and non-ODE type like in the recent issue #284, but might be bigger than the current issue.

I forgot about this TODO. I'll fix both in one go.

@pckroon pckroon closed this Dec 5, 2019
@tBuLi
Copy link
Owner

tBuLi commented Dec 5, 2019

Btw, a potential unified syntax could be as follows:

ODEModel({
    D(y, t): - k * y,
    y: 1,
    t: 0
})

This would move everything to the model_dict and makes the ambition of having expressions as initial conditions much easier. It would also streamline internal code regarding ODEModels, though at the expense of some readability for the user I think, since it is not immediately obvious that these are to be read as initial conditions. What do you think?

@tBuLi
Copy link
Owner

tBuLi commented Dec 5, 2019

It could also be a good idea an improved version of the current ODEModel but call it RawODEModel instead, and then make a new ODEModel which allows for mixing of ODE and normal expressions, but which then seperates the ODE components into a RawODEModel and then adds this as a numerical component to a CallableNumericalModel. This would essentially automate the process here: https://symfit.readthedocs.io/en/stable/examples/ex_CallableNumericalModel_ode.html

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.

ODEModel with constraints gives error
2 participants