Exemple #1
0
            /**
             * Ignores constraints which are not binary.
             */
            private int countLostValues(CSP <VAR, VAL> csp, Assignment <VAR, VAL> assignment, VAR var, VAL value)
            {
                int result = 0;
                Assignment <VAR, VAL> assign = new Assignment <VAR, VAL>();

                assign.add(var, value);
                foreach (IConstraint <VAR, VAL> constraint in csp.getConstraints(var))
                {
                    if (constraint.getScope().Size() == 2)
                    {
                        VAR neighbor = csp.getNeighbor(var, constraint);
                        if (!assignment.contains(neighbor))
                        {
                            foreach (VAL nValue in csp.getDomain(neighbor))
                            {
                                assign.add(neighbor, nValue);
                                if (!constraint.isSatisfiedWith(assign))
                                {
                                    ++result;
                                }
                            }
                        }
                    }
                }
                return(result);
            }
Exemple #2
0
        public override Assignment <VAR, VAL> solve(CSP <VAR, VAL> csp)
        {
            Assignment <VAR, VAL> assignment = new Assignment <VAR, VAL>();
            // Select a root from the List of Variables
            VAR root = _useRandom ? Util.selectRandomlyFromList(csp.getVariables()) : csp.getVariables().Get(0);
            // Sort the variables in topological order
            ICollection <VAR> orderedVars = CollectionFactory.CreateQueue <VAR>();
            IMap <VAR, IConstraint <VAR, VAL> > parentConstraints = CollectionFactory.CreateInsertionOrderedMap <VAR, IConstraint <VAR, VAL> >();

            topologicalSort(csp, root, orderedVars, parentConstraints);
            if (csp.getDomain(root).isEmpty())
            {
                return(null); // CSP has no solution! (needed if orderedVars.size() == 1)
            }
            // Establish arc consistency from top to bottom (starting at the bottom).
            csp = csp.copyDomains(); // do not change the original CSP!
            for (int i = orderedVars.Size() - 1; i > 0; i--)
            {
                VAR var = orderedVars.Get(i);
                IConstraint <VAR, VAL> constraint = parentConstraints.Get(var);
                VAR parent = csp.getNeighbor(var, constraint);
                if (makeArcConsistent(parent, var, constraint, csp))
                {
                    fireStateChanged(csp, null, parent);
                    if (csp.getDomain(parent).isEmpty())
                    {
                        return(null); // CSP has no solution!
                    }
                }
            }

            // Assign values to variables from top to bottom.
            for (int i = 0; i < orderedVars.Size(); ++i)
            {
                VAR var = orderedVars.Get(i);
                foreach (VAL value in csp.getDomain(var))
                {
                    assignment.add(var, value);
                    if (assignment.isConsistent(csp.getConstraints(var)))
                    {
                        fireStateChanged(csp, assignment, var);
                        break;
                    }
                }
            }
            return(assignment);
        }
Exemple #3
0
        /**
         * Computes an explicit representation of the tree structure and a total order which is consistent with the
         * parent-child relations. If the provided CSP has not the required properties (CSP contains only binary
         * constraints, constraint graph is tree-structured and connected), an exception is thrown.
         *
         * @param csp               A CSP
         * @param root              A root variable
         * @param orderedVars       The computed total order (initially empty)
         * @param parentConstraints The tree structure, maps a variable to the constraint representing the arc to the parent
         *                          variable (initially empty)
         */
        private void topologicalSort(CSP <VAR, VAL> csp, VAR root, ICollection <VAR> orderedVars,
                                     IMap <VAR, IConstraint <VAR, VAL> > parentConstraints)
        {
            orderedVars.Add(root);
            parentConstraints.Put(root, null);
            int currParentIdx = -1;

            while (currParentIdx < orderedVars.Size() - 1)
            {
                currParentIdx++;
                VAR currParent          = orderedVars.Get(currParentIdx);
                int arcsPointingUpwards = 0;
                foreach (IConstraint <VAR, VAL> constraint in csp.getConstraints(currParent))
                {
                    VAR neighbor = csp.getNeighbor(currParent, constraint);
                    if (neighbor == null)
                    {
                        throw new IllegalArgumentException("Constraint " + constraint + " is not binary.");
                    }
                    if (parentConstraints.ContainsKey(neighbor))
                    { // faster than orderedVars.contains(neighbor)!
                        arcsPointingUpwards++;
                        if (arcsPointingUpwards > 1)
                        {
                            throw new IllegalArgumentException("CSP is not tree-structured.");
                        }
                    }
                    else
                    {
                        orderedVars.Add(neighbor);
                        parentConstraints.Put(neighbor, constraint);
                    }
                }
            }
            if (orderedVars.Size() < csp.getVariables().Size())
            {
                throw new IllegalArgumentException("Constraint graph is not connected.");
            }
        }