Deltares / rtc-tools

The Deltares toolbox for control and optimization of environmental systems.
GNU Lesser General Public License v3.0
0 stars 2 forks source link

WIP: Implement a mixin to smooth model dynamics #1574

Closed SGeeversAtVortech closed 1 month ago

SGeeversAtVortech commented 1 month ago

In GitLab by @jvande42b on Jul 11, 2018, 19:51

In every model ever built, we observe that the control variables induce undesirably jumpy and pointed results into the model dynamics. We usually try to resolve this by adding rate-of-change goals ad-hoc onto the most misbehaved states. This new mixin:

  1. Provides a standardized approach to smoothing dynamics
  2. Avoids ad-hoc boilerplate der() goals
  3. Makes it easy for new users to get agreeable results out of RTC

Closes #1047

SGeeversAtVortech commented 1 month ago

In GitLab by @vreeken on Jul 12, 2018, 24:07

How often does it occur that freedom remains in the solution? I would assume that most optimization problems have a minimization goal at the end, which should leave relatively little (if at any) freedom.

Otherwise, I have no issue with adding something like this for cases where such minimization goals do not exist (or freedom remains). What's missing/wrong though is:

SGeeversAtVortech commented 1 month ago

In GitLab by @baayen on Jul 12, 2018, 08:29

Commented on src/rtctools/optimization/smooth_dynamics_mixin.py line 17

Shouldn't this be min_timestep so to obtain the largest possible abs_max_der?

SGeeversAtVortech commented 1 month ago

In GitLab by @jvande42b on Jul 12, 2018, 16:04

Commented on src/rtctools/optimization/smooth_dynamics_mixin.py line 17

correct. thanks.

SGeeversAtVortech commented 1 month ago

In GitLab by @jvande42b on Jul 12, 2018, 16:04

resolved all discussions

SGeeversAtVortech commented 1 month ago

In GitLab by @jvande42b on Jul 12, 2018, 16:37

Not all models are minimized- in some models where pumps dominate they are, but in many models weirs, turbines, or orifices are the dominant control structures. With these structures it is usually preferred to minimize the derivative of the flow rather than the actual flow. And even with a model where the only control variables are pumps, rapidly changing flow rates are also not desirable. It usually best to minimize total cost, leaving flexibility to minimize the derivatives at the end.

SGeeversAtVortech commented 1 month ago

In GitLab by @jvande42b on Jul 12, 2018, 20:26

Commented on src/rtctools/optimization/smooth_dynamics_mixin.py line 17

changed this line in version 2 of the diff

SGeeversAtVortech commented 1 month ago

In GitLab by @jvande42b on Jul 12, 2018, 20:26

added 1 commit

Compare with previous version

SGeeversAtVortech commented 1 month ago

In GitLab by @jvande42b on Jul 13, 2018, 04:55

added 1 commit

Compare with previous version

SGeeversAtVortech commented 1 month ago

In GitLab by @jvande42b on Jul 13, 2018, 04:58

@vreeken have a look now- I think I addressed all your concerns.

SGeeversAtVortech commented 1 month ago

In GitLab by @vreeken on Jul 13, 2018, 09:59

Commented on doc/examples/optimization/goal_programming.rst line 120

A welcome fix, but unrelated to the commit.

SGeeversAtVortech commented 1 month ago

In GitLab by @vreeken on Jul 13, 2018, 10:02

Commented on src/rtctools/optimization/smooth_dynamics_mixin.py line 43

What do you mean with aggressive. From what I can tell it only scales the objective? I agree that the user should be able to influence how the objective is scaled to avoid scaling issues, but it should be clear that that's what it does.

SGeeversAtVortech commented 1 month ago

In GitLab by @vreeken on Jul 13, 2018, 10:05

Commented on src/rtctools/optimization/smooth_dynamics_mixin.py line 57

Should we allow users to pass either a list, or a dict of state -> weight? That way users can prefer to minimize derivatives of one state over another.

SGeeversAtVortech commented 1 month ago

In GitLab by @vreeken on Jul 13, 2018, 10:07

Commented on src/rtctools/optimization/smooth_dynamics_mixin.py line 61

Wouldn't it be easier to have one single goal which returns (a weighted) sum of all the states?

SGeeversAtVortech commented 1 month ago

In GitLab by @vreeken on Jul 13, 2018, 10:09

