Exemplo n.º 1
0
        private int filterDomain(JolkaVariable variable)
        {
            int valuesRemoved = 0;

            variable.ResetDomain();//Domain = GetWordsWithLength(variable.EndIndex - variable.BeginIndex + 1);

            if (variable.Horizontal)
            {
                foreach (JolkaConstraint cons in Constraints[variable])
                {
                    if (!cons.VerticalVariable.IsEmpty())
                    {
                        valuesRemoved += variable.Domain.RemoveAll(word => word[cons.PosHorizontal] != cons.VerticalVariable.Word[cons.PosVertical]);
                    }
                }
            }
            else
            {
                foreach (JolkaConstraint cons in Constraints[variable])
                {
                    if (!cons.HorizontalVariable.IsEmpty())
                    {
                        valuesRemoved += variable.Domain.RemoveAll(word => word[cons.PosVertical] != cons.HorizontalVariable.Word[cons.PosHorizontal]);
                    }
                }
            }
            return(valuesRemoved);
        }
Exemplo n.º 2
0
 public JolkaConstraint(ref JolkaVariable horizontalVar, ref JolkaVariable verticalVar,
                        int row, int col)
 {
     HorizontalVariable = horizontalVar;
     VerticalVariable   = verticalVar;
     PosVertical        = row - VerticalVariable.BeginIndex;
     PosHorizontal      = col - HorizontalVariable.BeginIndex;
 }
Exemplo n.º 3
0
 public JolkaVariable(JolkaVariable other)
 {
     Word          = other.Word;
     Horizontal    = other.Horizontal;
     SectionNum    = other.SectionNum;
     BeginIndex    = other.BeginIndex;
     EndIndex      = other.EndIndex;
     Domain        = other.Domain;
     OrderedDomain = other.OrderedDomain;
 }
Exemplo n.º 4
0
        private void AddConstraint(int i, int j)
        {
            JolkaVariable horizontalVar = Variables.
                                          First(v => (v.Horizontal && v.SectionNum == i && v.BeginIndex <= j && v.EndIndex >= j));
            JolkaVariable verticalVar = Variables.
                                        First(v => (!v.Horizontal && v.SectionNum == j && v.BeginIndex <= i && v.EndIndex >= i));

            JolkaConstraint constraint = new JolkaConstraint(ref horizontalVar, ref verticalVar, i, j);

            Constraints[horizontalVar].Add(constraint);
            Constraints[verticalVar].Add(constraint);
        }
