public IEnumerable <IVariable> Calculate(IMilpManager milpManager, CompositeOperationType type, ICompositeOperationParameters parameters,
                                                 params IVariable[] arguments)
        {
            if (!SupportsOperation(type, parameters, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, parameters, arguments));
            }
            var typedParameters = parameters as ApproximateParameters;
            var x = arguments.First();

            if (arguments.All(i => i.IsConstant()))
            {
                return(new [] { milpManager.FromConstant(typedParameters.Function(x.ConstantValue.Value)) });
            }

            var one       = milpManager.FromConstant(1);
            var points    = typedParameters.Arguments.Select(a => Tuple.Create(milpManager.FromConstant(a), milpManager.FromConstant(typedParameters.Function(a)))).ToArray();
            var variables = points.Select(p => milpManager.CreateAnonymous(typedParameters.ArgumentMustBeOnAGrid ? Domain.BinaryInteger : Domain.PositiveOrZeroReal).Set(ConstraintType.LessOrEqual, one)).ToArray();

            x.Set(ConstraintType.Equal, milpManager.Operation(OperationType.Addition, points.Select((point, index) => variables[index].Operation(OperationType.Multiplication, point.Item1)).ToArray()));
            var y = milpManager.Operation(OperationType.Addition, points.Select((point, index) => variables[index].Operation(OperationType.Multiplication, point.Item2)).ToArray());

            milpManager.Operation(OperationType.Addition, variables).Set(ConstraintType.Equal, one);
            milpManager.Set(CompositeConstraintType.SpecialOrderedSetType2, variables.First(), variables.Skip(1).ToArray());

            y.ConstantValue = x.IsConstant() ? typedParameters.Function(x.ConstantValue.Value) : (double?)null;
            y.Expression    = $"approximation({typedParameters.FunctionDescription})";

            return(new[] { y });
        }
 public IVariable Set(IMilpManager milpManager, ConstraintType type, IVariable leftVariable,
     IVariable rightVariable)
 {
     switch (type)
     {
         case ConstraintType.Equal:
             milpManager.SetEqual(leftVariable, rightVariable);
             leftVariable.ConstantValue = rightVariable.ConstantValue ?? leftVariable.ConstantValue;
             rightVariable.ConstantValue = leftVariable.ConstantValue ?? rightVariable.ConstantValue;
             break;
         case ConstraintType.LessOrEqual:
             milpManager.SetLessOrEqual(leftVariable, rightVariable);
             break;
         case ConstraintType.GreaterOrEqual:
             milpManager.SetGreaterOrEqual(leftVariable, rightVariable);
             break;
         case ConstraintType.LessThan:
             milpManager.Operation(OperationType.IsLessThan, leftVariable, rightVariable)
                 .Set(ConstraintType.Equal, milpManager.FromConstant(1));
             break;
         case ConstraintType.GreaterThan:
             milpManager.Operation(OperationType.IsGreaterThan, leftVariable, rightVariable)
                 .Set(ConstraintType.Equal, milpManager.FromConstant(1));
             break;
         case ConstraintType.NotEqual:
             milpManager.Operation(OperationType.IsNotEqual, leftVariable, rightVariable)
                 .Set(ConstraintType.Equal, milpManager.FromConstant(1));
             break;
         default:
             throw new InvalidOperationException("Cannot set constraint");
     }
     return leftVariable;
 }
        public IEnumerable<IVariable> Calculate(IMilpManager milpManager, CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, parameters, arguments)) throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, parameters, arguments));
            var typedParameters = parameters as NthElementsParameters;
            if (arguments.All(a => a.IsConstant()) && typedParameters.Indexes.All(a => a.IsConstant()))
            {
                var sorted = arguments.OrderBy(a => a.ConstantValue.Value).ToArray();
                return typedParameters.Indexes.Select(i => sorted[(int)i.ConstantValue.Value]);
            }
            var variables = new List<IVariable>();
            var sums = arguments.Select(a => Tuple.Create(a, milpManager.Operation(OperationType.Addition,
                arguments.Where(b => a != b).Select(b => a.Operation(OperationType.IsGreaterOrEqual, b).Create()).ToArray()).Create())).ToArray();

            var huge = milpManager.FromConstant(milpManager.MaximumIntegerValue);
            foreach(var indexVariable in typedParameters.Indexes)
            {
                var result = huge;
                foreach (var sum in sums)
                {
                    result = result.Operation(OperationType.Minimum, milpManager.Operation(OperationType.Condition,
                        sum.Item2.Operation(OperationType.IsGreaterOrEqual, indexVariable),
                        sum.Item1,
                        huge
                    ));
                }

                var singleVariable = result.Create();
                singleVariable.Expression = $"nthElement(index: {indexVariable.FullExpression()}, {string.Join(",", arguments.Select(a => a.FullExpression()).ToArray())})";
                variables.Add(singleVariable);
            }

            return variables;
        }
        public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            }
            if (arguments.All(a => a.IsConstant()))
            {
                var constantResult = Math.Pow(arguments[0].ConstantValue.Value, arguments[1].ConstantValue.Value);
                if (arguments.All(a => a.IsInteger()))
                {
                    return(milpManager.FromConstant((int)constantResult));
                }
                else
                {
                    return(milpManager.FromConstant(constantResult));
                }
            }
            var number = arguments[0];
            var power  = arguments[1];

            var one  = milpManager.FromConstant(1);
            var zero = milpManager.FromConstant(0);
            var isNumberLessOrEqualOne = number.Operation(OperationType.IsLessOrEqual, one);
            var isPowerZero            = power.Operation(OperationType.IsLessOrEqual, zero);
            var isPowerOne             = power.Operation(OperationType.IsEqual, one);
            var isEdgeCase             = milpManager.Operation(OperationType.Disjunction, isNumberLessOrEqualOne, isPowerZero, isPowerOne);
            var result = milpManager.Operation(
                OperationType.Condition,
                isPowerZero,
                one,
                milpManager.Operation(
                    OperationType.Condition,
                    isNumberLessOrEqualOne,
                    number,
                    milpManager.Operation(
                        OperationType.Condition,
                        isPowerOne,
                        number,
                        CalculatePower(number, power, milpManager, isEdgeCase)
                        )
                    )
                );

            result.ConstantValue = number.ConstantValue.HasValue && power.ConstantValue.HasValue
                ? number.ConstantValue == 0
                    ? 0.0
                    : power.ConstantValue == 0
                        ? number.ConstantValue
                        : Math.Pow(number.ConstantValue.Value, power.ConstantValue.Value)
                : (double?)null;
            result.Expression = $"{number.FullExpression()} ** {power.FullExpression()}";
            return(result);
        }
        public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments)) throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            if (arguments.All(a => a.IsConstant()))
            {
                var constantResult = Math.Pow(arguments[0].ConstantValue.Value, arguments[1].ConstantValue.Value);
                if (arguments.All(a => a.IsInteger()))
                {
                    return milpManager.FromConstant((int) constantResult);
                }
                else
                {
                    return milpManager.FromConstant(constantResult);
                }
            }
            var number = arguments[0];
            var power = arguments[1];

            var one = milpManager.FromConstant(1);
            var zero = milpManager.FromConstant(0);
            var isNumberLessOrEqualOne = number.Operation(OperationType.IsLessOrEqual, one);
            var isPowerZero = power.Operation(OperationType.IsLessOrEqual, zero);
            var isPowerOne = power.Operation(OperationType.IsEqual, one);
            var isEdgeCase = milpManager.Operation(OperationType.Disjunction, isNumberLessOrEqualOne, isPowerZero, isPowerOne);
            var result = milpManager.Operation(
                OperationType.Condition,
                isPowerZero,
                one,
                milpManager.Operation(
                    OperationType.Condition,
                    isNumberLessOrEqualOne,
                    number,
                    milpManager.Operation(
                        OperationType.Condition,
                        isPowerOne,
                        number,
                        CalculatePower(number, power, milpManager, isEdgeCase)
                    )
                )
            );

            result.ConstantValue = number.ConstantValue.HasValue && power.ConstantValue.HasValue
                ? number.ConstantValue == 0
                    ? 0.0
                    : power.ConstantValue == 0
                        ? number.ConstantValue
                        : Math.Pow(number.ConstantValue.Value, power.ConstantValue.Value)
                : (double?) null;
            result.Expression = $"{number.FullExpression()} ** {power.FullExpression()}";
            return result;
        }
        public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments)) throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            if (arguments.All(a => a.IsConstant()))
            {
                return milpManager.FromConstant(arguments.Select(a => (int)a.ConstantValue).Aggregate((a, b) => (a + b) % 2));
            }

            var variable = arguments.Aggregate((a,b) =>
                milpManager.Operation(OperationType.Disjunction,
                milpManager.Operation(OperationType.Conjunction, a.Operation(OperationType.BinaryNegation), b),
                milpManager.Operation(OperationType.Conjunction, a, b.Operation(OperationType.BinaryNegation))));
            variable.Expression = $"{string.Join(" ^ ", arguments.Select(a => a.FullExpression()).ToArray())}";
            return variable;
        }
