public double CalculateFitness(List<int> R, out FluxPattern max, out List<int> R_trimmed, out bool too_many_genes, FluxPattern ub = null)
        {
            R_trimmed = R = new List<int>(R.Intersect(Rb));

            FluxPattern calcs;
            max = fca_solver.CalculateMax(R, n, witnesses, out new_witnesses, out calcs, ub == null ? MAX : ub, FREV, coupling);
            too_many_genes = max.Count == 0;

            foreach (FluxPattern a in new_witnesses)
                if (rand.NextDouble() < ((double)a.Count) / witnesses.Count)
                    witnesses.AddLast(a);

            return R.Count + (increase ? (n + 1) : -(n + 1)) * (max.Count - (n + 1) * R0.Intersect(max.Values).Count()) + 0.1 * rand.NextDouble();
        }
        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;
        }
Ejemplo n.º 3
0
 public static FluxPattern Uncompress(FluxPattern a, int n, List<List<int>> classes)
 {
     bool[] pattern = new bool[n];
     ICollection<int> reactions = a.Values;
     foreach (int r in reactions)
         foreach (int i in classes[r])
             pattern[i] = true;
     return new FluxPattern(pattern, a.Reversible);
 }
 private void Update(List<int> R, FluxPattern max = null)
 {
     if ((fe.DoIncrease && max.Count < this.opt_max_size) || (!fe.DoIncrease && max.Count > this.opt_max_size))
     {
         this.opt_genes.Clear();
         this.opt_genes.AddFirst(R);
         this.opt_max_size = max.Count;
         this.opt_r_size = R.Count;
     }
     else if (max.Count == this.opt_max_size)
     {
         this.opt_genes.AddLast(R);
         this.opt_r_size = Math.Min(this.opt_r_size, R.Count);
     }
 }
        public OptimizationTree(FitnessEstimator fe, List<int> R0, List<int> Rb, List<int> approximation = null, FluxPattern max_approx = null, bool output = true)
        {
            this.dot_tree = new LinkedList<string>();

            this.opt_genes = new LinkedList<List<int>>();
            this.opt_max_size = fe.DoIncrease ? int.MaxValue : int.MinValue;
            this.opt_r_size = fe.DoIncrease ? int.MinValue : int.MaxValue;

            this.fe = fe;
            this.T = new List<int>(R0);
            this.D = new List<int>(Rb);

            bool[] allowed = new bool[fe.n];
            for (int i = 0; i < fe.n; ++i)
                allowed[i] = true;
            foreach (int r in R0)
                allowed[r] = false;
            this.max_allowed = new FluxPattern(allowed, false);

            if (approximation != null)
                Update(approximation, max_approx);

            this.output = output;
            this.n_updates = 0;

            List<int> reactions = new List<int>();
            for (int i = 0; i < Rb.Count; ++i)
                reactions.Add(i);
            this.order = reactions.OrderBy(x => System.Guid.NewGuid()).ToList();

            Optimize(fe.DoIncrease ? this.D : new List<int>(), 0);
        }
        private void Optimize(List<int> R, int i, FluxPattern upper_bound = null)
        {
            ++n_updates;
            String name = "";

            FluxPattern max, ub, lb;
            bool tmg;
            fe.CalculateFitness(R, out max, out R, out tmg, upper_bound);

            if (this.T.Intersect(max.Values).Count() == (fe.DoIncrease ? this.T.Count : 0))
            {
                Update(R, max);
                name = (String.Format("\"{0}\" [label = \"\\\\tiny${0}$\\n\\\\tiny\\\\newline \\n\\\\tiny${1}$\", shape = rectangle, style = \"filled\", fillcolor=\"0.4166667 0.6 1\"]", NodeName(R), (max.Count > 0) ? NodeName(max.Values) : "\\\\emptyset"));
            }
            else
            {
                name = (String.Format("\"{0}\" [label = \"\\\\tiny${0}$\\n\\\\tiny\\\\newline \\n\\\\tiny${1}$\", shape = rectangle, style = \"dotted,rounded\"]", NodeName(R), (max.Count > 0) ? NodeName(max.Values) : "\\\\emptyset"));

                if (fe.DoIncrease)
                {
                    if (max.Count < this.opt_max_size)
                    {

                        // filter reactions to check
                        bool[] queue = new bool[fe.n], coupling;
                        foreach (int l in R)
                            queue[l] = true;
                        for (int l = 0; l < i; ++l)
                            queue[l] = false;
                        foreach (int l in R) if (l < i)
                            {
                                coupling = fe.Coupling.Max(l).Incidence;
                                for (int k = 0; k < fe.n; ++k)
                                    queue[k] &= queue[k];
                            }
                        // branch & bound
                        List<int> fix = new List<int>(), next, temp;
                        for (int j = 0; j < fe.n; ++j)
                            if (queue[j])
                            {
                                fix.Clear();
                                foreach (int l in R) if (l < j - 1)
                                        fix.Add(l);
                                fe.CalculateFitness(fix, out ub, out temp, out tmg);
                                ++n_updates;

                                if (this.T.Intersect(ub.Values).Count() < this.T.Count)
                                {
                                    String break_witness = (String.Format("\"{2}\" [label = \"\\\\tiny${0}$\\n\\\\tiny\\\\newline \\n\\\\tiny${1}$\", shape = rectangle, style = \"dotted,filled\", fillcolor=\"0.08333334 0.6 1\"]", NodeName(fix), (ub.Count > 0) ? NodeName(ub.Values) : "\\\\emptyset", NodeName(R) + "-b"));
                                    this.dot_tree.AddLast(break_witness);
                                    this.dot_tree.AddLast(String.Format("\"{0}\" -> \"{1}\" [style=bold]", NodeName(R) + "-b", NodeName(R)));
                                    break;
                                }
                                else
                                {
                                    next = new List<int>();
                                    foreach (int l in R) if (l != j)
                                            next.Add(l);
                                    this.dot_tree.AddLast(String.Format("\"{0}\" -> \"{1}\"", NodeName(R), NodeName(next)));
                                    Optimize(next, j, ub);
                                }
                            }
                    }
                }
                else
                {
                    if (max.Count > this.opt_max_size)
                    {

                        // filter reactions to check
                        bool[] queue = new bool[fe.n], relaxation, coupling;
                        IEnumerable<int> inter = D.Intersect(max.Values);
                        foreach (int l in inter)
                            queue[l] = true;

                        // branch & bound
                        List<int> possible = new List<int>(), next, temp;
                        for (int j = i; j < fe.n; ++j)
                            if (queue[j])
                            {
                                relaxation = max.Incidence;
                                coupling = fe.Coupling.Max(j).Incidence;
                                for (int l = 0; l < fe.n; ++l)
                                    relaxation[l] &= coupling[l];
                                ub = new FluxPattern(relaxation, false);

                                possible.Clear();
                                foreach (int l in R)
                                    possible.Add(l);
                                foreach (int l in this.D)
                                    if (l >= j)
                                        possible.Add(l);

                                fe.CalculateFitness(possible, out lb, out temp, out tmg, ub);
                                ++n_updates;

                                if (this.T.Intersect(lb.Values).Count() > 0)
                                {
                                    String break_witness = (String.Format("\"{2}\" [label = \"\\\\tiny${0}$\\n\\\\tiny\\\\newline \\n\\\\tiny${1}$\", shape = rectangle, style = \"dotted,filled\", fillcolor=\"0.08333334 0.6 1\"]", NodeName(possible), (lb.Count > 0) ? NodeName(lb.Values) : "\\\\emptyset", NodeName(R) + "-b"));
                                    this.dot_tree.AddLast(break_witness);
                                    this.dot_tree.AddLast(String.Format("\"{0}\" -> \"{1}\" [style=bold]", NodeName(R) + "-b", NodeName(R)));
                                    break;
                                }
                                else
                                {
                                    next = new List<int>(R);
                                    next.Add(j);

                                    this.dot_tree.AddLast(String.Format("\"{0}\" -> \"{1}\"", NodeName(R), NodeName(next)));
                                    Optimize(next, j, ub);
                                }
                            }
                    }
                }
            }
            this.dot_tree.AddFirst(name);
        }
