Skip to content

Conversation

@merkste
Copy link
Contributor

@merkste merkste commented Jun 4, 2018

The folding operation reduce is a principle iteration method for sequences of any kind. It takes an initial value and enables the accumulation of the results of each step. This PR adds reduceTransitions(state, init, ObjTransitionFunction) to enable iteration over the transitions of some model and accumulating the results, e.g., consider the following code change:

public default double sumOverTransitions(final int state, final TransitionToDoubleFunction f)
{
    class Sum {
        double sum = 0.0;
        void accept(int s, int t, double d)
        {
            sum += f.apply(s, t, d);
        }
    }
    Sum sum = new Sum();
    forEachTransition(s, sum::accept);
    return sum.sum;
}

to

public default double sumOverTransitions(final int state, final TransitionToDoubleFunction f)
{
    return reduceTransitions(state, 0.0, (r, s, t, d) -> r + f.apply(s, t, d));
}

In each step, the function f is applied to the current transition. The result of each step is the sum of all intermediate values up to the current transition.

The implementation of reduceTransitions should not rely on forEachTransition, as the access to a member-variable of some function (see the example above) is generally slower than to a temporary variable in reduceTransitions. Also, as functions passed to reduce are often clean, i.e., they do not reference external objects/variables, they could be efficiently inlined by the virtual machine (JVM does not yet apply this optimization).

We could go even futher by considering forEachTransition a special case of reduceTransitions that simply dismisses the intermediate results. This should yield equal performance in theory, but would require some tests to confirm that no performance hit occurs in practice.

@merkste merkste changed the title Implement reduceTransitions to allow efficient iteration over transitions Implement reduceTransitions to allow efficient accumulation over transitions Jun 4, 2018
@merkste merkste force-pushed the reduce-transitions branch from f4d16ac to e0eaed4 Compare June 4, 2018 13:28
@merkste merkste force-pushed the reduce-transitions branch 4 times, most recently from 3b005bd to f35b978 Compare May 8, 2019 09:51
@merkste
Copy link
Contributor Author

merkste commented May 8, 2019

I completed the implementation for DTMCs and CTMCs and added support for MDPs. The last commit implements forEachTransitionDo in terms of reduce.

@merkste merkste force-pushed the reduce-transitions branch from b6c31c2 to e3df4bb Compare May 8, 2019 10:11
@merkste
Copy link
Contributor Author

merkste commented May 8, 2019

@davexparker , @kleinj : Now with 4.5 out, what do you think?

@merkste merkste force-pushed the reduce-transitions branch from e3df4bb to 445f77c Compare May 22, 2019 11:44
@merkste merkste force-pushed the reduce-transitions branch from 445f77c to c1f16b2 Compare June 15, 2020 08:28
@davexparker davexparker added the WIP Work in progress/under discussion, not ready for merge label Sep 10, 2021
@davexparker davexparker force-pushed the master branch 2 times, most recently from ca12ca0 to 6bf73df Compare January 12, 2024 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

WIP Work in progress/under discussion, not ready for merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants