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

Question on eliminating repeated generated expressions. #32

Open
AaronRoseDA opened this issue Mar 12, 2025 · 0 comments
Open

Question on eliminating repeated generated expressions. #32

AaronRoseDA opened this issue Mar 12, 2025 · 0 comments

Comments

@AaronRoseDA
Copy link

Hi,

I have been using your package and it's been blowing my mind. It's really powerful stuff.

I do have a question on the possibility of removing or blacklisting functions that have already been used OR disallowing creating expressions that will return NAs.

When I use the function GrammaticalEvolution it often generates identical expressions. I understand why this happens and that these functions are technically not identical. However, I'm wondering if there is a parameter I can tweak or if my grammarDef could be improved to prevent identical functions. Possibly disallow function combinations that results in an error (sqrt(-1), Log(-1), etc..) Also, in my eval function (SSE) if NAs are produced it returns Inf. This might also be a weakness so any recommendations would be helpful.

If you need anything else from me please let me know. I appreciate your time!

warnings()
Warning messages:
1: In log(log(data$y)) : NaNs produced
...
14: In log(log(data$y)) : NaNs produced
15: In log(log(data$y)) : NaNs produced
...
38: In log(log(data$y)) : NaNs produced
39: In log(log(data$y)) : NaNs produced
40: In log(log(data$y)) : NaNs produced
...

Here's my grammarDef from my wrapper function below:

normGrammarDef$grammarDef
::= (, ) | () |
::= log
::= / | ^
::= data$x | data$y | 2

Here's my example data definition. It's a simple function for demo purposes.:

x <- runif(10, 0, 100)
y <- runif(10, 0, 1)

data <- data.frame(
  distType = "generic function",
  x=x,
  y=y,
  entropy = log(y/x) ^ 2
)

Here's my grammar definition and wrapper function:

create_grammar_wrapper <- function(data,
                                   operators = c("+", "-", "*", "/", "^"),
                                   functions = c('log', 'sqrt', 'exp'),
                                   variables,
                                   constants = c(1, 2, pi)) {

  ivCol <- variables  # Preserve variable names
  variables <- syms(paste0("data$",ivCol))

  functions <- syms(functions)
  operators <- syms(operators)

  # Define grammar rules
  ruleDef <- list(
    expr = grule(op(expr, expr), func(expr), var),
    func = do.call(grule, functions),  # Ensure function set includes `exp`
    op = do.call(grule, operators),
    var = do.call(grule, c(variables, constants))
  )

  # Create the grammar definition
  grammarDef <- CreateGrammar(ruleDef)

  return(c(
    list(
      name = variables,
      known_entropy = data$entropy,
      grammarDef = grammarDef
    ),
    setNames(lapply(ivCol, function(col) data[[col]]), ivCol)  # Dynamically add multiple elements
  ))
}
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