mesa icon indicating copy to clipboard operation
mesa copied to clipboard

ContinuousState: chained extrapolation differs depending on whether a Threshold is declared

Open lahari936 opened this issue 1 month ago • 1 comments

Question rather than a bug report — I ran into this probing mesa.experimental.states and cannot tell whether it is intended behaviour I am misusing, or a real defect. I have not opened a PR, since I would rather check the intent first.

What I see

A chained ContinuousState (position' = speed, speed' = accel) extrapolates differently depending on whether an unrelated Threshold is declared on the class. The threshold never fires — its limit is 1e9 — but declaring it changes the numbers.

from mesa import Agent, Model
from mesa.experimental.mesa_signals import HasEmitters, Observable
from mesa.experimental.states import ContinuousState, Threshold


class Mover(Agent, HasEmitters):
    accel = Observable(fallback_value=0.0)
    speed = ContinuousState(fallback_value=0.0, rate=lambda a: a.accel)
    position = ContinuousState(fallback_value=0.0, rate=lambda a: a.speed)
    # comment this line out and the numbers below change
    _never = Threshold(state=position, limit=1e9, callback="noop", direction="rising")

    def noop(self):
        pass


model = Model(rng=1)
mover = Mover(model)
mover.accel, mover.speed, mover.position = 2.0, 0.0, 0.0
model.run_for(5.0)
print(mover.position)

With x0 = 0, v0 = 0, a = 2, the expected trajectory is 0.5 * a * t**2:

t Threshold declared no Threshold exact
1 1.0 3.0 1.0
2 4.0 12.0 4.0
5 25.0 75.0 25.0

With the threshold the values are exact. Without it they are consistently 3 * t**2 instead of t**2.

What the stored state looks like

Reading the trajectory dict for position at t = 0, with speed sitting at 0.0:

base_value=0.0   current_rate=2.0   second_order_rate=2.0   last_time=0.0

current_rate is 2.0, which is the acceleration. At t = 0 the speed is 0.0, so I would have expected current_rate=0.0 and second_order_rate=2.0.

A guess at the mechanism, which may well be wrong

3 * t**2 is what you get if the rate is re-evaluated to its value now (speed(t) = 2t) but then applied across the whole interval since the old baseline (dt = t), and the second-order term is added on top:

2t * t  +  0.5 * 2 * t**2  =  2t**2 + t**2  =  3t**2

That would make the baseline snapshot, not the extrapolation formula, the thing that differs. With a Threshold attached, recalculate() runs _refresh_rate_if_dirty() and the state is re-snapshotted often enough that the error never accumulates — which would explain why Tram in tests/experimental/test_states.py is correct (test_chained_rate_extrapolates_quadratically asserts position == 25.0 at t = 5) while the same kinematics without a threshold are not.

I am not confident in that reading, which is why this is a question.

What I am asking

  1. Is a chained ContinuousState expected to be read directly without a Threshold attached, or is the threshold part of how the trajectory is meant to be kept fresh?
  2. If the first, is the 3 * t**2 result a bug worth fixing?

Happy to work on it if it is a defect — I just did not want to "fix" something that is working as designed, or to guess at the intended contract in a PR.

Mesa 4.0.0a0 at 949dc8c5, Python 3.14, Windows. Every number above is from that commit with no local changes.

lahari936 avatar Oct 26 '26 01:10 lahari936

A Threshold bound state was my original intent here, the rate only gets anchored at assignment when something subscribes to it, which is why declaring one changes the numbers. It is narrower than it looks though: the threshold has to be on that state, one on speed still leaves position wrong.

I agree that silently returning 3t**2 without one is not acceptable, so the refresh in __set__ should be unconditional.

codebreaker32 avatar Oct 27 '26 09:10 codebreaker32