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[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; }