public bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return type == CompositeOperationType.ArrayGet && arguments.Any() && parameters is ArrayGetParameters && ((ArrayGetParameters) parameters).Index.IsInteger() && ((ArrayGetParameters) parameters).Index.IsNonNegative(); }
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 IEnumerable<IVariable> Calculate(IMilpManager milpManager, CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { if (!SupportsOperation(type, parameters, arguments)) throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments)); var typedParameters = parameters as LexicographicalCompareParameters; if (arguments.All(i => i.IsConstant()) && typedParameters.Pattern.All(i => i.IsConstant())) { return new [] { CompareFinalResult(milpManager.FromConstant(arguments.Zip(typedParameters.Pattern, Tuple.Create) .Select(pair => pair.Item1.ConstantValue.Value - pair.Item2.ConstantValue.Value) .Select(v => v > 0 ? 1 : (v < 0 ? -1 : 0)) .FirstOrDefault(v => v != 0)), milpManager) }; } var compareResult = CalculateBatches(milpManager, parameters, arguments).ToArray(); while (compareResult.Length > 1) { var zero = milpManager.FromConstant(0); var newParameters = new LexicographicalCompareParameters { Pattern = Enumerable.Range(0, compareResult.Length).Select(x => zero).ToArray() }; compareResult = CalculateBatches(milpManager, newParameters, compareResult).ToArray(); } var result = compareResult.First(); result.ConstantValue = arguments.All(a => a.ConstantValue.HasValue) && typedParameters.Pattern.All(a => a.IsConstant()) ? ConstantFinalResult(arguments.Zip(typedParameters.Pattern, Tuple.Create).Select(p => p.Item1.ConstantValue.Value - p.Item2.ConstantValue.Value).Select(v => v > 0 ? 1 : v < 0 ? -1 : 0).TakeWhile(v => v != 0).FirstOrDefault()) : (double?)null; result.Expression = $"({string.Join(",", arguments.Select(a => a.FullExpression()).ToArray())}) {ComparerFinalResult} ({string.Join(",", typedParameters.Pattern.Select(a => a.FullExpression()).ToArray())})"; return new[] {CompareFinalResult(compareResult[0], milpManager)}; }
public bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return(type == CompositeOperationType.NthElements && parameters is NthElementsParameters && ((NthElementsParameters)parameters).Indexes.All( i => i.IsInteger() && (i.IsBinary() || i.IsPositiveOrZero()))); }
public bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return(type == CompositeOperationType.ArraySet && arguments.Any() && parameters is ArraySetParameters && ((ArraySetParameters)parameters).Index.IsInteger() && ((ArraySetParameters)parameters).Index.IsNonNegative()); }
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 = (ArrayGetParameters) parameters; if (typedParameters.Index.IsConstant()) { return new[] { arguments[(int) typedParameters.Index.ConstantValue.Value] }; } var index = typedParameters.Index; var result = milpManager.CreateAnonymous(arguments.Skip(1) .Aggregate(arguments[0].Domain, (domain, next) => domain.LowestEncompassingDomain(next.Domain))); for (int i = 0; i < arguments.Length; ++i) { milpManager.FromConstant(i).Operation(OperationType.IsEqual, index) .Operation(OperationType.MaterialImplication, result.Operation(OperationType.IsEqual, arguments[i])) .MakeTrue(); } result.ConstantValue = index.ConstantValue.HasValue ? arguments[(int)index.ConstantValue.Value].ConstantValue : null; result.Expression = $"arrayGet(index: {index.FullExpression()}, {string.Join(", ", arguments.Select(a => a.FullExpression()).ToArray())})"; return new[] {result}; }
public bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return type == CompositeOperationType.NthElements && parameters is NthElementsParameters && ((NthElementsParameters) parameters).Indexes.All( i => i.IsInteger() && (i.IsBinary() || i.IsPositiveOrZero())); }
public virtual IEnumerable <IVariable> CompositeOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] variables) { if (CompositeOperations[type].SupportsOperation(type, parameters, variables)) { return(CompositeOperations[type].Calculate(this, type, parameters, variables)); } throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, parameters, variables)); }
private static IEnumerable<IVariable> CalculateBatches(IMilpManager milpManager, ICompositeOperationParameters parameters, IVariable[] arguments) { var batchSize = milpManager.IntegerWidth; var batches = (arguments.Length - 1)/batchSize + 1; return Enumerable.Range(0, batches) .Select( index => CalculateBatch(milpManager, arguments.Skip(index*batchSize).Take(batchSize).ToArray(), (parameters as LexicographicalCompareParameters).Pattern.Skip(index*batchSize).Take(batchSize).ToArray())); }
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 decomposition = milpManager.CompositeOperation(CompositeOperationType.Decomposition, new DecompositionParameters { Base = 2 }, arguments); if (arguments[0].IsConstant()) { return decomposition; } return decomposition.Zip(Enumerable.Range(0, milpManager.IntegerWidth), (v, index) => { v.Expression = $"unsignedMagnitudeDecomposition(bit: {index}, {arguments[0].FullExpression()})"; return v; }); }
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 results = milpManager.CompositeOperation(CompositeOperationType.NthElements, new NthElementsParameters {Indexes = Enumerable.Range(0, arguments.Length).Select(milpManager.FromConstant).ToArray()}, arguments).ToArray(); for (int i = 0; i < results.Length; ++i) { results[i].Expression = $"selectionSort(position: {i+1}, {string.Join(",", arguments.Select(a => a.FullExpression()).ToArray())})"; } return results; }
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 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)); return InternalCalculate(milpManager, arguments, ((DecompositionParameters) parameters).Base); }
public virtual bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return(arguments.Length > 0 && (parameters as LexicographicalCompareParameters)?.Pattern?.Length == arguments.Length); }
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 results = milpManager.CompositeOperation(CompositeOperationType.NthElements, new NthElementsParameters { Indexes = Enumerable.Range(0, arguments.Length).Select(milpManager.FromConstant).ToArray() }, arguments).ToArray(); for (int i = 0; i < results.Length; ++i) { results[i].Expression = $"selectionSort(position: {i+1}, {string.Join(",", arguments.Select(a => a.FullExpression()).ToArray())})"; } return(results); }
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 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 override bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return(base.SupportsOperation(type, parameters, arguments) && type == _compositeType); }
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 decomposition = milpManager.CompositeOperation(CompositeOperationType.Decomposition, new DecompositionParameters { Base = 2 }, arguments); if (arguments[0].IsConstant()) { return(decomposition); } return(decomposition.Zip(Enumerable.Range(0, milpManager.IntegerWidth), (v, index) => { v.Expression = $"unsignedMagnitudeDecomposition(bit: {index}, {arguments[0].FullExpression()})"; return v; })); }
public bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return(type == CompositeOperationType.UnsignedMagnitudeDecomposition && arguments.Length == 1 && (arguments[0].IsPositiveOrZero() || arguments[0].IsBinary()) && arguments[0].IsInteger()); }
public virtual IEnumerable<IVariable> CompositeOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] variables) { if (CompositeOperations[type].SupportsOperation(type, parameters, variables)) { return CompositeOperations[type].Calculate(this, type, parameters, variables); } throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, parameters, variables)); }
public bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return(type == CompositeOperationType.Decomposition && parameters is DecompositionParameters && ((DecompositionParameters)parameters).Base >= 2 && arguments.Length == 1 && arguments[0].IsInteger() && arguments[0].IsNonNegative()); }
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)); } return(InternalCalculate(milpManager, arguments, ((DecompositionParameters)parameters).Base)); }
public bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return type == CompositeOperationType.Decomposition && parameters is DecompositionParameters && ((DecompositionParameters) parameters).Base >= 2 && arguments.Length == 1 && arguments[0].IsInteger() && arguments[0].IsNonNegative(); }
public bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return type == CompositeOperationType.SelectionSort && arguments.Any(); }
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); }
public IEnumerable <IVariable> Calculate(IMilpManager milpManager, CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { if (!SupportsOperation(type, parameters, arguments)) { throw new NotSupportedException(SolverUtilities.FormatUnsupportedMessage(type, arguments)); } var typedParameters = parameters as LexicographicalCompareParameters; if (arguments.All(i => i.IsConstant()) && typedParameters.Pattern.All(i => i.IsConstant())) { return(new [] { CompareFinalResult(milpManager.FromConstant(arguments.Zip(typedParameters.Pattern, Tuple.Create) .Select(pair => pair.Item1.ConstantValue.Value - pair.Item2.ConstantValue.Value) .Select(v => v > 0 ? 1 : (v < 0 ? -1 : 0)) .FirstOrDefault(v => v != 0)), milpManager) }); } var compareResult = CalculateBatches(milpManager, parameters, arguments).ToArray(); while (compareResult.Length > 1) { var zero = milpManager.FromConstant(0); var newParameters = new LexicographicalCompareParameters { Pattern = Enumerable.Range(0, compareResult.Length).Select(x => zero).ToArray() }; compareResult = CalculateBatches(milpManager, newParameters, compareResult).ToArray(); } var result = compareResult.First(); result.ConstantValue = arguments.All(a => a.ConstantValue.HasValue) && typedParameters.Pattern.All(a => a.IsConstant()) ? ConstantFinalResult(arguments.Zip(typedParameters.Pattern, Tuple.Create).Select(p => p.Item1.ConstantValue.Value - p.Item2.ConstantValue.Value).Select(v => v > 0 ? 1 : v < 0 ? -1 : 0).TakeWhile(v => v != 0).FirstOrDefault()) : (double?)null; result.Expression = $"({string.Join(",", arguments.Select(a => a.FullExpression()).ToArray())}) {ComparerFinalResult} ({string.Join(",", typedParameters.Pattern.Select(a => a.FullExpression()).ToArray())})"; return(new[] { CompareFinalResult(compareResult[0], milpManager) }); }
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 = (ArrayGetParameters)parameters; if (typedParameters.Index.IsConstant()) { return(new[] { arguments[(int)typedParameters.Index.ConstantValue.Value] }); } var index = typedParameters.Index; var result = milpManager.CreateAnonymous(arguments.Skip(1) .Aggregate(arguments[0].Domain, (domain, next) => domain.LowestEncompassingDomain(next.Domain))); for (int i = 0; i < arguments.Length; ++i) { milpManager.FromConstant(i).Operation(OperationType.IsEqual, index) .Operation(OperationType.MaterialImplication, result.Operation(OperationType.IsEqual, arguments[i])) .MakeTrue(); } result.ConstantValue = index.ConstantValue.HasValue ? arguments[(int)index.ConstantValue.Value].ConstantValue : null; result.Expression = $"arrayGet(index: {index.FullExpression()}, {string.Join(", ", arguments.Select(a => a.FullExpression()).ToArray())})"; return(new[] { result }); }
public bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return type == CompositeOperationType.Approximate2D && arguments.Length == 2 && parameters is Approximate2DParameters; }
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 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 bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return(type == CompositeOperationType.Approximate2D && arguments.Length == 2 && parameters is Approximate2DParameters); }
public virtual bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return arguments.Length > 0 && (parameters as LexicographicalCompareParameters)?.Pattern?.Length == arguments.Length; }
public bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return(type == CompositeOperationType.CountingSort && arguments.Any() && parameters is CountingSortParameters); }
public bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return type == CompositeOperationType.CountingSort && arguments.Any() && parameters is CountingSortParameters; }
public bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return(type == CompositeOperationType.SelectionSort && arguments.Any()); }
private static IEnumerable <IVariable> CalculateBatches(IMilpManager milpManager, ICompositeOperationParameters parameters, IVariable[] arguments) { var batchSize = milpManager.IntegerWidth; var batches = (arguments.Length - 1) / batchSize + 1; return(Enumerable.Range(0, batches) .Select( index => CalculateBatch(milpManager, arguments.Skip(index * batchSize).Take(batchSize).ToArray(), (parameters as LexicographicalCompareParameters).Pattern.Skip(index * batchSize).Take(batchSize).ToArray()))); }
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()); }
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 bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return type == CompositeOperationType.UnsignedMagnitudeDecomposition && arguments.Length == 1 && (arguments[0].IsPositiveOrZero() || arguments[0].IsBinary()) && arguments[0].IsInteger(); }
public bool SupportsOperation(CompositeOperationType type, ICompositeOperationParameters parameters, params IVariable[] arguments) { return(type == CompositeOperationType.Loop && (parameters as LoopParameters)?.Body?.Length == arguments.Length); }