Exemplo n.º 5
0
        private bool CheckConstraints(JolkaVariable currElem)
        {
            foreach (JolkaConstraint constraint in Constraints[currElem])
            {
                if (!constraint.isMet())
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 6
0
        private JolkaVariable SelectNextVariableMRV()
        {
            if (BackTrack)
            {
                JolkaVariable nextVar            = null;
                int           minRemainingValues = int.MaxValue;
                foreach (JolkaVariable variable in Variables)
                {
                    if (variable.IsEmpty())
                    {
                        int excludedValuesCount = 0;
                        foreach (JolkaConstraint cons in Constraints[variable])
                        {
                            JolkaVariable related;
                            if (variable.Horizontal)
                            {
                                related = cons.VerticalVariable;
                            }
                            else
                            {
                                related = cons.HorizontalVariable;
                            }

                            if (!related.IsEmpty())
                            {
                                foreach (string word in variable.Domain)
                                {
                                    variable.Word = word;
                                    if (!cons.isMet())
                                    {
                                        excludedValuesCount++;
                                    }
                                }
                            }
                            if (variable.Domain.Count - excludedValuesCount < minRemainingValues)
                            {
                                minRemainingValues = variable.Domain.Count - excludedValuesCount;
                                nextVar            = variable;
                            }
                            variable.Word = "";
                        }
                    }
                }
                return(nextVar);
            }
            else
            {
                return(Variables.OrderBy(v => v.Domain.Count).FirstOrDefault(v => v.IsEmpty()));
            }
        }
Exemplo n.º 7
0
        private void ResetDomainsOfAllRelatedVariables(JolkaVariable currElem)
        {
            JolkaVariable related;

            foreach (JolkaConstraint cons in Constraints[currElem])
            {
                if (currElem.Horizontal)
                {
                    related = cons.VerticalVariable;
                }
                else
                {
                    related = cons.HorizontalVariable;
                }
                related.ResetDomain();//Domain = GetWordsWithLength(related.EndIndex - related.BeginIndex + 1);
            }
        }
Exemplo n.º 8
0
 public void WriteVariable(JolkaVariable variable)
 {
     if (variable.Horizontal)
     {
         for (int i = 0; i < variable.Word.Length; i++)
         {
             Fields[variable.SectionNum][variable.BeginIndex + i] = variable.Word[i];
         }
     }
     else
     {
         for (int i = 0; i < variable.Word.Length; i++)
         {
             Fields[variable.BeginIndex + i][variable.SectionNum] = variable.Word[i];
         }
     }
 }
Exemplo n.º 9
0
        private void OrderDomain(JolkaVariable variable)
        {
            int valuesRemoved = 0;
            Dictionary <string, int> valuesRemovedByEachWord = new Dictionary <string, int>();

            foreach (string word in variable.Domain)
            {
                variable.Word = word;
                valuesRemoved = FilterDomainsOfAllRelatedVariables(variable);
                variable.Word = "";
                ResetDomainsOfAllRelatedVariables(variable);
                //if (Variables.Any(v => v.Word == word))
                //    valuesRemoved += 1000;
                valuesRemovedByEachWord.Add(word, valuesRemoved);
            }
            variable.OrderedDomain = variable.OrderedDomain.OrderBy(w => valuesRemovedByEachWord[w]).ToList();
            variable.ResetDomain();
        }
Exemplo n.º 10
0
        private int FilterDomainsOfAllRelatedVariables(JolkaVariable currElem)
        {
            JolkaVariable related;
            int           valuesRemoved = 0;

            foreach (JolkaConstraint cons in Constraints[currElem])
            {
                if (currElem.Horizontal)
                {
                    related = cons.VerticalVariable;
                }
                else
                {
                    related = cons.HorizontalVariable;
                }
                if (related.IsEmpty())
                {
                    valuesRemoved += filterDomain(related);
                }
            }

            return(valuesRemoved);
        }
Exemplo n.º 11
0
        public void RunForwardChecking()
        {
            if (firstRun)
            {
                StartTiming();
            }

            JolkaVariable currElem = SelectNextVariable();

            if (currElem == null) //znaleziono rozwiązanie
            {
                if (Solutions.Count == 0)
                {
                    SaveFirstSolutionData();
                }

                Solutions.Add(Variables.Select(v => new JolkaVariable(v)).ToList());
            }
            else
            {
                foreach (string value in currElem.Domain)//.Except(UsedWords))
                {
                    currElem.Word = value;
                    TotalNodesVisited++;
                    FilterDomainsOfAllRelatedVariables(currElem);
                    if (!IsAnyDomainEmpty())
                    {
                        RunForwardChecking();
                    }
                }
                currElem.Word = ""; //Nawrót - brak kolejnej wartosci
                FilterDomainsOfAllRelatedVariables(currElem);
            }
            TotalReturns++;
            TotalTime = stopwatch.Elapsed.TotalSeconds;
        }
Exemplo n.º 12
0
        public void RunBacktracking()
        {
            if (firstRun)
            {
                BackTrack = true;
                StartTiming();
            }

            JolkaVariable currElem = SelectNextVariable();

            if (currElem == null) //znaleziono rozwiązanie
            {
                if (Solutions.Count == 0)
                {
                    SaveFirstSolutionData();
                }

                Solutions.Add(Variables.Select(v => new JolkaVariable(v)).ToList());
            }
            else
            {
                foreach (string value in currElem.Domain)//.Except(UsedWords))
                {
                    currElem.Word = value;
                    TotalNodesVisited++;
                    bool correct = CheckConstraints(currElem);
                    if (correct)
                    {
                        RunBacktracking();
                    }
                }
                currElem.Word = "";
            }
            TotalReturns++;//Nawrót - brak kolejnej wartosci
            TotalTime = stopwatch.Elapsed.TotalSeconds;
        }