Giter Club home page Giter Club logo

physo's Introduction

$\Phi$-SO : Physical Symbolic Optimization

logo Physical symbolic optimization ( $\Phi$-SO ) - A symbolic optimization package built for physics.

GitHub Repo stars Documentation Status Coverage Status Twitter Follow Paper

Source code: WassimTenachi/PhySO
Documentation: physo.readthedocs.io

Highlights

$\Phi$-SO's symbolic regression module fully leverages physical units constraints in order to infer analytical physical laws from data points, searching in the space of functional forms. For more details see: [Tenachi et al 2023].

$\Phi$-SO recovering the equation for a damped harmonic oscillator:

demo_light.mp4

Performances on the standard Feynman benchmark from SRBench) comprising 120 expressions from the Feynman Lectures on Physics against popular SR packages.

$\Phi$-SO achieves state-of-the-art performance in the presence of noise (exceeding 0.1%) and shows robust performances even in the presence of substantial (10%) noise:

feynman_results

Installation

The package has been tested on:

  • Linux
  • OSX (ARM & Intel)
  • Windows

Install procedure

Virtual environment

To install the package it is recommended to first create a conda virtual environment:

conda create -n PhySO python=3.8

And activate it:

conda activate PhySO

Downloading

physo can be downloaded using git:

git clone https://github.com/WassimTenachi/PhySO

Or by direct downloading a zip of the repository: here

Dependencies

From the repository root:

Installing essential dependencies :

conda install --file requirements.txt

Installing optional dependencies (for advanced debugging in tree representation) :

conda install --file requirements_display1.txt
pip install -r requirements_display2.txt

In addition, latex should be installed on the system.


NOTE : physo supports CUDA but it should be noted that since the bottleneck of the code is free constant optimization, using CUDA (even on a very high-end GPU) does not improve performances over a CPU and can actually hinder performances.


Installing PhySO

Installing physo (from the repository root):

pip install -e .

Testing install

Import test:

python3
>>> import physo

This should result in physo being successfully imported.

Unit tests:

Running all unit tests except parallel mode ones (from the repository root):

python -m unittest discover -p "*UnitTest.py"

This should result in all tests being successfully passed (except for program_display_UnitTest tests if optional dependencies were not installed).

Running all unit tests (from the repository root):

python -m unittest discover -p "*Test.py"

This should take 5-15 min depending on your system (as if you have a lot of CPU cores, it will take longer to make the efficiency curves).

Uninstalling

Uninstalling the package.

conda deactivate
conda env remove -n PhySO

Getting Started

A simple symbolic regression task

Symbolic regression (SR) consists in the inference of a free-form symbolic analytical function $f: \mathbb{R}^n \longrightarrow \mathbb{R}$ that fits $y = f(x_0,..., x_n)$ given $(x_0,..., x_n, y)$ data.

Given a dataset $(x_0,..., x_n, y)$:

import numpy as np

z = np.random.uniform(-10, 10, 50)
v = np.random.uniform(-10, 10, 50)
X = np.stack((z, v), axis=0)
y = 1.234*9.807*z + 1.234*v**2

Where $X=(z,v)$, $z$ being a length of dimension $L^{1}, T^{0}, M^{0}$, v a velocity of dimension $L^{1}, T^{-1}, M^{0}$, $y=E$ if an energy of dimension $L^{2}, T^{-2}, M^{1}$.

Given the units input variables $(x_0,..., x_n)$ (here $(z, v)$ ), the root variable $y$ (here $E$) as well as free and fixed constants, you can run an SR task to recover $f$ via:

import physo

expression, logs = physo.SR(X, y,
                            X_units = [ [1, 0, 0] , [1, -1, 0] ],
                            y_units = [2, -2, 1],
                            fixed_consts       = [ 1.      ],
                            fixed_consts_units = [ [0,0,0] ],
                            free_consts_units  = [ [0, 0, 1] , [1, -2, 0] ],                          
)

(Allowing the use of a fixed constant $1$ of dimension $L^{0}, T^{0}, M^{0}$ (ie dimensionless) and free constants $m$ of dimension $L^{0}, T^{0}, M^{1}$ and $g$ of dimension $L^{1}, T^{-2}, M^{0}$.)

It should be noted that here the units vector are of size 3 (eg: [1, 0, 0]) as in this example the variables have units dependent on length, time and mass only. However, units vectors can be of any size $\leq 7$ as long as it is consistent across X, y and constants, allowing the user to express any units (dependent on length, time, mass, temperature, electric current, amount of light, or amount of matter). In addition, dimensional analysis can be performed regardless of the order in which units are given, allowing the user to use any convention ([length, mass, time] or [mass, time, length] etc.) as long as it is consistent across X,y and constants.


NOTE : If you are not working on a physics problem and all your variables/constants are dimensionless, do not specify any of the xx_units arguments (or specify them as [0,0] for all variables/constants) and physo will perform a dimensionless symbolic regression task.


It should also be noted that free constants search starts around 1. by default. Therefore when using default hyperparameters, normalizing the data around an order of magnitude of 1 is strongly recommended.

Finally, please note that SR capabilities of physo are heavily dependent on hyperparameters, it is therefore recommended to tune hyperparameters to your own specific problem for doing science. Summary of currently available hyperparameters configurations:

Configuration Notes
config0 Light config for demo purposes.
config1 Tuned on a few physical cases.
config2 [work in progress] Good starting point for doing science.

By default, config0 is used, however it is recommended to use the latest configuration currently available (config1) as a starting point for doing science by specifying it:

expression, logs = physo.SR(X, y,
                            X_units = [ [1, 0, 0] , [1, -1, 0] ],
                            y_units = [2, -2, 1],
                            fixed_consts       = [ 1.      ],
                            fixed_consts_units = [ [0,0,0] ],
                            free_consts_units  = [ [0, 0, 1] , [1, -2, 0] ],      
                            run_config = physo.config.config1.config1                    
)

You can also specify the choosable symbolic operations for the construction of $f$ and give the names of variables for display purposes by filling in optional arguments:

expression, logs = physo.SR(X, y,
                            X_names = [ "z"       , "v"        ],
                            X_units = [ [1, 0, 0] , [1, -1, 0] ],
                            y_name  = "E",
                            y_units = [2, -2, 1],
                            fixed_consts       = [ 1.      ],
                            fixed_consts_units = [ [0,0,0] ],
                            free_consts_names = [ "m"       , "g"        ],
                            free_consts_units = [ [0, 0, 1] , [1, -2, 0] ],
                            op_names = ["mul", "add", "sub", "div", "inv", "n2", "sqrt", "neg", "exp", "log", "sin", "cos"]
)

physo.SR saves monitoring curves, the pareto front (complexity vs accuracy optimums) and the logs. It also returns the best fitting expression found during the search which can be inspected in regular infix notation (eg. in ascii or latex) via:

>>> print(expression.get_infix_pretty(do_simplify=True))
  ⎛       2⎞
m⋅⎝g⋅z + v ⎠
>>> print(expression.get_infix_latex(do_simplify=True))
'm \\left(g z + v^{2}\\right)'

Free constants can be inspected via:

>>> print(expression.free_const_values.cpu().detach().numpy())
array([9.80699996, 1.234     ])

physo.SR also returns the log of the run from which one can inspect Pareto front expressions:


