protected virtual ContinuousSolution Recombine(ContinuousSolution[] pop, double[] lower_bounds, double[] upper_bounds, object constraints)
        {
            ContinuousSolution[] parents = new ContinuousSolution[mSelectedParentCount_rho];
            for (int i = 0; i < mSelectedParentCount_rho; ++i)
            {
                parents[i] = BinaryTournamentSelection(pop);
            }

            HashSet <int> intersection_points = new HashSet <int>();

            for (int i = 0; i < mSelectedParentCount_rho - 1; ++i)
            {
                int index = RandomEngine.NextInt(mDimension);
                while (intersection_points.Contains(index))
                {
                    index = RandomEngine.NextInt(mDimension);
                }
                intersection_points.Add(index);
            }

            int[] intersect_pts = intersection_points.OrderBy(s => s).ToArray();

            int start_pt = 0;

            double[] x = new double[mDimension];
            double[] child_strategy  = new double[mDimension];
            double[] parent_strategy = null;
            for (int i = 0; i < intersect_pts.Length; ++i)
            {
                int end_pt = intersect_pts[i];
                parent_strategy = parents[i].GetMutationStrategy();
                for (int j = start_pt; j < end_pt; ++j)
                {
                    x[j] = parents[i][j];
                    child_strategy[j] = parent_strategy[j];
                }
            }

            parent_strategy = parents[mSelectedParentCount_rho - 1].GetMutationStrategy();
            for (int i = start_pt; i < mDimension; ++i)
            {
                x[i] = parents[mSelectedParentCount_rho - 1][i];
                child_strategy[i] = parent_strategy[i];
            }

            ContinuousSolution child = new ContinuousSolution(x, double.MaxValue);

            child.SetMutationStrategy(child_strategy);

            return(child);
        }
        protected ContinuousSolution BinaryTournamentSelection(ContinuousSolution[] pop)
        {
            int index1 = RandomEngine.NextInt(pop.Length);
            int index2 = index1;

            do
            {
                index2 = RandomEngine.NextInt(pop.Length);
            } while (index1 == index2);

            if (pop[index1].Cost < pop[index2].Cost)
            {
                return(pop[index1]);
            }
            return(pop[index2]);
        }
Ejemplo n.º 3
0
        public static void Shuffle <T>(this List <T> list)
        {
            if (list.Count == 0)
            {
                return;
            }

            int index = 0;

            while (index < list.Count - 1)
            {
                int index2 = RandomEngine.NextInt(list.Count - index) + index;
                index2 = index2 % list.Count;
                T temp = list[index];
                list[index] = list[index2];
                list[index] = temp;
            }
        }
Ejemplo n.º 4
0
        protected ContinuousSolution[] SelectParents(ContinuousSolution[] pop, ContinuousSolution p0)
        {
            ContinuousSolution p1, p2, p3;

            do
            {
                p1 = pop[RandomEngine.NextInt(pop.Length)];
            } while (p1 == p0);
            do
            {
                p2 = pop[RandomEngine.NextInt(pop.Length)];
            } while (p2 == p1 || p2 == p0);
            do
            {
                p3 = pop[RandomEngine.NextInt(pop.Length)];
            } while (p3 == p2 || p3 == p1 || p3 == p0);

            return(new ContinuousSolution[] { p1, p2, p3 });
        }
Ejemplo n.º 5
0
        public override void Train(IEnumerable <T> data_store)
        {
            List <T> temp_samples = new List <T>();

            foreach (T rec in data_store)
            {
                temp_samples.Add(rec);
            }
            int sample_count = (int)(temp_samples.Count * mPercentageDataUsage);

            for (int t = 0; t < mForestSize; ++t)
            {
                List <T> new_training_sample = new List <T>();
                for (int i = 0; i < sample_count; ++i)
                {
                    int sample_index = RandomEngine.NextInt(sample_count);
                    new_training_sample.Add(temp_samples[sample_index]);
                }
                mClassifiers[t].Train(new_training_sample);
            }
        }