示例#7
0
        private static IEnumerable <IVariable> CalculateForVariable(IMilpManager milpManager, IVariable[] arguments, uint decompositionBase)
        {
            List <Tuple <IVariable, int> > variables =
                Enumerable.Range(0, GetDigitsCount(milpManager, decompositionBase))
                .Select(i =>
            {
                var baseRaised = (int)Math.Pow(decompositionBase, i);
                var variable   = milpManager.CreateAnonymous(decompositionBase == 2 ? Domain.BinaryInteger : Domain.PositiveOrZeroInteger);
                if (decompositionBase > 2)
                {
                    variable = variable.Set(ConstraintType.LessOrEqual, milpManager.FromConstant((int)decompositionBase - 1));
                }
                return(Tuple.Create(variable, baseRaised));
            })
                .ToList();

            milpManager.Operation(OperationType.Addition,
                                  variables.Select(v => v.Item1.Operation(OperationType.Multiplication, milpManager.FromConstant(v.Item2)))
                                  .ToArray()).Set(ConstraintType.Equal, arguments[0]);

            return(variables.Select((v, index) => {
                var result = v.Item1;
                result.Expression = $"decomposition(digit: {index}, base: {decompositionBase}, {arguments[0].FullExpression()})";
                return result;
            }));
        }
        public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            }
            if (arguments.All(a => a.IsConstant()))
            {
                var constantArgument = (int)arguments[0].ConstantValue.Value;
                var constantResult   = constantArgument == 0 ? 1 : Enumerable.Range(1, constantArgument).Aggregate((a, b) => a * b);
                return(milpManager.FromConstant(constantResult));
            }
            var number = arguments[0];
            var one    = milpManager.FromConstant(1);
            var result = one;

            for (int i = SoundBoundary(milpManager.MaximumIntegerValue); i >= 0; --i)
            {
                result = result.Operation(OperationType.Multiplication,
                                          milpManager.Operation(OperationType.Maximum, one,
                                                                number.Operation(OperationType.Subtraction, milpManager.FromConstant(i))));
            }

            var finalResult = result.ChangeDomain(Domain.PositiveOrZeroInteger);

            finalResult.Expression = $"{number.FullExpression()}!";
            return(finalResult);
        }
示例#9
0
        public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            }
            if (arguments[0].IsConstant())
            {
                if (arguments[0].ConstantValue.Value <= milpManager.Epsilon)
                {
                    return(arguments[2]);
                }
                return(arguments[1]);
            }
            var trueBranch  = arguments[0].Operation(OperationType.Multiplication, arguments[1]);
            var falseBranch = arguments[0].Operation(OperationType.BinaryNegation)
                              .Operation(OperationType.Multiplication, arguments[2]);
            var result = milpManager.Create(milpManager.Operation(OperationType.Addition,
                                                                  trueBranch,
                                                                  falseBranch
                                                                  ).ChangeDomain(trueBranch.LowestEncompassingDomain(falseBranch)));

            result.Expression    = $"{arguments[0].FullExpression()} ? {arguments[1].FullExpression()} : {arguments[2].FullExpression()}";
            result.ConstantValue = !arguments[0].ConstantValue.HasValue
                ? null
                : (int)arguments[0].ConstantValue.Value == 1 ? trueBranch.ConstantValue : falseBranch.ConstantValue;
            return(result);
        }
        public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
        {
            if(!SupportsOperation(type,arguments)) throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            if (arguments.All(x => x.IsConstant()))
            {
                if (arguments[0].IsInteger())
                {
                    return milpManager.FromConstant(Math.Abs((int)arguments[0].ConstantValue.Value));
                }
                return milpManager.FromConstant(Math.Abs(arguments[0].ConstantValue.Value));
            }

            var number = arguments[0];
            var numberNegated = number.Operation(OperationType.Negation);
            var result = milpManager.CreateAnonymous(number.IsInteger() ? Domain.PositiveOrZeroInteger : Domain.PositiveOrZeroReal);

            result.Set(ConstraintType.GreaterOrEqual, number)
                .Set(ConstraintType.GreaterOrEqual, numberNegated);

            milpManager.Operation(OperationType.Addition,
                    result.Operation(OperationType.IsEqual, number),
                    result.Operation(OperationType.IsEqual, numberNegated))
                .Set(ConstraintType.GreaterOrEqual, milpManager.FromConstant(1));

            result.ConstantValue = number.ConstantValue.HasValue ? Math.Abs(number.ConstantValue.Value) : number.ConstantValue;
            result.Expression = $"|{number.FullExpression()}|";
            return result;
        }
        private static IEnumerable<IVariable> CalculateForVariable(IMilpManager milpManager, IVariable[] arguments, uint decompositionBase)
        {
            List<Tuple<IVariable, int>> variables =
                Enumerable.Range(0, GetDigitsCount(milpManager, decompositionBase))
                    .Select(i =>
                    {
                        var baseRaised = (int)Math.Pow(decompositionBase, i);
                        var variable = milpManager.CreateAnonymous(decompositionBase == 2 ? Domain.BinaryInteger : Domain.PositiveOrZeroInteger);
                        if (decompositionBase > 2)
                        {
                            variable = variable.Set(ConstraintType.LessOrEqual,milpManager.FromConstant((int) decompositionBase - 1));
                        }
                        return Tuple.Create(variable, baseRaised);
                    })
                    .ToList();

            milpManager.Operation(OperationType.Addition,
                variables.Select(v => v.Item1.Operation(OperationType.Multiplication, milpManager.FromConstant(v.Item2)))
                    .ToArray()).Set(ConstraintType.Equal, arguments[0]);

            return variables.Select((v, index) => {
                var result = v.Item1;
                result.Expression = $"decomposition(digit: {index}, base: {decompositionBase}, {arguments[0].FullExpression()})";
                return result;
            });
        }
