问题
I have a sparse matrix in AMPL. As a result, it includes a lot of values that are coded as ".". The "." value in AMPL means "no value specified here." When I try to solve the optimization problem I get a message reading "no value specified for..." in reference to the cells containing the "." consequently, it won't solve the problem.
However, when I try to specify a default value to replace the ".", the problem churns and churns and doesn't solve. Is there any way I can set restrictions on the parameter so that the solver doesn't look at the "." values?
Hope this is clear enough.
回答1:
Instead of specifying a default value, you can work with a sparse matrix. For example:
param m integer > 0;
set C within {1..m,1..m};
param A{C};
data;
param m := 4;
param: C: A: 1 2 3 4 :=
1 36 . . -2
2 . 7 3 .
3 . . -8 16
4 12 3 . 77 ;
And in your model you should replace indexing over {i in 1..m, j in 1..m}
with indexing over {(i,j) in C}
.
See also https://groups.google.com/d/msg/ampl/1s1X-UNSCg4/RWZm0sVa0IQJ.
回答2:
Representing non-useful values with a dot (or a period) is not what I would call "sparse". It may save operations time, but it will use just as much memory as a dense representation. Having said that the above example is also not what I would call sparse. A lot of positions are non-zero.
Having said that, if you truly have a large sparse matrix, use the formulation below instead. It really takes advantage of the structure "within" and should save you some memory.
param m integer > 0;
set C within {1..m,1..m};
param A{C};
data;
param m := 4;
param: C: A:=
1 1 36
1 4 -2
2 2 7
2 3 3
3 3 -8
3 4 16
4 1 12
4 2 3
4 4 77;
来源:https://stackoverflow.com/questions/29955655/sparse-matrix-in-ampl