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);
        }
Ejemplo n.º 2
0
        public void UpdateScore(Node selectedNode, DomainStore domainStore)
        {
            foreach (var node in selectedNode.AdjacencyList)
            {
                if (node.IsColorAssigned)
                {
                    continue;
                }

                var numCandidates = Convert.ToString(domainStore.MaxColorsAllowed - domainStore.Domain[node].Count, 2);
                var impactedNodes = Convert.ToString(node.AdjacencyList.Count, 2);
                var newScore      = numCandidates + impactedNodes;

                var oldScore = this.NodeScore[node];
                this.ScoringDictionary[oldScore].Remove(node);

                if (this.ScoringDictionary[oldScore].Count == 0)
                {
                    this.ScoringDictionary.Remove(oldScore);
                }

                this.NodeScore[node] = newScore;

                if (!this.ScoringDictionary.ContainsKey(newScore))
                {
                    this.ScoringDictionary.Add(newScore, new List <Node>());
                }

                this.ScoringDictionary[newScore].Add(node);
            }
        }
Ejemplo n.º 3
0
        public DomainStore(DomainStore oldDomain)
        {
            Domain = new Dictionary <Node, HashSet <int> >();

            foreach (var node in oldDomain.Domain.Keys)
            {
                Domain.Add(node, new HashSet <int>(oldDomain.Domain[node]));
            }

            this.MaxColorsAllowed = oldDomain.MaxColorsAllowed;
        }
        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);
        }