public FitnessEstimator(List<int> R0, List<int> Rb, bool increase, int n, IIntFCACalculator<FluxPattern> fca_solver, LinkedList<FluxPattern> witnesses, FluxPattern max, FluxPattern frev, IIntCoupling coupling) { this.R0 = R0; this.Rb = Rb; this.increase = increase; this.rand = new Random(); this.n = n; this.fca_solver = fca_solver; this.witnesses = witnesses; this.MAX = max; this.FREV = frev; this.coupling = coupling; }
public static LinkedListCoupling Uncompress(IIntCoupling coupled, List<List<int>> classes, double[] multiplicators) { int n = multiplicators.Length; LinkedListCoupling res = new LinkedListCoupling(n); ICollection<int> coupling_i; for (int k = 0; k < coupled.Length; ++k) { coupling_i = coupled[k]; foreach (int l in coupling_i) { foreach (int i in classes[k]) foreach (int j in classes[l]) if (!coupled[l].Contains(k)) res.Add(new Couple(i, j)); else if (k < l) res.Add(new PartialCouple(i, j)); } } Console.WriteLine("({0})\tCompressed couples uncompressed.\n", System.DateTime.Now); foreach (List<int> cl in classes) for (int i = 0; i < cl.Count; ++i) for (int j = i + 1; j < cl.Count; ++j) res.Add(new FullCouple(cl[i], cl[j], multiplicators[cl[j]], multiplicators[cl[i]])); Console.WriteLine("({0})\tFully coupled pairs added.\n", System.DateTime.Now); return res; }
public static void Optimize(double[,] S, bool[] rev, bool increase, LinkedList<FluxPattern> witnesses, FluxPattern max, FluxPattern frev, IIntCoupling coupling, List<int> R0, List<int> Rb, bool lp, double max_value, double tolerance, bool output, int population_size, int generations, double recomb_rate, double mut_rate_act, double mut_rate_deact, double start_density, out LinkedList<String> dot_tree) { IIntFCACalculator<FluxPattern> fcacalculator = lp ? (IIntFCACalculator<FluxPattern>)new LPIntFCACalculator(S, rev, max_value, tolerance) : (IIntFCACalculator<FluxPattern>)new MILPIntFCACalculator(S, rev, max_value, tolerance); FitnessEstimator fe = new FitnessEstimator(R0, Rb, increase, rev.Length, fcacalculator, witnesses, max, frev, coupling); List<int> approximation = null; FluxPattern max_approx = null; if (population_size > 0) { Population population = new Population(fe, population_size, recomb_rate, mut_rate_act, mut_rate_deact, start_density); for (int i = 1; i < generations; ++i) population.evolve(); Individual best = population.FittestIndividual; if (best.Fitness < 0) { approximation = best.Genes; max_approx = best.Max; } } OptimizationTree tree = new OptimizationTree(fe, R0, Rb, approximation, max_approx, output); dot_tree = tree.DotTree; LinkedList<List<int>> opt_solutions = tree.OptGenes; Console.WriteLine("\n\n\nThere are {0} optimal solutions.\n\t{1} reactions are unblocked.\n\tYou have to use {2} drugs.", opt_solutions.Count, tree.OptMaxSize, tree.OptRSize); if (opt_solutions.Count > 0) { Individual opt = new Individual(opt_solutions.First.Value, fe, new EvolutionParams()); Console.WriteLine("\n\tOne optimal solution is: {0}", opt); } Console.WriteLine("\n\tI had to calculate {0} out of {1} maxima to find the optima.", tree.NumberNodes, ((long)1) << Rb.Count()); }
public static void Analyze(double[,] matrix, bool[] rev, bool[] trans, LinkedList<FluxPattern> forbidden_combinations, double max_value, double tolerance, bool lp, bool fc, bool do_efca, bool keep_witnesses, bool only_internal, bool show_output, out FluxPattern max, out IIntCoupling coupling, out EFCACount efca, out int reactions_unblocked, out int reactions_frev, out int couples, out ICollection<FluxPattern> circuits, out DateTime time_start, out DateTime time_unblocked, out DateTime time_frev, out DateTime time_couples, out DateTime time_efca, out DateTime[] time_reaction, out int lps_unblocked, out int lps_frev, out int lps_couples, out int lps_efca, out int[] lps_reaction, out int wits_unblocked, out int wits_frev, out int wits_couples, out int wits_efca, out int[] wits_reaction, out int[] wits_distribution, out int[] wits_usable, String name = null) { //initialisation time_start = DateTime.Now; efca = null; wits_usable = null; int n = rev.Length; List<int> irr = new List<int>(); for (int i = 0; i < n; ++i) if (!rev[i]) irr.Add(i); LinkedList<FluxPattern> witnesses = new LinkedList<FluxPattern>(); LinkedList<FluxPattern> blocking_witnesses, frev_witnesses; IIntFCACalculator<FluxPattern> fcacalculator = !lp ? (IIntFCACalculator<FluxPattern>)new MILPIntFCACalculator(matrix, rev, max_value, tolerance) : !fc ? (IIntFCACalculator<FluxPattern>)new LPIntFCACalculator(matrix, rev, max_value, tolerance) : forbidden_combinations == null || forbidden_combinations.Count == 0 ? (IIntFCACalculator<FluxPattern>)new FCIntFCACalculator(matrix, rev, trans, max_value, tolerance) : (IIntFCACalculator<FluxPattern>)new FastFCIntFCACalculator(matrix, rev, forbidden_combinations, max_value, tolerance); circuits = (fc && forbidden_combinations == null) ? ((FCIntFCACalculator) fcacalculator).Circles : null; // calculate blocked reactions FluxPattern calcs; int wits_used; bool[] internal_reactions = new bool[n]; for(int i = 0; i<n; ++i) internal_reactions[i] = !trans[i]; FluxPattern pattern_internal = new FluxPattern(internal_reactions, false); max = fcacalculator.CalculateMax(new List<int>(), n, new LinkedList<FluxPattern>(), out blocking_witnesses, out calcs, only_internal ? pattern_internal : null, null); reactions_unblocked = max.Count; foreach (FluxPattern a in blocking_witnesses) if (a.Count > 0) witnesses.AddLast(a); if (name != null) SaveStatistics(witnesses, n, name + "_max.stats"); time_unblocked = DateTime.Now; lps_unblocked = fcacalculator.SolverCalls; wits_unblocked = witnesses.Count; if (show_output) Console.WriteLine("({0})\tBlocked reactions calculated: {1} of {2} blocked.\n", time_unblocked, n-reactions_unblocked, n); // calculate fully reversible reactions FluxPattern frev = fcacalculator.CalculateMax(irr, n, blocking_witnesses, out frev_witnesses, out calcs, max, null); reactions_frev = frev.Count; foreach (FluxPattern a in frev_witnesses) if (a.Count > 0) witnesses.AddLast(a); if (name != null) SaveStatistics(witnesses, n, name + "_frev.stats"); time_frev = DateTime.Now; lps_frev = fcacalculator.SolverCalls - lps_unblocked; wits_frev = witnesses.Count - wits_unblocked; if (show_output) Console.WriteLine("({0})\tFRev calculated.\n", time_frev); // FCA Console.WriteLine("Witnesses so far:"); foreach (FluxPattern a in witnesses) Console.WriteLine("\t{0}", a); Console.WriteLine("\nStarting FCA.\n"); coupling = new IntCoupling(fcacalculator, show_output, witnesses, out time_reaction, out lps_reaction, out wits_reaction, out wits_used, max, frev); if (name != null) SaveStatistics(coupling.Witnesses, n, name + "_fca.stats"); time_couples = DateTime.Now; wits_couples = coupling.Witnesses.Count - wits_frev - wits_unblocked; lps_couples = 0; wits_couples = 0; for (int i = 0; i < n; ++i) { lps_couples += lps_reaction[i]; wits_couples += wits_reaction[i]; } if (show_output) Console.WriteLine("({0})\tCouples calculated.\n", time_couples); wits_distribution = new int[n + 1]; foreach (FluxPattern a in coupling.Witnesses) wits_distribution[a.Count]++; // EFCA lps_efca = 0; wits_efca = 0; if (do_efca) { efca = new EFCACount(fcacalculator, keep_witnesses, coupling.Witnesses, max, frev, out time_reaction, out lps_reaction, out wits_reaction, out wits_usable, coupling, null, show_output); lps_efca = efca.LPCount; wits_efca = efca.WitnessCount; } time_efca = DateTime.Now; couples = coupling.ToCoupling().Count; }
/// <summary> /// Does the FCA for the given network and saves the result in a new IntCoupling object. /// </summary> /// <param name="n">#reactions in the network.</param> /// <param name="calculator">An object that calculates the maximal set of reactions in the metabolic network constrained by disabled reactions.</param> /// <param name="witnesses">A subset of the flux lattice L.</param> /// <param name="max">The maximum of L / the set of all unblocked reactions in the metabolic network.</param> /// <param name="frev">The set of all fully reversible reactions in the metabolic network.</param> /// <param name="R">A set $R \subseteq [n]$ of disabled reactions.</param> /// <param name="coupling">Flux coupling we already know about.</param> /// <param name="start">This constructor calculated the flux coupling sets $C_i$ for $i \geq start$.</param> public IntCoupling(IIntFCACalculator<FluxPattern> calculator, bool show_output, ICollection<FluxPattern> witnesses, out DateTime[] time_reaction, out int[] lps_reaction, out int[] wits_reaction, out int wits_used, FluxPattern max, FluxPattern frev = null, List<int> R = null, IIntCoupling coupling = null, int start = 0) { this.max = max; this.n = max.Length; this.witnesses = new LinkedList<FluxPattern>(); time_reaction = new DateTime[n]; lps_reaction = new int[n]; wits_reaction = new int[n]; int lps_count = calculator.SolverCalls; this.maxima = new FluxPattern[n]; coupled = new LinkedList<int>[n]; for (int i = 0; i < n; ++i) { coupled[i] = new LinkedList<int>(); coupled[i].AddLast(i); } if (R == null) R = new List<int>(); LinkedList<FluxPattern> new_witnesses = new LinkedList<FluxPattern>(witnesses); witnesses = new LinkedList<FluxPattern>(); bool allowed; foreach (FluxPattern w in new_witnesses) { allowed = true; for (int i = 0; allowed && i < R.Count; ++i) allowed = !w[R[i]]; if (allowed) witnesses.Add(w); } wits_used = witnesses.Count; if (coupling == null) coupling = this; FluxPattern opt, calcs; R.Add(-1); ICollection<int> c; for (int i = start; i < n; ++i) if (maxima[i] == null) { if (max[i]) { R[R.Count - 1] = i; opt = calculator.CalculateMax(R, n, witnesses, out new_witnesses, out calcs, max, frev, coupling); lps_reaction[i] = calculator.SolverCalls - lps_count; lps_count = calculator.SolverCalls; c = coupling[i]; foreach (int r in c) if (r >= i && coupling.Max(r) != null && !coupling.Max(r)[i]) { maxima[r] = opt; for (int j = 0; j < n; ++j) if (max[j] && !maxima[r][j] && r != j) coupled[r].AddLast(j); } if (maxima[i] == null) { maxima[i] = opt; if (maxima[i] == null) Console.WriteLine("Fehler!"); else for (int j = 0; j < n; ++j) if (!maxima[i][j] && max[j] && i != j) coupled[i].AddLast(j); } foreach (FluxPattern a in new_witnesses) if (a.Count > 0) { this.witnesses.AddLast(a); witnesses.Add(a); wits_reaction[i]++; } time_reaction[i] = DateTime.Now; if (show_output) Console.WriteLine("({0})\tCoupling {1} from {2} calculated. ({3} LPs solved, {4} kept.)\n", time_reaction[i], i + 1, n, lps_reaction[i], witnesses.Count); } else maxima[i] = max; } R.RemoveAt(R.Count - 1); }
/// <summary> /// Simulates all $\choose n 2$ double reaction knock-outs and creates a new EFCACount object for the results. /// </summary> /// <param name="calculator">An object that calculates the maximal set of reactions in the metabolic network constrained by disabled reactions.</param> /// <param name="witnesses">A subset of the flux lattice L.</param> /// <param name="max">The maximum of L / the set of all unblocked reactions in the metabolic network.</param> /// <param name="frev">The set of all fully reversible reactions in the metabolic network.</param> /// <param name="coupling">A flux coupling for the network</param> public EFCACount(IIntFCACalculator<FluxPattern> calculator, bool keep_witnesses, ICollection<FluxPattern> witnesses, FluxPattern max, FluxPattern frev, out DateTime[] time_reaction, out int[] lps_reaction, out int[] wits_reaction, out int[] wits_usable, IIntCoupling coupling = null, ICollection<int> T = null, bool show_output = true) { this.max = max; this.n = max.Length; time_reaction = new DateTime[n]; lps_reaction = new int[n]; for (int i = 0; i < n; ++i) lps_reaction[i] = -1; wits_reaction = new int[n]; wits_usable = new int[n]; DateTime[] time; int[] lps, wits; int temp_counter = calculator.SolverCalls, temp_wits; if (coupling == null) { coupling = new IntCoupling(calculator, false, witnesses, out time, out lps, out wits, out temp_wits, max, frev); this.witness_counter = coupling.Witnesses.Count; this.lp_counter = calculator.SolverCalls - temp_counter; temp_counter = calculator.SolverCalls; } if (T == null) { this.T = new List<int>(n); for (int i = 0; i < n; ++i) this.T.Add(i); } else this.T = new List<int>(T); LinkedList<FluxPattern> new_witnesses = new LinkedList<FluxPattern>(); witnesses_kept = new LinkedList<FluxPattern>(witnesses); IIntCoupling row; max_size = new int[n, n]; targeted_size = new int[n, n]; for (int i = 0; i < n; ++i) { max_size[i, i] = -1; targeted_size[i, i] = -1; } List<int> R = new List<int>(); ICollection<int> c; int wits_used; R.Add(-1); for (int i = 0; i < n; ++i) if (max[i] && max_size[i, i] < 0) { R[0] = i; if (keep_witnesses) { foreach (FluxPattern a in new_witnesses) if (rand.NextDouble() < ((double)a.Count) / witnesses_kept.Count) witnesses_kept.AddLast(a); } row = new IntCoupling(calculator, false, witnesses_kept, out time, out lps, out wits, out wits_used, max, frev, R, coupling, i); wits_usable[i] = wits_used; if (show_output) Console.WriteLine("({0})\tRow {1} from {2} calculated of EFCA. ({3} LPs solved, {4} kept.)\n", System.DateTime.Now, i + 1, n, calculator.SolverCalls - temp_counter, witnesses_kept.Count); this.lp_counter += calculator.SolverCalls - temp_counter; temp_counter = calculator.SolverCalls; new_witnesses = row.Witnesses; this.witness_counter += row.Witnesses.Count; c = coupling[i]; foreach (int r in c) if (r >= i && !coupling.Max(r)[i]) for (int j = r; j < n; ++j) if (max[j]) { max_size[r, j] = row.Max(j).Count; max_size[j, r] = max_size[r, j]; targeted_size[r, j] = row.Max(j).Values.Intersect(this.T).Count(); targeted_size[j, r] = targeted_size[r, j]; } lps_reaction[i] = lp_counter; wits_reaction[i] = witness_counter; time_reaction[i] = DateTime.Now; } if (show_output) Console.WriteLine("({0})\tEFCA uncompressed.\n", System.DateTime.Now); }