-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimize_pathways_example.py
More file actions
85 lines (70 loc) · 4.39 KB
/
Copy pathoptimize_pathways_example.py
File metadata and controls
85 lines (70 loc) · 4.39 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
from optimize_pathways_library import upload_content, generate_initial_ratios, generate_next_ratios
from artificial_fitness import simulate_values
import numpy as np
from pathlib import Path
if __name__ == '__main__':
seed = None # fix for reproducibility, but note that the grid is generrated by Halton, so batches may converge to the same conditions if the seed is fixed it is recommended to set rand_if_same to True in this case)
debug_flag = False # set to True for printing some intermediate output in the command line
FILE_ARTIFICIAL_FITNESS = 'sym_data_artificial.txt' # name of the file with artificial fitness values to be used as simulated measurements
FILE_INSTRUCTIONS = 'example-2d.json' # file with parameter setups (see other 'example-Xd.json' files for X=1, 2, 4, or 5)
FILE_CONDITIONS = 'out_sorted.csv' # name of the file to save the conditions
FILE_INIT_MEASUREMENTS = 'out_measured_init.csv' # name of the file to save the initial conditions and measurements
N_INIT = 5 # number of conditions to generate initially
FILE_ITER_CONDITIONS = 'out_next.csv' # name of the file to save new conditions generated for the current iteration
FILE_ITER_MEASUREMENTS = 'out_measured_iter.csv' # name of the file to save conditions and measurements for current itreration
N_ITER = 5 # number of iterations
N_ITER_NEXT = 5 # number of conditions to generate per iteration
BATCH_METHOD = 'LP' # 'KB' = Kriging Believer; 'LP' = Local Penalizer , or 'EE' = Exploration/Exploitation methods for batch generation
BIAS_EXPLORE_EXPLOIT = 0 # value from -1 (primarily exploit) to 1 (primarily explore); 0 = default
rand_if_same = False # if set to True, will enforce sampling different conditions per batch by replacing repetitions with random
script_dir = Path(__file__).parent
# uploading all the input data
params, sim_groups, out_message = upload_content(FILE_INSTRUCTIONS)
col_names = [item['name'] for item in params]
if debug_flag:
print(out_message)
print(col_names)
print(sim_groups)
### EXAMPLE: generate initial set for measurement
output_sorted_file = FILE_CONDITIONS
n_out = N_INIT # number of conditions to generate initially
p_sorted = generate_initial_ratios(FILE_INSTRUCTIONS,
n_out, output_sorted_file,
debug_flag, seed,
sort_output=True)
if debug_flag:
print(p_sorted)
### filling in simulated values
file_in = FILE_CONDITIONS
file_out = FILE_INIT_MEASUREMENTS
file_train = FILE_ARTIFICIAL_FITNESS
y_new = simulate_values(file_in, file_out, file_train, col_names)
print("iteration %i, max = %d" % (0,np.max(y_new[:,0])))
### fitting a model and generating next set of point to test in a loop
input_file = FILE_INIT_MEASUREMENTS
iter_file = FILE_ITER_MEASUREMENTS
output_file = FILE_ITER_CONDITIONS
file_train = FILE_ARTIFICIAL_FITNESS
n_out = N_ITER_NEXT
n_iter = N_ITER
max_iter = np.max(y_new[:,0])
for i in range(n_iter):
p_next = generate_next_ratios(FILE_INSTRUCTIONS, input_file,
n_out, output_file,
use_stdvs = False,
debug_flag = debug_flag, seed = seed,
batch_method = BATCH_METHOD,
fixed_priority_param = False,
rand_if_same = rand_if_same,
explore_exploit_bias = BIAS_EXPLORE_EXPLOIT)
if debug_flag:
print(p_next)
y_new = simulate_values(output_file, output_file, file_train, col_names)
max_iter = max(max_iter, np.max(y_new[:,0]))
print("iteration %i, max = %d, iter max = %d" % (i+1,max_iter,np.max(y_new[:,0])))
data_old = np.loadtxt(input_file, skiprows=1)
data_iter = np.loadtxt(output_file, skiprows=1)
np.savetxt(iter_file, np.vstack((data_old,data_iter)),
delimiter='\t', header='\t'.join(col_names), comments='')
input_file = iter_file
print(data_old)