Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only works if setting physo.physym.reward.USE_PARALLEL_OPTI_CONST = False #50

Open
joelonsql opened this issue Dec 23, 2023 · 0 comments

Comments

@joelonsql
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant