Spaces:
Running
Running
Commit
·
b6ed59b
1
Parent(s):
c1807a5
Added multiprocessing experiment
Browse files- pysr/Problems.py +37 -6
pysr/Problems.py
CHANGED
|
@@ -89,20 +89,51 @@ class FeynmanProblem(Problem):
|
|
| 89 |
return ret
|
| 90 |
|
| 91 |
|
| 92 |
-
def run_on_problem(problem, verbosity=0):
|
| 93 |
"""
|
| 94 |
Takes in a problem and returns a tuple: (equations, best predicted equation, actual equation)
|
| 95 |
"""
|
| 96 |
from time import time
|
| 97 |
starting = time()
|
| 98 |
-
equations = pysr(problem.X, problem.y, variable_names=problem.variable_names, verbosity=verbosity)
|
| 99 |
timing = time()-starting
|
| 100 |
-
others = {"
|
| 101 |
-
|
|
|
|
|
|
|
| 102 |
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
def do_feynman_experiments(first=100, verbosity=0, dp=500, output_file_path="experiments/FeynmanExperiment.csv", data_dir="datasets/FeynmanEquations.csv"):
|
| 105 |
from tqdm import tqdm
|
|
|
|
| 106 |
problems = FeynmanProblem.mk_problems(first=first, gen=True, dp=dp, data_dir=data_dir)
|
| 107 |
indx = range(len(problems))
|
| 108 |
ids = []
|
|
@@ -116,7 +147,7 @@ def do_feynman_experiments(first=100, verbosity=0, dp=500, output_file_path="exp
|
|
| 116 |
true_equations.append(true_equation)
|
| 117 |
time_takens.append(others['time'])
|
| 118 |
with open(output_file_path, 'a') as f:
|
| 119 |
-
writer = csv.writer(
|
| 120 |
writer.writerow(['ID', 'Predicted', 'True', 'Time'])
|
| 121 |
for i in range(len(ids)):
|
| 122 |
writer.writerow([ids[i], predictions[i], true_equations[i], time_takens[i]])
|
|
@@ -124,4 +155,4 @@ def do_feynman_experiments(first=100, verbosity=0, dp=500, output_file_path="exp
|
|
| 124 |
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|
| 127 |
-
|
|
|
|
| 89 |
return ret
|
| 90 |
|
| 91 |
|
| 92 |
+
def run_on_problem(problem, verbosity=0, multiprocessing=True):
|
| 93 |
"""
|
| 94 |
Takes in a problem and returns a tuple: (equations, best predicted equation, actual equation)
|
| 95 |
"""
|
| 96 |
from time import time
|
| 97 |
starting = time()
|
| 98 |
+
equations = pysr(problem.X, problem.y, variable_names=problem.variable_names, verbosity=verbosity,)
|
| 99 |
timing = time()-starting
|
| 100 |
+
others = {"time": timing, "problem": problem}
|
| 101 |
+
if not multiprocessing:
|
| 102 |
+
others['equations'] = equations
|
| 103 |
+
return str(best(equations)), problem.form, others
|
| 104 |
|
| 105 |
|
| 106 |
+
def do_feynman_experiments_parallel(first=100, verbosity=0, dp=500, output_file_path="experiments/FeynmanExperiment.csv", data_dir="datasets/FeynmanEquations.csv"):
|
| 107 |
+
import multiprocessing as mp
|
| 108 |
+
from tqdm import tqdm
|
| 109 |
+
problems = FeynmanProblem.mk_problems(first=first, gen=True, dp=dp, data_dir=data_dir)
|
| 110 |
+
ids = []
|
| 111 |
+
predictions = []
|
| 112 |
+
true_equations = []
|
| 113 |
+
time_takens = []
|
| 114 |
+
pool = mp.Pool()
|
| 115 |
+
results = []
|
| 116 |
+
with tqdm(total=len(problems)) as pbar:
|
| 117 |
+
for i, res in enumerate(pool.imap(run_on_problem, problems)):
|
| 118 |
+
results.append(res)
|
| 119 |
+
pbar.update()
|
| 120 |
+
for res in results:
|
| 121 |
+
prediction, true_equation, others = res
|
| 122 |
+
problem = others['problem']
|
| 123 |
+
ids.append(problem.eq_id)
|
| 124 |
+
predictions.append(prediction)
|
| 125 |
+
true_equations.append(true_equation)
|
| 126 |
+
time_takens.append(others['time'])
|
| 127 |
+
with open(output_file_path, 'a') as f:
|
| 128 |
+
writer = csv.writer(f, delimiter=',')
|
| 129 |
+
writer.writerow(['ID', 'Predicted', 'True', 'Time'])
|
| 130 |
+
for i in range(len(ids)):
|
| 131 |
+
writer.writerow([ids[i], predictions[i], true_equations[i], time_takens[i]])
|
| 132 |
+
return
|
| 133 |
+
|
| 134 |
def do_feynman_experiments(first=100, verbosity=0, dp=500, output_file_path="experiments/FeynmanExperiment.csv", data_dir="datasets/FeynmanEquations.csv"):
|
| 135 |
from tqdm import tqdm
|
| 136 |
+
|
| 137 |
problems = FeynmanProblem.mk_problems(first=first, gen=True, dp=dp, data_dir=data_dir)
|
| 138 |
indx = range(len(problems))
|
| 139 |
ids = []
|
|
|
|
| 147 |
true_equations.append(true_equation)
|
| 148 |
time_takens.append(others['time'])
|
| 149 |
with open(output_file_path, 'a') as f:
|
| 150 |
+
writer = csv.writer(f, delimiter=',')
|
| 151 |
writer.writerow(['ID', 'Predicted', 'True', 'Time'])
|
| 152 |
for i in range(len(ids)):
|
| 153 |
writer.writerow([ids[i], predictions[i], true_equations[i], time_takens[i]])
|
|
|
|
| 155 |
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
| 158 |
+
do_feynman_experiments_parallel(first=10)
|