roulette wheel working code

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

roulette wheel working code

Paolo Pannarale
Hi, I modified the code of spin wheel as following and now it seems to work.
Thank you, BR, Paolo

  private IChromosome spinWheel(final RandomGenerator a_generator,
                                final double[] a_fitnessValues,
                                double[] a_counterValues,
                                final IChromosome[] a_chromosomes) {
    // Randomly choose a slot on the wheel.
    // ------------------------------------
    double selectedSlot =
        a_generator.nextDouble() * m_totalNumberOfUsedSlots;
    if (selectedSlot > m_totalNumberOfUsedSlots) {
      selectedSlot = m_totalNumberOfUsedSlots;
    }
    // Loop through the wheel until we find our selected slot. Here's
    // how this works: we have three arrays, one with the fitness values
    // of the chromosomes, one with the total number of slots on the
    // wheel that each chromosome occupies (its counter value), and
    // one with the chromosomes themselves. The array indices associate
    // each of the three together (eg, if a chromosome is at index 5,
    // then its fitness value and counter value are also at index 5 of
    // their respective arrays).
    //
    // We've already chosen a random slot number on the wheel from which
    // we want to select the Chromosome. We loop through each of the
    // array indices and, for each one, we add the number of occupied slots
    // (the counter value) to an ongoing total until that total
    // reaches or exceeds the chosen slot number. When that happenes,
    // we've found the chromosome sitting in that slot and we return it.
    // --------------------------------------------------------------------
    double currentSlot = 0.0d;
    FitnessEvaluator evaluator = getConfiguration().getFitnessEvaluator();
    boolean isFitter2_1 = evaluator.isFitter(2, 1);
    int selectedIdx;
    boolean found=false;
    for (selectedIdx = 0; selectedIdx < a_counterValues.length &&(!found); selectedIdx++) {
      // Increment our ongoing total and see if we've landed on the
      // selected slot.
      // ----------------------------------------------------------
        currentSlot += a_counterValues[selectedIdx];
      if (isFitter2_1) {
        // Introduced DELTA to fix bug 1449651
        found = selectedSlot - currentSlot <= DELTA;
      }
      else {
        // Introduced DELTA to fix bug 1449651
        found = Math.abs(currentSlot - selectedSlot) <= DELTA;
      }
    }
    selectedIdx--;
    // We have reached here because there were rounding errors when
    // computing with doubles or because the last entry is the right one.
    // ------------------------------------------------------------------
    if (!getDoubletteChromosomesAllowed()) {
        m_totalNumberOfUsedSlots -= a_counterValues[selectedIdx];
        a_counterValues[selectedIdx] = 0;
      }
      else {
        a_counterValues[selectedIdx] -= a_fitnessValues[selectedIdx];
        m_totalNumberOfUsedSlots -= a_fitnessValues[selectedIdx];
      }
      // Introduced DELTA to fix bug 1449651
      if (Math.abs(m_totalNumberOfUsedSlots) < DELTA) {
        m_totalNumberOfUsedSlots = 0.0d;
      }
      // Now return our selected Chromosome.
      // -----------------------------------
      return a_chromosomes[selectedIdx];
  }

--
‘[M]y work, which I’ve done for a long time, was not pursued  in order  to gain  the praise  I now enjoy, but chiefly  from a craving after knowledge, which  I notice  resides  in me more  than  in most other men. And wherewithal, whenever I found out anything remarkable, I have thought it my duty to put down my discovery on paper, so that all ingenious people might be informed thereof.’
(van Leeuwenhoek, Letter to the Royal Society, June 12 1716)

“Make it as simple as possible. But not simpler”.
(Albert Einstein)

"The best way to predict the future is to invent it".
(Alan Kay)

___________________________________

Questo documento è indirizzato esclusivamente al destinatario.
Tutte le informazioni ivi contenute, compresi eventuali allegati,
sono soggette a riservatezza secondo i termini del D.Lgs. 196/2003
in materia di "privacy" e ne è proibito l'utilizzo da parte di
altri soggetti. Se avesse ricevuto per errore questo messaggio,
La preghiamo cortesemente di contattare il mittente al più presto
e di cancellare il messaggio subito dopo. Grazie.
----
This document is exclusively intended for the stated addressee.
All information therein, including any attachment, are reserved
as per Italian D.Lgs. 196/2003 about privacy, and cannot be used
by third parties. In case you received this message by mistake,
please inform the sender and delete the message afterward.
Thank you.

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
jgap-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgap-users
Reply | Threaded
Open this post in threaded view
|

Re: roulette wheel working code

Klaus Meffert-5
Hi Paolo,
 
first of all thanx a lot for your interest in JGAP and your detailled problem description + resolution!
 
Would it be possible for you to contribute a test case (best would be a Junit test case) which exposes the problem?
 
