# JuMP variable definition

**URL:** https://discourse.julialang.org/t/jump-variable-definition/920
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [December 13, 2016, 2:43pm UTC](https://discourse.julialang.org/t/jump-variable-definition/920 "2016-12-13T14:43:02Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![ashefa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashefa/32/2525_2.png) [@ashefa](https://discourse.julialang.org/u/ashefa)
#### Post date: [December 13, 2016, 2:43pm UTC](https://discourse.julialang.org/t/jump-variable-definition/920/1 "2016-12-13T14:43:02Z")

</div>

how i can defined different bounds or fixed values to elements of a vector variable?

---

<div class="post-metadata">

### Author: ![joehuchette](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joehuchette/32/32_2.png) [@joehuchette](https://discourse.julialang.org/u/joehuchette)
#### Post date: [December 13, 2016, 3:36pm UTC](https://discourse.julialang.org/t/jump-variable-definition/920/2 "2016-12-13T15:36:26Z")

</div>

For example:

```julia
l = [0,1,2]
u = [1,2,3]
@variable(m, l[i] <= x[i=1:3] <= u[i])

```

---

<div class="post-metadata">

### Author: ![ashefa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashefa/32/2525_2.png) [@ashefa](https://discourse.julialang.org/u/ashefa)
#### Post date: [December 13, 2016, 5:02pm UTC](https://discourse.julialang.org/t/jump-variable-definition/920/3 "2016-12-13T17:02:30Z")

</div>

thanks for your answer,but, in this case Julia warning!@variable(PF, P[i=1:nGen; bus[:,2][i] == 2] == gen[:,2][i]/baseMVA)  
@variable(PF, P[i=1:nBus; bus[:,2][i] == 1] == -bus[:,3][i]/baseMVA)  
JuMP has been eliminated predefined variable values.

---

<div class="post-metadata">

### Author: ![joehuchette](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joehuchette/32/32_2.png) [@joehuchette](https://discourse.julialang.org/u/joehuchette)
#### Post date: [December 13, 2016, 5:40pm UTC](https://discourse.julialang.org/t/jump-variable-definition/920/4 "2016-12-13T17:40:31Z")

</div>

You’ll need to provide more information (minimal stand-alone code example, full output, expected behavior).

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [December 14, 2016, 5:27am UTC](https://discourse.julialang.org/t/jump-variable-definition/920/6 "2016-12-14T05:27:12Z")

</div>

You put code in triple backticks (`) to make it easier to follow (or use Ctrl+Shift+C, or the preformatted text button at the top).

---

<div class="post-metadata">

### Author: ![ashefa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashefa/32/2525_2.png) [@ashefa](https://discourse.julialang.org/u/ashefa)
#### Post date: [December 14, 2016, 5:36am UTC](https://discourse.julialang.org/t/jump-variable-definition/920/7 "2016-12-14T05:36:36Z")

</div>

```julia
using JuMP
PF = Model()
#system MVA 
basebaseMVA = 100;
# bus data
# bus_i type Pd Qd Gs Bs area Vm Va baseKV zone Vmax Vmin
bus = [
1 3 0 0 0 0 1 1 0 345 1 1.1 0.9
2 2 0 0 0 0 1 1 0 345 1 1.1 0.9
3 2 0 0 0 0 1 1 0 345 1 1.1 0.9
4 1 0 0 0 0 1 1 0 345 1 1.1 0.9
5 1 90 30 0 0 1 1 0 345 1 1.1 0.9
6 1 0 0 0 0 1 1 0 345 1 1.1 0.9
7 1 100 35 0 0 1 1 0 345 1 1.1 0.9
8 1 0 0 0 0 1 1 0 345 1 1.1 0.9
9 1 125 50 0 0 1 1 0 345 1 1.1 0.9];
# generator data
# bus Pg Qg Qmax Qmin Vg mBase status Pmax Pmin Pc1 Pc2 Qc1min Qc1max Qc2min Qc2max ramp_agc ramp_10 ramp_30 ramp_q apf
gen = [
1 0 0 300 -300 1 100 1 250 10 0 0 0 0 0 0 0 0 0 0 0
2 163 0 300 -300 1 100 1 300 10 0 0 0 0 0 0 0 0 0 0 0
3 85 0 300 -300 1 100 1 270 10 0 0 0 0 0 0 0 0 0 0 0;];
# branch data# fbus tbus r x b rateA rateB rateC ratio angle status angmin angmax
branch = [
1 4 0 0.0576 0 250 250 250 0 0 1 -360 360
4 5 0.017 0.092 0.158 250 250 250 0 0 1 -360 360
5 6 0.039 0.17 0.358 150 150 150 0 0 1 -360 360
3 6 0 0.0586 0 300 300 300 0 0 1 -360 360
6 7 0.0119 0.1008 0.209 150 150 150 0 0 1 -360 360
7 8 0.0085 0.072 0.149 250 250 250 0 0 1 -360 360
8 2 0 0.0625 0 250 250 250 0 0 1 -360 360
8 9 0.032 0.161 0.306 250 250 250 0 0 1 -360 360
9 4 0.01 0.085 0.176 250 250 250 0 0 1 -360 360];
nBus=size(bus,1); # Number of Busses
nGen=size(gen,1); # Number of Generators
@variables PF begin 
V[i=1:nBus], (lowerbound = bus[:,13][i], start = bus[:,8][i], upperbound = bus[:,12][i]) 
δ[i=1:nBus], (lowerbound = -pi, start = bus[:,9][i], upperbound = pi) 
P[i=1:nBus] 
Q[i=1:nBus]
end
@variable(PF, V[i=1:nBus;bus[:,2][i] == 3] == gen[:,6][i])
@variable(PF, δ[i=1:nBus;bus[:,2][i] == 3] == 0)
@variable(PF, gen[:,10][i]/baseMVA <= P[i=1:nBus;bus[:,2][i] == 3] <= gen[:,9][i]/baseMVA)
@variable(PF, gen[:,5][i]/baseMVA <= Q[i=1:nBus;bus[:,2][i] == 3] <= gen[:,4][i]/baseMVA)
@variable(PF, V[i=1:nBus;bus[:,2][i] == 2] == gen[:,6][i])
@variable(PF, P[i=1:nBus;bus[:,2][i] == 2] == gen[:,2][i]/baseMVA)
@variable(PF, gen[:,5][i]/baseMVA <= Q[i=1:nBus;bus[:,2][i] == 2] <= gen[:,4[i]/baseMVA)
@variable(PF, P[i=1:nBus;bus[:,2][i] == 1] == -bus[:,3][i]/baseMVA)
@variable(PF, Q[i=1:nBus;bus[:,2][i] == 1] == -bus[:,4][i]/baseMVA)
@variable(PF, bus[:,13][i] <= V[i=1:nBus;bus[:,2][i] == 1] <= bus[:,12][i])

```

NOTE: edited for formatting by @StefanKarpinski

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [January 5, 2017, 3:24pm UTC](https://discourse.julialang.org/t/jump-variable-definition/920/9 "2017-01-05T15:24:27Z")

</div>

For the last 10 lines, you should use `@constraint` instead of `@variable`

---

<div class="post-metadata">

### Author: ![ashefa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashefa/32/2525_2.png) [@ashefa](https://discourse.julialang.org/u/ashefa)
#### Post date: [January 6, 2017, 5:15am UTC](https://discourse.julialang.org/t/jump-variable-definition/920/10 "2017-01-06T05:15:28Z")

</div>

I did this, but the solution was infeasible.

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [January 9, 2017, 6:37pm UTC](https://discourse.julialang.org/t/jump-variable-definition/920/11 "2017-01-09T18:37:55Z")

</div>

This is surprising. Could you give the code that gave you the infeasible solution ?

---

<div class="post-metadata">

### Author: ![ccoffrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccoffrin/32/400_2.png) [@ccoffrin](https://discourse.julialang.org/u/ccoffrin)
#### Post date: [January 9, 2017, 8:38pm UTC](https://discourse.julialang.org/t/jump-variable-definition/920/12 "2017-01-09T20:38:21Z")

</div>

If you primary interest is to solve AC Power Flows you might want to have a look at PowerModels.jl as an example.

---

<div class="post-metadata">

### Author: ![ashefa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashefa/32/2525_2.png) [@ashefa](https://discourse.julialang.org/u/ashefa)
#### Post date: [January 10, 2017, 4:15am UTC](https://discourse.julialang.org/t/jump-variable-definition/920/13 "2017-01-10T04:15:05Z")

</div>

thanks for your response, but i wanted to solve own AC power flow to continue that to reach the solution of the “State Estimation” problem

---

<div class="post-metadata">

### Author: ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)
#### Post date: [January 15, 2017, 5:38am UTC](https://discourse.julialang.org/t/jump-variable-definition/920/14 "2017-01-15T05:38:41Z")

</div>

A post was merged into an existing topic: [Problem in Power Flow using Julia](https://discourse.julialang.org/t/problem-in-power-flow-using-julia/1491)

---

<div class="post-metadata">

### Author: ![KrishnaKanhaiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krishnakanhaiya/32/443_2.png) [@KrishnaKanhaiya](https://discourse.julialang.org/u/KrishnaKanhaiya)
#### Post date: [May 20, 2017, 3:54pm UTC](https://discourse.julialang.org/t/jump-variable-definition/920/15 "2017-05-20T15:54:42Z")

</div>

> [@ccoffrin](#):
>
> If you primary interest is to solve AC Power Flows you might want to have a look at PowerModels.jl as an example.

I am not an expert in power flow optimization but we solved a power flow problem using Convex.jl [here](http://nbviewer.jupyter.org/github/JuliaOpt/Convex.jl/blob/master/examples/optimization_with_complex_variables/power_flow_optimization.ipynb). It would be interesting to know if Convex.jl can be used to satisfy your needs?  
We also support optimization with complex variables and coefficients.

---

<div class="post-metadata">

### Author: ![ccoffrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccoffrin/32/400_2.png) [@ccoffrin](https://discourse.julialang.org/u/ccoffrin)
#### Post date: [May 20, 2017, 4:13pm UTC](https://discourse.julialang.org/t/jump-variable-definition/920/16 "2017-05-20T16:13:22Z")

</div>

Just to clarify this point. The referenced Convex.jl model solves the SDP relaxation of the AC Power Flow equations. In some cases the SDP model can find a globally optimal solution to the AC model, but this is not guaranteed. Also the scalability of SDP solvers is a significant challenge for power system applications.