示例#12
0
        public IEnumerable <IVariable> Calculate(IMilpManager milpManager, CompositeOperationType type, ICompositeOperationParameters parameters,
                                                 params IVariable[] arguments)
        {
            if (!SupportsOperation(type, parameters, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, parameters, arguments));
            }
            var index = ((ArraySetParameters)parameters).Index;
            var value = ((ArraySetParameters)parameters).Value;

            if (index.IsConstant())
            {
                arguments[(int)index.ConstantValue.Value] = value;
                return(arguments);
            }

            var catenatedArguments = string.Join(", ", arguments.Select(a => a.FullExpression()).ToArray());

            for (int i = 0; i < arguments.Length; ++i)
            {
                arguments[i]            = milpManager.Operation(OperationType.Condition, milpManager.FromConstant(i).Operation(OperationType.IsEqual, index), value, arguments[i]);
                arguments[i].Expression = $"arraySet(wantedIndex: {index.FullExpression()}, value: {value.FullExpression()}, inArrayIndex: {i}, {catenatedArguments})";
            }

            return(arguments);
        }
示例#13
0
        public IVariable Set(IMilpManager milpManager, CompositeConstraintType type, ICompositeConstraintParameters parameters,
                             IVariable leftVariable, params IVariable[] rightVariable)
        {
            var maximumIntegerValue = milpManager.FromConstant(milpManager.MaximumIntegerValue);
            var one = milpManager.FromConstant(1);

            var allVariables = new[] { leftVariable }.Concat(rightVariable).ToArray();
            var boundaryVariables = allVariables.Select(v => milpManager.CreateAnonymous(Domain.BinaryInteger)).ToArray();

            milpManager.Operation(OperationType.Addition, boundaryVariables).Set(ConstraintType.LessOrEqual, one);

            for (int i = 0; i < allVariables.Length; ++i)
            {
                IVariable sum = boundaryVariables[i];
                if (i < allVariables.Length - 1)
                {
                    sum = sum.Operation(OperationType.Addition, boundaryVariables[i + 1]);
                }
                allVariables[i]
                .Set(ConstraintType.LessOrEqual, sum.Operation(OperationType.Multiplication, maximumIntegerValue))
                .Set(ConstraintType.GreaterOrEqual, sum.Operation(OperationType.Multiplication, maximumIntegerValue).Operation(OperationType.Negation));
            }

            return(leftVariable);
        }
示例#14
0
        public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            }
            if (arguments.All(x => x.IsConstant()))
            {
                if (arguments[0].IsInteger())
                {
                    return(milpManager.FromConstant(Math.Abs((int)arguments[0].ConstantValue.Value)));
                }
                return(milpManager.FromConstant(Math.Abs(arguments[0].ConstantValue.Value)));
            }

            var number        = arguments[0];
            var numberNegated = number.Operation(OperationType.Negation);
            var result        = milpManager.CreateAnonymous(number.IsInteger() ? Domain.PositiveOrZeroInteger : Domain.PositiveOrZeroReal);

            result.Set(ConstraintType.GreaterOrEqual, number)
            .Set(ConstraintType.GreaterOrEqual, numberNegated);

            milpManager.Operation(OperationType.Addition,
                                  result.Operation(OperationType.IsEqual, number),
                                  result.Operation(OperationType.IsEqual, numberNegated))
            .Set(ConstraintType.GreaterOrEqual, milpManager.FromConstant(1));

            result.ConstantValue = number.ConstantValue.HasValue ? Math.Abs(number.ConstantValue.Value) : number.ConstantValue;
            result.Expression    = $"|{number.FullExpression()}|";
            return(result);
        }
示例#15
0
        public IEnumerable <IVariable> Calculate(IMilpManager milpManager, CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, parameters, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, parameters, arguments));
            }
            if (arguments.All(a => a.IsConstant()))
            {
                return(arguments.OrderBy(a => a.ConstantValue.Value));
            }
            var castedParameters = parameters as CountingSortParameters;
            var values           = castedParameters.Values;
            var valuesWithCounts = new Dictionary <IVariable, IVariable>();
            var zero             = milpManager.FromConstant(0);

            foreach (var value in values)
            {
                valuesWithCounts[value] = arguments.Aggregate(zero,
                                                              (current, val) =>
                                                              current.Operation(OperationType.Addition, val.Operation(OperationType.IsEqual, value)));
            }

            var sum = zero;

            foreach (var value in values)
            {
                sum = sum.Operation(OperationType.Addition, valuesWithCounts[value]);
                valuesWithCounts[value] = sum;
            }

            var infinity = milpManager.FromConstant(milpManager.MaximumIntegerValue);
            var results  = Enumerable.Range(1, arguments.Length).Select(p =>
            {
                var position = milpManager.FromConstant(p);
                var result   = milpManager.Operation(OperationType.Minimum,
                                                     values.Select(value =>
                                                                   milpManager.Operation(OperationType.Condition,
                                                                                         position.Operation(OperationType.IsLessOrEqual, valuesWithCounts[value]), value, infinity)
                                                                   ).ToArray());
                result.Expression = $"countingSort(position: {p}, {string.Join(", ", arguments.Select(a => a.FullExpression()).ToArray())})";
                return(result);
            }).ToArray();

            return(results);
        }
 public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
 {
     if (!SupportsOperation(type, arguments)) throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
     if (arguments.Length > 2)
     {
         return arguments[0].Operation(type, milpManager.Operation(type, arguments.Skip(1).ToArray()));
     }
     return CalculateForTwoVariables(milpManager, type, arguments);
 }
示例#17
0
        public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            }
            if (arguments.All(a => a.IsConstant()))
            {
                return(milpManager.FromConstant(arguments.Select(a => (int)a.ConstantValue).Aggregate((a, b) => (a + b) % 2)));
            }

            var variable = arguments.Aggregate((a, b) =>
                                               milpManager.Operation(OperationType.Disjunction,
                                                                     milpManager.Operation(OperationType.Conjunction, a.Operation(OperationType.BinaryNegation), b),
                                                                     milpManager.Operation(OperationType.Conjunction, a, b.Operation(OperationType.BinaryNegation))));

            variable.Expression = $"{string.Join(" ^ ", arguments.Select(a => a.FullExpression()).ToArray())}";
            return(variable);
        }
示例#18
0
 public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
 {
     if (!SupportsOperation(type, arguments))
     {
         throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
     }
     if (arguments.Length > 2)
     {
         return(arguments[0].Operation(type, milpManager.Operation(type, arguments.Skip(1).ToArray())));
     }
     return(CalculateForTwoVariables(milpManager, type, arguments));
 }
