Evocosm - A C++ Framework for Evolutionary Computing

Main Index

Created by Scott Robert Ladd at Coyote Gulch Productions.


Public Member Functions | Protected Attributes | List of all members
libevocosm::roulette_wheel Class Reference

A simulated roulette wheel for weighted selection. More...

#include <roulette.h>

Inheritance diagram for libevocosm::roulette_wheel:
libevocosm::globals

Public Member Functions

 roulette_wheel (const vector< double > &a_weights, double a_min_weight=std::numeric_limits< double >::epsilon(), double a_max_weight=std::numeric_limits< double >::max())
 Creation constructor (from vector) More...
 
 roulette_wheel (const double *a_weights, size_t a_length, double a_min_weight=std::numeric_limits< double >::epsilon(), double a_max_weight=std::numeric_limits< double >::max())
 Creation constructor (from c-type array) More...
 
 roulette_wheel (const roulette_wheel &a_source)
 Copy constructor. More...
 
roulette_wheeloperator= (const roulette_wheel &a_source)
 Assignment operator. More...
 
 ~roulette_wheel ()
 Destructor. More...
 
size_t get_size () const
 Get size (number of weights) More...
 
double set_weight (size_t a_index, double a_weight)
 Change the weight assigned to an entry. More...
 
double get_weight (size_t a_index) const
 Get the weight for an index. More...
 
size_t get_index () const
 Retrieve a random index. More...
 

Protected Attributes

size_t m_size
 Number of weights in this wheel.
 
double * m_weights
 Array of m_weights.
 
double m_total_weight
 Total weight of all indexes.
 
double m_min_weight
 Minimum possible weight value.
 
double m_max_weight
 Maximum possible weight value.
 

Additional Inherited Members

- Static Protected Member Functions inherited from libevocosm::globals
static size_t rand_index (size_t n)
 Static function to allow use of g_random function pointer in random_shuffle.
 
static void set_seed (uint32_t a_seed)
 Set the seed for the random number generator.
 
static uint32_t get_seed ()
 Set the seed for the random number generator.
 
static std::string version ()
 Get version number.
 
- Static Protected Attributes inherited from libevocosm::globals
static prng g_random
 A shared random number generator.
 
static std::string g_version
 Version number.
 

Detailed Description

You've probably seen a standard gambler's roulette wheel, a spinning circle divided into thirty-seven or thirty-eight equal-sized, pie-shaped sections. The croupier sets the wheel spinning and at the same time tosses an marble into the bowl in the direction opposite to that in which the wheel is moving; when the motion of the wheel ceases, the ball comes to rest in one of the numbered sections.

As an algorithm, a roulette wheel is an array of weights, and the marble is a random integer less than the sum of all weights in the population. To pick on of the "section" (in this case, an index), the algorithm simulates the bouncing metal ball by iterating through the array of weights; if the marble value is less than the current weight (i.e., the ball has stopped), the algorithm returns the index of that weight. Otherwise, the algorithm subtracts the current weight from the marble value, and repeats the process with the next element in the weight array. Thus the largest weights values tend to be the most likely resting places for the marble, since they use a larger area of the abstract wheel.

Constructor & Destructor Documentation

libevocosm::roulette_wheel::roulette_wheel ( const vector< double > &  a_weights,
double  a_min_weight = std::numeric_limits< double >::epsilon(),
double  a_max_weight = std::numeric_limits< double >::max() 
)

Creates a new roulette_wheel based on a set of weights.

Parameters
a_weights- A vector of weights defining the wheel
a_min_weight- Minimum possible weight value (defaults to epsilon for type)
a_max_weight- Maximum possible weight value (defaults to max for type)
libevocosm::roulette_wheel::roulette_wheel ( const double *  a_weights,
size_t  a_length,
double  a_min_weight = std::numeric_limits< double >::epsilon(),
double  a_max_weight = std::numeric_limits< double >::max() 
)

Creates a new roulette_wheel based on a set of weights.

Parameters
a_weights- An array of weights defining the wheel
a_length- Number of elements in a_weights
a_min_weight- Minimum possible weight value (defaults to 1.0)
a_max_weight- Maximum possible weight value (defaults to 100.0)
libevocosm::roulette_wheel::roulette_wheel ( const roulette_wheel a_source)

Creates a new roulette_wheel from an existing one.

Parameters
a_source- The source object
libevocosm::roulette_wheel::~roulette_wheel ( )

Destroys a roulette_wheel, freeing resources.

Member Function Documentation

size_t libevocosm::roulette_wheel::get_index ( ) const

Returns a randomly-selected index value by simulating the bouncing of a ball on a roulette wheel, where each index is a "slot" with a width determined by its assigned weight.

Returns
A random index value

Referenced by libevocosm::machine_tools::mutation_selector::get_index().

size_t libevocosm::roulette_wheel::get_size ( ) const
inline

Gets the number of weights indexed by the roulette wheel.

Returns
The number of weights

References m_size.

double libevocosm::roulette_wheel::get_weight ( size_t  a_index) const

Gets the weight assigned to a specific index.

Parameters
a_indexIndex for which weight should be returned
Returns
Weight for this index, or -1 if a_index is invalid
roulette_wheel& libevocosm::roulette_wheel::operator= ( const roulette_wheel a_source)

Assigns a roulette_wheel the state of another.

Parameters
a_source- The source object
double libevocosm::roulette_wheel::set_weight ( size_t  a_index,
double  a_weight 
)

Changes the weight assigned to a specific wheel index.

Parameters
a_index- Index to change
a_weight- New weight Value
Returns
Previous weight for this index, or -1 if a_index is invalid

Referenced by libevocosm::machine_tools::mutation_selector::set_weight().


The documentation for this class was generated from the following file:

© 1996-2005 Scott Robert Ladd. All rights reserved.
HTML documentation generated by Dimitri van Heesch's excellent Doxygen tool.