Ejemplo n.º 1
0
		/// <summary>
		/// main init <see cref="lifemix.lifereactor"/> class using seed+fraction to eliminate 
		/// rules and add them to the reactor.
		/// </summary>
		/// <param name="tm"> max type </param>
		/// <param name="sm"> max state </param>
		/// <param name="f"> fraction (0 < x < 1.0) </param>
		/// <param name="s"> seed for replay </param>
		public lifereactor (int tm, int sm, double f, int s, double r)
		{
			/// init members
			tmax = tm;
			smax = sm;
			fraction = f;
			seed = s;

			/// then init random generator 
			rd = new Random (seed);

			/// create reactor
			rec = new reactor ();

			int idx = 0;
			

			/// prepare the loop for all possible reaction 
			/// and thin it according to fraction 
			for (int t1 = 0; t1 < tmax; t1++) {
				for (int s1 = 0; s1 < smax; s1++) {
					for (int t2 = 0; t2 < tmax; t2++) {
						for (int s2 = 0; s2 < smax; s2++) {
							for (int s3 = 0; s3 < smax; s3++) {
								for (int s4 = 0; s4 < smax; s4++) {
									for (int c = 0; c < 2; c++) {
										for (int p = 0; p < 2; p++) {
											if ((s1 != s3) || (s2 != s4) || (c != p)) {
												double o = rd.NextDouble ();
												if (o < fraction) {												
													var ru = new b_rule (c == 1, t1 + 1, s1, t2 + 1, s2, p == 1, t1 + 1, s3, t2 + 1, s4, idx, r);
													idx++;
													rec.add_rule (ru);
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}

		/*	for (int t1 = 0; t1 < tmax; t1++) {
				for (int s1 = 0; s1 < smax; s1++) {
					for (int t2 = 0; t2 < tmax; t2++) {
						for (int s2 = 0; s2 < smax; s2++) {
							double o = rd.NextDouble ();
							if (o < fraction) {
								var ru = new m_rule (t1, s1, t2, s2, false, r, ++idx);
								rec.add_rule (ru);
							}
						}
					}
				}
			}
			*/	
		}
Ejemplo n.º 2
0
		public  bool apply(reactor rec,int pa){


			particle a = rec.get_particle_id(pa);
			//Console.WriteLine("a: " + a);
			rec.remove_particle (a);
			return true;
		}
Ejemplo n.º 3
0
		public static void graph(reactor rec, int seed, double lambda, int nb){
			rec.export_graphviz ("out_" + seed + "_" + lambda + "_" + nb + ".dot");
			// rec.export_particles_list ("part.rec");

			var g = rec.create_graph ();

			IDictionary<int,int> components = new Dictionary<int,int> ();
			int count = g.WeaklyConnectedComponents (components);

			Console.Write (count + " " + rec.npart + " " + ((double)count) / (rec.npart));
			List<particle>[] u = new List<particle>[count];
			for (int i = 0; i < count; i++)
				u [i] = new List<particle> ();

			foreach (KeyValuePair<int,int> kv in components) {			
				u [kv.Value].Add (rec.get_particle_id (kv.Key));				
			}

			int tk = 0;
			for (int i = 0; i < count; i++)
				tk += u [i].Count;
			Console.WriteLine (" " + ((float)tk) / count);
			for (int i = 0; i < count; i++) {
				List<particle> l = u [i];
				particle p = l.Find (x => x.st.type == 5);
				if (p != null) {

					particle current = p;

					bool done = true;
					while (done) {
						Console.Write (current.st);
						l.Remove (current);
						if (current.linked.Count > 0) {
							int nc = current.linked.Count;
							int z = 0;
							while (z < nc && !l.Contains (rec.get_particle_id (current.linked [z]))) {
								z++;
							}
							if (z == nc)
								done = false;
							else {
								current = rec.get_particle_id (current.linked [z]);							
							}
						}				
					}
					Console.WriteLine ("");
				} else {
					foreach (particle r in l) {
						Console.Write (r.st);
					}
					Console.WriteLine ("");
				}
			}


		}
Ejemplo n.º 4
0
		public  bool apply(reactor rec,int pa){


			particle a = rec.get_particle_id(pa);

			type_state old_a = a.st;
			type_state new_a = end;
			if (old_a != new_a)
				rec.update_new_state (a, old_a, new_a);
		

			if (unlink)
				rec.all_unlink (a.id);

			return true;
		}
Ejemplo n.º 5
0
		public void TestReactor ()
		{
			
			reactor rec = new reactor ();

			/// new configuration tests
			Assert.AreEqual (0, rec.particles.Count);
			Assert.AreEqual (0, rec.rules.Count);

			int t = 0, s = 1;
			type_state key = new type_state(t,s);
			particle p = new particle (t, s);
			rec.add_particle (p);

			// test add particles 
			Assert.AreEqual (1, rec.particles.Count);
			Assert.IsTrue(rec.stDict.ContainsKey(key));
			Assert.AreEqual (1, rec.stDict [key].Count);

			// check link -> no links 
			Assert.AreEqual (0, rec.pDict.Count);

			// double add 
			rec.add_particle (p);
			Assert.AreEqual (1, rec.particles.Count);


			// two particles 
			particle q = new particle (t, s);


			// test add 
			rec.add_particle (q);
			Assert.AreEqual (2, rec.particles.Count);

			// test dictionnaries 
			Assert.IsTrue(rec.stDict.ContainsKey(key));
			Assert.AreEqual (2, rec.stDict [key].Count);

			// test remove 

			rec.remove_particle (q);

			Assert.AreEqual (1, rec.particles.Count);
			Assert.IsTrue(rec.stDict.ContainsKey(key));
			Assert.AreEqual (1, rec.stDict [key].Count);


			rec.remove_particle (p);

			Assert.AreEqual (0, rec.particles.Count);
			Assert.IsTrue(rec.stDict.ContainsKey(key));
			Assert.AreEqual (0, rec.stDict [key].Count);


			// test link 
			rec.add_particle (q);
			rec.add_particle (p);

			Assert.AreEqual (2, rec.particles.Count);
			Assert.IsTrue(rec.stDict.ContainsKey(key));
			Assert.AreEqual (2, rec.stDict [key].Count);

			// create pair part 

			pair_st pst = new pair_st (p, q);
			pair_st pstn = new pair_st (p.st, q.st);

			Assert.AreEqual (pst.a, pstn.a);
			Assert.AreEqual (pst.b, pstn.b);


			rec.link (p, q);

			Assert.AreEqual (2, rec.particles.Count);
			Assert.IsTrue(rec.stDict.ContainsKey(key));
			Assert.AreEqual (2, rec.stDict [key].Count);

			Assert.IsTrue (rec.pDict.ContainsKey (pst));
			Assert.AreEqual (1, rec.pDict[pst].Count);

		}
Ejemplo n.º 6
0
		public static void Main (string[] args)
		{


			int seed = Int32.Parse (args [0]);
			double lambda = double.Parse (args [1]);
			int nb = Int32.Parse (args [2]);
			//double kappa = double.Parse (args [3]);

			string js_name = args [3];
			string json;
			using (System.IO.StreamReader r = new System.IO.StreamReader (js_name)) {
				json = r.ReadToEnd ();
			}
			
			ensemble e = JsonConvert.DeserializeObject<ensemble> (json);


			reactor rec = new reactor ();
			for(int x = 0; x < nb; x++)
				rec.insert_ensemble (e);


			var lr0 = b_rule.generate_rules (7, false, 5, 8, 5, 0, true, 4, 3,1,lambda);
			var lr1 = b_rule.generate_rules (7, true, -1, 4, -2, 1, true, 2, 5,2,lambda);
			var lr2 = b_rule.generate_rules (7, false, -1, 5, -1, 0, true, 7, 6,3,lambda);
			var lr3 = b_rule.generate_rules (7, false, -1, 3, -2, 6, true, 2, 13, 4,lambda);
			var lr4 = b_rule.generate_rules (7, true, -1, 7, -2, 13, true, 4, 3,5,lambda);
			var lr5 = b_rule.generate_rules (7, true, 6, 4, 6, 3, false, 8, 8,6,lambda);
			var lr6 = b_rule.generate_rules (7, true, -1, 2, -2, 8, true, 9, 1,7,lambda);
			var lr7 = b_rule.generate_rules (7, true, -1, 9, -2, 9, false, 8, 8, 8,lambda);

			foreach(b_rule r in lr0) rec.add_rule(r);
			foreach(b_rule r in lr1) rec.add_rule(r);		
			foreach(b_rule r in lr2) rec.add_rule(r);
			foreach(b_rule r in lr3) rec.add_rule(r);
			foreach(b_rule r in lr4) rec.add_rule(r);
			foreach(b_rule r in lr5) rec.add_rule(r);
			foreach(b_rule r in lr6) rec.add_rule(r);
			foreach(b_rule r in lr7) rec.add_rule(r);

			double lambda_p = 1e-6;
			for (int i = 1; i < 7; i++) {
				var er0 = new o_rule (i, 0, lambda_p); 
				rec.add_rule (er0);
			}

			double lambda_d = 1e-7;
			for (int i = 1; i < 7; i++) {
				for (int j = 0; j < 1; j++) {
					
					var er0 = new d_rule (i, j, lambda_d, 0);
					rec.add_rule (er0);
				}
			}
		

			for (int i = 0; i < 10; i++) {

				rec.add_particle (new particle (1, 0));
				rec.add_particle (new particle (2, 0));
				rec.add_particle (new particle (3, 0));
				rec.add_particle (new particle (4, 0)); 
				rec.add_particle (new particle (5, 0));
				rec.add_particle (new particle (6, 0));
			}

			double tau;
			double total_time = 0.0;
			Random rd = new Random (seed);
			int step = 0;
			int idx;
			int ndiv = 0;

			while (rec.gillespie_step (rd,out tau, out idx)) {

				total_time +=tau;
				step++;
				int npart = rec.particles.Count;
				if (npart>100000)
					break;

				Console.WriteLine (step+ " " + npart + " " + ndiv + " " + total_time + " " + idx + " " + rec.get_rule(idx).idx + " " +  rec.get_rule(idx));
				if (idx == 245)
					ndiv ++;

				if (step % 1000 == 0) {
					var g = rec.create_graph ();
					IDictionary<int,int> components = new Dictionary<int,int> ();
					int count = g.WeaklyConnectedComponents (components);
					List<particle>[] u = new List<particle>[count];
					for (int i = 0; i < count; i++)
						u [i] = new List<particle> ();

					foreach (KeyValuePair<int,int> kv in components) {			
						u [kv.Value].Add (rec.get_particle_id (kv.Key));				
					}

					using(System.IO.StreamWriter str = new System.IO.StreamWriter("out"+step+".out")){				
						for (int i = 0; i < count; i++)					
							str.Write(u [i].Count + " ");
					}
					rec.export_graphviz ("out_" + seed + "_" + lambda + "_" + nb + "_" + step + ".dot");
				}

				
				
			}						
		/*	var g = rec.create_graph ();

			IDictionary<int,int> components = new Dictionary<int,int> ();
			int count = g.WeaklyConnectedComponents (components);
			List<particle>[] u = new List<particle>[count];
			for (int i = 0; i < count; i++)
				u [i] = new List<particle> ();

			foreach (KeyValuePair<int,int> kv in components) {			
				u [kv.Value].Add (rec.get_particle_id (kv.Key));				
			}

			using(System.IO.StreamWriter str = new System.IO.StreamWriter("out.out")){				
				for (int i = 0; i < count; i++)					
					str.Write(u [i].Count + " ");
			}

			int tk = 0;
			for (int i = 0; i < count; i++)
				tk += u [i].Count;
				*/

/*			Console.Write(lambda + " " + nb + " " + kappa + " " + seed + " " + step+" " + total_time+ " " + count + " " + rec.npart + " " + ((double)count) / (rec.npart));
			Console.Write (" " + ndiv);
			Console.WriteLine (" " + ((float)tk) / count);
*/
			rec.export_graphviz ("out_" + seed + "_" + lambda + "_" + nb + ".dot");
		}
Ejemplo n.º 7
0
		public  override bool apply_rule(reactor rec, Random rd){

			particle o = new particle (target_type, target_state);
			rec.add_particle(o);
			return true;
		}
Ejemplo n.º 8
0
		public override double propensity(reactor rec){

			return propensity (rec.stDict, rec.pDict);
		}
Ejemplo n.º 9
0
		public override bool apply_rule(reactor rec, Random rd){

			var ret = pick_reactants (rec.stDict, rec.pDict, rd);
			if (ret.id1 == -1)
				return false;
			return apply (rec, ret);
		}
Ejemplo n.º 10
0
		public bool apply(reactor rec,pair_part ret){


			particle a = rec.get_particle_id(ret.id1);
			particle b = rec.get_particle_id(ret.id2);
			type_state old_a = a.st;
			type_state old_b = b.st;

			switch (function) {

			case 0:
			default: 
				return false;
			case 1: //link
				rec.link (a, b);
				break;
			case 2: // split 
				rec.unlink (a, b);
				break;
			case 3: // add 
				type_state new_a = new type_state(a.st.type,a.st.state+1);
				type_state new_b = new type_state(b.st.type,b.st.state+1);
				rec.update_new_state (a, old_a,new_a);
				rec.update_new_state (b, old_b,new_b);			
				break;
			case 4: // swap 
				rec.update_new_state (a, old_a,old_b);
				rec.update_new_state (b, old_b,old_a);	
				break;
			}

			return true;
		}
Ejemplo n.º 11
0
		public override bool apply_rule(reactor rec, Random rd){

			int pa = pick_reactants (rec.stDict, rec.pDict, rd);
			return apply (rec, pa);

		}
Ejemplo n.º 12
0
		public virtual bool apply_rule(reactor rec, Random rd){

			return false;
		}
Ejemplo n.º 13
0
		public virtual double propensity(reactor rec){

			//Console.WriteLine ("error");
			return 0.0;
		}			
Ejemplo n.º 14
0
		public override double propensity(reactor rec){
			var pDict = rec.pDict;
			var stDict = rec.stDict;

			pair_st st = reactants;

			if (reactionContact == true) {

				// looking for linked reactants as in pair 

				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;

				// counting the number of reactants 
				int na = stDict.ContainsKey (trireac) ? stDict [trireac].Count : 0;

				// we need to remove already linked 
				pair_st a1 = new pair_st(reactants.a, trireac);

				int rem = 0;
				if (pDict.ContainsKey (a1)) {
					foreach (pair_part p in pDict[a1]) {
						particle p1 = rec.get_particle_id(p.id1);
						particle p2 = rec.get_particle_id(p.id2);
						if (p1.st == trireac) {
							foreach (int id in p1.linked) {
								particle o = rec.get_particle_id (id);
								if (o.st == reactants.b)
									rem += 1;
							}							
						} else {
							foreach (int id in p2.linked) {
								particle o = rec.get_particle_id (id);
								if (o.st == reactants.b)
									rem += 1;
								;
							}
						}
					}
				}

				
				return rate * (count * na - rem); 

			}
			else {
				type_state a = st.a;
				type_state b = st.b;
				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;
				int ntot = na * nb - nc;

				if (ntot < 0)
					return 0.0;
								

				return rate * ntot;
			}
		}
Ejemplo n.º 15
0
		public bool apply(reactor rec,pair_part ret){


			particle a = rec.get_particle_id(ret.id1);
			particle b = rec.get_particle_id(ret.id2);
			type_state old_a = a.st;
			type_state old_b = b.st;
			//	Console.WriteLine ("A:" + (old_a.state == r.reactants.b.state));
			pair_st st = products;
			type_state new_a = st.a;
			type_state new_b = st.b;
			if (old_a != new_a)
				rec.update_new_state (a, old_a, new_a);
			if (old_b!=new_b)
				rec.update_new_state (b , old_b, new_b);				

			if (productContact)
				rec.link (a, b);
			else {
				a.unlink (b);
				rec.unlink (a, b);

			}

			return true;
		}