コード例 #1
0
ファイル: StateInteger.cs プロジェクト: nerdyrasa/Decider
        private void Search(out StateOperationResult searchResult, LinkedList <IVariable <int> > unassignedVariables,
                            IList <IVariable <int> > instantiatedVariables, DateTime startTime, int timeOut = Int32.MaxValue)
        {
            while (true)
            {
                if (this.Depth == this.VariableList.Length)
                {
                    searchResult  = StateOperationResult.Solved;
                    this.Runtime += DateTime.Now - startTime;

                    this.LastSolution = instantiatedVariables.ToArray();
                    ++this.NumberOfSolutions;

                    return;
                }

                DomainOperationResult instantiateResult;
                instantiatedVariables[this.Depth] = GetMostConstrainedVariable(unassignedVariables);
                instantiatedVariables[this.Depth].Instantiate(this.Depth, out instantiateResult);

                if (ConstraintsViolated() || unassignedVariables.Any(v => v.Size() == 0))
                {
                    if ((DateTime.Now - startTime).Seconds > timeOut)
                    {
                        throw new DeciderException();
                    }

                    Backtrack(unassignedVariables, instantiatedVariables);
                }

                ++this.Depth;
            }
        }
コード例 #2
0
ファイル: StateInteger.cs プロジェクト: nerdyrasa/Decider
        void IState <int> .StartSearch(out StateOperationResult searchResult)
        {
            var unassignedVariables = this.LastSolution == null
                                ? new LinkedList <IVariable <int> >(this.VariableList)
                                : new LinkedList <IVariable <int> >();
            var instantiatedVariables = this.LastSolution ?? new IVariable <int> [this.VariableList.Length];
            var startTime             = DateTime.Now;

            try
            {
                if (this.Depth == instantiatedVariables.Length)
                {
                    --this.Depth;
                    Backtrack(unassignedVariables, instantiatedVariables);
                    ++this.Depth;
                }
                else if (ConstraintsViolated())
                {
                    throw new DeciderException("No solution found.");
                }

                Search(out searchResult, unassignedVariables, instantiatedVariables, startTime);
            }
            catch (DeciderException)
            {
                searchResult  = StateOperationResult.Unsatisfiable;
                this.Runtime += DateTime.Now - startTime;
            }
        }
コード例 #3
0
ファイル: StateInteger.cs プロジェクト: nerdyrasa/Decider
        void IState <int> .StartSearch(out StateOperationResult searchResult, IVariable <int> optimiseVar, out IDictionary <string, IVariable <int> > solution, int timeOut)
        {
            var unassignedVariables = this.LastSolution == null
                                ? new LinkedList <IVariable <int> >(this.VariableList)
                                : new LinkedList <IVariable <int> >();
            var instantiatedVariables = this.LastSolution ?? new IVariable <int> [this.VariableList.Length];
            var startTime             = DateTime.Now;

            solution     = new Dictionary <string, IVariable <int> >();
            searchResult = StateOperationResult.Unsatisfiable;

            this.ConstraintList.Add(new ConstraintInteger((VariableInteger)optimiseVar > Int32.MinValue));

            try
            {
                while (true)
                {
                    if (this.Depth == instantiatedVariables.Length)
                    {
                        --this.Depth;
                        Backtrack(unassignedVariables, instantiatedVariables);
                        ++this.Depth;
                    }
                    else if (ConstraintsViolated())
                    {
                        throw new DeciderException("No solution found.");
                    }

                    startTime = DateTime.Now;
                    Search(out searchResult, unassignedVariables, instantiatedVariables, startTime, timeOut);

                    this.ConstraintList.RemoveAt(this.ConstraintList.Count - 1);
                    this.ConstraintList.Add(new ConstraintInteger((VariableInteger)optimiseVar > optimiseVar.InstantiatedValue));

                    // Console.WriteLine("Optimised Value: {0} ({1}s)", optimiseVar.InstantiatedValue, DateTime.Now - startTime);

                    solution = this.LastSolution.Select(v => v.Clone())
                               .Cast <IVariable <int> >()
                               .Select(v => new KeyValuePair <string, IVariable <int> >(v.Name, v))
                               .OrderBy(kvp => kvp.Key)
                               .ToDictionary(k => k.Key, v => v.Value);
                }
            }
            catch (DeciderException)
            {
                this.Runtime += DateTime.Now - startTime;
            }
        }
コード例 #4
0
ファイル: StateInteger.cs プロジェクト: nerdyrasa/Decider
        void IState <int> .StartSearch(out StateOperationResult searchResult,
                                       out IList <IDictionary <string, IVariable <int> > > solutions)
        {
            var unassignedVariables = this.LastSolution == null
                                ? new LinkedList <IVariable <int> >(this.VariableList)
                                : new LinkedList <IVariable <int> >();
            var instantiatedVariables = this.LastSolution ?? new IVariable <int> [this.VariableList.Length];
            var startTime             = DateTime.Now;

            searchResult = StateOperationResult.Unsatisfiable;
            var solutionsList = new List <IDictionary <string, IVariable <int> > >();

            try
            {
                while (true)
                {
                    if (this.Depth == instantiatedVariables.Length)
                    {
                        --this.Depth;
                        Backtrack(unassignedVariables, instantiatedVariables);
                        ++this.Depth;
                    }
                    else if (ConstraintsViolated())
                    {
                        throw new DeciderException("No solution found.");
                    }

                    startTime = DateTime.Now;
                    Search(out searchResult, unassignedVariables, instantiatedVariables, startTime);

                    solutionsList.Add(this.LastSolution.Select(v => v.Clone())
                                      .Cast <IVariable <int> >()
                                      .Select(v => new KeyValuePair <string, IVariable <int> >(v.Name, v))
                                      .OrderBy(kvp => kvp.Key)
                                      .ToDictionary(k => k.Key, v => v.Value));
                }
            }
            catch (DeciderException)
            {
                this.Runtime += DateTime.Now - startTime;
            }

            solutions = solutionsList;
        }