modelx icon indicating copy to clipboard operation
modelx copied to clipboard

Implement uncached Cells

Open fumitoh opened this issue 1 year ago • 8 comments

TBD

fumitoh avatar May 25 '24 11:05 fumitoh

Is the alternative to use custom imported modules for this purpose?

AB-Athene avatar Jun 05 '24 11:06 AB-Athene

Yes, this can be used like a normal Python function.

fumitoh avatar Jun 05 '24 13:06 fumitoh

Example

import modelx as mx

@mx.defcells
def cash_inflows():
    return [1, 2, 3]

@mx.defcells
def cash_outflows():
    return [2, 1, 0]


@mx.uncached
def get_pv(cashflows: list):
    rate = 0.05
    disc = 1 / (1 + rate)
    return sum(v * disc**(i + 1) for i, v in enumerate(cashflows))

@mx.defcells
def pv_inflows():
    return get_pv(cash_inflows())


@mx.defcells
def pv_outflows():
    return get_pv(cash_outflows())

@mx.defcells
def pv_netflows():
    return get_pv([i - o for i, o in zip(cash_inflows(), cash_outflows())])


print(f"pv_netflows():{pv_netflows()}")
print(f"pv_inflows() - pv_outflows():{pv_inflows() - pv_outflows()}")

fumitoh avatar Aug 22 '24 17:08 fumitoh

I like the imported module approach as it creates a clear split between regular Python functions and modelx grid functionality.

AB-Athene avatar Aug 22 '24 17:08 AB-Athene

You mean by "the import module approach" the way you import modules by new_module?

fumitoh avatar Aug 22 '24 17:08 fumitoh

yes

AB-Athene avatar Aug 22 '24 18:08 AB-Athene

This is a new feature. The import module approach is still available. I'll blog about pros and cons after introducing this.

fumitoh avatar Aug 22 '24 18:08 fumitoh

Completed with the release of modelx v0.27.0

fumitoh avatar Aug 25 '24 07:08 fumitoh