Ejemplo n.º 7
0
        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());
        }
Ejemplo n.º 8
0
        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;
        }
Ejemplo n.º 9
0
        public static IIntCoupling DoFCA(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, bool save_stats, String name, String model, String method, LinkedList<string> res, LinkedList<string> details, out FluxPattern max, out EFCACount efca, out ICollection<FluxPattern> circuits)
        {
            LinkedList<FluxPattern> witnesses;
            int n_unblocked;
            FluxPattern frev = null;
            IIntCoupling coupling = null;
            DateTime time_start, time_stop;
            DateTime[] time_reaction;

            int reactions_unblocked, reactions_frev, couples, lps, lps_unblocked, lps_frev, lps_couples, lps_efca, wits, wits_unblocked, wits_frev, wits_couples, wits_efca;
            int lps_total, wits_total;
            int[] lps_reaction, wits_reaction, wits_distribution, wits_usable;
            DateTime time_unblocked, time_frev, time_couples, time_efca;

            Analyze(matrix, rev, trans, forbidden_combinations, max_value, tolerance, lp, fc, do_efca, keep_witnesses, only_internal, show_output, out max, out coupling, out efca, out reactions_unblocked, out reactions_frev, out couples, out circuits, out time_start, out time_unblocked, out time_frev, out time_couples, out time_efca, out time_reaction, out lps_unblocked, out lps_frev, out lps_couples, out lps_efca, out lps_reaction, out wits_unblocked, out wits_frev, out wits_couples, out wits_efca, out wits_reaction, out wits_distribution, out wits_usable, save_stats ? name : null);

            lps = lps_unblocked + lps_frev + lps_couples + lps_efca;
            wits = wits_unblocked + wits_frev + wits_couples + wits_efca;

            /*String cellcolor = "\\cellcolor{gray!10}";
                        res.AddLast("\\midrule\n\\multirowbt{4}{*}{\\Var{" + model.Replace("_", "\\_") + "}}" + String.Format("& {4}Total &{4}{0}&{4}{2}&{4}{3}&{4}{1:0.0}\\\\", rev.Length, (time_efca - time_start).TotalSeconds, lps, wits, ""));
                        res.AddLast(String.Format("\\cmidrule{4} & {5}$1_L$ &{5}{0}&{5}{2}&{5}{3}&{5}{1:0.0}\\\\", reactions_unblocked, (time_unblocked - time_start).TotalSeconds, lps_unblocked, wits_unblocked, "{2-6}", cellcolor));
                        res.AddLast(String.Format("\\cmidrule{4} & $\\Frev$ &{0}&{2}&{3}&{1:0.0}\\\\", reactions_frev, (time_frev - time_unblocked).TotalSeconds, lps_frev, wits_frev, "{2-6}"));
                        res.AddLast(String.Format("\\cmidrule{4} & {5}$\\Coupling$ &{5}{0}&{5}{2}&{5}{3}&{5}{1:0.0}\\\\", couples, (time_couples - time_frev).TotalSeconds, lps_couples, wits_couples, "{2-6}", cellcolor));
            if(do_efca)
                res.AddLast(String.Format("\\cmidrule{4} & EFCA &{1}&{2}&{3}&{1:0.0}\\\\", efca.Length, (time_efca - time_couples).TotalSeconds, lps_efca, wits_efca, "{2-6}"));
              */
            res.AddLast(String.Format("{0} {1} {2} {3} {4} {5} {6} {7:0.000} {8:0.000} {9:0.000} {10:0.000} {11:0.000} {12} {13} {14} {15} {16} {17} {18} {19} {20} {21}", model, method, matrix.Length / rev.Length, rev.Length, reactions_unblocked, reactions_frev, couples, (time_efca - time_start).TotalSeconds, (time_unblocked - time_start).TotalSeconds, (time_frev - time_unblocked).TotalSeconds, (time_couples - time_frev).TotalSeconds, (time_efca - time_couples).TotalSeconds, lps, lps_unblocked, lps_frev, lps_couples, lps_efca, wits, wits_unblocked, wits_frev, wits_couples, wits_efca));

            //unblocked
            details.AddLast(String.Format("{0} {1} {2} {3} {4} {5} {6} {22:0.000} {23} {24} {25} {26} {27}", model, method, matrix.Length / rev.Length, rev.Length, reactions_unblocked, reactions_frev, couples, (time_efca - time_start).TotalSeconds, (time_unblocked - time_start).TotalSeconds, (time_frev - time_unblocked).TotalSeconds, (time_couples - time_frev).TotalSeconds, (time_efca - time_couples).TotalSeconds, lps, lps_unblocked, lps_frev, lps_couples, lps_efca, wits, wits_unblocked, wits_frev, wits_couples, wits_efca, time_start, -1, lps_unblocked, lps_unblocked, wits_unblocked, wits_unblocked));
            //frev
            details.AddLast(String.Format("{0} {1} {2} {3} {4} {5} {6} {22:0.000} {23} {24} {25} {26} {27}", model, method, matrix.Length / rev.Length, rev.Length, reactions_unblocked, reactions_frev, couples, (time_efca - time_start).TotalSeconds, (time_unblocked - time_start).TotalSeconds, (time_frev - time_unblocked).TotalSeconds, (time_couples - time_frev).TotalSeconds, (time_efca - time_couples).TotalSeconds, lps, lps_unblocked, lps_frev, lps_couples, lps_efca, wits, wits_unblocked, wits_frev, wits_couples, wits_efca, time_start, 0, lps_frev, lps_unblocked + lps_frev, wits_frev, wits_unblocked + wits_frev));
            //couples
            lps_total = lps_unblocked + lps_frev;
            wits_total = wits_unblocked + wits_frev;
            int temp_i = 0;
            for (int i = 0; i < rev.Length; ++i)
                if (max[i] && lps_reaction[i] > -1)
                {
                    lps_total += lps_reaction[i];
                    wits_total += wits_reaction[i];
                    temp_i = i;
                    details.AddLast(String.Format("{0} {1} {2} {3} {4} {5} {6} {22:0.000} {23} {24} {25} {26} {27} {28} {29}", model, method, matrix.Length / rev.Length, rev.Length, reactions_unblocked, reactions_frev, couples, do_efca ? (time_efca - time_start).TotalSeconds : 0, (time_unblocked - time_start).TotalSeconds, (time_frev - time_unblocked).TotalSeconds, (time_couples - time_frev).TotalSeconds, do_efca ? (time_efca - time_couples).TotalSeconds : 0, lps, lps_unblocked, lps_frev, lps_couples, lps_efca, wits, wits_unblocked, wits_frev, wits_couples, wits_efca, i + 1, (time_reaction[i] - time_start).TotalSeconds, lps_reaction[i], lps_total, wits_reaction[i], wits_total, wits_distribution[i + 1], do_efca ? wits_usable[i] : 0));
                }
                else
                    details.AddLast(String.Format("{0} {1} {2} {3} {4} {5} {6} {22:0.000} {23} {24} {25} {26} {27} {28} {29}", model, method, matrix.Length / rev.Length, rev.Length, reactions_unblocked, reactions_frev, couples, do_efca ? (time_efca - time_start).TotalSeconds : 0, (time_unblocked - time_start).TotalSeconds, (time_frev - time_unblocked).TotalSeconds, (time_couples - time_frev).TotalSeconds, do_efca ? (time_efca - time_couples).TotalSeconds : 0, lps, lps_unblocked, lps_frev, lps_couples, lps_efca, wits, wits_unblocked, wits_frev, wits_couples, wits_efca, i + 1, (time_reaction[temp_i] - time_start).TotalSeconds, 0, lps_total, 0, wits_total, wits_distribution[i + 1], do_efca ? wits_usable[i] : 0));
            return coupling;
        }
Ejemplo n.º 10
0
        /// <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);
        }
Ejemplo n.º 11
0
        /// <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);
        }