public IGraphColoringSolution Optimize()
        {
            GraphColoringSolution bestSolution = null;
            var solutionInfeasible             = false;
            var maxColorsAllowed = _nodeAndArcRepository.GetAllNodes().Count();

            while (!solutionInfeasible)
            {
                var domainStore     = new DomainStore(_nodeAndArcRepository.GetAllNodes(), maxColorsAllowed);
                var scoringFunction = new LexicographicScoringFunction(_nodeAndArcRepository.GetAllNodes(), maxColorsAllowed);
                var solution        = new GraphColoringSolution();

                var firstNode = scoringFunction.GetNewNode();
                solutionInfeasible = _searchAlgorithm.SearchFurther(firstNode, 0, domainStore, scoringFunction, solution);

                if (!solutionInfeasible)
                {
                    bestSolution     = solution;
                    maxColorsAllowed = solution.GetColorsUsed();
                }

                //solutionInfeasible = true;
            }
            return(bestSolution);
        }
Пример #2
0
        public LexicographicScoringFunction(LexicographicScoringFunction scoringFunction)
        {
            this.ScoringDictionary = new SortedDictionary <string, List <Node> >();
            this.NodeScore         = new Dictionary <Node, string>();
            foreach (var score in scoringFunction.ScoringDictionary.Keys)
            {
                this.ScoringDictionary.Add(score, new List <Node>(scoringFunction.ScoringDictionary[score]));
            }

            foreach (KeyValuePair <Node, string> keyValuePair in scoringFunction.NodeScore)
            {
                this.NodeScore.Add(keyValuePair.Key, keyValuePair.Value);
            }
        }
        public bool SearchFurther(Node prevnode, int chosenColor, DomainStore domainStore,
                                  LexicographicScoringFunction scoringFunction, GraphColoringSolution solution)
        {
            if (domainStore.UpdateDomainStore(prevnode, chosenColor))
            {
                return(true);
            }

            scoringFunction.UpdateScore(prevnode, domainStore);
            solution.AddSolution(prevnode, chosenColor);

            var nextNode = scoringFunction.GetNewNode();

            if (solution.NumberOfNodesColored() == domainStore.Domain.Count())
            {
                return(false);
            }

            foreach (var colorOption in domainStore.GetPossibleOptions(nextNode).ToList())
            {
                var solutionRejected = SearchFurther(nextNode, colorOption, new DomainStore(domainStore),
                                                     new LexicographicScoringFunction(scoringFunction), solution);

                if (solutionRejected)
                {
                    solution.RevertSolution(nextNode);
//                    if (domainStore.UpdateDomainStoreAfterBacktrack(nextNode, colorOption))
//                    {
//                        return true;
//                    }
                }
                else
                {
                    return(false);
                }
            }

            return(true);
        }