public Optimisation2DWrap(int populationSize, IChromosome chromosome, OptimizationFunction2D userFunction, Selection selection, bool showOnlyBest) { _showOnlyBest = showOnlyBest; _userFunction = userFunction; _populationSize = populationSize; // create population Population = new Population( size: populationSize, ancestor: chromosome, fitnessFunction: userFunction, selectionMethod: (selection == Selection.Elite) ? (ISelectionMethod) new EliteSelection() : (selection == Selection.Rank) ? (ISelectionMethod) new RankSelection() : (ISelectionMethod) new RouletteWheelSelection()); _solution = new double[(showOnlyBest) ? 1 : populationSize, 2]; }
// public static BackgroundWorkerObservable<ChromosomeWrapper> Create<TInput, ROutput>(Func<double, double, ROutput> func, TInput input, Func<TInput, ROutput, double> error, int count = 10) // { //function to optimise // Func<double, double, double> fc = (a, b) => error(input,func(a, b)); // // wrap-up in class // var of = new UserFunction2D(fc); // var main = BuildGeneticProgram(of); // return new BackgroundWorkerObservable<ChromosomeWrapper>(main, Observable.Repeat((bool?)true, 1), Observable.Repeat(count, 1), Observable.Repeat(0, 1)); // } // //public static BackgroundWorkerObservable<Accord.Genetic.IChromosome> Create2<TInput, ROutput>(Func<double, double, ROutput> func, TInput input, Func<TInput, ROutput, double> error, int count = 10) // //{ // // var main = BuildFunction2(func, input, error); // // return new BackgroundWorkerObservable<Accord.Genetic.IChromosome>(main, null, Observable.Repeat((bool?)true, 1), Observable.Repeat(count, 1), Observable.Repeat(0, 1)); // //} // public static BackgroundWorkerObservableQueue<ChromosomeWrapper> Create<TInput, ROutput>(IObservable<Tuple<TInput, Func<double, double, ROutput>>> funcinput, Func<TInput, ROutput, double> error, int count = 10) // { // var fc2 = funcinput.Select(_ => // { // //function to optimise // Func<double, double, double> fc = (a, b) => error(_.Item1,_.Item2(a, b)); // // wrap-up in class // var of = new UserFunction2D(fc); // var main = BuildGeneticProgram(of); // return main; // }); // return new BackgroundWorkerObservableQueue<ChromosomeWrapper>(fc2, Observable.Repeat((bool?)true, 1), Observable.Repeat(count, 1), Observable.Repeat(0, 1), Observable.Repeat(1, 1)); // } //public static BackgroundWorkerObservableQueue<ChromosomeWrapper> Create2<TInput, ROutput>(IObservable<Tuple<TInput, Func<double, double, ROutput>>> funcinput, Func<TInput, ROutput, double> error, int count = 10) //{ // var fc2 = funcinput.Select(_ => // { // //function to optimise // //function to optimise // Func<double, double, double> fc = (a, b) => error(_.Item1, _.Item2(a, b)); // // wrap-up in class // var of = new UserFunction2D(fc); // return BuildFunction2<TInput, ROutput>(of); // }); // var c =new ChromosomeWrapper { Chromosome= new BinaryChromosome(64) }; // return new BackgroundWorkerObservableQueue<ChromosomeWrapper>(fc2, c,Observable.Repeat((bool?)true, 1), Observable.Repeat(count, 1), Observable.Repeat(0, 1), Observable.Repeat(1, 1)); //} private static Func <int, ChromosomeWrapper> BuildGeneticProgram(OptimizationFunction2D of) { // build default genetic algorithm class to minimise output var w = AccordWrapFactory.BuildDefaultMinimisation(of); //cyclical method to run Func <int, ChromosomeWrapper> main = (i) => { w.Population.RunEpoch(); // var _ = w.Evaluate(); //var score = _.Output[0, 1]; //double[] xy = _.BestSolution.Split(' ').Select(d => double.Parse(d)).ToArray(); w.Population.RunEpoch(); var x = w.Evaluate(); return(new ChromosomeWrapper { Chromosome = w.Population.BestChromosome, Iteration = i, Score = x.Output[0, 1], Input = x.BestSolution.Split(' ').Select(d => double.Parse(d)).ToArray() }); }; return(main); }
public static Optimisation2DWrap BuildDefaultMinimisation(OptimizationFunction2D of, IChromosome bc) { of.Mode = OptimizationFunction2D.Modes.Minimization; return(new Optimisation2DWrap(100, bc, of, Selection.Elite, false)); }
public static Optimisation2DWrap BuildDefaultMaximisation(OptimizationFunction2D of) { of.Mode = OptimizationFunction2D.Modes.Maximization; return(new Optimisation2DWrap(20, 64, of, Selection.Elite, false)); }