-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
89 lines (63 loc) · 1.89 KB
/
main.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
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
'''
One of two general parameter estimation execution scripts (also see
main_alt.py). For details, consult README.md as well as the command-line info
given by
python main.py -h
'''
from __future__ import division
import os
import os.path
import argparse
import numpy as np
import problems
import optimize
# TODO: move this logic to __main__ in optimize.py?
parser = argparse.ArgumentParser()
parser.add_argument(
'--problem', type = str, default = 'all_scaled'
)
parser.add_argument(
'--seed', type = int, default = None
)
parser.add_argument(
'--n', type = int, default = 1
)
parser.add_argument(
'--force', type = bool, default = False
)
args = parser.parse_args()
try:
rules_and_weights = problems.DEFINITIONS[args.problem]
except KeyError:
raise Exception('Unknown problem "{}".'.format(args.problem))
print 'Using problem definition "{}".'.format(args.problem)
for seed_offset in xrange(args.n):
if args.seed is None:
random_state = np.random.RandomState(None)
print 'No seed provided; a random seed will be used.'
print 'Output will not be saved.'
outdir = None
else:
seed = args.seed + seed_offset
random_state = np.random.RandomState(seed)
print 'Using random seed {}.'.format(seed)
outdir = os.path.join('out', args.problem, 'seed-{}'.format(seed))
print 'Output will be saved to {}.'.format(os.path.abspath(outdir))
try:
os.makedirs(outdir)
except OSError:
assert os.path.exists(outdir)
pars_path = os.path.join(outdir, 'pars.npy')
obj_path = os.path.join(outdir, 'obj.npy')
if outdir is None or args.force or not os.path.exists(pars_path) or not os.path.exists(obj_path):
(pars, obj) = optimize.estimate_parameters(rules_and_weights, random_state)
if outdir is not None:
np.save(pars_path, pars)
np.save(obj_path, np.array([
obj.mass_eq,
obj.energy_eq,
obj.flux,
obj.fit
]))
else:
print 'Skipped - output already exists'