kstaats / karoo_gp

A Genetic Programming platform for Python with TensorFlow for wicked-fast CPU and GPU support.
Other
157 stars 61 forks source link

Sporadic `ERROR! In fx_evolve_grow_mutate: branch_depth -1 < 0` #55

Closed ezio-melotti closed 2 years ago

ezio-melotti commented 2 years ago

The following command sometimes fails with ERROR! In fx_evolve_grow_mutate: branch_depth -1 < 0:

python3 karoo-gp.py -ker r -typ g -bas 5 -pop 20 -fil karoo_gp/files/data_REGRESS.csv

To reproduce, you can run it in a loop until it fails using:

while true; do python3 karoo-gp.py -ker r -typ g -bas 5 -pop 20 -fil karoo_gp/files/data_REGRESS.csv; done

After the error it stops in the pause menu, which shouldn't happen for this type of runs.

kstaats commented 2 years ago

I'll take a look ASAP, but it won't be for a day or three ...

On 5/23/22 22:14, Ezio Melotti wrote:

The following command sometimes fails with ERROR! In fx_evolve_grow_mutate: branch_depth -1 < 0:

python3 karoo-gp.py -ker r -typ g -bas 5 -pop 20 -fil karoo_gp/files/data_REGRESS.csv

To reproduce, you can run it in a loop until it fails using:

while true; do python3 karoo-gp.py -ker r -typ g -bas 5 -pop 20 -fil karoo_gp/files/data_REGRESS.csv; done

After the error it stops in the pause menu, which shouldn't happen for this type of runs.

ezio-melotti commented 2 years ago

Apparently there's an even more sporadic failure, triggered by the same command:

ValueError: Error from parse_expr with transformed code: "(Symbol ('r' ))-(Symbol ('r' ))-(Symbol ('t' ))+(Symbol ('t' ))Function ('r' )(Symbol ('t' ))-(Symbol ('t' ))-(Symbol ('t' ))/(Symbol ('r' ))/(Symbol ('t' ))"

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

Traceback (most recent call last):
  File "/home/.../karoo_gp/venv/lib/python3.10/site-packages/sympy/core/sympify.py", line 495, in sympify
    expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
  File "/home/.../karoo_gp/venv/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py", line 1105, in parse_expr
    raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")
  File "/home/.../karoo_gp/venv/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py", line 1096, in parse_expr
    rv = eval_expr(code, local_dict, global_dict)
  File "/home/.../karoo_gp/venv/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py", line 915, in eval_expr
    expr = eval(
  File "<string>", line 1
    (Symbol ('r' ))-(Symbol ('r' ))-(Symbol ('t' ))+(Symbol ('t' ))Function ('r' )(Symbol ('t' ))-(Symbol ('t' ))-(Symbol ('t' ))/(Symbol ('r' ))/(Symbol ('t' ))
                                                                   ^^^^^^^^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/.../karoo_gp/karoo-gp.py", line 417, in <module>
    gp.fit()
  File "/home/.../karoo_gp/karoo_gp/base_class.py", line 262, in fit
    self.fx_eval_generation()  # evaluate all Trees in a single generation
  File "/home/.../karoo_gp/karoo_gp/base_class.py", line 1377, in fx_eval_generation
    self.fx_fitness_gym(self.population_b)
  File "/home/.../karoo_gp/karoo_gp/base_class.py", line 1424, in fx_fitness_gym
    self.fx_eval_poly(population[tree_id])  # extract the expression
  File "/home/.../karoo_gp/karoo_gp/base_class.py", line 1266, in fx_eval_poly
    self.algo_sym = sympify(self.algo_raw)
  File "/home/.../karoo_gp/venv/lib/python3.10/site-packages/sympy/core/sympify.py", line 497, in sympify
    raise SympifyError('could not parse %r' % a, exc)
sympy.core.sympify.SympifyError: Sympify of expression 'could not parse '(r)-(r)-(t)+(t)r(t)-(t)-(t)/(r)/(t)'' failed, because of exception being raised:
SyntaxError: invalid syntax (<string>, line 1)
kstaats commented 2 years ago

This is simply due to "(Symbol ('t' ))Function" where there is no operator between )) and Function. It's a malformed expression. Could be Karoo, could be Sympy.

On 5/25/22 08:18, Ezio Melotti wrote:

Apparently there's an even more sporadic failure, triggered by the same command:

ValueError: Error from parse_expr with transformed code: "(Symbol ('r' ))-(Symbol ('r' ))-(Symbol ('t' ))+(Symbol ('t' ))Function ('r' )(Symbol ('t' ))-(Symbol ('t' ))-(Symbol ('t' ))/(Symbol ('r' ))/(Symbol ('t' ))"

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

