commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Othmen Tiliouine <tiliouine.oth...@gmail.com>
Subject Re: [Bag] random pick
Date Tue, 12 Mar 2013 07:58:02 GMT
Thank you Ted,

I understand from your mail that there is no particular reason that makes
that the interface Bag no contains these methods and that this subject has
never been discussed in the mailing list.

I'll see if I can create this patch this weekend, but i want to know, what do
you think what are the methods I should add

public Object pick(); //pick random element (with remove)
public Object pickAndRemit() ; //pick random element (without remove)
public Collection pick(int n); //pick random n element (with remove)
public Collection pickAndRemit(int n) ; //pick random n element (without
remove)
public Iterator pick(int n); //pick random n element (with remove)
public Iterator pickAndRemit(int n) ; //pick random n element (without
remove)
public List pick(int n); //pick random n element (with remove)
public List pickAndRemit(int n) ; //pick random n element (without remove)
public Bag pick(int n); //pick random n element (with remove)
public Bag pickAndRemit(int n) ; //pick random n element (without remove)

maby i must provide the two kind of methods  ( Bag pick(int n) and Iterator
pickOrdered(int n) ) ?


there is something I do not understand why the bag does not use generics ?

Othmen TILIOUINE.


2013/3/12 Ted Dunning <ted.dunning@gmail.com>

> Othmen,
>
> The common way to contribute code is to file a bug report/enhancement
> request at the correct commons component:
>
> https://issues.apache.org/jira/secure/BrowseProjects.jspa#10260
>
> My guess is that you want collections which is at
>
> https://issues.apache.org/jira/browse/COLLECTIONS
>
> Then you should put your suggested solution onto that JIRA as an
> attachment.  The attachment should be a patch file.  That will be a place
> that the merits of the contribution can be discussed.
>
> My own comment here is that the common idiom in the English statistical
> literature for sampling from a bag is either sampling without replacement
> or sampling with replacement.  Moreover, it is typical that you allow for
> multiple items to be sampled rather than requiring sampling to proceed one
> element at a time.  Sampling n items from an n-item bag without
> replacement, for instance, would return a permutation of the bag (if you
> get an ordered sample) or a partition of the bag (if you get an unordered
> sample).
>
> There is also the question of whether the bag should be considered
> immutable during sampling.  If you want to leave the bag unchanged by
> sampling, then you probably should be returning a sampler object of some
> kind that is kind of a randomized iterator or iterable.
>
> These kinds of design decisions need to be hashed out in the process of
> getting your contribution into the code.
>
>
> Good luck with your contribution!
>
> On Mon, Mar 11, 2013 at 4:16 PM, Othmen Tiliouine <
> tiliouine.othmen@gmail.com> wrote:
>
> > Hello,
> >
> > I just saw the Bag interface and its implementations, I'm surprised that
> > Bag
> > <T> (and none of these implementations) expose the method
> > public T pick() and public T pickAndRemit() (pick a random element)
> > The Bag object we see in nature, it is mainly used to this, that's why
> > it is widely
> > used in the probability that when I met 2 white balls and one black,
> when I
> > draw a ball randomly I have 2 times more likely to have a white ball
> >
> > I think that if this caracteristic exists it would be very valuable.
> >
> >
> > this is a simple implementation of pick() and pickAndRemit() with HashBag
> >
> > package com.otiliouine.commons.collections;
> >
> > import java.util.Iterator;
> >
> > import org.apache.commons.collections.bag.HashBag;
> >
> > public class OpaqueHashBag extends HashBag implements OpaqueBag {
> >
> >     public Object pcik() {
> >         if (size() == 0) {
> >             return null;
> >         }
> >         int randomIndex = (int) (Math.random() * size());
> >         int searchIndex = randomIndex;
> >
> >         Iterator iterator = this.iterator();
> >         //iterator = this.map.keySet().iterator()
> >         Object selectedItem = iterator.next();
> >         int count;
> >
> >         while (searchIndex > 0) {
> >             searchIndex --;
> >             selectedItem = iterator.next();
> >         }
> > //        while ((count = getCount(selectedItem)) < searchIndex + 1) {
> > //            searchIndex -= count;
> > //            selectedItem = iterator.next();
> > //        }
> >         return selectedItem;
> >     }
> >
> >     public Object pickAndRemit() {
> >         Object picked = pcik();
> >         if (picked != null) {
> >             remove(picked, 1);
> >         }
> >         return picked;
> >     }
> > }
> >
> >
> >
> > it can be optimized if it is writen in AbstractMapBag class
> >
> > and this is the test
> >
> > public class TestOpaqueHashBag {
> >
> >     private OpaqueHashBag bag;
> >
> >     public static void main(String ... args) {
> >         TestOpaqueHashBag t = new TestOpaqueHashBag();
> >         t.before();
> >         t.testpick();
> >     }
> >
> >     public void before(){
> >         bag = new OpaqueHashBag();
> >         bag.add("white", 6);
> >         bag.add("black", 3);
> >         bag.add("red", 1);
> >     }
> >
> >     public void testpick() {
> >         int w = 0, b = 0, r = 0;
> >         for (int i = 0; i < 1000; i++) {
> >             String ball = (String) bag.pcik();
> >
> >             if (ball.equals("white")) {
> >                 w ++;
> >             } else if (ball.equals("black")) {
> >                 b ++;
> >             } else {
> >                 r ++;
> >             }
> >         }
> >         System.out.println("%white = " + w/10);
> >         System.out.println("%black = " + b/10);
> >         System.out.println("%red = " + r/10);
> >     }
> >
> > }
> >
> > output :
> >
> > %white = 59
> > %black = 30
> > %red = 9
> >
> >
> > Othmen TILIOUINE
> >
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message