jakobbossek / ecr2

ecr: Evolutionary Computation in R (version 2)
https://jakobbossek.github.io/ecr2/
42 stars 8 forks source link

How to view code of mutators/selectors? #91

Closed kevinwolz closed 6 years ago

kevinwolz commented 6 years ago

Since you insisted that I post other question... ;.)

I can view the code of most functions in ecr by just typing the name into R (like all typical R functions). However, this is not the case for mutator/selector functions. Maybe because these are coded in C?

mutPolynomial ECR2 OPERATOR: Supported representations: float

selSimple ECR2 OPERATOR: Supported representations: permutation,binary,float,custom Supported #objectives: single-objectiveSupported #objectives: multi-objective

jakobbossek commented 6 years ago

Hi,

I can view the code of most functions in ecr by just typing the name into R (like all typical R functions). However, this is not the case for mutator/selector functions. Maybe because these are coded in C?

Only a few operators are implemented in C. The reason you do not see the R code by typing, e.g., mutPolynomial, is the special print function for ecr operators which overrides the default. You can easily bypass:

unclass(mutPolynomial)

This produces the following output:

function (ind, p = 0.2, eta = 10, lower, upper)
{
    assertNumber(p, lower = 0, upper = 1, na.ok = FALSE)
    assertNumber(eta, lower = 1, finite = TRUE, na.ok = FALSE)
    assertNumeric(lower, any.missing = FALSE, all.missing = FALSE)
    assertNumeric(lower, any.missing = FALSE, all.missing = FALSE)
    if (length(lower) != length(upper)) {
        stopf("Polynomial mutator: length of lower and upper bounds need to be equal!")
    }
    child = .Call("polynomialMutationC", ind, as.numeric(lower),
        as.numeric(upper), p, eta, PACKAGE = "ecr")
    return(child)
}
<bytecode: 0x7fc5f6a4bd48>
<environment: namespace:ecr>
attr(,"supported")
[1] "float"

In fact, mutPolynomial is implemented in C (see the .Call(...) in the code). However, you can take a glimpse at the C code here at GitHub.

Best, Jakob

jakobbossek commented 6 years ago

Closing