Sparse Matrix in AMPL

天涯浪子 提交于 2020-01-25 03:53:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!