Beispiel #1
0
 public LatinSquareFC(int size, ValueMode valueMode, VariableMode variableMode, bool firstOnly = false)
 {
     _size         = size;
     _valueMode    = valueMode;
     _variableMode = variableMode;
     _firstOnly    = firstOnly;
     board         = new int[_size, _size];
     _domain       = _size;
     processed     = new List <Position2>();
     notProcessed  = new List <Position2>();
     Solutions     = new List <int[, ]>();
     for (int i = 0; i < size; i++)
     {
         for (int j = 0; j < size; j++)
         {
             var pos = new Position2()
             {
                 X = i, Y = j, Domain = new SortedSet <int>()
             };
             for (int x = 1; x <= _domain; x++)
             {
                 pos.Domain.Add(x);
             }
             pos.Removed = new List <Position2>();
             notProcessed.Add(pos);
         }
     }
 }
Beispiel #2
0
        private bool IsVariableInConflict(Position2 currentVariable, int currentValue)
        {
            for (int i = 0; i < _size; i++)
            {
                if (currentVariable.Y != i && board[currentVariable.X, i] == currentValue || currentVariable.X != i && board[i, currentVariable.Y] == currentValue)
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #3
0
        private void Unfilter(Position2 variable)
        {
            var value = board[variable.X, variable.Y];

            //foreach (var position2 in notProcessed.Where(np => np.X == variable.X || np.Y == variable.Y))
            //{
            //    position2.Domain.Add(value);
            //}
            foreach (var position2 in variable.Removed)
            {
                position2.Domain.Add(value);
            }
            variable.Removed.Clear();
        }
Beispiel #4
0
        private bool Process(Position2 variable)
        {
            notProcessed.Remove(variable);
            processed.Add(variable);
            if (!notProcessed.Any())
            {
                if (first)
                {
                    watch.Stop();
                    TimeOfOneSolution = watch.ElapsedMilliseconds;
                    first             = false;
                }
                SaveSolution();
                notProcessed.Insert(0, variable);
                processed.Remove(variable);
                //board[variable.X, variable.Y] = 0;
                return(true);
            }

            if (!FilterRemainingVariables(variable))
            {
                Unfilter(variable);
                notProcessed.Insert(0, variable);
                processed.Remove(variable);
                return(false);
            }



            var nextVariable = GetNextVariable();
            int nextValue    = GetNextValue(nextVariable);

            while (nextValue != (int)Value.NoValue)
            {
                board[nextVariable.X, nextVariable.Y] = nextValue;
                Process(nextVariable);

                nextValue = GetNextValue(nextVariable);
            }
            Unfilter(variable);
            notProcessed.Insert(0, variable);
            processed.Remove(variable);
            board[nextVariable.X, nextVariable.Y] = 0;
            return(false);
        }
Beispiel #5
0
        private bool FilterRemainingVariables(Position2 variable)
        {
            var value = board[variable.X, variable.Y];

            foreach (var position2 in notProcessed.Where(np => np.X == variable.X || np.Y == variable.Y))
            {
                var b = position2.Domain.Remove(value);
                if (b)
                {
                    variable.Removed.Add(position2);
                }
                if (!position2.Domain.Any())
                {
                    return(false);
                }
            }
            return(true);
        }
Beispiel #6
0
        private int GetNextValue(Position2 currentVariable)
        {
            var next = currentVariable.Domain.FirstOrDefault(x => x > board[currentVariable.X, currentVariable.Y]);

            return(next == 0 ? (int)Value.NoValue : next);
        }
Beispiel #7
0
 private Position2 GetPreviousVariable(Position2 curentVaiable)
 {
     board[curentVaiable.X, curentVaiable.Y] = 0;
     return(processed.Last());
 }
Beispiel #8
0
 private void StepBack(Position2 currentVariable)
 {
     notProcessed.Insert(0, currentVariable);
     processed.Remove(processed.Last());
 }