setAssignment() public méthode

public setAssignment ( Variable var, Object value ) : void
var Variable
value Object
Résultat void
Exemple #1
0
        private bool revise(Variable xi, Variable xj, Constraint constraint,
                            CSP csp, DomainRestoreInfo info)
        {
            bool       revised    = false;
            Assignment assignment = new Assignment();

            foreach (Object iValue in csp.getDomain(xi))
            {
                assignment.setAssignment(xi, iValue);
                bool consistentExtensionFound = false;
                foreach (Object jValue in csp.getDomain(xj))
                {
                    assignment.setAssignment(xj, jValue);
                    if (constraint.isSatisfiedWith(assignment))
                    {
                        consistentExtensionFound = true;
                        break;
                    }
                }
                if (!consistentExtensionFound)
                {
                    info.storeDomainFor(xi, csp.getDomain(xi));
                    csp.removeValueFromDomain(xi, iValue);
                    revised = true;
                }
            }
            return(revised);
        }
Exemple #2
0
        private Object getMinConflictValueFor(Variable var, Assignment assignment,
                                              CSP csp)
        {
            List <Constraint> constraints  = csp.getConstraints(var);
            Assignment        duplicate    = assignment.copy();
            int           minConflict      = int.MAX_VALUE;
            List <Object> resultCandidates = new List <Object>();

            foreach (Object value in csp.getDomain(var))
            {
                duplicate.setAssignment(var, value);
                int currConflict = countConflicts(duplicate, constraints);
                if (currConflict <= minConflict)
                {
                    if (currConflict < minConflict)
                    {
                        resultCandidates.clear();
                        minConflict = currConflict;
                    }
                    resultCandidates.Add(value);
                }
            }
            if (!resultCandidates.isEmpty())
            {
                return(Util.selectRandomlyFromList(resultCandidates));
            }
            else
            {
                return(null);
            }
        }
 /**
  * Template method, which can be configured by overriding the three
  * primitive operations below.
  */
 private Assignment recursiveBackTrackingSearch(CSP csp,
         Assignment assignment)
 {
     Assignment result = null;
     if (assignment.isComplete(csp.getVariables()))
     {
         result = assignment;
     }
     else
     {
         Variable var = selectUnassignedVariable(assignment, csp);
         foreach (Object value in orderDomainValues(var, assignment, csp))
         {
             assignment.setAssignment(var, value);
             fireStateChanged(assignment, csp);
             if (assignment.isConsistent(csp.getConstraints(var)))
             {
                 DomainRestoreInfo info = inference(var, assignment, csp);
                 if (!info.isEmpty())
                     fireStateChanged(csp);
                 if (!info.isEmptyDomainFound())
                 {
                     result = recursiveBackTrackingSearch(csp, assignment);
                     if (result != null)
                         break;
                 }
                 info.restoreDomains(csp);
             }
             assignment.removeAssignment(var);
         }
     }
     return result;
 }
        private Assignment generateRandomAssignment(CSP csp) {
		Assignment assignment = new Assignment();
        foreach (Variable var in csp.getVariables())
        {
			Object randomValue = Util.selectRandomlyFromList(csp.getDomain(var)
					.asList());
			assignment.setAssignment(var, randomValue);
		}
		return assignment;
	}
Exemple #5
0
        public Assignment copy()
        {
            Assignment copy = new Assignment();

            foreach (Variable var in variables)
            {
                copy.setAssignment(var, variableToValue.get(var));
            }
            return(copy);
        }
        private int countLostValues(Variable var, Object value, CSP csp)
        {
            int        result     = 0;
            Assignment assignment = new Assignment();

            assignment.setAssignment(var, value);
            foreach (Constraint constraint in csp.getConstraints(var))
            {
                Variable neighbor = csp.getNeighbor(var, constraint);
                foreach (Object nValue in csp.getDomain(neighbor))
                {
                    assignment.setAssignment(neighbor, nValue);
                    if (!constraint.isSatisfiedWith(assignment))
                    {
                        ++result;
                    }
                }
            }
            return(result);
        }
Exemple #7
0
        private Assignment generateRandomAssignment(CSP csp)
        {
            Assignment assignment = new Assignment();

            foreach (Variable var in csp.getVariables())
            {
                Object randomValue = Util.selectRandomlyFromList(csp.getDomain(var)
                                                                 .asList());
                assignment.setAssignment(var, randomValue);
            }
            return(assignment);
        }
        private bool revise(Variable var, Constraint constraint,
                            Assignment assignment, CSP csp, DomainRestoreInfo info)
        {
            bool revised = false;

            foreach (Object value in csp.getDomain(var))
            {
                assignment.setAssignment(var, value);
                if (!constraint.isSatisfiedWith(assignment))
                {
                    info.storeDomainFor(var, csp.getDomain(var));
                    csp.removeValueFromDomain(var, value);
                    revised = true;
                }
                assignment.removeAssignment(var);
            }
            return(revised);
        }
Exemple #9
0
        public Assignment solve(CSP csp)
        {
            Assignment assignment = generateRandomAssignment(csp);

            fireStateChanged(assignment, csp);
            for (int i = 0; i < maxSteps; i++)
            {
                if (assignment.isSolution(csp))
                {
                    return(assignment);
                }
                else
                {
                    List <Variable> vars  = getConflictedVariables(assignment, csp);
                    Variable        var   = Util.selectRandomlyFromList(vars);
                    Object          value = getMinConflictValueFor(var, assignment, csp);
                    assignment.setAssignment(var, value);
                    fireStateChanged(assignment, csp);
                }
            }
            return(null);
        }
        /**
         * Template method, which can be configured by overriding the three
         * primitive operations below.
         */
        private Assignment recursiveBackTrackingSearch(CSP csp,
                                                       Assignment assignment)
        {
            Assignment result = null;

            if (assignment.isComplete(csp.getVariables()))
            {
                result = assignment;
            }
            else
            {
                Variable var = selectUnassignedVariable(assignment, csp);
                foreach (Object value in orderDomainValues(var, assignment, csp))
                {
                    assignment.setAssignment(var, value);
                    fireStateChanged(assignment, csp);
                    if (assignment.isConsistent(csp.getConstraints(var)))
                    {
                        DomainRestoreInfo info = inference(var, assignment, csp);
                        if (!info.isEmpty())
                        {
                            fireStateChanged(csp);
                        }
                        if (!info.isEmptyDomainFound())
                        {
                            result = recursiveBackTrackingSearch(csp, assignment);
                            if (result != null)
                            {
                                break;
                            }
                        }
                        info.restoreDomains(csp);
                    }
                    assignment.removeAssignment(var);
                }
            }
            return(result);
        }
Exemple #11
0
 private bool revise(Variable xi, Variable xj, Constraint constraint,
         CSP csp, DomainRestoreInfo info)
 {
     bool revised = false;
     Assignment assignment = new Assignment();
     foreach (Object iValue in csp.getDomain(xi))
     {
         assignment.setAssignment(xi, iValue);
         bool consistentExtensionFound = false;
         foreach (Object jValue in csp.getDomain(xj))
         {
             assignment.setAssignment(xj, jValue);
             if (constraint.isSatisfiedWith(assignment))
             {
                 consistentExtensionFound = true;
                 break;
             }
         }
         if (!consistentExtensionFound)
         {
             info.storeDomainFor(xi, csp.getDomain(xi));
             csp.removeValueFromDomain(xi, iValue);
             revised = true;
         }
     }
     return revised;
 }
Exemple #12
0
 public Assignment copy()
 {
     Assignment copy = new Assignment();
     foreach (Variable var in variables)
     {
         copy.setAssignment(var, variableToValue.get(var));
     }
     return copy;
 }
        private bool revise(Variable var, Constraint constraint,
                Assignment assignment, CSP csp, DomainRestoreInfo info)
        {

            bool revised = false;
            foreach (Object value in csp.getDomain(var))
            {
                assignment.setAssignment(var, value);
                if (!constraint.isSatisfiedWith(assignment))
                {
                    info.storeDomainFor(var, csp.getDomain(var));
                    csp.removeValueFromDomain(var, value);
                    revised = true;
                }
                assignment.removeAssignment(var);
            }
            return revised;
        }
 private int countLostValues(Variable var, Object value, CSP csp)
 {
     int result = 0;
     Assignment assignment = new Assignment();
     assignment.setAssignment(var, value);
     foreach (Constraint constraint in csp.getConstraints(var))
     {
         Variable neighbor = csp.getNeighbor(var, constraint);
         foreach (Object nValue in csp.getDomain(neighbor))
         {
             assignment.setAssignment(neighbor, nValue);
             if (!constraint.isSatisfiedWith(assignment))
             {
                 ++result;
             }
         }
     }
     return result;
 }