Exemplo n.º 1
0
        // Update the walkabout strengths and stay flags of all variables
        // downstream of the given constraint. Answer a collection of
        // unsatisfied constraints sorted in order of decreasing strength.
        //
        public ArrayList removePropagateFrom(Variable outvar)
        {
            outvar.determinedBy = null;
            outvar.walkStrength = Strength.weakest;
            outvar.stay         = true;
            ArrayList unsatisfied = new ArrayList();
            ArrayList todo        = new ArrayList();

            todo.Add(outvar);
            while (!(todo.Count == 0))
            {
#if USE_STACK
                Variable v = (Variable)todo[todo.Count - 1];
                todo.RemoveAt(todo.Count - 1);
#else
                Variable v = (Variable)todo[0];
                todo.RemoveAt(0);
#endif
                for (int i = 0; i < v.constraints.Count; ++i)
                {
                    Constraint c = (Constraint)v.constraints[i];
                    if (!c.isSatisfied())
                    {
                        unsatisfied.Add(c);
                    }
                }
                Constraint determiningC = v.determinedBy;
                for (int i = 0; i < v.constraints.Count; ++i)
                {
                    Constraint nextC = (Constraint)v.constraints[i];
                    if (nextC != determiningC && nextC.isSatisfied())
                    {
                        nextC.recalculate();
                        todo.Add(nextC.output());
                    }
                }
            }
            return(unsatisfied);
        }
Exemplo n.º 2
0
        // Recompute the walkabout strengths and stay flags of all variables
        // downstream of the given constraint and recompute the actual
        // values of all variables whose stay flag is true. If a cycle is
        // detected, remove the given constraint and answer
        // false. Otherwise, answer true.
        // Details: Cycles are detected when a marked variable is
        // encountered downstream of the given constraint. The sender is
        // assumed to have marked the inputs of the given constraint with
        // the given mark. Thus, encountering a marked node downstream of
        // the output constraint means that there is a path from the
        // constraint's output to one of its inputs.
        //
        public Boolean addPropagate(Constraint c, int mark)
        {
            ArrayList todo = new ArrayList();

            todo.Add(c);
            while (!(todo.Count == 0))
            {
#if USE_STACK
                Constraint d = (Constraint)todo[todo.Count - 1];
                todo.RemoveAt(todo.Count - 1);
#else
                Constraint d = (Constraint)todo[0];
                todo.RemoveAt(0);
#endif
                if (d.output().mark == mark)
                {
                    incrementalRemove(c);
                    return(false);
                }
                d.recalculate();
                addConstraintsConsumingTo(d.output(), todo);
            }
            return(true);
        }