-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
98 lines (76 loc) · 2.88 KB
/
main.py
File metadata and controls
98 lines (76 loc) · 2.88 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import argparse
import pygame
import numpy as np
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
from display import Menu
from pop import Pop
from game import Pendulum
from model import Seq
def main():
args = get_args()
# User plays
if args.purpose == 'play':
p = Pendulum(sim=True)
p.play()
# NN plays
elif args.purpose == 'nn':
model = Seq()
p = Pendulum(model=model, sim=True)
ind = np.load(args.weights)
p.nn(train=False, ind=ind)
# Train a NN
elif args.purpose == 'train':
model = Seq()
p = Pendulum(model=model, sim=args.sim)
pop = Pop(popsize=args.pop_size,
n_traits=model.n_params,
ngen=args.ngen,
lr=args.lr,
elitesize=args.elite_size,
weight_domain=[-1,1],
seed_arr=args.seed_arr)
pop.evolve(fitness_fn=lambda ind: p.nn(train=True, ind=ind), sequential=True)
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--purpose',
default='play',
action='store',
choices=['play', 'nn', 'train'],
help='Purpose of this run.')
parser.add_argument('--weights',
default='demo/161833.npy',
action='store',
help='Numpy file holding serialized model weights from training.')
parser.add_argument('--sim',
default=False,
action='store',
help='Run with or without a graphical simulation.')
parser.add_argument('--pop_size',
default=100,
type=int,
action='store',
help='Number of individuals in the evolving population.')
parser.add_argument('--ngen',
default=10000,
type=int,
action='store',
help='Number of generations for evolving the population.')
parser.add_argument('--lr',
default=.1,
type=float,
action='store',
help='Learning rate or step size for evolutionary algorithm.')
parser.add_argument('--elite_size',
default=.2,
type=float,
action='store',
help='Percentage of individuals to be considered elite.')
parser.add_argument('--seed_arr',
default=None,
action='store',
help='Numpy file holding serialized model weights from training to seed the population.')
args = parser.parse_args()
return args
if __name__ == '__main__':
main()