Back to index

python-biopython  1.60
Public Member Functions | Private Member Functions | Private Attributes
test_GAQueens.QueensRepair Class Reference

List of all members.

Public Member Functions

def __init__
def repair

Private Member Functions

def _get_duplicates
def _get_unused

Private Attributes

 _repair_prob

Detailed Description

A repair function to help create correct N-Queens solutions.

This attempts to help generate correct solutions by offering some
amount of repair to remove queens that are located in the same rows.
After repair, a sequence should have no queens in the same row.

So, if you start with something infeasible like (1, 2, 2, 3, 3, 4),
after running it through repair you'll get a feasible individual
like (1, 2, 5, 3, 6, 4). This should greatly reduce the number of
individuals that need to be searched through in a population.

Definition at line 157 of file test_GAQueens.py.


Constructor & Destructor Documentation

def test_GAQueens.QueensRepair.__init__ (   self,
  repair_prob = 1 
)
Initialize the repairer.

Arguments:

o repair_prob -- The probability that we'll repair a genome.
By default, we always repair.

Definition at line 169 of file test_GAQueens.py.

00169 
00170     def __init__(self, repair_prob = 1):
00171         """Initialize the repairer.
00172 
00173         Arguments:
00174 
00175         o repair_prob -- The probability that we'll repair a genome.
00176         By default, we always repair.
00177         """
00178         self._repair_prob = repair_prob


Member Function Documentation

def test_GAQueens.QueensRepair._get_duplicates (   self,
  genome 
) [private]
Return all of the letters in the genome that are duplicated.

This checks every letter in the genome (which are the rows of
the chessboard, in this case), and adds them to a list of duplicated
items if there is more than one of them, and then returns this list.

Definition at line 179 of file test_GAQueens.py.

00179 
00180     def _get_duplicates(self, genome):
00181         """Return all of the letters in the genome that are duplicated.
00182 
00183         This checks every letter in the genome (which are the rows of
00184         the chessboard, in this case), and adds them to a list of duplicated
00185         items if there is more than one of them, and then returns this list.
00186         """
00187         duplicates = []
00188         for item in genome.alphabet.letters:
00189             if genome.count(str(item)) > 1:
00190                 duplicates.append(item)
00191 
00192         return duplicates

Here is the caller graph for this function:

def test_GAQueens.QueensRepair._get_unused (   self,
  genome 
) [private]
Return all of the letters in the genome which are unused.

This checks the letters in the genome (which are th rows on the
chessboard) and returns all items which are not used.

Definition at line 193 of file test_GAQueens.py.

00193 
00194     def _get_unused(self, genome):
00195         """Return all of the letters in the genome which are unused.
00196 
00197         This checks the letters in the genome (which are th rows on the
00198         chessboard) and returns all items which are not used.
00199         """
00200         unused = []
00201         for item in genome.alphabet.letters:
00202             if genome.count(str(item)) == 0:
00203                 unused.append(item)
00204 
00205         return unused

Here is the caller graph for this function:

def test_GAQueens.QueensRepair.repair (   self,
  organism 
)
Repair the specified genome to make it feasible.

Arguments:

o organism -- The Organism object we are going to perform the
repair on.

Definition at line 206 of file test_GAQueens.py.

00206 
00207     def repair(self, organism):
00208         """Repair the specified genome to make it feasible.
00209 
00210         Arguments:
00211 
00212         o organism -- The Organism object we are going to perform the
00213         repair on.
00214         """
00215         # check if we should repair or not
00216         repair_chance = random.random()
00217         if repair_chance <= self._repair_prob:
00218             while 1:
00219                 # get the duplicated items we need to work on
00220                 duplicated_items = self._get_duplicates(organism.genome)
00221 
00222                 if len(duplicated_items) == 0:
00223                     break
00224 
00225                 # take the first duplicated element, and convert it to
00226                 # a row that is not already taken
00227                 duplicated_pos = organism.genome.index(duplicated_items[0])
00228 
00229                 free_rows = self._get_unused(organism.genome)
00230                 assert len(free_rows) > 0, "Unexpected lack of empty rows"
00231 
00232                 new_item = random.choice(free_rows)
00233                 organism.genome[duplicated_pos] = new_item
00234 
00235         return organism
        

Here is the call graph for this function:


Member Data Documentation

Definition at line 177 of file test_GAQueens.py.


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