kstaats / karoo_gp

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

TypeError: '<' not supported between instances of 'GreaterThan' and 'GreaterThan' #99

Open asksak opened 2 years ago

asksak commented 2 years ago

I get the following error: (note that I use anaconda navigator to create an environment with the required versions of the dependent modules) and the following command line and functions:

operator, arity
+,2
-,2
/,2
*,2
<,2
>=,2
or,2
and,2
(macos-TF2) aymansalsaket@AYMANs-MacBook-Pro latest % python -V
Python 3.8.8
(macos-TF2) aymansalsaket@AYMANs-MacBook-Pro latest % sudo nice -1 python karoo-gp.pypython -V 

Select (c)lassification, (r)egression, (m)atching, or (p)lay (default m): c
Select (f)ull, (g)row, or (r)amped 50/50 method (default r): r
Enter depth of the initial population of Trees (default 3): **7**
Enter maximum Tree depth (default 7): **7**
Enter minimum number of nodes for any given Tree (default 3; max 255): **5**
Enter number of Trees in each population (default 100): 75
Enter max number of generations (default 10): 100

In the settings above only 5,5,3 work till the end. Here, I used 7,7,5 and it crashed as shown in the trace:

ERROR TRACE:

Evaluate all Trees in Generation 4
ValueError: Error from parse_expr with transformed code:
   "(((Symbol ('A1x' ))>=(Symbol ('A21x' )))<((Symbol ('A11x' ))>=(Symbol ('A2x' ))))"

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

Traceback (most recent call last):
  File "karoo-gp.py", line 593, in <module>
    gp.fit(X, y)
  File "/Users/aymansalsaket/Desktop/latest/karoo_gp/base_class.py", line 804, in fit
    super().fit(X, y, *args, **kwargs)
  File "/Users/aymansalsaket/Desktop/latest/karoo_gp/base_class.py", line 460, in fit
    self.population.evaluate(X_train, y_train, self.X_train_hash)
  File "/Users/aymansalsaket/Desktop/latest/karoo_gp/population.py", line 93, in evaluate
    predictions = self.model.batch_predict(X, self.trees, X_hash)
  File "/Users/aymansalsaket/Desktop/latest/karoo_gp/base_class.py", line 494, in batch_predict
    y = self.engine.predict(trees, X, X_hash)
  File "/Users/aymansalsaket/Desktop/latest/karoo_gp/engine.py", line 155, in predict
    expr = tree.expression
  File "/Users/aymansalsaket/Desktop/latest/karoo_gp/tree.py", line 76, in expression
    return self.root.parse(simplified=True)
  File "/Users/aymansalsaket/Desktop/latest/karoo_gp/node.py", line 203, in parse
    return (f'({self.children[0].parse(simplified)}'
  File "/Users/aymansalsaket/Desktop/latest/karoo_gp/node.py", line 203, in parse
    return (f'({self.children[0].parse(simplified)}'
  File "/Users/aymansalsaket/Desktop/latest/karoo_gp/node.py", line 200, in parse
    return (f'({self.children[0].parse(simplified)}{ws}{self.label}'
  File "/Users/aymansalsaket/Desktop/latest/karoo_gp/node.py", line 200, in parse
    return (f'({self.children[0].parse(simplified)}{ws}{self.label}'
  File "/Users/aymansalsaket/Desktop/latest/karoo_gp/node.py", line 200, in parse
    return (f'({self.children[0].parse(simplified)}{ws}{self.label}'
  File "/Users/aymansalsaket/Desktop/latest/karoo_gp/node.py", line 172, in parse
    result = str(sympify(raw_expr))
  File "/opt/anaconda3/envs/macos-TF2/lib/python3.8/site-packages/sympy/core/sympify.py", line 495, in sympify
    expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
  File "/opt/anaconda3/envs/macos-TF2/lib/python3.8/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 "/opt/anaconda3/envs/macos-TF2/lib/python3.8/site-packages/sympy/parsing/sympy_parser.py", line 1096, in parse_expr
    rv = eval_expr(code, local_dict, global_dict)
  File "/opt/anaconda3/envs/macos-TF2/lib/python3.8/site-packages/sympy/parsing/sympy_parser.py", line 915, in eval_expr
    expr = eval(
  File "<string>", line 1, in <module>
TypeError: '<' not supported between instances of 'GreaterThan' and 'GreaterThan'
granawkins commented 2 years ago

Hi - some questions to try to isolate the issue:

asksak commented 2 years ago

Which version of Karoo are you using? i.e. when did you last fetch the latest version?

This morning was the last fetch. Same error.

Does the error only happen when the < operator is included?

The error occurs when any comparison operator is used. Note: 5,5,3 does not give an error.

What kind of output are you expecting from the trees? > or <= will give a boolean output, while the others (*, +..) would give a numeric output.

I use (+,-,/,*, if, or, and,>= ,<), and am using it for classification. I anticipate that regardless of the output of the function, the classification code will convert it to 0 or 1. Again the run works with 5,5,3 but crashes with anything else.

Are you working with a modified version of Karoo?

Not at all.

asksak commented 2 years ago

if I remove (or, and) and use (+,-,/,*, if,>= ,<) instead, the error is eliminated. I can use any configuration of tree depth.

This is not practical at all as the logical operators in combination with comparative operators are needed for classification problems. Therefore this error needs to be fixed for functional purposes.

asksak commented 2 years ago

Any comments about this bug?