Spaces:
Running
Running
Commit
·
271a473
1
Parent(s):
c88fbe0
Fixed silly bugs and added a run_on_problems function
Browse files- pysr/Problems.py +18 -6
pysr/Problems.py
CHANGED
|
@@ -1,16 +1,20 @@
|
|
| 1 |
import numpy as np
|
| 2 |
import csv
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
class Problem:
|
| 6 |
"""
|
| 7 |
Problem API to work with PySR.
|
| 8 |
|
|
|
|
|
|
|
| 9 |
Should be able to call pysr(problem.X, problem.y, var_names=problem.var_names) and have it work
|
| 10 |
"""
|
| 11 |
-
def __init__(self, X, y, variable_names=None):
|
| 12 |
self.X = X
|
| 13 |
self.y = y
|
|
|
|
| 14 |
self.variable_names = variable_names
|
| 15 |
|
| 16 |
|
|
@@ -25,9 +29,9 @@ class FeynmanProblem(Problem):
|
|
| 25 |
gen: If true the problem will have dp X and y values randomly generated else they will be None
|
| 26 |
"""
|
| 27 |
self.eq_id = row['Filename']
|
| 28 |
-
self.form = row['Formula']
|
| 29 |
self.n_vars = int(row['# variables'])
|
| 30 |
-
super(FeynmanProblem, self).__init__(None, None,
|
| 31 |
variable_names=[row[f'v{i + 1}_name'] for i in range(self.n_vars)])
|
| 32 |
#self.var_names = [row[f'v{i+1}_name'] for i in range(self.n_vars)]
|
| 33 |
self.low = [float(row[f'v{i+1}_low']) for i in range(self.n_vars)]
|
|
@@ -38,8 +42,8 @@ class FeynmanProblem(Problem):
|
|
| 38 |
if gen:
|
| 39 |
self.X = np.random.uniform(0.01, 25, size=(self.dp, self.n_vars))
|
| 40 |
d = {}
|
| 41 |
-
for var in range(len(self.
|
| 42 |
-
d[self.
|
| 43 |
d['exp'] = np.exp
|
| 44 |
d['sqrt'] = np.sqrt
|
| 45 |
d['pi'] = np.pi
|
|
@@ -78,13 +82,21 @@ class FeynmanProblem(Problem):
|
|
| 78 |
p = FeynmanProblem(row, gen=gen, dp=dp)
|
| 79 |
ret.append(p)
|
| 80 |
except Exception as e:
|
| 81 |
-
|
| 82 |
#print(row)
|
| 83 |
print(f"FAILED ON ROW {i}")
|
| 84 |
ind += 1
|
| 85 |
return ret
|
| 86 |
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
if __name__ == "__main__":
|
| 89 |
ret = FeynmanProblem.mk_problems(first=100, gen=True)
|
| 90 |
print(ret)
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import csv
|
| 3 |
+
import traceback
|
| 4 |
|
| 5 |
|
| 6 |
class Problem:
|
| 7 |
"""
|
| 8 |
Problem API to work with PySR.
|
| 9 |
|
| 10 |
+
Has attributes: X, y as pysr accepts, form which is a string representing the correct equation and variable_names
|
| 11 |
+
|
| 12 |
Should be able to call pysr(problem.X, problem.y, var_names=problem.var_names) and have it work
|
| 13 |
"""
|
| 14 |
+
def __init__(self, X, y, form=None, variable_names=None):
|
| 15 |
self.X = X
|
| 16 |
self.y = y
|
| 17 |
+
self.form = form
|
| 18 |
self.variable_names = variable_names
|
| 19 |
|
| 20 |
|
|
|
|
| 29 |
gen: If true the problem will have dp X and y values randomly generated else they will be None
|
| 30 |
"""
|
| 31 |
self.eq_id = row['Filename']
|
| 32 |
+
#self.form = row['Formula']
|
| 33 |
self.n_vars = int(row['# variables'])
|
| 34 |
+
super(FeynmanProblem, self).__init__(None, None, form=row['Formula'],
|
| 35 |
variable_names=[row[f'v{i + 1}_name'] for i in range(self.n_vars)])
|
| 36 |
#self.var_names = [row[f'v{i+1}_name'] for i in range(self.n_vars)]
|
| 37 |
self.low = [float(row[f'v{i+1}_low']) for i in range(self.n_vars)]
|
|
|
|
| 42 |
if gen:
|
| 43 |
self.X = np.random.uniform(0.01, 25, size=(self.dp, self.n_vars))
|
| 44 |
d = {}
|
| 45 |
+
for var in range(len(self.variable_names)):
|
| 46 |
+
d[self.variable_names[var]] = self.X[:, var]
|
| 47 |
d['exp'] = np.exp
|
| 48 |
d['sqrt'] = np.sqrt
|
| 49 |
d['pi'] = np.pi
|
|
|
|
| 82 |
p = FeynmanProblem(row, gen=gen, dp=dp)
|
| 83 |
ret.append(p)
|
| 84 |
except Exception as e:
|
| 85 |
+
traceback.print_exc()
|
| 86 |
#print(row)
|
| 87 |
print(f"FAILED ON ROW {i}")
|
| 88 |
ind += 1
|
| 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 . import pysr, best
|
| 97 |
+
equations = pysr(problem.X, problem.y, variable_names=problem.variable_names, verbosity=verbosity)
|
| 98 |
+
return equations, best(equations), problem.form
|
| 99 |
+
|
| 100 |
if __name__ == "__main__":
|
| 101 |
ret = FeynmanProblem.mk_problems(first=100, gen=True)
|
| 102 |
print(ret)
|