Exemple #1
0
        public State Climb(State problem, long timeoutMillis)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            List <State> population = new List <State>();

            for (int i = 0; i < populationSize; i++)
            {
                population.Add(problem.GetRandomState(r));
            }
            while (sw.ElapsedMilliseconds < timeoutMillis)
            {
                population = NextGen(population);
                if (heuristic.Evaluate(population.Take(1).ToList()[0]) == 0)
                {
                    return(population.Take(1).ToList()[0]);
                }
            }
            return(population.Take(1).ToList()[0]);
        }
        public State Climb(State problem, long timeoutMillis)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            State current     = problem.GetRandomState(r);
            int   currentEval = heuristic.Evaluate(current);
            long  elapsed     = sw.ElapsedMilliseconds;

            while (elapsed < timeoutMillis)
            {
                State  next     = current.GetRandomNeighbour(r);
                int    nextEval = heuristic.Evaluate(next);
                double temp     = Temperature(timeoutMillis, elapsed);
                if (nextEval <= currentEval || Worsen(temp, currentEval, nextEval))
                {
                    current     = next;
                    currentEval = nextEval;
                }
                elapsed = sw.ElapsedMilliseconds;
            }
            return(current);
        }
Exemple #3
0
        public State Climb(State problem, long timeoutMillis)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            if (heuristic.Evaluate(problem) == 0)
            {
                return(problem);
            }
            State currState = problem.GetRandomState(r);
            int   currEval  = heuristic.Evaluate(currState);

            if (currEval == 0)
            {
                return(problem);
            }
            List <State> neighbours = currState.GetNeighbours();

            while (neighbours.Count != 0 &&
                   (sw.ElapsedMilliseconds < timeoutMillis))
            {
                State next = neighbours[0];
                neighbours.RemoveAt(0);
                int nextEval = heuristic.Evaluate(next);
                if (nextEval == 0)
                {
                    return(next);
                }
                else if (nextEval < currEval)
                {
                    currState  = next;
                    currEval   = nextEval;
                    neighbours = currState.GetNeighbours();
                }
            }
            return(currState);
        }