示例#19
0
        public IEnumerable<IVariable> Calculate(IMilpManager milpManager, CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, parameters, arguments)) throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, parameters, arguments));
            if (arguments.All(a => a.IsConstant()))
            {
                return arguments.OrderBy(a => a.ConstantValue.Value);
            }
            var castedParameters = parameters as CountingSortParameters;
            var values = castedParameters.Values;
            var valuesWithCounts = new Dictionary<IVariable, IVariable>();
            var zero = milpManager.FromConstant(0);
            foreach (var value in values)
            {
                valuesWithCounts[value] = arguments.Aggregate(zero,
                    (current, val) =>
                        current.Operation(OperationType.Addition, val.Operation(OperationType.IsEqual, value)));
            }

            var sum = zero;
            foreach (var value in values)
            {
                sum = sum.Operation(OperationType.Addition, valuesWithCounts[value]);
                valuesWithCounts[value] = sum;
            }

            var infinity = milpManager.FromConstant(milpManager.MaximumIntegerValue);
            var results = Enumerable.Range(1, arguments.Length).Select(p =>
            {
                var position = milpManager.FromConstant(p);
                var result = milpManager.Operation(OperationType.Minimum,
                    values.Select(value =>
                        milpManager.Operation(OperationType.Condition,
                            position.Operation(OperationType.IsLessOrEqual, valuesWithCounts[value]), value, infinity)
                        ).ToArray());
                result.Expression = $"countingSort(position: {p}, {string.Join(", ", arguments.Select(a => a.FullExpression()).ToArray())})";
                return result;
            }).ToArray();

            return results;
        }
        public IVariable Set(IMilpManager milpManager, ConstraintType type, IVariable leftVariable,
                             IVariable rightVariable)
        {
            switch (type)
            {
            case ConstraintType.Equal:
                milpManager.SetEqual(leftVariable, rightVariable);
                leftVariable.ConstantValue  = rightVariable.ConstantValue ?? leftVariable.ConstantValue;
                rightVariable.ConstantValue = leftVariable.ConstantValue ?? rightVariable.ConstantValue;
                break;

            case ConstraintType.LessOrEqual:
                milpManager.SetLessOrEqual(leftVariable, rightVariable);
                break;

            case ConstraintType.GreaterOrEqual:
                milpManager.SetGreaterOrEqual(leftVariable, rightVariable);
                break;

            case ConstraintType.LessThan:
                milpManager.Operation(OperationType.IsLessThan, leftVariable, rightVariable)
                .Set(ConstraintType.Equal, milpManager.FromConstant(1));
                break;

            case ConstraintType.GreaterThan:
                milpManager.Operation(OperationType.IsGreaterThan, leftVariable, rightVariable)
                .Set(ConstraintType.Equal, milpManager.FromConstant(1));
                break;

            case ConstraintType.NotEqual:
                milpManager.Operation(OperationType.IsNotEqual, leftVariable, rightVariable)
                .Set(ConstraintType.Equal, milpManager.FromConstant(1));
                break;

            default:
                throw new InvalidOperationException("Cannot set constraint");
            }
            return(leftVariable);
        }
示例#21
0
        public IVariable Calculate(IMilpManager milpManager, GoalType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            }
            if (arguments.All(a => a.IsConstant()))
            {
                return(milpManager.Operation(OperationType.Minimum, arguments));
            }

            return(milpManager.MakeGoal(GoalType.MaximizeMaximum, arguments).MakeGoal(GoalType.Minimize));
        }
示例#22
0
        public IEnumerable <IVariable> Calculate(IMilpManager milpManager, CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, parameters, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, parameters, arguments));
            }
            var typedParameters = parameters as NthElementsParameters;

            if (arguments.All(a => a.IsConstant()) && typedParameters.Indexes.All(a => a.IsConstant()))
            {
                var sorted = arguments.OrderBy(a => a.ConstantValue.Value).ToArray();
                return(typedParameters.Indexes.Select(i => sorted[(int)i.ConstantValue.Value]));
            }
            var variables = new List <IVariable>();
            var sums      = arguments.Select(a => Tuple.Create(a, milpManager.Operation(OperationType.Addition,
                                                                                        arguments.Where(b => a != b).Select(b => a.Operation(OperationType.IsGreaterOrEqual, b).Create()).ToArray()).Create())).ToArray();

            var huge = milpManager.FromConstant(milpManager.MaximumIntegerValue);

            foreach (var indexVariable in typedParameters.Indexes)
            {
                var result = huge;
                foreach (var sum in sums)
                {
                    result = result.Operation(OperationType.Minimum, milpManager.Operation(OperationType.Condition,
                                                                                           sum.Item2.Operation(OperationType.IsGreaterOrEqual, indexVariable),
                                                                                           sum.Item1,
                                                                                           huge
                                                                                           ));
                }


                var singleVariable = result.Create();
                singleVariable.Expression = $"nthElement(index: {indexVariable.FullExpression()}, {string.Join(",", arguments.Select(a => a.FullExpression()).ToArray())})";
                variables.Add(singleVariable);
            }

            return(variables);
        }
        public IVariable Calculate(IMilpManager milpManager, GoalType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments)) throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            if (arguments.All(a => a.IsConstant()))
            {
                return milpManager.Operation(OperationType.Maximum, arguments);
            }

            var result = milpManager.CreateAnonymous(arguments.Any(a => a.IsReal()) ? Domain.AnyReal : Domain.AnyInteger);
            arguments.Aggregate(milpManager.FromConstant(0),(existing, next) => existing.Operation(OperationType.Disjunction, result.Operation(OperationType.IsEqual, next)))
                .Set(ConstraintType.Equal, milpManager.FromConstant(1));

            return result;
        }
        private IVariable MultiplyIntegers(IMilpManager baseMilpManager, Domain domain, IVariable[] arguments)
        {
            var binaries    = arguments.Where(a => a.IsBinary()).ToArray();
            var nonBinaries = arguments.Where(a => !a.IsBinary()).ToArray();

            if (binaries.Any())
            {
                IVariable conjucted = baseMilpManager.Operation(OperationType.Multiplication, binaries);
                return(MultipleByBinaryDigit(baseMilpManager, nonBinaries[0], conjucted).ChangeDomain(domain)
                       .Operation(OperationType.Multiplication, nonBinaries.Skip(1).ToArray()));
            }

            return(MultiplyNonBinaryIntegers(baseMilpManager, nonBinaries, domain));
        }
        private static IVariable CalculatePower(IVariable number, IVariable power, IMilpManager milpManager, IVariable isEdgeCase)
        {
            var digits = (int)Math.Ceiling(Math.Log(milpManager.IntegerWidth, 2.0));

            var infinity      = milpManager.FromConstant(milpManager.MaximumIntegerValue);
            var currentPower  = milpManager.Operation(OperationType.Minimum, number, isEdgeCase.Operation(OperationType.BinaryNegation).Operation(OperationType.Multiplication, infinity));
            var decomposition = power.CompositeOperation(CompositeOperationType.UnsignedMagnitudeDecomposition).Take(digits).ToArray();
            var one           = milpManager.FromConstant(1);
            var result        = one;

            for (int i = 0; i < digits; ++i)
            {
                if (i > 0)
                {
                    var isAnyNonzeroDigitLater = milpManager.Operation(OperationType.Disjunction, decomposition.Skip(i).ToArray());
                    var numberToMultiply       = milpManager.Operation(OperationType.Minimum, currentPower, isAnyNonzeroDigitLater.Operation(OperationType.Multiplication, infinity));
                    currentPower = numberToMultiply.Operation(OperationType.Multiplication, numberToMultiply);
                }

                result = result.Operation(OperationType.Multiplication, one.Operation(OperationType.Maximum, currentPower.Operation(OperationType.Multiplication, decomposition[i])));
            }

            return(result);
        }
