Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nstratify good #87

Merged
merged 24 commits into from
Sep 27, 2023
Merged

Conversation

neonWhiteout
Copy link
Collaborator

@neonWhiteout neonWhiteout commented Sep 6, 2023

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.

…ssv, etc to StockFlow.jl, several methods to Syntax.jl (incl infer_links, which is now exported), and a Stratification.jl file. Also created a Stratification tests file and added more tests to Syntax.
…ch are unmapped. Was previously two separate loops.
…ssary variables. Additional type annotations.
… and replaced a few sfstratify calls in the test file with the macro call.
…ant information. Contains dictionaries and vectors which are updated as it goes along.

Created macro n_stratify, which generalizes stratify and can take an arbitrary number of stockflows as arguments.  Still needs tests.

Updated tests to reflect changes in stratification functions.
… System Structure so instead of folding right, it checks all vops are equal (line 195)
…ally make sense to take the pullback of 0 items so removed that as an option. Now allow single symbols to be interpreted as a tuple with one element, so n_stratify is far less cumbersome to write for.
@neonWhiteout
Copy link
Collaborator Author

neonWhiteout commented Sep 7, 2023

 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
Copy link
Collaborator Author

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

@neonWhiteout neonWhiteout reopened this Sep 27, 2023
@neonWhiteout
Copy link
Collaborator Author

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

@neonWhiteout
Copy link
Collaborator Author

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

@Xiaoyan-Li Xiaoyan-Li merged commit 8d512cc into AlgebraicJulia:master Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants