예제 #1
0
        public override TSolution Solve(NullParameters parameters, TSolution initial, List <Func <TSolution, Exploration, Random, TSolution> > neighborhoods, Exploration expCondition, Func <TSolution, double> cost, Random rdObj)
        {
            ResetProgress();
            timer.Start();

            TSolution current     = (TSolution)initial.Clone();
            double    currentCost = cost(current);

            int iteration = 0;

            OnIteration(new IterationEventArgs <TSolution>(current));

            for (int i = 0; i < neighborhoods.Count;)
            {
                TSolution neighborhoodSolution = neighborhoods[i](current, expCondition, rdObj);
                double    neighborhoodCost     = cost(neighborhoodSolution);

                if (neighborhoodCost < currentCost)
                {
                    current     = (TSolution)neighborhoodSolution.Clone();
                    currentCost = neighborhoodCost;
                    i           = 0;
                }
                else
                {
                    i++;
                }
                iteration++;
                OnIteration(new IterationEventArgs <TSolution>(current, iteration, timer.Elapsed.TotalSeconds));
            }

            timer.Stop();
            return(current);
        }
        public override TSolution Solve(NullParameters parameters, TSolution initial, List <Func <TSolution, Exploration, Random, TSolution> > neighborhoods, Exploration expCondition, Func <TSolution, double> cost, Random rdObj)
        {
            ResetProgress();

            TSolution current     = (TSolution)initial.Clone();
            double    currentCost = cost(current);
            int       iteration   = 0;

            OnIteration(new IterationEventArgs <TSolution>(current));

            timer.Start();
            while (true)
            {
                TSolution neighborhoodSolution     = SelectRandomNeigborStrategy(neighborhoods, rdObj)(current, expCondition, rdObj);
                double    neighborhoodSolutionCost = cost(neighborhoodSolution);
                if (neighborhoodSolutionCost < currentCost)
                {
                    currentCost = neighborhoodSolutionCost;
                    current     = (TSolution)neighborhoodSolution.Clone();
                    iteration++;
                    OnIteration(new IterationEventArgs <TSolution>(current, iteration, timer.Elapsed.TotalSeconds));
                }
                else
                {
                    break;
                }
            }
            timer.Stop();
            return(current);
        }
예제 #3
0
        public override Verb CreateVerb(string[] tokens)
        {
            var tokens1Length = tokens[1].Length;

            Color(position, tokens1Length, Whitespaces);
            var        index = position + tokens1Length;
            Block      block;
            Parameters parameters;
            var        returnValue = true;

            if (blockParser.Scan(source, index))
            {
                if (blockParser.Value is Lambda lambda)
                {
                    block      = lambda.Block;
                    parameters = lambda.Parameters;
                }
                else
                {
                    block      = (Block)blockParser.Value;
                    parameters = new NullParameters();
                }

                index = blockParser.Result.Position;
            }
            else
            {
                return(null);
            }

            if (index < source.Length && source.Substring(index, 1) == ".")
            {
                index++;
                returnValue = false;
            }

            overridePosition = index;
            Replacement      = new BlockReplacement(block, parameters, returnValue);
            return(new NullOp());
        }
예제 #4
0
        public override TSolution Solve(NullParameters parameters, TSolution initial, List <Func <TSolution, Exploration, Random, TSolution> > neighborhoods, Exploration expCondition, Func <TSolution, double> cost, Random rdObj)
        {
            ResetProgress();
            timer.Start();

            TSolution current     = (TSolution)initial.Clone();
            double    currentCost = cost(current);

            OnIteration(new IterationEventArgs <TSolution>(current));

            int iteration = 0;

            List <Func <TSolution, Exploration, Random, TSolution> > availables = new List <Func <TSolution, Exploration, Random, TSolution> >();

            availables.AddRange(neighborhoods);

            while (availables.Count > 0)
            {
                int       neighborIndex        = rdObj.Next(availables.Count);
                TSolution neighborhoodSolution = availables[neighborIndex](current, expCondition, rdObj);
                double    neighborhoodCost     = cost(neighborhoodSolution);
                if (neighborhoodCost < currentCost)
                {
                    current     = (TSolution)neighborhoodSolution.Clone();
                    currentCost = neighborhoodCost;
                    availables.Clear();
                    availables.AddRange(neighborhoods);
                }
                else
                {
                    availables.RemoveAt(neighborIndex);
                }
                iteration++;
                OnIteration(new IterationEventArgs <TSolution>(current, iteration, timer.Elapsed.TotalSeconds));
            }

            timer.Stop();
            return(current);
        }