It seems like matrix multiplication does not work in the usual way with DenseAxisArray,
If I use, ones(5,15)*x
using the code in the MWE it throws an error. Interestingly,
array slicing ones(5,15)[1,:]'*x
works fine, but if I loop through it I get an error.
The error message points to aff_expr.jl:319, so after a bit of battle with JuMP, I managed to fix it by defining an affine expression. However, just to multiply the matrices I am using this loop in an inefficient manner as iterations are through the rows.
One =ones(5,15)
Affine=[AffExpr(0) for i = 1:5]
for i in eachindex(Affine)
Affine[i] = One[i,:]'*x
end
Extracting the results into a normal array/vector after solving the problem is proving too difficult.
I have two questions:
1. How can I extract optimization results into a normal array/vector or a dataframe? The standard method for extracting results do not seem to work with DenseAxisArray.
2. I don’t mind using a loop to multiply matrices, but is there a more efficient way of multiplying matrices than that in the code above?
using JuMP, Clp, DataFrames
function get_data()
Relationship = DataFrame(ID = ["A1","A2","A4"], LINK_1 = ["A5","A6","A10"],
LINK_2 = ["A3","A7",""], LINK_3 = ["A9","A11","A12"])
ID_t = collect(1:15)
ID = Vector{String}(undef,15)
for i in 1:15
ID[i]="A".*string(ID_t[i])
end
PRICE = rand(10.0:100.0,15)
Master = DataFrame(ID = ID,PRICE = PRICE)
return Relationship, Master
end
Relationship, Master = get_data()
price = Master.PRICE
model = Model(Clp.Optimizer)
@variable(model, 0<=x[Master.ID]<=1)
@variable(model,t[1:length(Relationship.ID)])
for (i, row) in enumerate(eachrow(Relationship))
for key in values(row)
if !isempty(key)
@constraint(model, t[i] == x[key])
end
end
end
@objective(model,Min,sum(price*x[id] for (id, price) in zip(Master.ID, Master.PRICE))
@constraint(model,con1, sum(price*x[id] for (id, price) in zip(Master.ID, Master.PRICE)) >=200)
@constraint(model,con2, sum(price*x[id] for (id, price) in zip(Master.ID, Master.PRICE)) <=300)
optimize!(model)
Res = value.(x)
This part throws an error if I try to multiply x
by any matrix (apart from vectors) before solving the model:
One = ones(5,15)
One*x
# This can be fixed by
#One =ones(5,15)
#Affine=[AffExpr(0) for i = 1:5]
# for i in eachindex(Affine)
# Affine[i] = One[i,:]'*x
# end