-
Notifications
You must be signed in to change notification settings - Fork 0
/
ga.py
51 lines (43 loc) · 1.46 KB
/
ga.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import argparse
import numpy as np
import problems
from population import Population
def main():
parser = argparse.ArgumentParser(description="Genetic algorithm")
""" required arguments """
required = parser.add_argument_group("required arguments")
required.add_argument(
"-problem",
help="name of the problem at the problems.py module",
required=True,
choices=["maze", "even_odd", "ackley", "queens"],
)
required.add_argument(
"-gen", type=int, help="n of generations", required=True
)
""" optional arguments """
parser.add_argument("-lb", type=int, help="lower bound", default=0)
parser.add_argument("-ub", type=int, help="upper bound", default=10)
parser.add_argument("-ctax", type=float, help="crossover tax", default=0.8)
parser.add_argument("-mtax", type=float, help="mutate tax", default=0.03)
parser.add_argument("-csize", type=int, help="chromosome size", default=10)
parser.add_argument(
"-tsize", type=int, help="tournment size (for tournment selection)"
)
parser.add_argument("-psize", type=int, help="population size", default=10)
args = parser.parse_args()
problem = problems.get_problem(args.problem)
pop = Population(
problem,
args.gen,
args.psize,
args.csize,
args.ctax,
args.mtax,
args.lb,
args.ub,
args.tsize,
)
pop.evolve()
if __name__ == "__main__":
main()