@expression instead of constraints

I have two trigonometric variables (i.e., cs_fr and si_fr) in my code and at the same time, there are two trigonometric terms (i.e., cs_to and si_to) which linearly depend on cs_fr and si_fr. I have defined cs_fr and si_fr as variables with their corresponding bounds. I also defined expressions for cs_to and si_to as follows.

 @expression(model, cs_to, A*si_fr + B*cs_fr)
 @expression(model, si_to, C*cs_fr + D*cs_fr)

I also separately defined bounds for cs_to and si_to as follows.

           sin_max_to = E
           sin_min_to = F
           sin_max_to = G
           sin_min_to = H

I wanted to confine cs_to and si_to within their bounds through the following constraints but I got an error (i.e., ERROR: UndefVarError: si_to not defined). It seems that defining these terms by @expression doesn’t count!

    @constraint(model,  sin_min_to  <= si_to <= sin_max_to)
    @constraint(model,  cos_min_to <= cs_to <= cos_max_to)

I don’t want to increase the number of variables in my code by defining new variables for cs_to and si_to because they can be defined based on cs_fr and si_fr by expressions. Can you please let me know how I can define cs_to and si_to and confine them within their bounds through the expression macro? Thanks in advance!

  1. Are you on Julia 1.0?

  2. Please provide a minimum working example that replicates your problem.

I need to confined an expression in its bounds. Is there any specific way to do that?

This works for me

using JuMP
model = Model()
@variable(model, x)
@expression(model, my_expr, 2x + 1)
@constraint(model, 0 <= my_expr <= 1)

Thanks for the response! The question is, variable x has its own lower and upper bounds thus the bounds on"my_expr" must be defined automatically based on the bounds on “x” and the linear relationship between “my_expr” and “x”. Are we allowed to restrict “my_expr” again (i.e., last line in the code)?

using JuMP
model = Model()
@variable(model, x)
@expression(model, my_expr, 2x + 1)
@constraint(model, 0 <= my_expr <= 1)

JuMP expressions just get expanded into the constraints where they appear. They are intended as a helper if you are re-using the same expression in different constraints.

using JuMP
model = Model()
@variable(model, x)
@expression(model, my_expr, 2x + 1)

@constraint(model, 0 <= my_expr <= 1)  # is equivalent to
@constraint(model, 0 <= 2x + 1 <= 1)

You can add whatever bounds on x that you like. (Of course, this may make your problem infeasible.)

I am looking for a command that gives me the bounds on an expression. Let suppose the expression has a linear relationship with two different variables.

using JuMP
model=Model()
@variable(model, -1<=x<=1)
@variable(model, -2<=y<=3)
@expression(model, my_expr, 2x + 3y)

Now I wanna get the bounds on my_expr. I do know that the bounds on “my_expr” depend on the bounds on x and y variables. However, I don’t want to write a piece of script to compute my_expr’s bounds based on the bounds on “x” and “y” variables. In other words, is there any way to extract my_expr’s bounds directly?

my_expr doesn’t have bounds. It is just 2x + 3y. You could compute bounds by

using JuMP
model=Model()
@variable(model, -1<=x<=1)
@variable(model, -2<=y<=3)
@expression(model, my_expr, 2x + 3y)

@objective(model, Max, my_expr)
solve(model)
getobjectivevalue(model)  # upper bound of my_expr