It could be desirable to also have this Mixin work for fully linear models. That would mean that the order of the minimization goal can be set to 1 (indirectly), and an additional helper variable is needed for the absolute value of a derivative state.

It would also be possible to get a much better nominal (especially important with order=2 I think) by using results of previous goals to get an estimate. This is not possible using individual goals (as then the weighting/relative importance would change), but would be possible for the sum of all der()'s in a single goal.

SGeeversAtVortech commented 1 month ago

In GitLab by @jvande42b on Jul 13, 2018, 14:41

Commented on src/rtctools/optimization/smooth_dynamics_mixin.py line 57

If they want that fine level of control, then they should implement their own goals. I would say that is outside of the scope of this mixin- we would over-complicate the API. Also, I generally find that using weights is only useful when the model is linear.

SGeeversAtVortech commented 1 month ago

In GitLab by @jvande42b on Jul 13, 2018, 14:42

Commented on src/rtctools/optimization/smooth_dynamics_mixin.py line 57

On the other hand, I think the initial state estimation mixin does have a way to pass in weights, right?

SGeeversAtVortech commented 1 month ago

In GitLab by @jvande42b on Jul 13, 2018, 14:44

Commented on src/rtctools/optimization/smooth_dynamics_mixin.py line 61

easier? not sure- this was easy to implement this way. Sorting out the scaling was really simple.

SGeeversAtVortech commented 1 month ago

In GitLab by @jvande42b on Jul 13, 2018, 14:57

Commented on src/rtctools/optimization/smooth_dynamics_mixin.py line 43

If you run the cascading channel example with a multiplier of [0.01, 0.05, 0.1, 0.5, 1.0], you will find that as you approach 1.0 the scaling continuously progresses from hardly smoothed at all to nice and smooth, but with an increasing number of iterations to find a solution. At some point greater than 1.0, you will cross the max_iter cut-off. This is expected, because the flexibility is limited at the prio this goal runs at. I found that 0.1 was the ideal multiplier for this model, providing both fast convergence and excellent smoothing. I imagine there will be a similar multiplier for most models, but cannot guarantee that it will the same for all. It would probably require tuning.

So this parameter helps you tune the smoothing into a sweet spot that provides sufficient smoothing while not needing hundreds or thousands of iterations.

What do you think about this approach?

SGeeversAtVortech commented 1 month ago

In GitLab by @vreeken on Jul 13, 2018, 15:31

Commented on src/rtctools/optimization/smooth_dynamics_mixin.py line 43

I'll check that myself later, but I find that behavior somewhat suspicious of no other goals are present at that priority. Maybe I'm missing something, but if all it does is to scale the objective, results shouldn't change that much.

SGeeversAtVortech commented 1 month ago

In GitLab by @vreeken on Jul 14, 2018, 17:19

Commented on src/rtctools/optimization/smooth_dynamics_mixin.py line 43

Tested with the goal_programming example, but as I expected nothing changes except for the value of the objective. This goes squared with the scaling factor, because of the order=2. Testing it with the cascading example, the same holds true if you only smooth one single state. So the convergence issues you are seeing are because of the interaction of various states, possibly combined with the order=2.

SGeeversAtVortech commented 1 month ago

In GitLab by @baayen on Jul 16, 2018, 08:15

I like the idea of making the order an option and defaulting to order=1. That may also address scaling issues.

Minimizing abs(x-y) is equivalent to min z, s.t. x-y <= z and -(x-y) <= z.

SGeeversAtVortech commented 1 month ago

In GitLab by @baayen on Jul 16, 2018, 08:16

Commented on src/rtctools/optimization/smooth_dynamics_mixin.py line 57

I'd suggest to stick with a flat list of variables for now. We generally try to stay clear of weights as much as possible.

SGeeversAtVortech commented 1 month ago

In GitLab by @vreeken on Sep 3, 2018, 18:08

Another idea is to define priority_completed as well, and let that (dynamically) calculate the value of the nominal (such that the objective is on the order of 1). Then we avoid scaling issues where the bounds are very wide to the typically observed changes.

SGeeversAtVortech commented 1 month ago

In GitLab by @vreeken on Sep 6, 2019, 13:14

marked as a Work In Progress

SGeeversAtVortech commented 1 month ago

In GitLab by @SGeeversAtVortech on Dec 8, 2022, 14:38

Closed, since this MR has been inactive for quite some time.