Ejemplo n.º 6
0
        protected ContinuousSolution Reproduce(ContinuousSolution[] pop, ContinuousSolution p0, double[] lower_bounds, double[] upper_bounds)
        {
            ContinuousSolution[] parents = SelectParents(pop, p0);
            ContinuousSolution   p1      = parents[0];
            ContinuousSolution   p2      = parents[1];
            ContinuousSolution   p3      = parents[2];

            int cut_point            = RandomEngine.NextInt(mDimension);
            ContinuousSolution child = new ContinuousSolution(mDimension);

            for (int i = 0; i < mDimension; ++i)
            {
                child[i] = p0[i];
                if (i == cut_point || RandomEngine.NextDouble() < mCrossoverRate)
                {
                    child[i] = p1[i] + mWeightingFactor * (p2[i] - p3[i]);
                    child[i] = System.Math.Max(lower_bounds[i], child[i]);
                    child[i] = System.Math.Min(upper_bounds[i], child[i]);
                }
            }

            return(child);
        }
Ejemplo n.º 7
0
        public override ContinuousSolution Minimize(CostEvaluationMethod evaluate, GradientEvaluationMethod calc_gradient, TerminationEvaluationMethod should_terminate, object constraints = null)
        {
            double?improvement = null;
            int    iteration   = 0;

            ContinuousSolution[] pop = new ContinuousSolution[mPopSize];

            double[] lower_bounds = null;
            double[] upper_bounds = null;
            if (mLowerBounds == null || mUpperBounds == null)
            {
                if (constraints is Tuple <double[], double[]> )
                {
                    Tuple <double[], double[]> bounds = constraints as Tuple <double[], double[]>;
                    lower_bounds = bounds.Item1;
                    upper_bounds = bounds.Item2;
                }
                else
                {
                    throw new InvalidCastException();
                }
            }
            else
            {
                lower_bounds = mLowerBounds;
                upper_bounds = mUpperBounds;
            }

            if (lower_bounds.Length < mDimension)
            {
                throw new IndexOutOfRangeException();
            }
            if (upper_bounds.Length < mDimension)
            {
                throw new IndexOutOfRangeException();
            }

            double[,] init_strategy_bounds = new double[mDimension, 2];
            for (int j = 0; j < mDimension; ++j)
            {
                init_strategy_bounds[j, 0] = 0;
                init_strategy_bounds[j, 1] = (upper_bounds[j] - lower_bounds[j]) * 0.05;
            }


            ContinuousSolution best_solution = null;

            for (int i = 0; i < mPopSize; ++i)
            {
                double[]           x        = mSolutionGenerator(mDimension, constraints);
                double             fx       = evaluate(x, mLowerBounds, mUpperBounds, constraints);
                ContinuousSolution s        = new ContinuousSolution(x, fx);
                double[]           strategy = new double[mDimension];
                for (int j = 0; j < mDimension; ++j)
                {
                    strategy[j] = init_strategy_bounds[j, 0] + (init_strategy_bounds[j, 1] - init_strategy_bounds[j, 0]) * RandomEngine.NextDouble();
                }
                s.SetMutationStrategy(strategy);
                pop[i] = s;
            }

            pop = pop.OrderBy(s => s.Cost).ToArray();

            best_solution = pop[0].Clone() as ContinuousSolution;

            ContinuousSolution[] children   = new ContinuousSolution[mPopSize];
            ContinuousSolution[] generation = new ContinuousSolution[mPopSize * 2];

            while (!should_terminate(improvement, iteration))
            {
                for (int i = 0; i < mPopSize; ++i)
                {
                    children[i]      = Mutate(pop[i], lower_bounds, upper_bounds, constraints);
                    children[i].Cost = evaluate(children[i].Values, mLowerBounds, mUpperBounds, constraints);
                }

                children = children.OrderBy(s => s.Cost).ToArray();
                if (best_solution.TryUpdateSolution(children[0].Values, children[0].Cost, out improvement))
                {
                    OnSolutionUpdated(best_solution, iteration);
                }


                for (int i = 0; i < mPopSize; ++i)
                {
                    generation[i] = pop[i];
                }
                for (int i = 0; i < mPopSize; ++i)
                {
                    generation[i + mPopSize] = children[i];
                }


                for (int i = 0; i < generation.Length; ++i)
                {
                    int wins = 0;
                    ContinuousSolution si = generation[i];
                    for (int j = 0; j < mBoutSize; ++j)
                    {
                        ContinuousSolution sj = generation[RandomEngine.NextInt(generation.Length)];
                        if (si.Cost < sj.Cost)
                        {
                            wins++;
                        }
                    }
                    si.SetWins(wins);
                }

                generation = generation.OrderByDescending(s => s.GetWins()).ToArray();

                for (int i = 0; i < mPopSize; ++i)
                {
                    pop[i] = generation[i];
                }


                OnStepped(best_solution, iteration);
                iteration++;
            }

            return(best_solution);
        }