Hi All

       Here is a sample use of two options.

*Option1*: Declaring factory interface in MutationPolicy, CrossoverPolicy
and SelectionPolicy. A sample implementation has been shown here for
MutationPolicy. Similar would be required for all other relevant interfaces
and implemented classes.

--CUT--

 public interface MutationPolicy<P> {
     Chromosome<P> mutate(Chromosome<P> original, double mutationRate);

     interface Factory<P> {
         /**
          * Creates an instance with a dedicated source of randomness.
          *
          * @param rng RNG algorithm.
          * @param seed Seed.
          * @return an instance that must <em>not</em> be shared among
 threads.
          */
         MutationPolicy<P> create(RandomSource rng, Object... args);

         default MutationPolicy<P> create(RandomSource rng) {
             return create(rng, null);
         }
         default MutationPolicy<P> create() {
             return create(RandomSource.SPLIT_MIX_64);
         }
     }
 }
//Implementation Class
public class IntegralValuedMutation<P> implements MutationPolicy<P> {

    private final UniformRandomProvider provider;

    private IntegralValuedMutation(RandomSource rng) {
         provider = ThreadLocalRandomSource.current(rng);
    }
    ...
    ...
    public static class MutationFactory<Q> implements Factory<Q> {
        private static final MutationFactory instance = new
MutationFactory<>();
        private MutationFactory() {}

        @Override
        public MutationPolicy<Q> create(RandomSource rng, Object... args) {
            return new IntegralValuedMutation<>(args[0], args[1]);
        }
        public static <Q> MutationFactory<Q> getInstance() {
            return instance;
        }
    }
//Usage
        MutationPolicy<Integer> policy =
IntegralValuedMutation.MutationFactory.<Integer>getInstance().create();
--CUT--

Option2:  Optional constructor argument can also be used as an alternative
solution.
--CUT--
public class IntegralValuedMutation<P> implements MutationPolicy<P> {
    private final UniformRandomProvider provider;
    public IntegralValuedMutation() {
        provider = ThreadLocalRandomSource.current(RandomSource.DEFAULT);
//DEFAULT is a chosen source.
    }
    public IntegralValuedMutation(RandomSource rng) {
        provider = ThreadLocalRandomSource.current(rng);
    }
    ...
}
//Usages
MutationPolicy<Integer> policy = new IntegralValuedMutation(rng);
--CUT--

Option2 looks to be much simpler regarding implementation and I would vote
for the same if we decide to allow customization of RandomSource.

Thanks & Regards
--Avijit Basak


On Mon, 22 Nov 2021 at 19:28, Gilles Sadowski <gillese...@gmail.com> wrote:

> Hello.
>
> Le lun. 22 nov. 2021 à 13:49, Avijit Basak <avijit.ba...@gmail.com> a
> écrit :
> >
> > Hi All
> >
> >         I would like to request everyone to share their opinion regarding
> > use and customization of RNG functionality in the Genetic Algorithm
> > library.
> >         In current design RNG functionality has been used internally by
> the
> > RandomProviderManager class. This class encapsulates a predefined
> instance
> > of RandomSource and utilizes the same for all random number generation
> > requirements. This makes the API cleaner and easy to use for users.
> >         However, during the review an alternate thought has been proposed
> > related to customization of RandomSource by users. According to the new
> > proposal the users will be able to provide a RandomSource instance of
> their
> > choice to the crossover and mutation operators and other places like
> > ChromosomeRepresentationUtils. The drawback of this customization could
> be
> > increased complexity of the API.
>
> Please provide an usage example of both (showing that the alternative
> would actually increase the API complexity).
>
> Thanks,
> Gilles
>
> >         We need to decide here whether we really need this kind of
> > customization by users and if yes the method of doing so. Here two
> options
> > have been proposed.
> > *Option1:*
> > ---CUT---
> > public interface MutationPolicy<P> {
> >     Chromosome<P> mutate(Chromosome<P> original, double mutationRate);
> >
> >     interface Factory<P> {
> >         /**
> >          * Creates an instance with a dedicated source of randomness.
> >          *
> >          * @param rng RNG algorithm.
> >          * @param seed Seed.
> >          * @return an instance that must <em>not</em> be shared among
> > threads.
> >          */
> >         MutationPolicy<P> create(RandomSource rng, Object seed);
> >
> >         default MutationPolicy<P> create(RandomSource rng) {
> >             return create(rng, null);
> >         }
> >         default MutationPolicy<P> create() {
> >             return create(RandomSource.SPLIT_MIX_64);
> >         }
> >     }
> > }
> > ---CUT---
> >
> > *Option 2:*
> > Use of an optional constructor argument for all crossover and mutation
> > operators. Users will be providing a RandomSource instance of their
> choice
> > or use the default one configured while instantiating the operators.
> >
> > Thanks & Regards
> > -- Avijit Basak
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
> For additional commands, e-mail: dev-h...@commons.apache.org
>
>

-- 
Avijit Basak

Reply via email to