remove duplicated elements in set?

Archive of Gamsworld Google Group
Post Reply
Archiver
User
User
Posts: 7876
Joined: 7 years ago

remove duplicated elements in set?

Post by Archiver »


Hello,

I have a set which is created by some random numbers. Is there any function I can use to remove the duplicated elements and keep those elements unique? Thanks.

P.L

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.
Archiver
User
User
Posts: 7876
Joined: 7 years ago

Re: remove duplicated elements in set?

Post by Archiver »


Hi,

There is no command/option that will allow you to remove duplicates in a set. The compiler rejects this. There are some ways around this:

1) Assuming the set is supplied in an external text file, you can use the tools sort and uniq to make the elements unique:

$onecho > i.txt
6
5
4
6
5
3
2
2
1
$offecho
$call sort i.txt | uniq > i_uniq.txt
$if errorlevel 1 $abort problems making i unique
set i /
$include i_uniq.txt
/;
display i;

2) Add a unique index and make it a tuple and then project:

$onecho > i.txt
6
5
4
6
5
3
2
2
1
$offecho
$onecho > makeuniq.gms
$ call awk -vr="r" "{ print r NR,$0; }" i.txt > ipair.txt
$ if errorlevel 1 $abort problems adding a unique index
alias (*,u1,u2);
set ipair(u1,u2) /
$ ondelim
$ include ipair.txt
$ offdelim
/
i(u2);
option i<ipair;
$offecho
$call gams makeuniq.gms lo=%gams.lo% gdx=i
$if errorlevel 1 $abort problems running makeuniq

set i;
$gdxin i
$load i
display i;

Please note the different order of i in the display statement:

1) 1, 2, 3, 4, 5, 6
2) 6, 5, 4, 3, 2, 1

The sort in the first solution, changes the order of the labels, while the second solution preserves the order from the input file.

Hope this helps,
Michael Bussieck - GAMSWorld Coordinator

On Thursday, March 24, 2016 at 2:58:09 AM UTC-4, Peiyu Luo wrote:

Hello,

I have a set which is created by some random numbers. Is there any function I can use to remove the duplicated elements and keep those elements unique? Thanks.

P.L

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.
Post Reply