Besides I noticed in your code suggestion that the following comment seems wrong:
// We have reached here because there were rounding errors when
    // computing with doubles or because the last entry is the right one.
The program will *always* come to this place as the FOR-LOOP is not exited via return!
 
Can you help clarify this?
 
Thanx and best
 


From: Paolo Pannarale [mailto:[hidden email]]
Sent: Friday, July 20, 2012 2:39 PM
To: [hidden email]
Subject: [jgap-users] roulette wheel working code

Hi, I modified the code of spin wheel as following and now it seems to work.
Thank you, BR, Paolo

  private IChromosome spinWheel(final RandomGenerator a_generator,
                                final double[] a_fitnessValues,
                                double[] a_counterValues,
                                final IChromosome[] a_chromosomes) {
    // Randomly choose a slot on the wheel.
    // ------------------------------------
    double selectedSlot =
        a_generator.nextDouble() * m_totalNumberOfUsedSlots;
    if (selectedSlot > m_totalNumberOfUsedSlots) {
      selectedSlot = m_totalNumberOfUsedSlots;
    }
    // Loop through the wheel until we find our selected slot. Here's
    // how this works: we have three arrays, one with the fitness values
    // of the chromosomes, one with the total number of slots on the
    // wheel that each chromosome occupies (its counter value), and
    // one with the chromosomes themselves. The array indices associate
    // each of the three together (eg, if a chromosome is at index 5,
    // then its fitness value and counter value are also at index 5 of
    // their respective arrays).
    //
    // We've already chosen a random slot number on the wheel from which
    // we want to select the Chromosome. We loop through each of the
    // array indices and, for each one, we add the number of occupied slots
    // (the counter value) to an ongoing total until that total
    // reaches or exceeds the chosen slot number. When that happenes,
    // we've found the chromosome sitting in that slot and we return it.
    // --------------------------------------------------------------------
    double currentSlot = 0.0d;
    FitnessEvaluator evaluator = getConfiguration().getFitnessEvaluator();
    boolean isFitter2_1 = evaluator.isFitter(2, 1);
    int selectedIdx;
    boolean found=false;
    for (selectedIdx = 0; selectedIdx < a_counterValues.length &&(!found); selectedIdx++) {
      // Increment our ongoing total and see if we've landed on the
      // selected slot.
      // ----------------------------------------------------------
        currentSlot += a_counterValues[selectedIdx];
      if (isFitter2_1) {
        // Introduced DELTA to fix bug 1449651
        found = selectedSlot - currentSlot <= DELTA;
      }
      else {
        // Introduced DELTA to fix bug 1449651
        found = Math.abs(currentSlot - selectedSlot) <= DELTA;
      }
    }
    selectedIdx--;
    // We have reached here because there were rounding errors when
    // computing with doubles or because the last entry is the right one.
    // ------------------------------------------------------------------
    if (!getDoubletteChromosomesAllowed()) {
        m_totalNumberOfUsedSlots -= a_counterValues[selectedIdx];
        a_counterValues[selectedIdx] = 0;
      }
      else {
        a_counterValues[selectedIdx] -= a_fitnessValues[selectedIdx];
        m_totalNumberOfUsedSlots -= a_fitnessValues[selectedIdx];
      }
      // Introduced DELTA to fix bug 1449651
      if (Math.abs(m_totalNumberOfUsedSlots) < DELTA) {
        m_totalNumberOfUsedSlots = 0.0d;
      }
      // Now return our selected Chromosome.
      // -----------------------------------
      return a_chromosomes[selectedIdx];
  }

--
‘[M]y work, which I’ve done for a long time, was not pursued  in order  to gain  the praise  I now enjoy, but chiefly  from a craving after knowledge, which  I notice  resides  in me more  than  in most other men. And wherewithal, whenever I found out anything remarkable, I have thought it my duty to put down my discovery on paper, so that all ingenious people might be informed thereof.’
(van Leeuwenhoek, Letter to the Royal Society, June 12 1716)

“Make it as simple as possible. But not simpler”.
(Albert Einstein)

"The best way to predict the future is to invent it".
(Alan Kay)

___________________________________

Questo documento è indirizzato esclusivamente al destinatario.
Tutte le informazioni ivi contenute, compresi eventuali allegati,
sono soggette a riservatezza secondo i termini del D.Lgs. 196/2003
in materia di "privacy" e ne è proibito l'utilizzo da parte di
altri soggetti. Se avesse ricevuto per errore questo messaggio,
La preghiamo cortesemente di contattare il mittente al più presto
e di cancellare il messaggio subito dopo. Grazie.
----
This document is exclusively intended for the stated addressee.
All information therein, including any attachment, are reserved
as per Italian D.Lgs. 196/2003 about privacy, and cannot be used
by third parties. In case you received this message by mistake,
please inform the sender and delete the message afterward.
Thank you.

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
jgap-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgap-users