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 MakeLongMultiplication(IMilpManager baseMilpManager, Domain domain, IVariable zero, IVariable second,
                                                 IVariable first)
        {
            var result = zero;

            var secondDigits = second.CompositeOperation(CompositeOperationType.UnsignedMagnitudeDecomposition).ToArray();

            for (int index = 0, power = 1; index < secondDigits.Length; ++index, power = power * 2)
            {
                result = result.Operation(OperationType.Addition,
                                          MultipleByBinaryDigit(baseMilpManager, first, secondDigits[index])
                                          .ChangeDomain(domain)
                                          .Operation(OperationType.Multiplication, baseMilpManager.FromConstant(power))
                                          );
            }
            return(result);
        }
        private IVariable MultiplyNonBinaryIntegers(IMilpManager baseMilpManager, IVariable[] nonBinaries, Domain domain)
        {
            var first = nonBinaries[0];
            var second = nonBinaries[1];
            var mightBeNegatives = first.Domain == Domain.AnyInteger || first.Domain == Domain.AnyConstantInteger ||
                     second.Domain == Domain.AnyInteger || second.Domain == Domain.AnyConstantInteger;
            first = MakePositiveIfNeeded(first);
            second = MakePositiveIfNeeded(second);

            var zero = baseMilpManager.FromConstant(0);
            var result = MakeLongMultiplication(baseMilpManager, domain, zero, second, first);
            result = FixSign(baseMilpManager, nonBinaries, mightBeNegatives, zero, result);
            result = result.ChangeDomain(domain);

            return result.Operation(OperationType.Multiplication, nonBinaries.Skip(2).ToArray());
        }
        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 MakeLongMultiplication(IMilpManager baseMilpManager, Domain domain, IVariable zero, IVariable second,
            IVariable first)
        {
            var result = zero;

            var secondDigits = second.CompositeOperation(CompositeOperationType.UnsignedMagnitudeDecomposition).ToArray();
            for (int index = 0, power = 1; index < secondDigits.Length; ++index, power = power*2)
            {
                result = result.Operation(OperationType.Addition,
                    MultipleByBinaryDigit(baseMilpManager, first, secondDigits[index])
                        .ChangeDomain(domain)
                        .Operation(OperationType.Multiplication, baseMilpManager.FromConstant(power))
                    );
            }
            return result;
        }
Example #6
0
 public MsfMilpVariable(IMilpManager milpManager, string name, Domain domain)
 {
     MilpManager = milpManager;
     Name = name;
     Domain = domain;
 }
Example #7
0
 protected override IVariable InternalSumVariables(IVariable first, IVariable second, Domain domain)
 {
     var firstVariable = (MsfMilpVariable) first;
     var secondVariable = (MsfMilpVariable) second;
     return new MsfMilpVariable(this, NewVariableName(), domain)
     {
         Term = firstVariable.Term + secondVariable.Term
     };
 }
Example #8
0
 protected override IVariable InternalNegateVariable(IVariable variable, Domain domain)
 {
     var casted = (MsfMilpVariable) variable;
     return new MsfMilpVariable(this, NewVariableName(), domain)
     {
         Term = -casted.Term
     };
 }
Example #9
0
 protected override IVariable InternalMultiplyVariableByConstant(IVariable variable, IVariable constant, Domain domain)
 {
     return new MsfMilpVariable(this, NewVariableName(), domain)
     {
         Term = ((MsfMilpVariable) variable).Term*((MsfMilpVariable) constant).Term
     };
 }
Example #10
0
 protected override IVariable InternalFromConstant(string name, double value, Domain domain)
 {
     var variable = new MsfMilpVariable(this, name, domain) { Term = 1 * value };
     return variable;
 }
Example #11
0
        protected override IVariable InternalCreate(string name, Domain domain)
        {
            var variable = new MsfMilpVariable(this, name, domain);
            var msfDomain = (domain == Domain.BinaryConstantInteger ||
                             domain == Domain.BinaryInteger)
                ? Microsoft.SolverFoundation.Services.Domain.Boolean
                : (domain == Domain.PositiveOrZeroConstantInteger || domain == Domain.PositiveOrZeroInteger)
                    ? Microsoft.SolverFoundation.Services.Domain.IntegerRange(0, Int64.MaxValue)
                    : (domain == Domain.PositiveOrZeroConstantReal || domain == Domain.PositiveOrZeroReal)
                        ? Microsoft.SolverFoundation.Services.Domain.RealNonnegative
                        : (domain == Domain.AnyConstantInteger || domain == Domain.AnyInteger)
                            ? Microsoft.SolverFoundation.Services.Domain.IntegerRange(Int64.MinValue, Int64.MaxValue)
                            : Microsoft.SolverFoundation.Services.Domain.Real;
            variable.Decision = new Decision(msfDomain, name);
            Solver.AddDecision(variable.Decision);
            variable.Term = 1*variable.Decision;

            return variable;
        }
        private IVariable MultiplyNonBinaryIntegers(IMilpManager baseMilpManager, IVariable[] nonBinaries, Domain domain)
        {
            var first            = nonBinaries[0];
            var second           = nonBinaries[1];
            var mightBeNegatives = first.Domain == Domain.AnyInteger || first.Domain == Domain.AnyConstantInteger ||
                                   second.Domain == Domain.AnyInteger || second.Domain == Domain.AnyConstantInteger;

            first  = MakePositiveIfNeeded(first);
            second = MakePositiveIfNeeded(second);

            var zero   = baseMilpManager.FromConstant(0);
            var result = MakeLongMultiplication(baseMilpManager, domain, zero, second, first);

            result = FixSign(baseMilpManager, nonBinaries, mightBeNegatives, zero, result);
            result = result.ChangeDomain(domain);

            return(result.Operation(OperationType.Multiplication, nonBinaries.Skip(2).ToArray()));
        }