示例#1
0
        public void Test_Dictionary_Not_Updates_After_Assignment()
        {
            var a = new Assignment <int, int>();
            var d = a.AsReadOnlyDictionary();

            var newA = a.Assign(v1, 2);
            var newD = newA.AsReadOnlyDictionary();

            Assert.AreEqual(2, newD[v1]);
            Assert.IsFalse(d.ContainsKey(v1));
        }
示例#2
0
        // It might be possible to parallelize this method
        // giving to each task a different value of X
        // but I'm not sure if it's worth it or if it creates problem with
        // the paralelized IsConsistent check.
        private async Task <bool> Revise(ConstraintSatisfactionProblem <Tval> csp, Variable <Tval> variableX, Variable <Tval> variableY, InferenceResults <Tval> inference)
        {
            bool revised = false;
            Assignment <Tval> assignment    = new Assignment <Tval>();
            Domain <Tval>     oldDomain     = new Domain <Tval>(variableX.GetDomain());
            List <Tval>       domain_values = new List <Tval>(oldDomain.GetValues());

            foreach (Tval valueX in domain_values)
            {
                assignment.Assign(variableX, valueX);
                bool satisfiable = true;

                foreach (Tval valueY in variableY.GetDomain().GetValues())
                {
                    satisfiable = false;
                    assignment.Assign(variableY, valueY);
                    bool isConsistent = assignment.IsConsistent(csp.GetAllDiffConstraints());
                    if (isConsistent)
                    {
                        satisfiable = true;
                        break;
                    }
                }
                if (!satisfiable)
                {
                    variableX.GetDomain().RemoveValue(valueX);
                    revised = true;
                }
            }
            if (revised)
            {
                inference.StoreDomainForVariable(variableX, oldDomain);
            }

            return(revised);
        }
示例#3
0
    private Assignment <Tval> CreateAssignment <Tval>(Variable <Tval>[,] variables)
    {
        Assignment <Tval> assignment = new Assignment <Tval>();

        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                List <Tval> values = new List <Tval>(variables[i, j].GetDomain().GetValues());
                if (values.Count == 1)
                {
                    assignment.Assign(variables[i, j], values[0]);
                }
            }
        }
        return(assignment);
    }
示例#4
0
        private async Task <Assignment <Tval> > Backtrack(ConstraintSatisfactionProblem <Tval> csp, Assignment <Tval> assignment)
        {
            if (assignment.IsComplete(csp.GetVariables()))
            {
                return(assignment);
            }

            Variable <Tval> variable = variableSelectionStrategy.SelectUnassignedVariable(csp, assignment);

            foreach (Tval value in domainValueSelectionStragety.getOrderedDomainValues(variable, assignment, csp))
            {
                assignment.Assign(variable, value);
                OnVariableAssigned(variable, value);

                InferenceResults <Tval> inference = new InferenceResults <Tval>();
                inference.StoreDomainForVariable(variable, variable.GetDomain());
                variable.UpdateDomain(new Domain <Tval>(value));

                // it should already be consistent if it reached this point
                // since we make a pre-solving inference

                //bool isConsistent = assignment.IsConsistent(csp.GetAllDiffConstraints());
                //if (isConsistent)
                if (true)
                {
                    inference = await inferenceStrategy.Infer(csp, variable, value, inference);

                    if (inference.IsAssignmentConsistent())
                    {
                        Assignment <Tval> result = await Backtrack(csp, assignment);

                        if (result != null)
                        {
                            return(result);
                        }
                    }
                }

                assignment.RemoveAssignment(variable);
                OnAssignmentRemoved(variable, value);
                inference.RestoreOldDomains();
            }

            return(null);
        }
示例#5
0
        // we assume that, if the variable has already been assigned,
        // the old value of the variable has been removed
        // by an inference at a previous step, and therefore it is not
        // present in the domain of its neighbours, but since we might
        // have another variable that has the same value in its neighbour
        // (the assignment was not consistent) there is going to be another
        // inference to rule out inconsistent domains.

        public override async Task <InferenceResults <Tval> > UpdateVariable(ConstraintSatisfactionProblem <Tval> csp, Assignment <Tval> assignment, Variable <Tval> variable, Tval value)
        {
            if (csp == null)
            {
                throw new ArgumentNullException("csp");
            }
            if (assignment == null)
            {
                throw new ArgumentNullException("assignment");
            }
            if (variable == null)
            {
                throw new ArgumentNullException("variable");
            }

            InferenceResults <Tval> removeInference = await RemoveVariable(csp, assignment, variable);

            assignment.Assign(variable, value);
            variable.UpdateDomain(new Domain <Tval>(value));
            return(await inferenceStrategy.Infer(csp, variable, value, removeInference, false));
        }
示例#6
0
        public void Test_Constructor_Values()
        {
            var v         = new Variable <int, int>(1, new List <int>());
            var variables = new List <Variable <int, int> > {
                v
            };

            var c           = new Mock <IConstraint <int, int> >().Object;
            var constraints = new List <IConstraint <int, int> > {
                c
            };

            var a = new Assignment <int, int>();

            a.Assign(v, 1);

            var p = new Problem <int, int>(variables, constraints, a);

            Assert.AreEqual(v, p.Variables.ToArray()[0]);
            Assert.AreEqual(c, p.Constraints.ToArray()[0]);
            Assert.AreEqual(a.GetValue(v), p.InitialAssignment.GetValue(v));
        }