AlgebraicJulia / StockFlow.jl

https://algebraicjulia.github.io/StockFlow.jl/
MIT License
63 stars 6 forks source link

Nstratify good #87

Closed neonWhiteout closed 11 months ago

neonWhiteout commented 1 year ago
chain_stratify = @n_stratify l_type l_type l_type l_type begin

        :stocks
        [(pop,), (pop,), (pop,)] => pop

        :parameters
        [(μ,), (μ,), (μ,)] => μ
        [(δ,), (δ,), (δ,)] => δ
        [(rFstOrder,), (rFstOrder,), (rFstOrder,)] => rFstOrder
        [(rage,), (rage,), (rage,)] => rage

        :dynamic_variables
        [(v_aging,), (v_aging,), (v_aging,)] => v_aging
        [(v_fstOrder,), (v_fstOrder,), (v_fstOrder,)] => v_fstOrder
        [(v_birth,), (v_birth,), (v_birth,)] => v_birth
        [(v_death,), (v_death,), (v_death,)] => v_death

        :flows
        [(f_aging,), (f_aging,), (f_aging,)] => f_aging
        [(f_fstOrder,), (f_fstOrder,), (f_fstOrder,)] => f_fstOrder
        [(f_birth,), (f_birth,), (f_birth,)] => f_birth
        [(f_death,), (f_death,), (f_death,)] => f_death

        :sums
        [(N,), (N,), (N,)] => N

end

image

I don't like this syntax, but it works, and it's really easy to deal with.

Anyways, still need to write a lot of tests, but both normal stratification and stratification of arbitrary length seems to work.

neonWhiteout commented 1 year ago
 age_weight_6 = @n_stratify WeightModel ageWeightModel l_type begin

        :flows
        [~Death, ~Death] => f_death
        [~id, ~aging] => f_aging 
        [~Becoming, ~id] => f_fstOrder
        [_, f_NB] => f_birth

        :dynamic_variables
        [v_NewBorn, v_NB] => v_birth
        [~Death, ~Death] => v_death
        [~id, (v_agingCA, v_agingAS)] => v_aging
        [(v_BecomingOverWeight, v_BecomingObese), (v_idC, v_idA, v_idS)] => v_fstOrder

        :parameters
        [μ, μ] => μ
        [(δw, δo), (δC, δA, δS)] => δ
        [(rw, ro), r] => rFstOrder
        [rage, (rageCA, rageAS)] => rage

    end

is equivalent to

     age_weight_3 =  @stratify WeightModel l_type ageWeightModel begin

        :flows
        f_NewBorn => f_birth <= f_NB
        ~Death => f_death <= ~Death
        ~id => f_aging <= ~aging
        ~Becoming => f_fstOrder <= ~id

        :dynamic_variables
        v_NewBorn => v_birth <= v_NB
        ~Death => v_death <= ~Death
        ~id  => v_aging <= ~aging
        ~Becoming => v_fstOrder <= ~id

        :parameters
        μ => μ <= μ
        ~δ => δ <= ~δ
        rage => rage <= rageCA, rageAS
        _ => rFstOrder <= _

    end 

is equivalent to


    l_type_noatts = map(l_type, Name=NothingFunction, Op=NothingFunction, Position=NothingFunction);

  begin
        s, = parts(l_type, :S)
        N, = parts(l_type, :SV)
        lsn, = parts(l_type, :LS)
        f_aging, f_fstorder, f_birth, f_death = parts(l_type, :F)
        i_aging, i_fstorder, i_birth = parts(l_type, :I)
        o_aging, o_fstorder, o_death = parts(l_type, :O)
        v_aging, v_fstorder, v_birth, v_death = parts(l_type, :V)
        lv_aging1, lv_fstorder1, lv_death1 = parts(l_type, :LV)
        lsv_birth1, = parts(l_type, :LSV)
        p_μ, p_δ, p_rfstOrder, p_rage = parts(l_type, :P)
        lpv_aging2, lpv_fstorder2, lpv_birth2, lpv_death2 = parts(l_type, :LPV)
    end;

    typed_WeightModel=ACSetTransformation(WeightModel, l_type_noatts,
      S = [s,s,s],
      SV = [N],
      LS = [lsn,lsn,lsn],   
      F = [f_birth, f_death, f_fstorder, f_death, f_fstorder, f_death, f_aging, f_aging, f_aging],    
      I = [i_birth, i_aging, i_fstorder, i_aging, i_fstorder, i_aging], 
      O = [o_death, o_fstorder, o_aging, o_death, o_fstorder, o_aging, o_death, o_aging],
      V = [v_birth, v_death, v_fstorder, v_death, v_fstorder, v_death, v_aging, v_aging, v_aging],
      LV = [lv_death1, lv_fstorder1, lv_death1, lv_fstorder1, lv_death1, lv_aging1, lv_aging1, lv_aging1],
      LSV = [lsv_birth1],
      P = [p_μ, p_δ, p_rfstOrder, p_rfstOrder, p_δ, p_rage],
      LPV = [lpv_birth2, lpv_death2, lpv_fstorder2, lpv_death2, lpv_fstorder2, lpv_death2, lpv_aging2, lpv_aging2, lpv_aging2],
      Name=NothingFunction, Op=NothingFunction, Position=NothingFunction
    );
    @assert is_natural(typed_WeightModel);

    typed_ageWeightModel=ACSetTransformation(ageWeightModel, l_type_noatts,
      S = [s,s,s],
      SV = [N],
      LS = [lsn,lsn,lsn],   
      F = [f_birth, f_fstorder, f_death, f_aging, f_fstorder, f_death, f_aging, f_fstorder, f_death],    
      I = [i_birth, i_fstorder, i_aging, i_fstorder, i_aging, i_fstorder], 
      O = [o_fstorder, o_death, o_aging, o_fstorder, o_death, o_aging, o_fstorder, o_death],
      V = [v_birth, v_death, v_fstorder, v_aging, v_death, v_fstorder, v_aging, v_death, v_fstorder],
      LV = [lv_death1, lv_fstorder1, lv_aging1, lv_death1, lv_fstorder1, lv_aging1, lv_death1, lv_fstorder1],
      LSV = [lsv_birth1],
      P = [p_μ, p_δ, p_δ, p_δ, p_rage, p_rage, p_rfstOrder],
      LPV = [lpv_birth2, lpv_death2, lpv_fstorder2, lpv_aging2, lpv_death2, lpv_fstorder2, lpv_aging2, lpv_death2, lpv_fstorder2],
      Name =NothingFunction, Op=NothingFunction, Position=NothingFunction
    );
    @assert is_natural(typed_ageWeightModel);

    aged_weight = pullback(typed_WeightModel, typed_ageWeightModel) |> apex |> rebuildStratifiedModelByFlattenSymbols;
neonWhiteout commented 1 year ago

I think I'm going to try holding off on any further edits on this until I hear from @Xiaoyan-Li

neonWhiteout commented 11 months ago

Passes test on my computer, if it passes here should be able to merge.

neonWhiteout commented 11 months ago

ok @Xiaoyan-Li the remaining tests are the notebooks which shouldn't have been modified so hopefully it's good at this point