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

the problem of generating expression of Strongly Typed GP #579

Closed
liningbo opened this issue Jul 8, 2021 · 8 comments · May be fixed by #737
Closed

the problem of generating expression of Strongly Typed GP #579

liningbo opened this issue Jul 8, 2021 · 8 comments · May be fixed by #737

Comments

@liningbo
Copy link

liningbo commented Jul 8, 2021

version : 1.2
problem: deap can not add terminal when primitive set has not suitable type but terminal set has.

example:
pset = gp.PrimitiveSetTyped('main', [], pd.DataFrame)

pset.addPrimitive(func1, [pd.DataFrame, pd.DataFrame], pd.DataFrame)
pset.addPrimitive(func2, [pd.DataFrame,int], pd.DataFrame)

pset.addTerminal(terminal=pd.DataFrame(), ret_type=pd.DataFrame,name='t1')
pset.addTerminal(1, int, name=str(1))

expr = gp.genFull(pset, min_=1, max_=10)
tree = gp.PrimitiveTree(expr)
print(str(tree))

the code will the error:
IndexError: The gp.generate function tried to add a primitive of type '<class 'int'>', but there is none available.

I think though primitive set has not the function which will return int, but the terminal set has the constant of type int. so deap should add terminal of type int into the expression but not raise error.

@jasonzutty
Copy link
Contributor

jasonzutty commented Jul 15, 2021

I believe this issue comes from predetermined tree depth that is trying to be filled out. A workaround in the mean time is to add a pass through primitive:

def pass_through(x):
    return x

pset.addPrimitive(pass_through, [int], int, name='pass_int')

@liningbo
Copy link
Author

liningbo commented Jul 16, 2021

I believe this issue comes from predetermined tree depth that is trying to be filled out. A workaround in the mean time is to add a pass through primitive:

def pass_through(x):
    return x

pset.addPrimitive(pass_through, [int], int, name='pass_int')

Thank you for reply, I used the method to avoid the problem, but this led the expressions to be redundant, and not graceful.

@fmder
Copy link
Member

fmder commented Nov 13, 2021

Yes but logically it does not make sense to ask the program to build full trees if for some non-leave it is impossible to find an other non-leave if the required type. I guess there could be a tree generation utility that allows for it ...

@hebrd
Copy link

hebrd commented May 5, 2022

I believe this issue comes from predetermined tree depth that is trying to be filled out. A workaround in the mean time is to add a pass through primitive:

def pass_through(x):
    return x

pset.addPrimitive(pass_through, [int], int, name='pass_int')

Thank you for reply, I used the method to avoid the problem, but this led the expressions to be redundant, and not graceful.

Hi @liningbo, have you figured out how to avoid this problem?

@liningbo
Copy link
Author

liningbo commented May 22, 2022

I believe this issue comes from predetermined tree depth that is trying to be filled out. A workaround in the mean time is to add a pass through primitive:

def pass_through(x):
    return x

pset.addPrimitive(pass_through, [int], int, name='pass_int')

Thank you for reply, I used the method to avoid the problem, but this led the expressions to be redundant, and not graceful.

Hi @liningbo, have you figured out how to avoid this problem?

@BrandonDotLin I just do as jasonzutty said, make a function to return a int value equal to the input of the function.

@wukan1986
Copy link

wukan1986 commented Jan 4, 2024

    while len(stack) != 0:
        depth, type_ = stack.pop()
        if condition(height, depth) or len(pset.primitives[type_]) == 0:
            try:
                term = random.choice(pset.terminals[type_])
            except IndexError:
                _, _, traceback = sys.exc_info()
                raise IndexError("The gp.generate function tried to add "
                                 "a terminal of type '%s', but there is "
                                 "none available." % (type_,)).with_traceback(traceback)
            if isclass(term):
                term = term()
            expr.append(term)

you can add some code to generate in gp.py

@Happylinzy
Copy link

Hope this pull request can be merged ASAP.

@fmder
Copy link
Member

fmder commented Mar 1, 2024

This cannot be merged as it breaks the logic of a full tree.

@fmder fmder closed this as completed Mar 1, 2024
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 a pull request may close this issue.

6 participants