# @expression instead of constraints

**URL:** https://discourse.julialang.org/t/expression-instead-of-constraints/16508
**Category:** Optimization (Mathematical)
**Created:** [October 18, 2018, 6:43pm UTC](https://discourse.julialang.org/t/expression-instead-of-constraints/16508 "2018-10-18T18:43:14Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)
#### Post date: [October 18, 2018, 6:43pm UTC](https://discourse.julialang.org/t/expression-instead-of-constraints/16508/1 "2018-10-18T18:43:15Z")

</div>

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.

```julia
 @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.

```julia
           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!

```julia
    @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!

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [October 18, 2018, 9:29pm UTC](https://discourse.julialang.org/t/expression-instead-of-constraints/16508/2 "2018-10-18T21:29:49Z")

</div>

1. Are you on Julia 1.0?

2. Please provide a [minimum working example](https://stackoverflow.com/help/mcve) that replicates your problem.

---

<div class="post-metadata">

### Author: ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)
#### Post date: [October 18, 2018, 9:34pm UTC](https://discourse.julialang.org/t/expression-instead-of-constraints/16508/3 "2018-10-18T21:34:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [October 19, 2018, 2:18pm UTC](https://discourse.julialang.org/t/expression-instead-of-constraints/16508/4 "2018-10-19T14:18:46Z")

</div>

This works for me

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

```

---

<div class="post-metadata">

### Author: ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)
#### Post date: [October 19, 2018, 2:23pm UTC](https://discourse.julialang.org/t/expression-instead-of-constraints/16508/5 "2018-10-19T14:23:41Z")

</div>

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)?

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

```

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [October 19, 2018, 3:05pm UTC](https://discourse.julialang.org/t/expression-instead-of-constraints/16508/6 "2018-10-19T15:05:03Z")

</div>

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.

```julia
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.)

---

<div class="post-metadata">

### Author: ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)
#### Post date: [October 19, 2018, 3:36pm UTC](https://discourse.julialang.org/t/expression-instead-of-constraints/16508/7 "2018-10-19T15:36:02Z")

</div>

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.

```julia
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?

```julia

```

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [October 19, 2018, 4:12pm UTC](https://discourse.julialang.org/t/expression-instead-of-constraints/16508/8 "2018-10-19T16:12:50Z")

</div>

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

```julia
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

```
