Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My parameters are not updated when I re-solve it? #107

Closed
sasadat opened this issue Oct 21, 2019 · 3 comments
Closed

My parameters are not updated when I re-solve it? #107

sasadat opened this issue Oct 21, 2019 · 3 comments

Comments

@sasadat
Copy link

sasadat commented Oct 21, 2019

Hi,
I want to use parametron but when I test it for a simple problem, my parameters are not updated when I resolve it.

using Gurobi
using Parametron
using Random, LinearAlgebra
const model=Model(Gurobi.Optimizer(OutputFlag=0));
n = 8; m = 2
x = [Variable(model) for _ = 1 : n]
A_val=ones(n,n);
A= Parameter(model, val=A_val);
b_val=zeros(n);
b= Parameter(model, val=b_val);
C_val=ones(m,n);
C= Parameter(model, val=C_val);
d_val=zeros(m);
d= Parameter(model, val=d_val);
residual = @expression A * x - b
@objective(model, Minimize, residual  b)
@constraint(model, C * x == d)
@time solve!(model) # curren
println("x: ", value.(model, x));
objvalue=objectivevalue(model);
println("Objective Value: ", objvalue, "(",terminationstatus(model),")");
A_val=randn(n,n);
A= Parameter(model, val=A_val);
b_val=randn(n)+randn(n);
b= Parameter(model, val=b_val);
C_val=randn(m,n)+randn(m,n);
C= Parameter(model, val=C_val);
d_val=rand(m)+rand(m);
d= Parameter(model, val=d_val);
@time solve!(model)
println("x: ", value.(model, x));
objvalue=objectivevalue(model);
println("Objective Value: ", objvalue, "(",terminationstatus(model),")");

Here is the result

  5.465271 seconds (16.84 M allocations: 836.260 MiB, 8.30% gc time)
x: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Objective Value: 0.0(OPTIMAL)
  0.000167 seconds (77 allocations: 8.906 KiB)
x: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Objective Value: 0.0(OPTIMAL)
@sasadat sasadat changed the title My parameters are not updated when resolved? My parameters are not updated when I re-solve it? Oct 21, 2019
@tkoolen
Copy link
Owner

tkoolen commented Oct 22, 2019

When you do

A = Parameter(model, val=A_val)

the model stores a reference to the Parameter A, and A in turn stores a reference to the Matrix A_val. If you subsequently do

A_val = randn(n, n)

you've now made it so that the variable A_val refers to different data than the reference that's stored in the Parameter A. Instead, you should be doing e.g.

A_val .= randn(n, n) # note the dot

or better yet:

randn!(A)

to modify the matrix A_val in place. The second A= Parameter(model, val=A_val) is then unnecessary.

See e.g. https://discourse.julialang.org/t/question-regarding-references/7610/7?u=tkoolen for more info on how references work in Julia.

@tkoolen tkoolen closed this as completed Oct 22, 2019
@tkoolen
Copy link
Owner

tkoolen commented Oct 22, 2019

Feel free to ask more usage questions by the way, either here or (perhaps preferably) on discourse. Just closed the issue because there are no actionable items in the codebase.

@sasadat
Copy link
Author

sasadat commented Oct 22, 2019

Thank you @tkoolen for your response.
I just noticed that in @expression and in @constraint sparse matrices are not accepted.
For example, A=spzeros(n,m). If I use
A_expr = @expression A or @constraint(model, A * X ==0)
It gives me this error:
ERROR: LoadError: MethodError: no method matching one(::Type{Any})
Closest candidates are:
one(::Type{Union{Missing, T}}) where T at missing.jl:87
one(::Missing) at missing.jl:83
one(::BitArray{2}) at bitarray.jl:400
...
Stacktrace:
[1] one(::Type{Any}) at ./missing.jl:87
[2] *(::SparseMatrixCSC{Float32,Int64}, ::Array{Variable,1})at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/SparseArrays/src/linalg.jl:53

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants