The Expression class is particularly useful when defining constraints. It allows you to create complex expressions that involve variables, constants, and mathematical operations. These expressions can then be used to represent constraints or objective functions in optimization problems, but also variables of expression type.
Creating Expressions
To create an Expression
object, simply instantiate it with a valid mathematical expression. Any variable involved
in the expression must to be created before the expression.
For example:
expr_1 = Expression(2 * x + 3)
expr_2 = Expression(y ** 2 - 4)
Once you have Expression objects, you can perform various mathematical operations on them. For example:
result_expr = expr_1 + expr_2
result_expr = expr_1 * 2 - expr_2
You can also use the comparison operators to create boolean expressions:
bool_expr = expr_1 >= expr_2
bool_expr = expr_1 == 0
Expression Functions
A set of functions for performing mathematical operations on expressions:
maximum(expr: ExpressionArray) -> Expression
:
Returns an expression representing the maximum value of an expression array.
minimum(expr: ExpressionArray) -> Expression
:
Returns an expression representing the minimum value of an expression array.
sum_of(expr: ExpressionArray) -> Expression
:
Returns an expression representing the sum of an expression array.
absolute(expr: Expression) -> Expression
:
Returns an expression representing the absolute value of an expression.
power(expr: Expression, val) -> Expression
:
Returns an expression representing the exponentiation of an expression by a value.
nroot(expr: Expression, val) -> Expression
:
Returns an expression representing the nth root of an expression by a value.
sqr(expr: Expression) -> Expression
:
Returns an expression representing the square of an expression.
sqrt(expr: Expression) -> Expression
:
Returns an expression representing the square root of an expression.
sin(expr: Expression) -> Expression
:
Returns an expression representing the sine of an expression.
cos(expr: Expression) -> Expression
:
Returns an expression representing the cosine of an expression.
tan(expr: Expression) -> Expression
:
Returns an expression representing the tangent of an expression.
asin(expr: Expression) -> Expression
:
Returns an expression representing the arcsine of an expression.
acos(expr: Expression) -> Expression
:
Returns an expression representing the arccosine of an expression.
atan(expr: Expression) -> Expression
:
Returns an expression representing the arctangent of an expression.
log(expr: Expression) -> Expression
:
Returns an expression representing the natural logarithm of an expression.
exp(expr: Expression) -> Expression
:
Returns an expression representing the exponential function of an expression.
Example 1
from qaekwy.model.constraint.relational import RelationalExpression
from qaekwy.model.function import sqr, power
from qaekwy.model.modeller import Modeller
from qaekwy.model.variable.integer import IntegerVariable, IntegerVariableArray
# Create variables
x = IntegerVariable("x", domain_low=0, domain_high=100)
y = IntegerVariable("y", domain_low=0, domain_high=100)
z = IntegerVariable("z", domain_low=0, domain_high=100)
# Create array variable
arr = IntegerVariableArray(var_name="arr", length=3, domain_low=0, domain_high=100)
expr_1 = arr[0] > x + 1
expr_2 = sqr(y) < (arr[1] * z)
expr_3 = exp(arr[2]) + power(z, 3) >= arr[0] + arr[1]
# Modelling
my_model = Modeller()
my_model.add_variable(variable=x)
my_model.add_variable(variable=y)
my_model.add_variable(variable=z)
my_model.add_variable(variable=arr)
my_model.add_constraint(constraint=RelationalExpression(expr_1))
my_model.add_constraint(constraint=RelationalExpression(expr_2))
my_model.add_constraint(constraint=RelationalExpression(expr_3))
# ...
Example 2
from qaekwy.model.function import power
from qaekwy.model.variable.integer import IntegerExpressionVariable, IntegerVariable
from qaekwy.model.constraint.relational import RelationalExpression
from qaekwy.model.modeller import Modeller
from qaekwy.model.specific import SpecificMaximum
# Create variables
x = IntegerVariable('x', domain_low=0, domain_high=100)
y = IntegerVariable('y', domain_low=0, domain_high=100)
z = IntegerVariable('z', domain_low=0, domain_high=100)
# Create a complex nonlinear expression
nonlinear_expr = (power(x, 2) + power(y, 3)) * z + x * y - 10
# Create the variable to use as the objective
objective_variable = IntegerExpressionVariable(
var_name="objective_variable", expression=nonlinear_expr
)
# Setting the expression as a constraint
nonlinear_constraint = RelationalExpression(expr=objective_variable < 100)
# Setting the objective
maximum_nonlinear = SpecificMaximum(objective_variable)
# Create the Modeller
my_model = Modeller()
# Add variables
my_model.add_variable(x)
my_model.add_variable(y)
my_model.add_variable(z)
my_model.add_variable(objective_variable)
# Adding the constraint
my_model.add_constraint(nonlinear_constraint)
# Adding the objective
my_model.add_objective(maximum_nonlinear)