Exemplo n.º 1
0
        public ResultModel Solve(IProblem problem)
        {
            var rand     = new Random();
            var tabuList = CreateEmptyTabuList(problem);

            var solution = new List <int>(problem.SolutionArray);
            var fitness  = problem.CalculateFitness();

            for (var k = 1; k <= MaxIterations; k++)
            {
                var bestMove = FindBestMove(problem, k, tabuList, fitness);
                problem.SolutionArray = bestMove.bestSolutionInIteration;
                problem.CalculateFitness();
                tabuList[bestMove.bestIndexInIteration - 1] = k + MinTabu + rand.Next(ExtraTabu);

                if (!(bestMove.bestFitnessInIteration < fitness))
                {
                    continue;
                }

                solution = bestMove.bestSolutionInIteration;
                fitness  = bestMove.bestFitnessInIteration;
            }

            problem.SolutionArray = solution;

            return(new ResultModel(solution, fitness));
        }
Exemplo n.º 2
0
        public void CalculateFitness(IProblem problem)
        {
            Fitness = problem.CalculateFitness(Genom);
            //int weight = 0;
            //int cost = 0;
            //for (int i = 0; i < Genom.Length; i++)
            //    if (Genom[i])
            //    {
            //        weight += items[i].Weight;
            //        cost += items[i].Price;
            //    }

            //if (weight > maxWeight)
            //    Fitness = maxWeight - weight;
            //else
            //    Fitness = cost;
        }