On Thursday, April 23, 2015 at 12:51:05 PM UTC+1, Michela Di Lullo wrote:
>
> [...] and I need to declare the B susceptance matrix defined, in AMPL, as:
>
> param B{(k,m) in YBUS} := if(k == m) then sum{(l,k,i) in BRANCH}
> 1/branch_x[l,k,i]
>
> +sum{(l,i,k) in BRANCH} 1/branch_x[l,i,k]
> else if(k != m) then
>
> sum{(l,k,m) in BRANCH} 1/branch_x[l,k,m]
>
> +sum{(l,m,k) in BRANCH} 1/branch_x[l,m,k];
>
> I'm trying to make it but it's not working because of the indexes. I don't
> know how to declare the parameter branch_x indexed by (n,b_from,b_to).
>
branch_x_dict = {BRANCH[idx]=>branch_x[idx] for idx=1:20}
then you can access the elements as you would expect: branch_x_dict[l,k,m]
> Any idea about how to declare the B matrix correctly?
>
If I understand correctly, the loops inside the sums are only over the
indices of variables not previously specified, so e.g. sum{(l,k,i) in
BRANCH} would iterate over the elements of BRANCH for which the second
value is equal to the k from the outer loop (over YBUS). It doesn't look as
neat (maybe someone can improve this), but I think the following does what
you want.
B = Dict()
for (k, m) in Y_BUS
v = 0.0
if k == m
for (l,k_,i) in BRANCH
k == k_ || continue
v += 1/branch_x_dict[l,k,i]
end
for (l,i,k_) in BRANCH
k == k_ || continue
v += 1/branch_x_dict[l,i,k]
end
else
for (l,k_,m_) in BRANCH
k == k_ || continue
m == m_ || continue
v += 1/branch_x_dict[l,k,m]
end
for (l,m_,k_) in BRANCH
k == k_ || continue
m == m_ || continue
v += 1/branch_x_dict[l,m,k]
end
end
B[k,m] = v
end
This is a bit cumbersome but does what you want, I think. Maybe someone
else has a good idea how to simplify it.
I've used a Dict() for B; you can also just define B as a matrix and the
code should run just the same:
N_NODE = maximum([NODE_FROM,NODE_TO]) # largest node number = size of B
matrix
B = spzeros(N_NODE, N_NODE)
(or just zeros if you want a full, not a sparse matrix.)
Alternatively, when B is a Dict, you can convert it into a matrix by
iterating over the elements:
N_NODE = maximum([NODE_FROM,NODE_TO]) # maximum number of nodes = size of B
matrix
Bmat = spzeros(N_NODE, N_NODE)
for ((k, m), v) in B
Bmat[k,m] = v
end
For one-dimensional sparse matrices there's sparsevec() which does this,
but there doesn't seem to be an equivalent for two-dimensional sparse
matrices/Dicts?...
Hope this helps.
best regards,
ST