private static void ConstructVariableList(ExpressionInteger expression, ISet <IVariable <int> > variableSet)
        {
            if (expression.Left is VariableInteger)
            {
                variableSet.Add((VariableInteger)expression.Left);
            }
            else if (expression.Left is ExpressionInteger)
            {
                ConstructVariableList((ExpressionInteger)expression.Left, variableSet);
            }

            if (expression.Right is VariableInteger)
            {
                variableSet.Add((VariableInteger)expression.Right);
            }
            else if (expression.Right is ExpressionInteger)
            {
                ConstructVariableList((ExpressionInteger)expression.Right, variableSet);
            }
        }
Exemple #2
0
        private static void ConstructVariableList(ExpressionInteger expression, ISet <IVariable <int> > variableSet)
        {
            if (expression.Left is VariableInteger)
            {
                variableSet.Add((VariableInteger)expression.Left);
            }
            else if (expression.Left is MetaExpressionInteger)
            {
                ConstructVariableList((ExpressionInteger)expression.Left, variableSet);
                foreach (var variable in ((IMetaExpression <int>)expression.Left).Support)
                {
                    variableSet.Add(variable);
                }
            }
            else if (expression.Left is ExpressionInteger)
            {
                ConstructVariableList((ExpressionInteger)expression.Left, variableSet);
            }


            if (expression.Right is VariableInteger)
            {
                variableSet.Add((VariableInteger)expression.Right);
            }
            else if (expression.Right is MetaExpressionInteger)
            {
                ConstructVariableList((ExpressionInteger)expression.Right, variableSet);
                foreach (var variable in ((IMetaExpression <int>)expression.Right).Support)
                {
                    variableSet.Add(variable);
                }
            }
            else if (expression.Right is ExpressionInteger)
            {
                ConstructVariableList((ExpressionInteger)expression.Right, variableSet);
            }
        }
Exemple #3
0
        public static ExpressionInteger operator -(ExpressionInteger left, ExpressionInteger right)
        {
            var expression = new ExpressionInteger(left, right)
            {
                evaluate       = (l, r) => l.Value - r.Value,
                evaluateBounds = (l, r) =>
                {
                    var leftBounds  = l.GetUpdatedBounds();
                    var rightBounds = r.GetUpdatedBounds();

                    return(new Bounds <int>
                           (
                               leftBounds.LowerBound - rightBounds.UpperBound,
                               leftBounds.UpperBound - rightBounds.LowerBound
                           ));
                },
                propagator = (first, second, enforce) =>
                {
                    var result = ConstraintOperationResult.Undecided;
                    if (first.Bounds.LowerBound < enforce.LowerBound + second.Bounds.LowerBound)
                    {
                        first.Bounds.LowerBound = enforce.LowerBound + second.Bounds.LowerBound;
                        result = ConstraintOperationResult.Propagated;
                    }

                    if (first.Bounds.UpperBound > enforce.UpperBound + second.Bounds.UpperBound)
                    {
                        first.Bounds.UpperBound = enforce.UpperBound + second.Bounds.UpperBound;
                        result = ConstraintOperationResult.Propagated;
                    }

                    if (second.Bounds.LowerBound < first.Bounds.LowerBound - enforce.UpperBound)
                    {
                        second.Bounds.LowerBound = first.Bounds.LowerBound - enforce.UpperBound;
                        result = ConstraintOperationResult.Propagated;
                    }

                    if (second.Bounds.UpperBound > first.Bounds.UpperBound - enforce.LowerBound)
                    {
                        second.Bounds.UpperBound = first.Bounds.UpperBound - enforce.LowerBound;
                        result = ConstraintOperationResult.Propagated;
                    }

                    if (first.Bounds.LowerBound > first.Bounds.UpperBound || second.Bounds.LowerBound > second.Bounds.UpperBound)
                    {
                        result = ConstraintOperationResult.Violated;
                    }

                    return(result);
                }
            };

            expression.remove = prune =>
            {
                var result = DomainOperationResult.ElementNotInDomain;
                if (expression.left.Bounds.UpperBound - expression.left.Bounds.LowerBound == 0)
                {
                    result = ((ExpressionInteger)expression.right).remove(expression.left.Bounds.LowerBound - prune);
                }

                if (result == DomainOperationResult.EmptyDomain)
                {
                    return(result);
                }

                if (expression.right.Bounds.UpperBound - expression.right.Bounds.LowerBound == 0)
                {
                    result = ((ExpressionInteger)expression.left).remove(expression.right.Bounds.LowerBound + prune);
                }

                return(result);
            };

            return(expression);
        }