-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_alt.py
145 lines (106 loc) · 3.15 KB
/
main_alt.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'''
One of two general parameter estimation execution scripts (also see main.py).
For details, consult README.md as well as the command-line info given by
python main_alt.py -h
'''
from __future__ import division
import os
import os.path
import argparse
import numpy as np
import problems
import optimize
parser = argparse.ArgumentParser()
parser.add_argument(
'--seed', type = int, default = None
)
parser.add_argument(
'--n', type = int, default = 1 # does this feature still work? is it needed?
)
parser.add_argument(
'--force', action = 'store_true'
)
parser.add_argument(
'--naive', action = 'store_true'
)
parser.add_argument(
'--problem', type = str, default = 'all_scaled'
)
parser.add_argument(
'--outdir', type = str, default = None
)
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.'
else:
seed = args.seed + seed_offset
random_state = np.random.RandomState(seed)
print 'Using random seed {}.'.format(seed)
if args.outdir is None:
if args.seed is None:
outdir = None
else:
subdir = 'naive' if args.naive else 'standard'
outdir = os.path.join('out', 'history', subdir, 'seed-{}'.format(seed))
else:
outdir = os.path.join('out', args.outdir, 'seed-{}'.format(seed))
if outdir is None:
print 'Output will not be saved.'
else:
print 'Output will be saved to {}.'.format(os.path.abspath(outdir))
try:
os.makedirs(outdir)
except OSError:
assert os.path.exists(outdir)
if args.naive:
print 'Using naive perturbations'
else:
print 'Using parsimonious perturbations'
pars_path = os.path.join(outdir, 'pars.npy')
obj_path = os.path.join(outdir, 'obj.npy')
# hist_path = os.path.join(outdir, 'hist.npy')
# hist = []
# def collect(epoch, iteration, constraint_penalty_weight, obj):
# hist.append((
# epoch,
# iteration,
# constraint_penalty_weight,
# obj.mass_eq + obj.energy_eq + obj.flux,
# obj.fit,
# ))
# if outdir is None or args.force or not all(os.path.exists(p) for p in (pars_path, obj_path, hist_path)):
if outdir is None or args.force or not all(os.path.exists(p) for p in (pars_path, obj_path)):
# callback = collect if (outdir is not None) else optimize.empty_callback
callback = optimize.empty_callback
(pars, obj) = optimize.estimate_parameters(
rules_and_weights,
random_state = random_state,
naive = args.naive,
random_direction = False,
callback = callback
)
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
]))
# np.save(hist_path, np.array(hist, dtype = [
# ('epoch', np.int64),
# ('iter', np.int64),
# ('const_pen', np.float64),
# ('constraints', np.float64),
# ('fit', np.float64),
# ]))
print 'Saved to {}'.format(outdir)
else:
print 'Skipped - output already exists'