for i, prog in enumerate(pareto_front_expressions):
    # Showing expression
    print(prog.get_infix_pretty(do_simplify=True))
    # Showing free constant
    free_consts = prog.free_const_values.detach().cpu().numpy()
    for j in range (len(free_consts)):
        print("%s = %f"%(prog.library.free_const_names[j], free_consts[j]))
    # Showing RMSE
    print("RMSE = {:e}".format(pareto_front_rmse[i]))
    print("-------------")

Returning:

   2
m⋅v
g = 1.000000
m = 1.486251
RMSE = 6.510109e+01
-------------
g⋅m⋅z
g = 3.741130
m = 3.741130
RMSE = 5.696636e+01
-------------
  ⎛       2⎞
m⋅⎝g⋅z + v ⎠
g = 9.807000
m = 1.234000
RMSE = 1.675142e-07
-------------

In addition, physo.SR can create log files which can be inspected later on. Pareto front expressions and their constants can be loaded into a list of sympy expressions via:

sympy_expressions = physo.read_pareto_csv("SR_curves_pareto.csv")

This demo can be found in here.

Citing this work

@ARTICLE{2023arXiv230303192T,
       author = {{Tenachi}, Wassim and {Ibata}, Rodrigo and {Diakogiannis}, Foivos I.},
        title = "{Deep symbolic regression for physics guided by units constraints: toward the automated discovery of physical laws}",
      journal = {arXiv e-prints},
     keywords = {Astrophysics - Instrumentation and Methods for Astrophysics, Computer Science - Machine Learning, Physics - Computational Physics},
         year = 2023,
        month = mar,
          eid = {arXiv:2303.03192},
        pages = {arXiv:2303.03192},
          doi = {10.48550/arXiv.2303.03192},
archivePrefix = {arXiv},
       eprint = {2303.03192},
 primaryClass = {astro-ph.IM},
       adsurl = {https://ui.adsabs.harvard.edu/abs/2023arXiv230303192T},
      adsnote = {Provided by the SAO/NASA Astrophysics Data System}
}

physo's People

Contributors

abekipnis avatar felixonmars avatar m-colley avatar reichherzerp avatar wassimtenachi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

physo's Issues

Visualization plots recursion depth resource limits?

After running for a few days (the long runtime due to PyTorch not working with multicore) I encountered this error:

Unable to make visualisation plots.
=========== Epoch 05563 ===========
-> Time 34.42 s

Overall best  at R=0.999970
-> Raw expression : 
   1                  
 ──────               
 ⎛ 1  ⎞               
 ⎜────⎟               
 ⎝α₀⋅t⎠               
ℯ      ⋅cos(f⋅t + 4⋅φ)

Best of epoch at R=0.867387
-> Raw expression : 
          ⎛                    1 ⎞
          ⎜                   ───⎟
          ⎜                   f⋅t⎟
log(φ)⋅sin⎝-φ + -f⋅t - 3⋅φ - ℯ   ⎠
──────────────────────────────────Unable to make visualisation plots.
=========== Epoch 05563 ===========
-> Time 34.42 s

Overall best  at R=0.999970
-> Raw expression : 
   1                  
 ──────               
 ⎛ 1  ⎞               
 ⎜────⎟               
 ⎝α₀⋅t⎠               
ℯ      ⋅cos(f⋅t + 4⋅φ)

Best of epoch at R=0.867387
-> Raw expression : 
          ⎛                    1 ⎞
          ⎜                   ───⎟
          ⎜                   f⋅t⎟
log(φ)⋅sin⎝-φ + -f⋅t - 3⋅φ - ℯ   ⎠
──────────────────────────────────
               α₀⋅t               


Unable to save last plots and data before stopping.
Figure(4000x1800)
Traceback (most recent call last):
  File "/home/jabowery/anaconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/colorbar.py", line 247, in __call__
    pos = self._orig_locator(ax, renderer)
  File "/home/jabowery/anaconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/colorbar.py", line 247, in __call__
    pos = self._orig_locator(ax, renderer)
  File "/home/jabowery/anaconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/colorbar.py", line 247, in __call__
    pos = self._orig_locator(ax, renderer)
  [Previous line repeated 997 more times]
RecursionError: maximum recursion depth exceeded
               α₀⋅t               


Unable to save last plots and data before stopping.
Figure(4000x1800)
Traceback (most recent call last):
  File "/home/jabowery/anaconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/colorbar.py", line 247, in __call__
    pos = self._orig_locator(ax, renderer)
  File "/home/jabowery/anaconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/colorbar.py", line 247, in __call__
    pos = self._orig_locator(ax, renderer)
  File "/home/jabowery/anaconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/colorbar.py", line 247, in __call__
    pos = self._orig_locator(ax, renderer)
  [Previous line repeated 997 more times]
RecursionError: maximum recursion depth exceeded

About reducing the number of operators

Hi @WassimTenachi,
When I test the demo code, I tried to reduce the number of operators, for example:

expression, logs = physo.SR(X, y,
                            X_names = [ "z"       , "v"        ],
                            X_units = [ [1, 0, 0] , [1, -1, 0] ],
                            y_name  = "E",
                            y_units = [2, -2, 1],
                            free_consts_names = [ "m"       , "g"        ],
                            free_consts_units = [ [0, 0, 1] , [1, -2, 0] ],
                            op_names = ["mul", "add", "sub", "div"]
)

So now there will only be four operators: add, subtract, multiply and divide.
However, when I run the code above, it went wrong:

Parallel mode is not available because physo is being ran from a notebook on a system returning multiprocessing.get_start_method() = 'spawn' (typically MACs/Windows). Run physo from the terminal to use parallel mode.
Parallel mode is not available because physo is being ran from a notebook on a system returning multiprocessing.get_start_method() = 'spawn' (typically MACs/Windows). Run physo from the terminal to use parallel mode.

default get_start_method : spawn
Running from notebook : True
Is CUDA available : False
Total nb. of CPUs :  32
Recommended config {'parallel_mode': False, 'n_cpus': 32}
SR task started...
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[4], line 1
----> 1 expression2, logs2 = physo.SR(X, y,
      2                             X_names = [ "z"       , "v"        ],
      3                             X_units = [ [1, 0, 0] , [1, -1, 0] ],
      4                             y_name  = "E",
      5                             y_units = [2, -2, 1],
      6                             fixed_consts       = [ 1.      ],
      7                             fixed_consts_units = [ [0,0,0] ],
      8                             free_consts_names = [ "m"       , "g"        ],
      9                             free_consts_units = [ [0, 0, 1] , [1, -2, 0] ],
     10                             op_names = ["mul", "add", "sub", "div"]
     11 )

File ~\PhySO\physo\task\sr.py:279, in SR(X, y, X_units, X_names, y_units, y_name, fixed_consts, fixed_consts_units, free_consts_units, free_consts_names, op_names, use_protected_ops, stop_reward, max_n_evaluations, epochs, run_config, get_run_logger, get_run_visualiser, parallel_mode, n_cpus)
    276 # ------------------------------- RUN -------------------------------
    278 print("SR task started...")
--> 279 rewards, candidates = fit (X, y, run_config,
    280                             stop_reward         = stop_reward,
    281                             stop_after_n_epochs = default_stop_after_n_epochs,
    282                             max_n_evaluations   = max_n_evaluations,
    283                            )
    285 # ------------------------------- RESULTS -------------------------------
    287 pareto_front_complexities, pareto_front_programs, pareto_front_r, pareto_front_rmse = run_logger.get_pareto_front()

File ~\PhySO\physo\task\fit.py:58, in fit(X, y, run_config, candidate_wrapper, stop_reward, stop_after_n_epochs, max_n_evaluations)
     45 def batch_reseter():
     46     return  Batch.Batch (library_args          = run_config["library_config"],
     47                          priors_config         = run_config["priors_config"],
     48                          batch_size            = run_config["learning_config"]["batch_size"],
   (...)
     55                          observe_units     = run_config["learning_config"]["observe_units"],
     56                          )
---> 58 batch = batch_reseter()
     60 def cell_reseter ():
     61     input_size  = batch.obs_size

File ~\PhySO\physo\task\fit.py:46, in fit.<locals>.batch_reseter()
     45 def batch_reseter():
---> 46     return  Batch.Batch (library_args          = run_config["library_config"],
     47                          priors_config         = run_config["priors_config"],
     48                          batch_size            = run_config["learning_config"]["batch_size"],
     49                          max_time_step         = run_config["learning_config"]["max_time_step"],
     50                          rewards_computer      = run_config["learning_config"]["rewards_computer"],
     51                          free_const_opti_args  = run_config["free_const_opti_args"],
     52                          X        = X,
     53                          y_target = y,
     54                          candidate_wrapper = candidate_wrapper,
     55                          observe_units     = run_config["learning_config"]["observe_units"],
     56                          )

File ~\PhySO\physo\physym\batch.py:89, in Batch.__init__(self, library_args, priors_config, X, y_target, rewards_computer, batch_size, max_time_step, free_const_opti_args, candidate_wrapper, observe_units)
     84 self.programs = program.VectPrograms(batch_size        = self.batch_size,
     85                                      max_time_step     = self.max_time_step,
     86                                      library           = self.library,
     87                                      candidate_wrapper = candidate_wrapper)
     88 # Prior
---> 89 self.prior   = prior.make_PriorCollection(programs      = self.programs,
     90                                           library       = self.library,
     91                                           priors_config = priors_config,)
     92 # Dataset
     93 self.dataset = dataset.Dataset(
     94     library = self.library,
     95     X = X,
     96     y_target = y_target,)

File ~\PhySO\physo\physym\prior.py:804, in make_PriorCollection(library, programs, priors_config)
    802         prior_args = {}
    803     # Appending individual prior
--> 804     prior = PRIORS_DICT[name](library = library, programs = programs, **prior_args)
    805     priors.append (prior)
    806 # Setting priors in PriorCollection

File ~\PhySO\physo\physym\prior.py:456, in NestedFunctions.__init__(self, library, programs, functions, max_nesting)
    453 assert len(functions.shape) == 1 and functions.dtype.char == "U", err_msg
    454 err_msg = "Some tokens given in argument functions: %s are not in the library of tokens: %s" \
    455           % (functions, library.lib_name)
--> 456 assert np.isin(functions, library.lib_name).all(), err_msg
    458 # max_nesting argument ---
    459 err_msg = "Argument max_nesting should be an int >= 1."

AssertionError: Some tokens given in argument functions: ['exp'] are not in the library of tokens: ['mul' 'add' 'sub' 'div' '1.0' 'g' 'm' 'z' 'v' 'E' 'dummy' '-']

Do I have to add the exp operator? I don't want to add too many operators when running my own SR problems so could you please give me some advice?

Pistil

Requirements.txt change pytorch to torch

Hey, I think in the requirements.txt you could change

pytorch >= 1.11.0

to

torch >= 1.11.0.

I got an installation error and also usually see that torch is used here.

Error:

ERROR: Could not find a version that satisfies the requirement pytorch>=1.11.0 (from versions: 0.1.2, 1.0.2)
ERROR: No matching distribution found for pytorch>=1.11.0

Find out if Code is Run in Notebook

Hey,

you have defined

def IsNotebook():
    try:
        shell = get_ipython().__class__.__name__
        if shell == 'ZMQInteractiveShell':
            return True   # Jupyter notebook or qtconsole
        elif shell == 'TerminalInteractiveShell':
            return False  # Terminal running IPython
        else:
            return False  # Other type (?)
    except NameError:
        return False      # Probably standard Python interpreter

However, when running PhySO in Google Colab, this returns false.

You could enhance the method such as:

def IsNotebook():
    try:
        if 'google.colab' in str(get_ipython()):
            return True   # Google Colab
        shell = get_ipython().__class__.__name__
        if shell == 'ZMQInteractiveShell':
            return True   # Jupyter notebook or qtconsole
        elif shell == 'TerminalInteractiveShell':
            return False  # Terminal running IPython
        else:
            return False  # Other type (?)
    except NameError:
        return False      # Probably standard Python interpreter

With this being new:

if 'google.colab' in str(get_ipython()):
            return True   # Google Colab

code

monitoring.py class:RunVisualiser,in def update_plot (self,):

for x_dim in batch.dataset.X:
            x_dim_min = x.min().detach().cpu().numpy()
            x_dim_max = x.max().detach().cpu().numpy()
            x_dim_plot = torch.tensor(np.linspace(x_dim_min-x_expand, x_dim_max+x_expand, n_plot))
            stack.append(x_dim_plot)
        X_plot = torch.stack(stack).to(batch.dataset.detected_device)
        x_plot = X_plot[cut_on_dim]

x.min ??? why? x_dim.min()???

FAIL: test_infix_repr

Hello Wassim, amazing work you are doing here!

I've followed the installation instructions of the README, and I'm getting the following failed test when running python -m unittest discover -p "*UnitTest.py" :

FAIL: test_infix_repr (physo.physym.tests.program_display_UnitTest.DisplayTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/user/PhySO/physo/physym/tests/program_display_UnitTest.py", line 77, in test_infix_repr
    self.assertTrue(works_bool)
AssertionError: False is not true

----------------------------------------------------------------------
Ran 70 tests in 27.472s

FAILED (failures=1)

Any idea how I can fix this?

Running on Pop OS 22.04

Cheers!

Very slow on cuda

Hi Wassim,

I am trying to use the PhySO on the Colab. But I found it is very slow when I run the Demo on the gpu cuda, which is even slower than the cpu (~60s/epoch on cpu vs. ~170s/epoch on gpu). Did you test the gpu version? Is it also slow?

Thank you,

Song

pytorch 1.11.0 needed - not on anaconda

Tried installing the file requirements.txt after having installed pytorch, but it needs an older version (1.11.0) that is no longer on anaconda - any tips on how to solve this?
Thanks!

Parallel mode failed

Hi there, thanks for this amazing algorithm!
I tried to run parallel mode on cluster. Although there was no error apparedin the codes, I found that the program was always waiting for the response, resulting in wall time up to the time limit and cpu time to only 1 sec. However, everything goes well if i turn off parallel mode both with cpu and gpu. How can I get over this issue? I really need to use parallel mode to save the running time.

RuntimeError: Failed to process string with tex because latex could not be found

Unable to make visualisation plots.
Unable to save pareto figure.
“Unable to save last plots and data before stopping.”Does this mean that the next round of learning cannot be started!

Because I am training on the server, I want to not show the pictures of each step, but save the pictures. I am more concerned about the final result, but it seems that without visualization, training cannot be performed. is that so?

Fallow my result and warning:
UserWarning: Can not import display packages. warnings.warn("Can not import display packages.")
-> Raw expression :
2
m
────────────
1
m⋅────────⋅1
2
g⋅z + v

Best of epoch at R=0.655170
-> Raw expression :
⎛ 1 v⎞
m⋅v⋅v⋅cos⎜───── + ─⎟
⎜⎛v⋅v⎞ v⎟
⎜⎜───⎟ ⎟
⎝⎝g⋅z⎠ ⎠

Unable to make visualisation plots.
Unable to save pareto figure.
=========== Epoch 00006 ===========
-> Time 2.59 s

Overall best at R=1.000000
-> Raw expression :
2
m
────────────
1
m⋅────────⋅1
2
g⋅z + v

Best of epoch at R=0.606942
-> Raw expression :
m
───────
v
1⋅─────
g⋅v⋅z

Unable to save last plots and data before stopping.

How to know if the model has converged?

Hello! Thank you very much for the excellent work! I would like to know how to control the number of training epochs, how to train on GPU, and how to know if the model has converged?

latex could not be found

Hi Wassim,
I am trying to use the demo_mechanical_energy
but it run error.
the error message is below
Traceback (most recent call last):
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/texmanager.py", line 233, in _run_checked_subprocess
report = subprocess.check_output(
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/subprocess.py", line 415, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/subprocess.py", line 493, in run
with Popen(*popenargs, **kwargs) as process:
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/subprocess.py", line 858, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/subprocess.py", line 1704, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] 没有那个文件或目录: 'latex'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/backends/backend_qt.py", line 455, in _draw_idle
self.draw()
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py", line 436, in draw
self.figure.draw(self.renderer)
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/artist.py", line 73, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
return draw(artist, renderer)
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/figure.py", line 2810, in draw
mimage._draw_list_compositing_images(
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
return draw(artist, renderer)
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 3082, in draw
mimage._draw_list_compositing_images(
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
return draw(artist, renderer)
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/axis.py", line 1159, in draw
ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/axis.py", line 1085, in _get_tick_bboxes
return ([tick.label1.get_window_extent(renderer)
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/axis.py", line 1085, in
return ([tick.label1.get_window_extent(renderer)
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/text.py", line 910, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/text.py", line 309, in _get_layout
_, lp_h, lp_d = renderer.get_text_width_height_descent(
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py", line 259, in get_text_width_height_descent
w, h, d = texmanager.get_text_width_height_descent(
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/texmanager.py", line 335, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/texmanager.py", line 271, in make_dvi
self._run_checked_subprocess(
File "/home/dz/miniconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/texmanager.py", line 237, in _run_checked_subprocess
raise RuntimeError(
RuntimeError: Failed to process string with tex because latex could not be found

Thank you.
dz

How can I add custom operator?

Hi Wassim,
Thanks for open source for this amazing tool. I found that physo.physym.functions.py contains a series of pre-defined operators for the regression process. I'd like to ask if user can introduce custom operations, such as 'erf' functions, to enrich the fitting capabilities.

how to change the batch.programs

Hello,

Sorry to bother you with my issue, but I don't think I've seen anyone else report the same problem as mine.
I'm still very much a neophyte when it comes to python and, by extension, anaconda.
So, I hope that perhaps my problem is actually trivial.

For example, I want to fit a function with four variables('x','m','L','MT') , but the program :

batch.programs.get_programs_array()[4]: [sqrt mul L c_x]

It is useless in my programs, how can I delete it and only keep the functions with 4 variables?

thanks

RuntimeError: Failed to process string with tex because latex could not be found

When I tested your demo, the following error occurred.
what should I do? Thank u so much!! :)


/Users/qixin/miniconda3/envs/pyhso/bin/python /Users/qixin/Desktop/PhySO-main/demo/demo_damped_harmonic_oscillator/demo_damped_harmonic_oscillator.py
cpu
Traceback (most recent call last):
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/texmanager.py", line 233, in _run_checked_subprocess
report = subprocess.check_output(
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/subprocess.py", line 415, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/subprocess.py", line 493, in run
with Popen(*popenargs, **kwargs) as process:
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/subprocess.py", line 858, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/subprocess.py", line 1704, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'latex'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/backends/backend_macosx.py", line 41, in _draw
self.figure.draw(renderer)
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/artist.py", line 73, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
return draw(artist, renderer)
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/figure.py", line 2810, in draw
mimage._draw_list_compositing_images(
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
return draw(artist, renderer)
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 3082, in draw
mimage._draw_list_compositing_images(
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
return draw(artist, renderer)
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/axis.py", line 1159, in draw
ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/axis.py", line 1085, in _get_tick_bboxes
return ([tick.label1.get_window_extent(renderer)
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/axis.py", line 1085, in
return ([tick.label1.get_window_extent(renderer)
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/text.py", line 910, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/text.py", line 309, in _get_layout
_, lp_h, lp_d = renderer.get_text_width_height_descent(
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py", line 259, in get_text_width_height_descent
w, h, d = texmanager.get_text_width_height_descent(
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/texmanager.py", line 335, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/texmanager.py", line 271, in make_dvi
self._run_checked_subprocess(
File "/Users/qixin/miniconda3/envs/pyhso/lib/python3.8/site-packages/matplotlib/texmanager.py", line 237, in _run_checked_subprocess
raise RuntimeError(
RuntimeError: Failed to process string with tex because latex could not be found
/Users/qixin/Desktop/PhySO-main/physo/physym/reward.py:118: UserWarning: Unable to optimize.py free constants of prog 115 -> r = 0
warnings.warn("Unable to optimize.py free constants of prog %i -> r = 0"%(i))
Figure(2800x1400)
Unable to make visualisation plots.
Unable to save pareto figure.

Only works if setting `physo.physym.reward.USE_PARALLEL_OPTI_CONST = False`

When not setting physo.physym.reward.USE_PARALLEL_OPTI_CONST = False in the program, it is starting 10 additional processes:

(PhySO) joel@My-MacBook-Pro Flimitless % python physo_test.py
SR task started...
SR task started...
SR task started...
SR task started...
SR task started...
SR task started...
SR task started...
SR task started...
SR task started...
SR task started...
SR task started...
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/multiprocessing/spawn.py", line 125, in _main
    prepare(preparation_data)
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/multiprocessing/spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/multiprocessing/spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/runpy.py", line 265, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/Users/joel/src/Flimitless/physo_test.py", line 21, in <module>
    expression, logs = physo.SR(X, y,
  File "/Users/joel/src/PhySO/physo/task/sr.py", line 279, in SR
    rewards, candidates = fit (X, y, run_config,
  File "/Users/joel/src/PhySO/physo/task/fit.py", line 74, in fit
    hall_of_fame_R, hall_of_fame = learn.learner (
  File "/Users/joel/src/PhySO/physo/learn/learn.py", line 165, in learner
    R = batch.get_rewards()
  File "/Users/joel/src/PhySO/physo/physym/batch.py", line 428, in get_rewards
    rewards = self.rewards_computer(programs             = self.programs,
  File "/Users/joel/src/PhySO/physo/physym/reward.py", line 234, in rewards_computer
    R = RewardsComputer(programs = programs,
  File "/Users/joel/src/PhySO/physo/physym/reward.py", line 153, in RewardsComputer
    programs.batch_optimize_constants(X        = X,
  File "/Users/joel/src/PhySO/physo/physym/program.py", line 2266, in batch_optimize_constants
    Exec.BatchFreeConstOpti(progs                = self,
  File "/Users/joel/src/PhySO/physo/physym/execute.py", line 532, in BatchFreeConstOpti
    pool = mp.Pool(processes=n_cpus)
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/multiprocessing/context.py", line 119, in Pool
    return Pool(processes, initializer, initargs, maxtasksperchild,
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/multiprocessing/pool.py", line 212, in __init__
    self._repopulate_pool()
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/multiprocessing/pool.py", line 303, in _repopulate_pool
    return self._repopulate_pool_static(self._ctx, self.Process,
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/multiprocessing/pool.py", line 326, in _repopulate_pool_static
    w.start()
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/multiprocessing/context.py", line 284, in _Popen
    return Popen(process_obj)
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 42, in _launch
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/multiprocessing/spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "/Users/joel/miniconda3/envs/PhySO/lib/python3.8/multiprocessing/spawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

Here is my program:

import numpy as np
import physo

#physo.physym.reward.USE_PARALLEL_EXE = False
physo.physym.reward.USE_PARALLEL_OPTI_CONST = False

# Constants
g = 9.80665  # Acceleration due to gravity in m/s^2

# Generating data

L = np.random.uniform(0.1, 100, 100)
T = 2 * np.pi * np.sqrt(L / g)      # Calculating periods

# Preparing data for PhySO
X = np.stack((L,), axis=0)

y = T

# Corrected run of symbolic regression with the right dimensions for g
expression, logs = physo.SR(X, y,
                            X_units=[ [1, 0, 0] ],  # Length of the pendulum
                            y_units=[0, 1, 0],      # Period of the pendulum
                            fixed_consts=[1.],      # Dimensionless constant
                            fixed_consts_units=[[0, 0, 0]],
                            free_consts_units=[[1, -2, 0]],  # Correct units for g
                            run_config=physo.config.config1.config1)

# Print the resulting expression
print("Derived Expression:")
print(expression.get_infix_pretty(do_simplify=True))
print("")
print("Free constants:")
print(expression.free_const_values.cpu().detach().numpy())

pareto_front_complexities, pareto_front_programs, pareto_front_r, pareto_front_rmse = logs.get_pareto_front()

for i, prog in enumerate(pareto_front_programs):
    # Showing expression
    print(prog.get_infix_pretty(do_simplify=True))
    # Showing free constant
    free_consts = prog.free_const_values.detach().cpu().numpy()
    for j in range (len(free_consts)):
        print("%s = %f"%(prog.library.free_const_names[j], free_consts[j]))
    # Showing RMSE
    print("RMSE = {:e}".format(pareto_front_rmse[i]))
    print("-------------")

My hardware is a MacBook Pro M1 Max:

% uname -a
Darwin My-MacBook-Pro.local 22.6.0 Darwin Kernel Version 22.6.0: Thu Nov 2 07:43:57 PDT 2023; root:xnu-8796.141.3.701.17~6/RELEASE_ARM64_T6000 arm64

I've installed it using miniconda.

Issue with package_data parameter in setup.py causing installation error

I've encountered an issue during installation where the package_data parameter in the setup.py file is causing an error. It appears that the description in the dictionary should not be an absolute path but should be a list of strings. When I modified it to an arbitrary list of strings, the installation proceeded without any errors.

This is the relevant part of the setup.py file that needs to be addressed:

# Before modification
   package_data  = {
"physo": ["/benchmark/FeynmanDataset/*.csv"]
}

# After modification
package_data={
    'my_module': ['111', '222'],
}

Please consider making this adjustment to ensure smooth installations. Thank you!

Custom equations

Hi Wassim, I tried to play your demo by directly changing the mechanical-energy equation in the data points codes:
y_array = mgz + mvz**2 into:
y_array = m
gz * z/(z+1)+ mvz**2
I assume the newly added switch function z/(z+1) has no units.

The original equation can be successfully inferred after nine epochs.
However, for the revised equation, it turned out the PhySO could not get the correct answer even after 4 hours of running.

Should I change other parts of the code if I do the above equation revise?

Thank you!

Question about necessary X.shape

Hey,

in sr.py, there is the following assertion assert len(X.shape) == 2, "X must have shape = (n_dim, data_size,)".

Why must X be in this shape?

Wouldn't it also work with 1-dimensional x and 1-dimensional y data?

Kind regards

Minor changes?

Thank you for making this availlable.

I got complaints about requirements.txt suggesting that pytorch there should be torch instead?

At the "Getting started" section, it might be helpful to add import numpy as np statement.

Error running program_display_UnitTest.py because of missing dependencies

Hi,

I encountered an error when running the unit tests that I was able to solve installing further packages. The error was the following:

FAIL: test_tree_rpr (physo.physym.tests.program_display_UnitTest.DisplayTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ammunoz1/Desktop/Ficheros/PhySO/PhySO/physo/physym/tests/program_display_UnitTest.py", line 160, in test_tree_rpr
    tree_latex = my_programs.get_tree_latex(prog_idx=0,)
  File "/home/ammunoz1/Desktop/Ficheros/PhySO/PhySO/physo/physym/program.py", line 2674, in get_tree_latex
    tree_latex = dot2tex.dot2tex(G.to_string(),
  File "/home/ammunoz1/.local/lib/python3.9/site-packages/dot2tex/__init__.py", line 62, in dot2tex
    return d2t.convert_graph(dotsource, **kwargs)
  File "/home/ammunoz1/.local/lib/python3.9/site-packages/dot2tex/dot2tex.py", line 3125, in convert_graph
    tex = main(True, dotsource, options)
  File "/home/ammunoz1/.local/lib/python3.9/site-packages/dot2tex/dot2tex.py", line 3075, in main
    s = conv.convert(dotdata)
  File "/home/ammunoz1/.local/lib/python3.9/site-packages/dot2tex/dot2tex.py", line 800, in convert
    return self.do_preview_preproc()
  File "/home/ammunoz1/.local/lib/python3.9/site-packages/dot2tex/dot2tex.py", line 1062, in do_preview_preproc
    sys.exit(1)
SystemExit: 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ammunoz1/Desktop/Ficheros/PhySO/PhySO/physo/physym/tests/program_display_UnitTest.py", line 164, in test_tree_rpr
    self.fail("Tree generation failed : get_tree_latex")
AssertionError: Tree generation failed : get_tree_latex

Upon checking dot2tex.py around line 1062, it included this error message:

Failed to preprocess the graph.
Is the preview LaTeX package installed? ((Debian package preview-latex-style)
To see what happened, run dot2tex with the --debug option.

I indeed did not have that package, and according to https://ctan.org/pkg/preview-latex it is provided with auctex. In Fedora 35 I installed emacs-auctex using dnf and after that I did not encounter the get_tree_latex error. However, dnf also installed other packages (emacs, emacs-common, liblockfile, libotf and tex-preview), so I am not sure which of them actually solved the problem. I thought this info could be useful to you for adding missing dependencies in the lists of requirements.

Why doesn't it work like the video?

I successfully ran the program unit test, but could not achieve the dynamic effect in the video.
How is the effect achieved in the video?

demo_light.mp4

image

Is there a python program that needs to be run?

How to fit a function I wanted

Hi Wassim

I have a model: log(k)= v*(x1c1log(t1)+(1-x1)c2log(t2) ) + (1-v)log(c0t0)

it’s doesn’t work now, so I wanna build a new function like f(x1,c1log(t1),c2log(t2)) * log(k) = v(x1c1log(t1)+(1-x1)c2log(t2) ) + (1-v)log(c0t0)

could I use physo to fit f(x1,c1*log(t1),c2log(t2)) And follow the formula above?

if its ok,how to set the training parameter0.0?

thanks

Parallel Execution Throws numerous RuntimeErrors

Hello there, I have installed the newest version of PhySO today and used conda as described here.

Without changing anything at the config or for using parallelization, the program runs into numerous:

anaconda3\envs\physo\Lib\multiprocessing\spawn.py", line 138, in _check_not_importing_main
raise RuntimeError('''
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.

    This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

        if __name__ == '__main__':
            freeze_support()
            ...

    The "freeze_support()" line can be omitted if the program
    is not going to be frozen to produce an executable.

I have now disabled this via

physo.physym.reward.USE_PARALLEL_EXE = False
physo.physym.reward.USE_PARALLEL_OPTI_CONST = False

I am on a Windows 11 host system with Python 3.11. Any idea why this could happen?

How to add new units?

Hello! Thank you very much for the excellent work! I am recently studying your code and plan to apply it to scientific problems, but I am now encountering a problem with the units:

Apart from the existing L, T, M units, how can I add new units (like temperature K) to make the program applicable to solve thermodynamic or aerodynamic problems? and how to represent it in the dict "library_config" ?

Error occured when import physo: "Can not import display packages"

`(physo) freeman@FreemanWorkStation:~/PhySO$ python3
Python 3.8.0 (default, Nov 6 2019, 21:49:08)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.

import physo
/home/freeman/PhySO/physo/physym/program.py:20: UserWarning: Can not import display packages.
warnings.warn("Can not import display packages.")

`
I used the latest version of pytorch 2.0 & cudatookit 11.7 , all depend packages were successfully installed, so how to fix this ? Thank you !

Double requirement of matplotlib

In the requirements.txt, there is matplotlib==3.5.1 and matplotlib

As the code is currently not working with matplotlib 3.7.0 and 3.7.1 due to a bug in that library, that you should be explicit in that file :)

Kind regards

trainning is wrong ,cant get the resuelt of model

Hi Wassim
`Unable to make visualisation plots.
Unable to save pareto data.
Unable to save pareto figure.
=========== Epoch 65122 ===========
-> Time 4.61 s

Overall best at R=0.000000
`
long time training,the overall best is abnormal!,what's wrong?
Can you take a look for me, thank you very much!

n3 expression

can I just add 'n3' into op_names, it looks like the formula is limited at n2 order. It can't give a result as y=x^3.

About input units

halo! WassimTenachi , I would like to know how to represent the identifiers and units of variables entered in the Library config module with numbers. Additionally, could you kindly advise me on how to determine the necessary constants and complexity based on my specific situation when using the module?
tks a lot~

Unit test failure

Hello,

Sorry to bother you with my issue, but I don't think I've seen anyone else report the same problem as mine.
I'm still very much a neophyte when it comes to python and, by extension, anaconda.
So, I hope that perhaps my problem is actually trivial.

I seem to have succeeded in installing PhySO because when I list the modules installed for python in the Anaconda prompt, physo appears in the list.
I was also able to install all the requirements.

However, when I run the unit test, I get a failure.

Also, despite installing MiKTeX and the necessary packages, I get question marks instead of mathematical symbols in the Anaconda Prompt.

My results are below.
I'd be very grateful if you could help me understand what's wrong.

Kind regards.

.....Dummy epoch time = 262.106800 ms
.Dummy epoch time (w duplicate elimination) = 207.696900 ms (found 17/1000 candidates with R > 0 and 1/1000 with R = 1 +/- 0.000010)
.Dummy epoch time (w free const) = 15307.137200 ms (found 30/1000 candidates with R = 1 +/- 0.000010)
.Dummy epoch time (w free const and duplicate elimination) = 11886.376700 ms (found 17/1000 candidates with R > 0 and 1/1000 with R = 1 +/- 0.000010)
.Dummy epoch time (w free const and duplicate elimination, keeping lowest complexity) = 11928.847900 ms (found 16/1000 candidates with R > 0 and 1/1000 with R = 1 +/- 0.000010)
....
Required units time (in ideal non-mixed cases) : 0.013004 ms / step / (prog in batch)
.
ComputeInfixNotation time = 0.024 ms
.
ExecuteProgram time = 10.363 ms

ExecuteProgram time (wo tokens) = 10.127 ms
.
ExecuteProgram time = 10.524 ms

ExecuteProgram time (wo tokens) = 10.074 ms
.LBFGS const opti: 5.459267 ms / step
.................D:\User\Program\PhySO\physo\physym\library.py:245: UserWarning: The units of token y were not provided (is_constraining_phy_units=False ; phy_units=[nan nan nan nan nan nan nan]), unable to compute units constraints.
  warnings.warn("The units of token %s were not provided (is_constraining_phy_units=%s ; phy_units=%s), "
D:\User\Program\PhySO\physo\physym\library.py:245: UserWarning: The units of token x1 were not provided (is_constraining_phy_units=False ; phy_units=[nan nan nan nan nan nan nan]), unable to compute units constraints.
  warnings.warn("The units of token %s were not provided (is_constraining_phy_units=%s ; phy_units=%s), "
D:\User\Program\PhySO\physo\physym\library.py:245: UserWarning: The units of token pi were not provided (is_constraining_phy_units=False ; phy_units=[nan nan nan nan nan nan nan]), unable to compute units constraints.
  warnings.warn("The units of token %s were not provided (is_constraining_phy_units=%s ; phy_units=%s), "
.......................get_infix_sympy time = 2.035 ms
get_infix_str time = 0.052 ms

get_infix_image time = 2.796 s
.Not testing tree representation features on Windows as this generally causes problems and is only useful for physo developers.
.
Reward_SquashedNRMSE time = 2.927 ms
................SR task started...
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1000/1000 [00:00<00:00, 1184.97it/s]
Unable to make visualisation plots.
Unable to save train curves data.
Unable to save pareto data.
Unable to save pareto figure.
=========== Epoch 00000 ===========
-> Time 3.20 s

Overall best  at R=0.549237
-> Raw expression :
      ⎛                ⎛⎛       1.0⎞    ⎞⎞
m⋅v⋅v⋅⎝-1.0 + 1.0 + log⎝⎝1.0 + ℯ   ⎠⋅1.0⎠⎠

Best of epoch at R=0.549237
-> Raw expression :
      ⎛                ⎛⎛       1.0⎞    ⎞⎞
m⋅v⋅v⋅⎝-1.0 + 1.0 + log⎝⎝1.0 + ℯ   ⎠⋅1.0⎠⎠


100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1000/1000 [00:01<00:00, 790.09it/s]
Unable to make visualisation plots.
Unable to save train curves data.
Unable to save pareto data.
Unable to save pareto figure.
=========== Epoch 00001 ===========
-> Time 5.33 s

Overall best  at R=0.549237
-> Raw expression :
      ⎛                ⎛⎛       1.0⎞    ⎞⎞
m⋅v⋅v⋅⎝-1.0 + 1.0 + log⎝⎝1.0 + ℯ   ⎠⋅1.0⎠⎠

Best of epoch at R=0.549237
-> Raw expression :
  1.0
───────
⎛ -1  ⎞
⎜─────⎟
⎜⎛  2⎞⎟
⎜⎜ v ⎟⎟
⎜⎜───⎟⎟
⎜⎜⎛1⎞⎟⎟
⎜⎜⎜─⎟⎟⎟
⎝⎝⎝m⎠⎠⎠


100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1000/1000 [00:00<00:00, 9144.91it/s]
Unable to make visualisation plots.
Unable to save train curves data.
Unable to save pareto data.
Unable to save pareto figure.
=========== Epoch 00002 ===========
-> Time 4.37 s

Overall best  at R=0.579454
-> Raw expression :
                2
  ⎛       ⎛ 1 ⎞⎞
  ⎜1.0⋅cos⎜───⎟⎟
  ⎜       ⎝1.0⎠⎟
z⋅⎜────────────⎟
  ⎜ ⎛  -1    ⎞ ⎟
  ⎜ ⎜────────⎟ ⎟
  ⎜ ⎜     0.5⎟ ⎟
  ⎝ ⎝(g⋅m)   ⎠ ⎠

Best of epoch at R=0.579454
-> Raw expression :
                2
  ⎛       ⎛ 1 ⎞⎞
  ⎜1.0⋅cos⎜───⎟⎟
  ⎜       ⎝1.0⎠⎟
z⋅⎜────────────⎟
  ⎜ ⎛  -1    ⎞ ⎟
  ⎜ ⎜────────⎟ ⎟
  ⎜ ⎜     0.5⎟ ⎟
  ⎝ ⎝(g⋅m)   ⎠ ⎠


100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1000/1000 [00:00<00:00, 4924.20it/s]
Unable to make visualisation plots.
Unable to save train curves data.
Unable to save pareto data.
Unable to save pareto figure.
=========== Epoch 00003 ===========
-> Time 4.41 s

Overall best  at R=1.000000
-> Raw expression :
  ⎛        2                    ⎞
m⋅⎝-g⋅z + v  - v⋅v⋅log(sin(1.0))⎠

Best of epoch at R=1.000000
-> Raw expression :
  ⎛        2                    ⎞
m⋅⎝-g⋅z + v  - v⋅v⋅log(sin(1.0))⎠


100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1000/1000 [00:00<00:00, 1560.50it/s]
Unable to make visualisation plots.
Unable to save train curves data.
Unable to save pareto data.
Unable to save pareto figure.
=========== Epoch 00004 ===========
-> Time 4.91 s

Overall best  at R=1.000000
-> Raw expression :
  ⎛        2                    ⎞
m⋅⎝-g⋅z + v  - v⋅v⋅log(sin(1.0))⎠

Best of epoch at R=0.579454
-> Raw expression :
          z
─────────────────────
                    2
⎛          0.5     ⎞
⎜     ⎛ 1 ⎞        ⎟
⎜     ⎜───⎟        ⎟
⎜     ⎝g⋅m⎠        ⎟
⎜──────────────────⎟
⎜⎛      1.0       ⎞⎟
⎜⎜────────────────⎟⎟
⎜⎜   ⎛         1 ⎞⎟⎟
⎜⎜   ⎜        ───⎟⎟⎟
⎜⎜   ⎜   2    1.0⎟⎟⎟
⎝⎝cos⎝1.0  + ℯ   ⎠⎠⎠


.D:\User\Program\PhySO\physo\task\sr.py:182: UserWarning: No information about fixed constants, not using any.
  warnings.warn("No information about fixed constants, not using any.")
D:\User\Program\PhySO\physo\task\sr.py:190: UserWarning: No units given for fixed constants, assuming dimensionless units.
  warnings.warn("No units given for fixed constants, assuming dimensionless units.")
SR task started...
D:\User\Program\PhySO\physo\physym\prior.py:808: UserWarning: And error while making prior NestedFunctions, this prior will be ignored. Error message:
Some tokens given in argument functions: ['exp'] are not in the library of tokens: ['mul' 'add' 'sub' 'div' 'g' 'm' 'z' 'v' 'E' 'dummy' '-']
  warnings.warn("And error while making prior %s, this prior will be ignored. Error message:\n%s"%(name,e))
D:\User\Program\PhySO\physo\physym\prior.py:808: UserWarning: And error while making prior NestedFunctions, this prior will be ignored. Error message:
Some tokens given in argument functions: ['log'] are not in the library of tokens: ['mul' 'add' 'sub' 'div' 'g' 'm' 'z' 'v' 'E' 'dummy' '-']
  warnings.warn("And error while making prior %s, this prior will be ignored. Error message:\n%s"%(name,e))
D:\User\Program\PhySO\physo\physym\prior.py:557: UserWarning: No trigonometric functions detected, no prior from NestedTrigonometryPrior (tokens = [], nesting forbidden)
  warnings.warn("No trigonometric functions detected, no prior from %s" % (self))
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1000/1000 [00:00<00:00, 2132.22it/s]
Unable to make visualisation plots.
Unable to save train curves data.
Unable to save pareto data.
Unable to save pareto figure.
=========== Epoch 00000 ===========
-> Time 2.66 s

Overall best  at R=0.579454
-> Raw expression :
m⋅(-g + g + g)⋅(-z + z + z)

Best of epoch at R=0.579454
-> Raw expression :
m⋅(-g + g + g)⋅(-z + z + z)


D:\User\Program\PhySO\physo\physym\prior.py:808: UserWarning: And error while making prior NestedFunctions, this prior will be ignored. Error message:
Some tokens given in argument functions: ['exp'] are not in the library of tokens: ['mul' 'add' 'sub' 'div' 'g' 'm' 'z' 'v' 'E' 'dummy' '-']
  warnings.warn("And error while making prior %s, this prior will be ignored. Error message:\n%s"%(name,e))
D:\User\Program\PhySO\physo\physym\prior.py:808: UserWarning: And error while making prior NestedFunctions, this prior will be ignored. Error message:
Some tokens given in argument functions: ['log'] are not in the library of tokens: ['mul' 'add' 'sub' 'div' 'g' 'm' 'z' 'v' 'E' 'dummy' '-']
  warnings.warn("And error while making prior %s, this prior will be ignored. Error message:\n%s"%(name,e))
D:\User\Program\PhySO\physo\physym\prior.py:557: UserWarning: No trigonometric functions detected, no prior from NestedTrigonometryPrior (tokens = [], nesting forbidden)
  warnings.warn("No trigonometric functions detected, no prior from %s" % (self))
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1000/1000 [00:00<00:00, 2205.75it/s]
Unable to make visualisation plots.
Unable to save train curves data.
Unable to save pareto data.
Unable to save pareto figure.
=========== Epoch 00001 ===========
-> Time 4.05 s

Overall best  at R=0.579454
-> Raw expression :
m⋅(-g + g + g)⋅(-z + z + z)

Best of epoch at R=0.579454
-> Raw expression :
     g⋅m⋅z
───────────────
  z   z   g + g
- ─ + ─ + ─────
  z   z     g


F
======================================================================
FAIL: test_prior_not_in_op_names (physo.task.tests.sr_UnitTest.Test_SR)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\User\Program\PhySO\physo\task\tests\sr_UnitTest.py", line 112, in test_prior_not_in_op_names
    assert pareto_front_r.max() > 0.9999, "Solution expression was not found."
AssertionError: Solution expression was not found.

----------------------------------------------------------------------
Ran 77 tests in 108.571s

FAILED (failures=1)

Support for Matrix or Tensor Symbolic Regression Tasks

Hi Wassim!

It seems that the current version only supports scalar dependent variables in the training set.
assert len(y_target.shape) == 1, "y_target must have shape = (data_size,)"

If there are any plans to support matrix or tensor variables as dependent variables (e.g. y_target.shape = (9600,3,3)) in symbolic regression tasks in future versions of the program?

Thank you so much!

Display of images is not dynamic

I'm having some problems running the code, after plt.show draws a graph, the code doesn't run, what can I do to make it draw dynamically? I wonder if I am the only one with that problem? (btw, My computer is a MacBook air with M1.)

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

when I try to run the following command:
python3.8 -m unittest discover -p "*UnitTest.py"

I get the error

======================================================================
ERROR: test_optimization_process (physo.physym.tests.free_const_UnitTest.FreeConstUtilsTest)

Traceback (most recent call last):
File "/home/cloudyu/git/PhySO/physo/physym/tests/free_const_UnitTest.py", line 153, in test_optimization_process
MSE_before_opti_0 = data_conversion_inv(torch.mean((prog0(X) - y_target0) ** 2))
File "/home/cloudyu/git/PhySO/physo/physym/program.py", line 275, in call
return self.execute(X=X)
File "/home/cloudyu/git/PhySO/physo/physym/program.py", line 245, in execute
y = self.candidate_wrapper(lambda X: self.execute_wo_wrapper(X), X)
File "/home/cloudyu/git/PhySO/physo/physym/program.py", line 675, in
candidate_wrapper = lambda func, X : func(X)
File "/home/cloudyu/git/PhySO/physo/physym/program.py", line 245, in
y = self.candidate_wrapper(lambda X: self.execute_wo_wrapper(X), X)
File "/home/cloudyu/git/PhySO/physo/physym/program.py", line 228, in execute_wo_wrapper
y = Exec.ExecuteProgram(input_var_data = X,
File "/home/cloudyu/git/PhySO/physo/physym/execute.py", line 60, in ExecuteProgram
res = token.function(*args)
File "/home/cloudyu/git/PhySO/physo/physym/functions.py", line 193, in protected_div
return torch.where(torch.abs(x2) > 0.001, torch.divide(x1, x2), 1.)
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

Implementation of operator x^n

Hi Wassim,

I have a question similar to "How can I add a custom operator?". Is it possible to add the operator x^n, x is an input variable and n is a free constant?

Thanks in advance

How to set the custom priors config?

Hi Wassim!
Thanks for sharing such an excellent package. I have tried to apply it in my own research, but I have 4 puzzles about the priors config.

If y(x) is as an example,
1- I hope the functions ,which are given by PhySO, contain the x**gam, and gam is a free constant. Should I add the "pow" into the "op_name=[ ......, "pow", ...... ]"? If not ,what should I do some change in the function.py?

I read the paper which you just put on the arXiv named "Class Symbolic Regression: Gotta Fit 'Em All". In the article, you say we could give the constrains on the number of occurrences of given parameters, the length of the expression and more.
2-If I want to control the number of occurrences of given parameters, should I change the arity in the Token?
3-How can I set the length of the expression?
4-As the more , could you tell more about it?

By the way, I'm really looking forward to your tutorials about the Custom symbolic optimization task in the PhySO’s documentation

I am also very looking forward your reply.

Best,
Branden

! LaTeX Error: File `type1ec.sty' not found.

As per README.md instructions I got the error upon running the suggested unit test:

$ uname -a
Linux ML 5.15.0-67-generic #74-Ubuntu SMP Wed Feb 22 14:14:39 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
$ git clone https://github.com/WassimTenachi/PhySO
$ cd PhySO/
$ conda create -n PhySO python=3.8
$ conda activate PhySO
$ conda install --file requirements.txt
$ conda install --file requirements_display1.txt
$ pip install -r requirements_display2.txt
$ pip install -e .
$ python -m unittest discover -p "*UnitTest.py"

.Dummy epoch time = 138.905667 ms
.Dummy epoch time (w duplicate elimination) = 85.356149 ms (found 17/1000 candidates with R > 0 and 1/1000 with R = 1 +/- 0.000010)
.Dummy epoch time (w free const) = 3963.447029 ms (found 50/1000 candidates with R = 1 +/- 0.000010)
.Dummy epoch time (w free const and duplicate elimination) = 234.603148 ms (found 17/1000 candidates with R > 0 and 1/1000 with R = 1 +/- 0.000010)
.Dummy epoch time (w free const and duplicate elimination, keeping lowest complexity) = 210.498765 ms (found 17/1000 candidates with R > 0 and 1/1000 with R = 1 +/- 0.000010)
....
Required units time (in ideal non-mixed cases) : 0.006427 ms / step / (prog in batch) 
.
ComputeInfixNotation time = 0.010 ms
.
ExecuteProgram time = 2.828 ms
.
ExecuteProgram time = 2.264 ms
.LBFGS const opti: 1.499337 ms / step
................/home/jabowery/dev/PhySO/physo/physym/library.py:245: UserWarning: The units of token y were not provided (is_constraining_phy_units=False ; phy_units=[nan nan nan nan nan nan nan]), unable to compute units constraints.
  warnings.warn("The units of token %s were not provided (is_constraining_phy_units=%s ; phy_units=%s), "
/home/jabowery/dev/PhySO/physo/physym/library.py:245: UserWarning: The units of token x1 were not provided (is_constraining_phy_units=False ; phy_units=[nan nan nan nan nan nan nan]), unable to compute units constraints.
  warnings.warn("The units of token %s were not provided (is_constraining_phy_units=%s ; phy_units=%s), "
/home/jabowery/dev/PhySO/physo/physym/library.py:245: UserWarning: The units of token pi were not provided (is_constraining_phy_units=False ; phy_units=[nan nan nan nan nan nan nan]), unable to compute units constraints.
  warnings.warn("The units of token %s were not provided (is_constraining_phy_units=%s ; phy_units=%s), "
.......................get_infix_sympy time = 0.966 ms
get_infix_str time = 0.022 ms
F
get_tree_latex time = 0.859 s

get_tree_image time = 0.097 s

get_tree_image_via_tex time = 1.318 s

get_tree_latex time = 0.953 s

get_tree_image time = 0.097 s

get_tree_image_via_tex time = 1.305 s
.
Reward_SquashedNRMSE time = 0.283 ms
................
======================================================================
FAIL: test_infix_repr (physo.physym.tests.program_display_UnitTest.DisplayTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/jabowery/anaconda3/envs/PhySO/lib/python3.8/site-packages/matplotlib/texmanager.py", line 233, in _run_checked_subprocess
    report = subprocess.check_output(
subprocess.CalledProcessError: Command '['latex', '-interaction=nonstopmode', '--halt-on-error', '../88161d9f41eeaed453da104b681404a4.tex']' returned non-zero exit status 1.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jabowery/dev/PhySO/physo/physym/tests/program_display_UnitTest.py", line 104, in test_infix_repr
    img = my_programs.get_infix_image(prog_idx=0,)
RuntimeError: latex was not able to process the following string:
b'lp'

Here is the full report generated by latex:
This is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2022/dev/Debian) (preloaded format=latex)
 restricted \write18 enabled.
entering extended mode
(../88161d9f41eeaed453da104b681404a4.tex
LaTeX2e <2021-11-15> patch level 1
L3 programming layer <2022-01-21>
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2021/10/04 v1.4n Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/type1cm/type1cm.sty)

! LaTeX Error: File `type1ec.sty' not found.

Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: sty)

Enter file name: 
! Emergency stop.
<read *> 
         
l.6 \usepackage
               [utf8]{inputenc}^^M
No pages of output.
Transcript written on 88161d9f41eeaed453da104b681404a4.log.




During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/jabowery/dev/PhySO/physo/physym/tests/program_display_UnitTest.py", line 108, in test_infix_repr
    self.fail("Infix generation failed : get_infix_image")
AssertionError: Infix generation failed : get_infix_image

----------------------------------------------------------------------
Ran 70 tests in 23.099s

FAILED (failures=1)

Can I run PhySO in parallel?

Hi Wassim,

I just played with PhySO and saw huge potential in it.

However, running PhySO on single thread is a little bit slow (about 60-70s/epoch). How can I run it in parallel to speed up?

Thanks
Dianmo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.