# [R-sig-ME] Genetic Algorithm in R

Narahara Chari D dnchariworld at yahoo.com
Thu Jan 8 14:34:58 CET 2015

```Hi All,
I am trying to implement Genetic Algorithm based on the request to fill 500 ask based on the inventory. Algorithm should pick optimized combinations based on the rank (low rank inventory should be selected first) and update the inventory after fulfilling the request.

dataGA
Inventory Rank
1        200    2
2        150    1
3        300    3
4        100    4
5         50    1
6        250    1
7        350    2
8        100    2
9        250    3
10        25    4
11       125    4
12       100    5
13       225    1
14       220    2
15       150    3

library(GA)
Rastrigin <- function(x1, x2)
{
x1*x2 - 500
}

x1 <- dataGA\$Inventory
x2 <- seq(0.01, 1, by = 0.01)

f <- outer(x1, x2, Rastrigin)
GA <- ga(type = "real-valued", fitness = function(x) -Rastrigin(x1, x2),
min = c(1000, 0.01), max = c(10000000, 1), popSize = 50, maxiter = 100)
summary(GA)I have solutions
+-----------------------------------+
|         Genetic Algorithm         |
+-----------------------------------+

GA settings:
Type                  =  real-valued
Population size       =  50
Number of generations =  100
Elitism               =  2
Crossover probability =  0.8
Mutation probability  =  0.1
Search domain
x1   x2
Min  25 0.01
Max 350 1.00

GA results:
Iterations             = 100
Fitness function value = 498
Solutions              =
x1        x2
[1,]  194.44965 0.3886958
[2,]  191.24609 0.4016915
[3,]  243.08729 0.3699107
[4,]  137.36235 0.4050092
[5,]  177.33917 0.4570788
[6,]  139.32841 0.3755744
[7,]  137.68704 0.3534409
[8,]  156.28470 0.3825039
[9,]  196.84368 0.4089103
[10,]  80.42056 0.3796008
...
[48,] 183.48574 0.4611285For the ask of 500, I want solutions to be x1[c(2,5,6,13),] and x2[c(2,5,6,13),] with uniqueID which is a row number here.
Or please suggest any other Package/Algorithm combination for this problem,
Thank You,Best Regards,Chari

[[alternative HTML version deleted]]

```