示例#26
0
        public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments)) throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            if (arguments.All(a => a.IsConstant()))
            {
                return milpManager.FromConstant(arguments[0].ConstantValue.Value < arguments[1].ConstantValue.Value ? 1 : 0);
            }
            var result = milpManager.Operation(OperationType.IsGreaterThan, arguments[1], arguments[0]);

            result.ConstantValue = arguments.All(a => a.ConstantValue.HasValue)
                ? arguments[0].ConstantValue < arguments[1].ConstantValue ? 1 : 0
                : (double?)null;
            result.Expression = $"{arguments[0].FullExpression()} ?< {arguments[1].FullExpression()}";
            return result;
        }
        private static IVariable CalculatePower(IVariable number, IVariable power, IMilpManager milpManager, IVariable isEdgeCase)
        {
            var digits = (int)Math.Ceiling(Math.Log(milpManager.IntegerWidth, 2.0));

            var infinity = milpManager.FromConstant(milpManager.MaximumIntegerValue);
            var currentPower = milpManager.Operation(OperationType.Minimum, number, isEdgeCase.Operation(OperationType.BinaryNegation).Operation(OperationType.Multiplication, infinity));
            var decomposition = power.CompositeOperation(CompositeOperationType.UnsignedMagnitudeDecomposition).Take(digits).ToArray();
            var one = milpManager.FromConstant(1);
            var result = one;

            for (int i = 0; i < digits; ++i)
            {
                if (i > 0)
                {
                    var isAnyNonzeroDigitLater = milpManager.Operation(OperationType.Disjunction, decomposition.Skip(i).ToArray());
                    var numberToMultiply = milpManager.Operation(OperationType.Minimum, currentPower, isAnyNonzeroDigitLater.Operation(OperationType.Multiplication, infinity));
                    currentPower = numberToMultiply.Operation(OperationType.Multiplication, numberToMultiply);
                }

                result = result.Operation(OperationType.Multiplication, one.Operation(OperationType.Maximum, currentPower.Operation(OperationType.Multiplication, decomposition[i])));
            }

            return result;
        }
        public IVariable Calculate(IMilpManager milpManager, GoalType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments)) throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            if (arguments.All(a => a.IsConstant()))
            {
                return milpManager.Operation(OperationType.Minimum, arguments);
            }

            var result = milpManager.CreateAnonymous(arguments.Any(a => a.IsReal()) ? Domain.AnyReal : Domain.AnyInteger);
            foreach (var argument in arguments)
            {
                result.Set(ConstraintType.LessOrEqual, argument);
            }

            return result;
        }
        public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            }
            if (arguments.Length == 1)
            {
                return(arguments[0]);
            }
            if (MultiplyOnlyConstants(arguments))
            {
                var result = arguments.Select(a => a.ConstantValue.Value).Aggregate((a, b) => a * b);
                if (arguments.All(a => a.IsInteger()))
                {
                    return(milpManager.FromConstant((int)result));
                }
                else
                {
                    return(milpManager.FromConstant(result));
                }
            }

            var domain = CalculateDomain(arguments);

            if (MultiplyAtMostOneNonconstant(arguments))
            {
                return(arguments.Aggregate((x, y) =>
                {
                    var result = y.IsConstant()
                        ? milpManager.MultiplyVariableByConstant(x, y, domain)
                        : milpManager.MultiplyVariableByConstant(y, x, domain);
                    result.ConstantValue = x.ConstantValue * y.ConstantValue;
                    result.Expression = $"{x.FullExpression()} * {y.FullExpression()}";
                    return result;
                }));
            }

            if (MultiplyBinaryVariables(arguments))
            {
                return(milpManager.Operation(OperationType.Conjunction, arguments));
            }

            return(MultiplyIntegers(milpManager, domain, arguments));
        }
示例#30
0
        public IVariable Set(IMilpManager milpManager, CompositeConstraintType type, ICompositeConstraintParameters parameters,
            IVariable leftVariable, params IVariable[] rightVariable)
        {
            var one = milpManager.FromConstant(1);
            var maximumIntegerValue = milpManager.FromConstant(milpManager.MaximumIntegerValue);

            var allVariables = new[] {leftVariable}.Concat(rightVariable).ToArray();
            var boundaryVariables = allVariables.Select(v => milpManager.CreateAnonymous(Domain.BinaryInteger)).ToArray();
            milpManager.Operation(OperationType.Addition, boundaryVariables).Set(ConstraintType.LessOrEqual, one);
            foreach (var pair in allVariables.Zip(boundaryVariables, Tuple.Create))
            {
                pair.Item1
                    .Set(ConstraintType.LessOrEqual, pair.Item2.Operation(OperationType.Multiplication, maximumIntegerValue))
                    .Set(ConstraintType.GreaterOrEqual, pair.Item2.Operation(OperationType.Multiplication, maximumIntegerValue).Operation(OperationType.Negation));
            }

            return leftVariable;
        }
示例#31
0
        public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            }
            if (arguments.All(a => a.IsConstant()))
            {
                return(milpManager.FromConstant(arguments[0].ConstantValue.Value < arguments[1].ConstantValue.Value ? 1 : 0));
            }
            var result = milpManager.Operation(OperationType.IsGreaterThan, arguments[1], arguments[0]);

            result.ConstantValue = arguments.All(a => a.ConstantValue.HasValue)
                ? arguments[0].ConstantValue < arguments[1].ConstantValue ? 1 : 0
                : (double?)null;
            result.Expression = $"{arguments[0].FullExpression()} ?< {arguments[1].FullExpression()}";
            return(result);
        }
        public IVariable Calculate(IMilpManager milpManager, GoalType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            }
            if (arguments.All(a => a.IsConstant()))
            {
                return(milpManager.Operation(OperationType.Maximum, arguments));
            }

            var result = milpManager.CreateAnonymous(arguments.Any(a => a.IsReal()) ? Domain.AnyReal : Domain.AnyInteger);

            arguments.Aggregate(milpManager.FromConstant(0), (existing, next) => existing.Operation(OperationType.Disjunction, result.Operation(OperationType.IsEqual, next)))
            .Set(ConstraintType.Equal, milpManager.FromConstant(1));

            return(result);
        }
