Open akashyap13 opened 1 year ago
Great question -- I am not sure what you are doing is sensible though. The constraint matrices for ConstrainedSR3 are defined assuming the original size of the FULL library, while you are asking it to build constrained SR3 models using a subset of the library, so the constraints have the wrong dimensions for this sub-library! This is probably worth implementing a fix at some point, or at least a warning in the code that this is not allowed behavior.
Also, could you post the error message you are seeing?
Hey @akaptano,
Thank you so much for your response. Yes I think for the sub-library it might not make sense to reduce terms when having a constraint. The error I get for that is "ValueError: cannot reshape array of size 30 into shape (3,9)".
However, for the bagging, I feel like it should work since its just running on a subset and then taking the average coefficients. Currently when I run it, it just ignoring the constraints I am passing to it and I get the solution if I didn't have any constraints.
Okay sounds like we have diagnosed the library bagging issue at least. I can reproduce the issue you mentioned with the bagging, so this is likely a real error on our side. Will try to take a look this week.
Incredible!!! Thank you so much. This would be very helpful for my code.
Hey @akaptano, Just wondering if you all were able to do a quick fix on this bug or it was going to take some time longer, thanks happy holidays!
Sorry for the very slow response here -- we have not gotten around to implementing this, although if it is important for your research, it is straightforward to edit the PySINDy python source code to do as your hoping here.
@himkwtn here's another slightly-mathy issue if you want a meatier issue
The question of how to disallow library bagging of constrained optimizers is a thorn. It can be handled in type checking if we parametrize the BaseOptimizer
type based upon whether it is constrained. Here's an example of how that works
from typing import Literal, Generic, overload, TypeVar
from typing_extensions import assert_type
T = TypeVar("T", bound=bool) #Constrained flag
class Optimizer(Generic[T]):
def __new__(cls) -> Optimizer[Literal[False]]: ...
assert_type(Optimizer(), Optimizer[Literal[False]])
class ConstrainedSR3(Optimizer[T]):
@overload
def __new__(cls, constraints: None) -> ConstrainedSR3[Literal[False]]: ...
@overload
def __new__(cls, constraints: str) -> ConstrainedSR3[Literal[True]]: ...
assert_type(ConstrainedSR3(None), ConstrainedSR3[Literal[False]])
assert_type(ConstrainedSR3("a"), ConstrainedSR3[Literal[True]])
@overload
def ensemble(opt: Optimizer[Literal[False]], bag_lib: Literal[True]) -> None: ...
@overload
def ensemble(opt: Optimizer[Literal[True]], bag_lib: Literal[False]) -> None: ...
ensemble(ConstrainedSR3(None), True)
ensemble(ConstrainedSR3("a"), True) # Error
ensemble(ConstrainedSR3("a"), False)
The alternative would be to have a subclass for Constrained optimizers, but then we couldn't handle situations when a constrained optimizer isn't being used with constraints (e.g. MIOSR).
In execution, it's a bit trickier, but we can check if the ensembled optimizer is of a constrainable type, and then check constraint_lhs
.
We also want to disallow the SBR
optimizer, or any other that result in a distribution (e.g. ensembling an ensembler). So maybe we're at a point to split the BaseOptimizer into StatisticalOptimizer and DiscreteOptimizer, and ensembling only accepts the latter.
Hi PYSINDy,
I am having trouble with getting the ensemble optimizer working with the constrained sr3 module. When I include library ensemble it errors out, and when I use bagging it solves the system without using the constraints.
I have attached an example code consisting of the examples provided with pysindy to illustrate these two points. I might have made an error, so please let me know if I missed an easy fix.
Thanks for your help Amrit
python 3.9.13 pysindy 1.7.2
CODE