Skip to content

Commit

Permalink
Added docstrings to some examples
Browse files Browse the repository at this point in the history
--HG--
branch : dev
  • Loading branch information
fmder committed Nov 1, 2012
1 parent dc548c6 commit 03a92f9
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 5 deletions.
5 changes: 5 additions & 0 deletions examples/ga/ga_onemax.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
# You should have received a copy of the GNU Lesser General Public
# License along with DEAP. If not, see <http://www.gnu.org/licenses/>.

"""Complete example featuring a fully developped algorithm.
**Keywords:** bit string, minimization, complete algorithm
"""

import random

from deap import base
Expand Down
9 changes: 7 additions & 2 deletions examples/ga/ga_onemax_island.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
# You should have received a copy of the GNU Lesser General Public
# License along with DEAP. If not, see <http://www.gnu.org/licenses/>.

"""Example distributing multiple island populations on multiple processors.
**Keywords:** bit string, minimization, :mod:`multiprocessing`, multipopulation, pipe migrator
"""

import array
from collections import deque
from multiprocessing import Event, Pipe, Process
Expand Down Expand Up @@ -107,7 +112,7 @@ def main(procid, pipein, pipeout, sync, seed=None):
ind.fitness.values = toolbox.evaluate(ind)
stats.update(deme)
hof.update(deme)
logger.logGeneration(gen="0", deme=procid, evals=len(deme), stats=stats)
logger.logGeneration(gen=0, deme=procid, evals=len(deme), stats=stats)

for gen in range(1, NGEN):
deme = toolbox.select(deme, len(deme))
Expand All @@ -119,7 +124,7 @@ def main(procid, pipein, pipeout, sync, seed=None):

stats.update(deme)
hof.update(deme)
logger.logGeneration(gen="%d" % gen, deme=procid, evals=len(invalid_ind), stats=stats)
logger.logGeneration(gen=gen, deme=procid, evals=len(invalid_ind), stats=stats)

if gen % MIG_RATE == 0 and gen > 0:
toolbox.migrate(deme)
Expand Down
5 changes: 5 additions & 0 deletions examples/ga/ga_onemax_mp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
# You should have received a copy of the GNU Lesser General Public
# License along with DEAP. If not, see <http://www.gnu.org/licenses/>.

"""Example distributing evaluations on multiple processors.
**Keywords:** bit string, minimization, :mod:`multiprocessing`
"""

import array
import multiprocessing
import random
Expand Down
5 changes: 5 additions & 0 deletions examples/ga/ga_onemax_multidemic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
# You should have received a copy of the GNU Lesser General Public
# License along with DEAP. If not, see <http://www.gnu.org/licenses/>.

"""The One Max example resolved with multiple populations.
**Keywords:** bit string, minimization, multipopulation, migration
"""

import array
import random

Expand Down
6 changes: 6 additions & 0 deletions examples/ga/ga_onemax_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
# You should have received a copy of the GNU Lesser General Public
# License along with DEAP. If not, see <http://www.gnu.org/licenses/>.

"""The same One Max example similar to the short version but using
:class:`numpy.ndarray` as base class.
**Keywords:** bit string, minimization, :class:`numpy.ndarray`
"""

import numpy
import random

Expand Down
6 changes: 6 additions & 0 deletions examples/ga/ga_onemax_short.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
# You should have received a copy of the GNU Lesser General Public
# License along with DEAP. If not, see <http://www.gnu.org/licenses/>.

"""Example exposing the use of the :func:`~deap.algorithms.eaSimple` function.
It also uses :class:`array.array` as base class.
**Keywords:** Bit String, Minimization, :class:`array.array`
"""

import array
import random

Expand Down
26 changes: 23 additions & 3 deletions examples/ga/ga_tsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
# You should have received a copy of the GNU Lesser General Public
# License along with DEAP. If not, see <http://www.gnu.org/licenses/>.

"""Resolution of the TSP.
**Keywords:** permutation, minimization
"""

import array
import random
import json
Expand All @@ -24,9 +29,24 @@

# gr*.json contains the distance map in list of list style in JSON format
# Optimal solutions are : gr17 = 2085, gr24 = 1272, gr120 = 6942
tsp = json.load(open("tsp/gr17.json", "r"))
distance_map = tsp["DistanceMatrix"]
IND_SIZE = tsp["TourSize"]
try:
tsp = json.load(open("tsp/gr17.json", "r"))
except IOError:
import warnings
warnings.warn("No TSP file found.")
tsp = False

if tsp:
IND_SIZE = tsp["TourSize"]
distance_map = tsp["DistanceMatrix"]
else:
IND_SIZE = 25
distance_map = [[0]*IND_SIZE for _ in range(IND_SIZE)]
for i in range(IND_SIZE):
for j in range(IND_SIZE):
d = random.random()
distance_map[i][j] = d
distance_map[j][i] = d

creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", array.array, typecode='i', fitness=creator.FitnessMin)
Expand Down

0 comments on commit 03a92f9

Please sign in to comment.