示例#33
0
        public IEnumerable <IVariable> Calculate(IMilpManager milpManager, CompositeOperationType type, ICompositeOperationParameters parameters,
                                                 params IVariable[] arguments)
        {
            if (!SupportsOperation(type, parameters, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, parameters, arguments));
            }
            var options = parameters as LoopParameters;

            var totalBound = milpManager.CreateAnonymous(Domain.PositiveOrZeroInteger);

            totalBound.Set(ConstraintType.LessOrEqual, milpManager.FromConstant(options.MaxIterations));

            options.BeforeLoopAction(totalBound, arguments);

            for (int i = 1; i <= options.MaxIterations; ++i)
            {
                var counter   = milpManager.FromConstant(i);
                var isLooping = counter.Operation(OperationType.IsLessOrEqual, totalBound);

                options.BeforeIterationAction(counter, isLooping, totalBound, arguments);

                for (int v = 0; v < arguments.Length; ++v)
                {
                    if (options.BeforeBody.Length > v)
                    {
                        options.BeforeBody[v](arguments[v], counter, isLooping, totalBound, arguments);
                    }

                    arguments[v] = milpManager.Operation(OperationType.Condition, isLooping, options.Body[v](arguments[v], counter, isLooping, totalBound, arguments), arguments[v]);

                    if (options.AfterBody.Length > v)
                    {
                        options.AfterBody[v](arguments[v], counter, isLooping, totalBound, arguments);
                    }
                }

                options.AfterIterationAction(counter, isLooping, totalBound, arguments);
            }

            options.AfterLoopAction(totalBound, arguments);

            return(arguments.Concat(new[] { totalBound }).ToArray());
        }
示例#34
0
        public IVariable Set(IMilpManager milpManager, CompositeConstraintType type, ICompositeConstraintParameters parameters,
                             IVariable leftVariable, params IVariable[] rightVariable)
        {
            var one = milpManager.FromConstant(1);
            var maximumIntegerValue = milpManager.FromConstant(milpManager.MaximumIntegerValue);

            var allVariables = new[] { leftVariable }.Concat(rightVariable).ToArray();
            var boundaryVariables = allVariables.Select(v => milpManager.CreateAnonymous(Domain.BinaryInteger)).ToArray();

            milpManager.Operation(OperationType.Addition, boundaryVariables).Set(ConstraintType.LessOrEqual, one);
            foreach (var pair in allVariables.Zip(boundaryVariables, Tuple.Create))
            {
                pair.Item1
                .Set(ConstraintType.LessOrEqual, pair.Item2.Operation(OperationType.Multiplication, maximumIntegerValue))
                .Set(ConstraintType.GreaterOrEqual, pair.Item2.Operation(OperationType.Multiplication, maximumIntegerValue).Operation(OperationType.Negation));
            }

            return(leftVariable);
        }
        public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments)) throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            if (arguments.Length == 1)
            {
                return arguments[0];
            }
            if (MultiplyOnlyConstants(arguments))
            {
                var result = arguments.Select(a => a.ConstantValue.Value).Aggregate((a, b) => a*b);
                if (arguments.All(a => a.IsInteger()))
                {
                    return milpManager.FromConstant((int) result);
                }
                else
                {
                    return milpManager.FromConstant(result);
                }
            }

            var domain = CalculateDomain(arguments);

            if (MultiplyAtMostOneNonconstant(arguments))
            {
                return arguments.Aggregate((x, y) =>
                {
                    var result = y.IsConstant()
                        ? milpManager.MultiplyVariableByConstant(x, y, domain)
                        : milpManager.MultiplyVariableByConstant(y, x, domain);
                    result.ConstantValue = x.ConstantValue*y.ConstantValue;
                    result.Expression = $"{x.FullExpression()} * {y.FullExpression()}";
                    return result;
                });
            }

            if (MultiplyBinaryVariables(arguments))
            {
                return milpManager.Operation(OperationType.Conjunction, arguments);
            }

            return MultiplyIntegers(milpManager, domain, arguments);
        }
        private IVariable MultipleByBinaryDigit(IMilpManager baseMilpManager, IVariable number, IVariable digit)
        {
            if (number.Domain == Domain.AnyConstantInteger || number.Domain == Domain.AnyInteger)
            {
                var absoluteNumber = number.Operation(OperationType.AbsoluteValue);
                var result         = MultipleByBinaryDigit(baseMilpManager, absoluteNumber, digit);
                var two            = baseMilpManager.FromConstant(2);
                return(MultipleByBinaryDigit(baseMilpManager, result, number.Operation(OperationType.IsGreaterOrEqual, baseMilpManager.FromConstant(0)))
                       .Operation(OperationType.Subtraction,
                                  result.Operation(OperationType.Division, two))
                       .Operation(OperationType.Multiplication, two));
            }

            IVariable digitMultipliedByInfinity = digit.Operation(OperationType.Multiplication, baseMilpManager.FromConstant(baseMilpManager.MaximumIntegerValue));

            return(baseMilpManager.Operation(OperationType.Minimum,
                                             number,
                                             digitMultipliedByInfinity
                                             ));
        }
        public IVariable Calculate(IMilpManager milpManager, GoalType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            }
            if (arguments.All(a => a.IsConstant()))
            {
                return(milpManager.Operation(OperationType.Minimum, arguments));
            }

            var result = milpManager.CreateAnonymous(arguments.Any(a => a.IsReal()) ? Domain.AnyReal : Domain.AnyInteger);

            foreach (var argument in arguments)
            {
                result.Set(ConstraintType.LessOrEqual, argument);
            }

            return(result);
        }
        public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            }
            if (arguments.All(a => a.IsConstant()))
            {
                return(milpManager.FromConstant(arguments.Select(a => (int)a.ConstantValue).Aggregate(Math.Min)));
            }
            var variable       = milpManager.CreateAnonymous(Domain.BinaryInteger);
            var sum            = milpManager.Operation(OperationType.Addition, arguments);
            var argumentsCount = arguments.Length;

            sum.Operation(OperationType.Subtraction,
                          milpManager.FromConstant(argumentsCount).Operation(OperationType.Multiplication, variable))
            .Set(ConstraintType.LessOrEqual, milpManager.FromConstant(argumentsCount - 1))
            .Set(ConstraintType.GreaterOrEqual, milpManager.FromConstant(0));

            variable.ConstantValue = arguments.Aggregate((double?)1.0, (a, b) => a.HasValue && b.ConstantValue.HasValue ? Math.Min(a.Value, b.ConstantValue.Value) : (double?)null);
            variable.Expression    = $"{string.Join(" && ", arguments.Select(a => a.FullExpression()).ToArray())}";
            return(variable);
        }
示例#39
0
        public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
        {
            if (!SupportsOperation(type, arguments)) throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
            if (arguments.All(a => a.IsConstant()))
            {
                var constantArgument = (int) arguments[0].ConstantValue.Value;
                var constantResult = constantArgument == 0 ? 1 : Enumerable.Range(1, constantArgument).Aggregate((a, b) => a*b);
                return milpManager.FromConstant(constantResult);
            }
            var number = arguments[0];
            var one = milpManager.FromConstant(1);
            var result = one;
            for (int i = SoundBoundary(milpManager.MaximumIntegerValue); i >= 0; --i)
            {
                result = result.Operation(OperationType.Multiplication,
                    milpManager.Operation(OperationType.Maximum, one,
                        number.Operation(OperationType.Subtraction, milpManager.FromConstant(i))));
            }

            var finalResult = result.ChangeDomain(Domain.PositiveOrZeroInteger);
            finalResult.Expression = $"{number.FullExpression()}!";
            return finalResult;
        }