Traceback (most recent call last):
   File "/home/.../karoo_gp/venv/lib/python3.10/site-packages/sympy/core/sympify.py", line 495, in sympify
     expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
   File "/home/.../karoo_gp/venv/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py", line 1105, in parse_expr
     raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")
   File "/home/.../karoo_gp/venv/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py", line 1096, in parse_expr
     rv = eval_expr(code, local_dict, global_dict)
   File "/home/.../karoo_gp/venv/lib/python3.10/site-packages/sympy/parsing/sympy_parser.py", line 915, in eval_expr
     expr = eval(
   File "<string>", line 1
     (Symbol ('r' ))-(Symbol ('r' ))-(Symbol ('t' ))+(Symbol ('t' ))Function ('r' )(Symbol ('t' ))-(Symbol ('t' ))-(Symbol ('t' ))/(Symbol ('r' ))/(Symbol ('t' ))
                                                                    ^^^^^^^^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
   File "/home/.../karoo_gp/karoo-gp.py", line 417, in <module>
     gp.fit()
   File "/home/.../karoo_gp/karoo_gp/base_class.py", line 262, in fit
     self.fx_eval_generation()  # evaluate all Trees in a single generation
   File "/home/.../karoo_gp/karoo_gp/base_class.py", line 1377, in fx_eval_generation
     self.fx_fitness_gym(self.population_b)
   File "/home/.../karoo_gp/karoo_gp/base_class.py", line 1424, in fx_fitness_gym
     self.fx_eval_poly(population[tree_id])  # extract the expression
   File "/home/.../karoo_gp/karoo_gp/base_class.py", line 1266, in fx_eval_poly
     self.algo_sym = sympify(self.algo_raw)
   File "/home/.../karoo_gp/venv/lib/python3.10/site-packages/sympy/core/sympify.py", line 497, in sympify
     raise SympifyError('could not parse %r' % a, exc)
sympy.core.sympify.SympifyError: Sympify of expression 'could not parse '(r)-(r)-(t)+(t)r(t)-(t)-(t)/(r)/(t)'' failed, because of exception being raised:
SyntaxError: invalid syntax (<string>, line 1)
kstaats commented 2 years ago

Per our meeting, this particular error is due to a mutation function resulting in a root node at a less than 0 position. I can investigate, but would rather wait to see if this entire function is replaced by Grant's revised code. Let's keep this open, and we can return if necessary.

On 5/23/22 22:14, Ezio Melotti wrote:

The following command sometimes fails with ERROR! In fx_evolve_grow_mutate: branch_depth -1 < 0:

python3 karoo-gp.py -ker r -typ g -bas 5 -pop 20 -fil karoo_gp/files/data_REGRESS.csv

To reproduce, you can run it in a loop until it fails using:

while true; do python3 karoo-gp.py -ker r -typ g -bas 5 -pop 20 -fil karoo_gp/files/data_REGRESS.csv; done

After the error it stops in the pause menu, which shouldn't happen for this type of runs.

granawkins commented 2 years ago

This is because the default tree_depth_max is set to 4 for cli mode, and you're setting tree_depth_base to 5.

This causes some problems in branch_mutate because it expects trees are less than tree_depth_max.

I can see two ways of handling it:

  1. Allow initial trees to be larger than the maximum, and only apply maximum for evolved trees.
  2. For the CLI mode, if -bas flag is used, set tree_depth_max to max(4, tree_depth_base)
  3. In BaseGP.__init__, set self.tree_depth_max = max(tree_depth_max, tree_depth_base).

I think 3 is the best approach. Will confirm it works and do a quick PR.

kstaats commented 2 years ago

Grant,

On 6/29/22 00:03, Grant wrote:

This is because the default tree_depth_max is set to 4 for cli mode, and you're setting tree_depth_base to 5.

This causes some problems in branch_mutate because it expects trees are less than tree_depth_max.

I can see two ways of handling it:

  1. Allow initial trees to be larger than the maximum, and only apply maximum for evolved trees.

Yes, you can prune trees at the very last step.

  1. For the CLI mode, if -bas flag is used, set tree_depth_max to max(4, tree_depth_base)

Yes.

  1. In BaseGP.__init__, set self.tree_depth_max = max(tree_depth_max, tree_depth_base).

I think 3 is the best approach. Will confirm it works and do a quick PR.

Sounds good. Consider the effect on the quality of that particular tree, if the depth is cut short up-front vs pruning along the way.

kai