Skip to content

Add a function to convert a PrimitiveTree object into a mathematical expression.#739

Open
Rabbytr wants to merge 1 commit intoDEAP:masterfrom
Rabbytr:master
Open

Add a function to convert a PrimitiveTree object into a mathematical expression.#739
Rabbytr wants to merge 1 commit intoDEAP:masterfrom
Rabbytr:master

Conversation

@Rabbytr
Copy link

@Rabbytr Rabbytr commented Feb 2, 2024

One major advantage of genetic programming is the ability to obtain specific tree-like expressions.
However, the current representation of PrimitiveTree still involves function calls:

add(mul(sub(sub(x0, x1), add(x2, x5)), truediv(add(x3, x1), add(x0, x2))), mul(add(sub(x4, x4), truediv(x5, x2)), add(add(x2, x0), sub(x3, x3))))

This representation may not be easy to understand.
Converting tree-like expressions into mathematical expressions can significantly facilitate human observation and analysis. The above example can be simplified to:

(x1 + x3)*(x0 - x1 - x2 - x5)/(x0 + x2) + x5*(x0 + x2)/x2

Although this may still be challenging to comprehend, it is indeed clearer compared to the previous representation, right?

In this commit, I added a member function to PrimitiveTree that allows it to convert itself into a mathematical expression string:

class PrimitiveTree(list):
  ...
  def expression(self, sym_mapping: Dict[str, Callable] = None, **kwargs) -> str:
  ...

Example:

>>> tree = gp.PrimitiveTree.from_string("min(sqrt(pow(x0, 2)), min(x1, add(x0, x0)))", pset=pset)
>>> func_sym_mapping = {
            'add': operator.add,
            'pow': operator.pow,
            'sqrt': sp.sqrt,
            'min': sp.Min
        }
>>> terminal_sym_mapping = {
            'x0': sp.Symbol('x0', negative=True),
            'x1': sp.Symbol('x0', positive=True)
        }
>>> tree.expression(func_sym_mapping, **terminal_sym_mapping)
        2*x0

@cxl0506
Copy link

cxl0506 commented Oct 14, 2024

This function is so practical! This is exactly what I need. Manually condensing long mathematical expressions is too boring.

Also, there's a clerical error, which "'x1': sp.Symbol('x0', positive=True)" should be "'x1': sp.Symbol('x1', positive=True)" .

terminal_sym_mapping can also be represented by :

terminal_sym_mapping = {
f'x{i}': sp.Symbol(f'x{i}', negative=True) for i in range(2)
}

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

Successfully merging this pull request may close these issues.

2 participants