示例#40
0
        public IVariable Set(IMilpManager milpManager, CompositeConstraintType type, ICompositeConstraintParameters parameters,
            IVariable leftVariable, params IVariable[] rightVariable)
        {
            var maximumIntegerValue = milpManager.FromConstant(milpManager.MaximumIntegerValue);
            var one = milpManager.FromConstant(1);

            var allVariables = new[] { leftVariable }.Concat(rightVariable).ToArray();
            var boundaryVariables = allVariables.Select(v => milpManager.CreateAnonymous(Domain.BinaryInteger)).ToArray();
            milpManager.Operation(OperationType.Addition, boundaryVariables).Set(ConstraintType.LessOrEqual, one);

            for (int i = 0; i < allVariables.Length; ++i)
            {
                IVariable sum = boundaryVariables[i];
                if (i < allVariables.Length - 1)
                {
                    sum = sum.Operation(OperationType.Addition, boundaryVariables[i + 1]);
                }
                allVariables[i]
                    .Set(ConstraintType.LessOrEqual, sum.Operation(OperationType.Multiplication, maximumIntegerValue))
                    .Set(ConstraintType.GreaterOrEqual, sum.Operation(OperationType.Multiplication, maximumIntegerValue).Operation(OperationType.Negation));
            }

            return leftVariable;
        }
示例#41
0
 public IVariable Calculate(IMilpManager milpManager, OperationType type, params IVariable[] arguments)
 {
     if (!SupportsOperation(type, arguments)) throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments));
     if (arguments[0].IsConstant())
     {
         if (arguments[0].ConstantValue.Value <= milpManager.Epsilon)
         {
             return arguments[2];
         }
         return arguments[1];
     }
     var trueBranch = arguments[0].Operation(OperationType.Multiplication, arguments[1]);
     var falseBranch = arguments[0].Operation(OperationType.BinaryNegation)
         .Operation(OperationType.Multiplication, arguments[2]);
     var result = milpManager.Create(milpManager.Operation(OperationType.Addition,
         trueBranch,
         falseBranch
         ).ChangeDomain(trueBranch.LowestEncompassingDomain(falseBranch)));
     result.Expression = $"{arguments[0].FullExpression()} ? {arguments[1].FullExpression()} : {arguments[2].FullExpression()}";
     result.ConstantValue = !arguments[0].ConstantValue.HasValue
         ? null
         : (int) arguments[0].ConstantValue.Value == 1 ? trueBranch.ConstantValue : falseBranch.ConstantValue;
     return result;
 }
        private IVariable MultiplyIntegers(IMilpManager baseMilpManager, Domain domain, IVariable[] arguments)
        {
            var binaries = arguments.Where(a => a.IsBinary()).ToArray();
            var nonBinaries = arguments.Where(a => !a.IsBinary()).ToArray();

            if (binaries.Any())
            {
                IVariable conjucted = baseMilpManager.Operation(OperationType.Multiplication, binaries);
                return MultipleByBinaryDigit(baseMilpManager, nonBinaries[0], conjucted).ChangeDomain(domain)
                    .Operation(OperationType.Multiplication, nonBinaries.Skip(1).ToArray());
            }

            return MultiplyNonBinaryIntegers(baseMilpManager, nonBinaries, domain);
        }
        private IVariable MultipleByBinaryDigit(IMilpManager baseMilpManager, IVariable number, IVariable digit)
        {
            if (number.Domain == Domain.AnyConstantInteger || number.Domain == Domain.AnyInteger)
            {
                var absoluteNumber = number.Operation(OperationType.AbsoluteValue);
                var result = MultipleByBinaryDigit(baseMilpManager, absoluteNumber, digit);
                var two = baseMilpManager.FromConstant(2);
                return MultipleByBinaryDigit(baseMilpManager, result, number.Operation(OperationType.IsGreaterOrEqual, baseMilpManager.FromConstant(0)))
                        .Operation(OperationType.Subtraction,
                            result.Operation(OperationType.Division, two))
                        .Operation(OperationType.Multiplication, two);
            }

            IVariable digitMultipliedByInfinity = digit.Operation(OperationType.Multiplication, baseMilpManager.FromConstant(baseMilpManager.MaximumIntegerValue));
            return baseMilpManager.Operation(OperationType.Minimum,
                number,
                digitMultipliedByInfinity
                );
        }
