public Population(FitnessEstimator fe, int population_size, double recombination_rate, double mut_rate_act, double mut_rate_deact, double start_density) { this.population_size = population_size; this.recombination_rate = recombination_rate; EvolutionParams prms = new EvolutionParams(mut_rate_act, mut_rate_deact); population = new SortedList<double, Individual>(); Individual temp; for (int i = 0; i < population_size; ++i) { temp = Individual.RandomIndividual(fe, start_density, prms); population.Add(temp.Fitness, temp); } }
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); }
public Individual(List<int> genes, FitnessEstimator fe, EvolutionParams prms) { this.fe = fe; this.prms = prms; this.fitness = fe.CalculateFitness(genes, out this.max, out this.genes, out this.too_many_genes); }
public static Individual RandomIndividual(FitnessEstimator fe, double density, EvolutionParams prms) { Individual res = null; do try { List<int> r = new List<int>(); for (int i = 0; i < fe.n; ++i) if (prms.rand.NextDouble() < density) r.Add(i); res = new Individual(r, fe, prms); } catch (CalculationException e) { } while (res == null); 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()); }