Re: Counting number of non zeros ?

325 views
Skip to first unread message

Paul

unread,
Oct 31, 2012, 4:56:22 PM10/31/12
to am...@googlegroups.com
If the array is a parameter array, rather than a variable array, then yes. If it is a variable array, I think you'll need a binary variable array of equal dimension and a whole bunch of constraints.

Paul

victor.z...@gmail.com

unread,
Oct 31, 2012, 5:42:13 PM10/31/12
to am...@googlegroups.com
Just want to add that there is a count operator that can be used for this purpose both with parameters and variables. The count operator for variables is only supported by the ilogcp solver currently, but there is no such limitation for parameters. See http://www.ampl.com/NEW/LOGIC/index.html#cardop for details.

Here's an example:

set S = 1..10;
var x{S} integer, >= 0;
minimize o: sum{s in S} x[s];
s.t. c: count{s in S} (x[s] != 0) >= 3;
option solver ilogcp;
solve;
display x;

x [*] :=
 1  1
 2  0
 3  0
 4  0
 5  1
 6  1
 7  0
 8  0
 9  0
10  0
;

Victor

On Wed, Oct 31, 2012 at 1:56 PM, Paul <paru...@gmail.com> wrote:
If the array is a parameter array, rather than a variable array, then yes. If it is a variable array, I think you'll need a binary variable array of equal dimension and a whole bunch of constraints.

Paul


--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To view this discussion on the web visit https://groups.google.com/d/msg/ampl/-/iYIpZ_2UpxgJ.

To post to this group, send email to am...@googlegroups.com.
To unsubscribe from this group, send email to ampl+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ampl?hl=en.

Reply all
Reply to author
Forward
0 new messages