示例#44
0
        public IEnumerable <IVariable> Calculate(IMilpManager milpManager, CompositeOperationType type, ICompositeOperationParameters parameters,
                                                 params IVariable[] arguments)
        {
            if (!SupportsOperation(type, parameters, arguments))
            {
                throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, parameters, arguments));
            }
            var typedParameters = parameters as Approximate2DParameters;
            var x = arguments.First();
            var y = arguments.Skip(1).First();

            if (arguments.All(i => i.IsConstant()))
            {
                return(new[] { milpManager.FromConstant(typedParameters.Function(x.ConstantValue.Value, y.ConstantValue.Value)) });
            }

            var one = milpManager.FromConstant(1);

            var variables =
                Enumerable.Range(0, typedParameters.ArgumentsX.Count()).Select(p =>
                                                                               Enumerable.Range(0, typedParameters.ArgumentsY.Count()).Select(q =>
                                                                                                                                              milpManager.CreateAnonymous(typedParameters.ArgumentMustBeOnAGrid
                        ? Domain.BinaryInteger
                        : Domain.PositiveOrZeroReal).Set(ConstraintType.LessOrEqual, one)).ToArray())
                .ToArray();

            x.Set(ConstraintType.Equal, milpManager.Operation(OperationType.Addition,
                                                              Enumerable.Range(0, typedParameters.ArgumentsX.Count()).SelectMany(indexX =>
                                                                                                                                 Enumerable.Range(0, typedParameters.ArgumentsY.Count()).Select(indexY =>
                                                                                                                                                                                                variables[indexX][indexY].Operation(OperationType.Multiplication, milpManager.FromConstant(typedParameters.ArgumentsX.ElementAt(indexX))))).ToArray()
                                                              ));
            y.Set(ConstraintType.Equal, milpManager.Operation(OperationType.Addition,
                                                              Enumerable.Range(0, typedParameters.ArgumentsX.Count()).SelectMany(indexX =>
                                                                                                                                 Enumerable.Range(0, typedParameters.ArgumentsY.Count()).Select(indexY =>
                                                                                                                                                                                                variables[indexX][indexY].Operation(OperationType.Multiplication, milpManager.FromConstant(typedParameters.ArgumentsY.ElementAt(indexY))))).ToArray()
                                                              ));
            var z = milpManager.Operation(OperationType.Addition,
                                          Enumerable.Range(0, typedParameters.ArgumentsX.Count()).SelectMany(indexX =>
                                                                                                             Enumerable.Range(0, typedParameters.ArgumentsY.Count()).Select(indexY =>
                                                                                                                                                                            variables[indexX][indexY].Operation(OperationType.Multiplication, milpManager.FromConstant(typedParameters.Function(typedParameters.ArgumentsX.ElementAt(indexX), typedParameters.ArgumentsY.ElementAt(indexY)))))).ToArray()
                                          );

            milpManager.Operation(OperationType.Addition, variables.SelectMany(v => v).ToArray()).Set(ConstraintType.Equal, one);

            var xSet = Enumerable.Range(0, typedParameters.ArgumentsX.Count()).Select(indexX => milpManager.Operation(OperationType.Addition, Enumerable.Range(0, typedParameters.ArgumentsY.Count()).Select(indexY => variables[indexX][indexY]).ToArray())).ToArray();

            milpManager.Set(CompositeConstraintType.SpecialOrderedSetType2, xSet.First(), xSet.Skip(1).ToArray());

            var ySet = Enumerable.Range(0, typedParameters.ArgumentsY.Count()).Select(indexY => milpManager.Operation(OperationType.Addition, Enumerable.Range(0, typedParameters.ArgumentsX.Count()).Select(indexX => variables[indexX][indexY]).ToArray())).ToArray();

            milpManager.Set(CompositeConstraintType.SpecialOrderedSetType2, ySet.First(), ySet.Skip(1).ToArray());

            if (!typedParameters.ArgumentMustBeOnAGrid)
            {
                var triangleSet = Enumerable.Range(0, typedParameters.ArgumentsY.Count()).SelectMany(indexY =>
                {
                    var variablesToSet = Enumerable.Range(0, typedParameters.ArgumentsX.Count()).Where(indexX => indexX + indexY < variables[indexX].Length).Select(indexX => variables[indexX][indexX + indexY]).ToArray();
                    if (variablesToSet.Any())
                    {
                        return(new[] { milpManager.Operation(OperationType.Addition, variablesToSet) });
                    }

                    return(new IVariable[0]);
                }).ToArray();

                milpManager.Set(CompositeConstraintType.SpecialOrderedSetType2, triangleSet.First(), triangleSet.Skip(1).ToArray());
            }

            z.ConstantValue = x.IsConstant() && y.IsConstant() ? typedParameters.Function(x.ConstantValue.Value, y.ConstantValue.Value) : (double?)null;
            z.Expression    = $"approximation2D({typedParameters.FunctionDescription})";

            return(new[] { z });
        }
        public IEnumerable<IVariable> Calculate(IMilpManager milpManager, CompositeOperationType type, ICompositeOperationParameters parameters,
            params IVariable[] arguments)
        {
            if (!SupportsOperation(type, parameters, arguments)) throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, parameters, arguments));
            var typedParameters = parameters as Approximate2DParameters;
            var x = arguments.First();
            var y = arguments.Skip(1).First();

            if (arguments.All(i => i.IsConstant()))
            {
                return new[] { milpManager.FromConstant(typedParameters.Function(x.ConstantValue.Value, y.ConstantValue.Value)) };
            }

            var one = milpManager.FromConstant(1);

            var variables =
                Enumerable.Range(0, typedParameters.ArgumentsX.Count()).Select(p =>
                Enumerable.Range(0, typedParameters.ArgumentsY.Count()).Select(q =>
                    milpManager.CreateAnonymous(typedParameters.ArgumentMustBeOnAGrid
                        ? Domain.BinaryInteger
                        : Domain.PositiveOrZeroReal).Set(ConstraintType.LessOrEqual, one)).ToArray())
                .ToArray();

            x.Set(ConstraintType.Equal, milpManager.Operation(OperationType.Addition,
                Enumerable.Range(0, typedParameters.ArgumentsX.Count()).SelectMany(indexX =>
                Enumerable.Range(0, typedParameters.ArgumentsY.Count()).Select(indexY =>
                    variables[indexX][indexY].Operation(OperationType.Multiplication, milpManager.FromConstant(typedParameters.ArgumentsX.ElementAt(indexX))))).ToArray()
            ));
            y.Set(ConstraintType.Equal, milpManager.Operation(OperationType.Addition,
                Enumerable.Range(0, typedParameters.ArgumentsX.Count()).SelectMany(indexX =>
                Enumerable.Range(0, typedParameters.ArgumentsY.Count()).Select(indexY =>
                    variables[indexX][indexY].Operation(OperationType.Multiplication, milpManager.FromConstant(typedParameters.ArgumentsY.ElementAt(indexY))))).ToArray()
            ));
            var z = milpManager.Operation(OperationType.Addition,
                Enumerable.Range(0, typedParameters.ArgumentsX.Count()).SelectMany(indexX =>
                Enumerable.Range(0, typedParameters.ArgumentsY.Count()).Select(indexY =>
                    variables[indexX][indexY].Operation(OperationType.Multiplication, milpManager.FromConstant(typedParameters.Function(typedParameters.ArgumentsX.ElementAt(indexX), typedParameters.ArgumentsY.ElementAt(indexY)))))).ToArray()
            );

            milpManager.Operation(OperationType.Addition, variables.SelectMany(v => v).ToArray()).Set(ConstraintType.Equal, one);

            var xSet = Enumerable.Range(0, typedParameters.ArgumentsX.Count()).Select(indexX => milpManager.Operation(OperationType.Addition, Enumerable.Range(0, typedParameters.ArgumentsY.Count()).Select(indexY => variables[indexX][indexY]).ToArray())).ToArray();
             milpManager.Set(CompositeConstraintType.SpecialOrderedSetType2, xSet.First(), xSet.Skip(1).ToArray());

            var ySet = Enumerable.Range(0, typedParameters.ArgumentsY.Count()).Select(indexY => milpManager.Operation(OperationType.Addition, Enumerable.Range(0, typedParameters.ArgumentsX.Count()).Select(indexX => variables[indexX][indexY]).ToArray())).ToArray();
            milpManager.Set(CompositeConstraintType.SpecialOrderedSetType2, ySet.First(), ySet.Skip(1).ToArray());

            if (!typedParameters.ArgumentMustBeOnAGrid)
            {
                var triangleSet = Enumerable.Range(0, typedParameters.ArgumentsY.Count()).SelectMany(indexY =>
                {
                    var variablesToSet = Enumerable.Range(0, typedParameters.ArgumentsX.Count()).Where(indexX => indexX + indexY < variables[indexX].Length).Select(indexX => variables[indexX][indexX + indexY]).ToArray();
                    if (variablesToSet.Any())
                    {
                        return new[] {milpManager.Operation(OperationType.Addition, variablesToSet)};
                    }

                    return new IVariable[0];
                }).ToArray();

                milpManager.Set(CompositeConstraintType.SpecialOrderedSetType2, triangleSet.First(), triangleSet.Skip(1).ToArray());
            }

            z.ConstantValue = x.IsConstant() && y.IsConstant() ? typedParameters.Function(x.ConstantValue.Value, y.ConstantValue.Value) : (double?)null;
            z.Expression = $"approximation2D({typedParameters.FunctionDescription})";

            return new[] { z };
        }