public override double propensity(Dictionary<type_state,List<int>> stDict, Dictionary<pair_st,List<pair_part>> pDict){ type_state a = new type_state(target_type,p1); type_state b = new type_state(target_type,p2); pair_st st = new pair_st(a,b); switch (function) { case 0: default: return 0.0; case 1: // link int na = stDict.ContainsKey (a) ? stDict [a].Count : 0; int nb = stDict.ContainsKey (b) ? stDict [b].Count : 0; int nc = pDict.ContainsKey (st) ? pDict [st].Count : 0; nc += pDict.ContainsKey (st.swap ()) ? pDict [st.swap ()].Count : 0; if (a.state == b.state && a.type == b.type) nb += -1; Console.WriteLine (na + " " + nb + " " + nc + " " + rate * (na * nb - nc)); if (nc > na * nb) return 0.0; return rate * (na * nb - nc); case 2: // split case 3: // add case 4: // swap int count = 0; if (pDict.ContainsKey (st)) count += pDict [st].Count; pair_st st_swap = st.swap (); if (pDict.ContainsKey (st_swap)) count += pDict [st_swap].Count; Console.WriteLine (count); return rate * count; } }
public pair_part pick_reactants(Dictionary<type_state,List<int>> stDict, Dictionary<pair_st,List<pair_part>> pDict,Random rd){ type_state a = new type_state(target_type,p1); type_state b = new type_state(target_type,p2); pair_st st = new pair_st(a,b); switch (function) { case 0: default: return new pair_part (-1, -1); case 1: // link var la = stDict [a]; var lb = stDict [b]; bool done = false; int tmx = 0; while (!done) { tmx++; int na = (int)(rd.NextDouble () * la.Count); int nb = (int)(rd.NextDouble () * lb.Count); int pa = la [na]; int pb = lb [nb]; if (pa != pb){ if (!pDict.ContainsKey (st)) { var ret = new pair_part (pa, pb); return ret; } else { pair_part pp = new pair_part (pa, pb); if (!pDict [st].Contains (pp) && !pDict [st].Contains (pp.swap ())) { var ret = new pair_part (pa, pb); return ret; } } } if (tmx > 100) done = true; } for (int xa = 0; xa < la.Count; xa++) for (int xb = 0; xb < lb.Count; xb++) { int pa = la [xa]; int pb = lb [xb]; pair_part pp = new pair_part (pa, pb); //Console.WriteLine (pp.id1+"-"+pp.id2 + " " + xa + " " + xb + " " + pDict [st].Contains (pp) + pDict [st].Contains (pp.swap ())); if (!pDict [st].Contains (pp) && !pDict [st].Contains (pp.swap ())) { var ret = new pair_part (pa, pb); return ret; } } int ra = stDict.ContainsKey (a) ? stDict [a].Count : 0; int rb = stDict.ContainsKey (b) ? stDict [b].Count : 0; int nc = pDict.ContainsKey (st) ? pDict [st].Count : 0; nc += pDict.ContainsKey (st.swap()) ? pDict [st.swap()].Count : 0; if (a.state == b.state && a.type ==b.type ) rb += -1; // Console.WriteLine ("early stop :"+a +" "+b+" " + ra + " " + rb + " " + nc + " " + (ra*rb -nc) ); return new pair_part (-1, -1); case 2: // split case 3: // add case 4: // swap double ur = rd.NextDouble (); int n = 0, u = 0, v = 0; if (pDict.ContainsKey (st)) { u = pDict [st].Count; n += u; } if (pDict.ContainsKey (st.swap ())) { v = pDict [st.swap ()].Count; n += v; } if (ur * n < u) return pDict [st] [(int)(ur * n)]; else { var m = pDict [st.swap ()]; return m [(int)(ur * n - u)].swap (); } } }