4

I am just not able to figure out how to use ga() of GA package in R.

Let me give an example of what I am trying to do.

I want to get the best order in which pizzas should be displayed to a customer to maximize the chances of selling all of them.

Let us say, I have 3 pizzas, "pizza1", "pizza2", "pizz3". For each order (there are 3! orders possible for this problem) I calculate some fitness using say, fitness(order) function which returns some numeric value.

I want to implement this whole thing on a very large scale.

Could someone please help me setting up the parameters of ga() function.

Here is what I am trying

library(GA)

algo <- ga(type = "permutation", fitness = fitness, min= ?, max= ?, maxiter = ?)

Can someone help me setup min, max and maxiter and popsize parameters for this problem and what would they mean specifically for my problem in layman terms?

user3664020
  • 2,980
  • 6
  • 24
  • 45
  • 2
    Dear useRs of GA package, as the developer and maintainer of the package, I'm very happy that you are using the GA package for solving your optimisation problems. However, it is a bit disappointing to read that the "documentation isn't great". For several reasons. The help/man pages of R functions in a package must describe the arguments and the basic usage. It is not intended as the main documentation. I have recently added a vignette which hopefully give some better explanation, but the main documentation of the package are the following two papers – Luca Scrucca Aug 19 '17 at 08:37
  • 3
    Scrucca L. (2013) GA: A Package for Genetic Algorithms in R. **Journal of Statistical Software**, Vol. 53, Issue 4, pp. 1–37. http://www.jstatsoft.org/v53/i04 Scrucca L. (2017) On some extensions to GA package: hybrid optimisation, parallelisation and islands evolution, **The R Journal**, 9(1), 187–206. https://journal.r-project.org/archive/2017/RJ-2017-008 Both papers can be freely downloaded, have full explanation with a lot of examples (and more examples were included in the preliminary versions, but anonymous referees required to remove some of them). – Luca Scrucca Aug 19 '17 at 08:37

1 Answers1

3

The documentation isn't great - there are no examples for anything other than type="real-valued". AFAICT, this seems to work:

library(GA)
f <-function(z) sum((z-c(3,2,1))^2)  # best order is 1, 2, 3
result <- ga(type="permutation", fitness=f, 
             min=c(1,1,1), max=c(3,3,3), names=paste0("pizza",1:3))
summary(result)$solution
#      pizza1 pizza2 pizza3
# [1,]      1      2      3

So this creates a fitness function, f(...) which is maximized when the pizzas are ordered 1,2,3. The argument to this function has to be a list (or coercible to a list: so, a vector) with numerical elements representing the different options. So, for example, (1,2,3) or (3,2,1), etc. min=... and max=... are both vectors specifying the "lowest" and "highest" possible values.

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • Thanks for the solution. I have a question, what is 'z' ? What value does it take? I am assuming that the algorithm passes this 'z' to this function, but what exactly it is? – user3664020 Sep 16 '15 at 17:25
  • That seems pretty self-explanatory. It's the number of generations. – jlhoward Sep 16 '15 at 17:38
  • Here a full permutation example using GA library: http://rpubs.com/somasdhavala/GAeg – Pablo Casas Nov 30 '17 at 17:31
  • In case of an error *Sample size must be <= n when not using replacement* with a default setting use `gaControl("useRcpp" = F)` – Antoni Mar 01 '20 at 10:22