Exemplo n.º 1
0
        protected ContinuousSolution Mutate(ContinuousSolution solution, double[] lower_bounds, double[] upper_bounds, object constraints)
        {
            ContinuousSolution child = MutateVector(solution, lower_bounds, upper_bounds, constraints);

            double[] child_strategy = MutateStrategy(solution.GetMutationStrategy());
            child.SetMutationStrategy(child_strategy);
            return(child);
        }
        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);
        }
Exemplo n.º 3
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);
        }