# make 3D perspective plot
entropy <- function(x,y)
{
-(0.5*x*log(y)+0.5*(1-x)*log(1-x)+(1-0.5)*y*log(y)+(1-0.5)*(1-y)*log(1-y))
}
#p <- x <- seq(0,.5, by=.1)
p <- 0.5;
x <- seq(0,.5, by=.1)
y <- seq(0,.5, by=.1)
f <- outer(x, y, entropy)
library(GA)
## Loading required package: foreach
## Loading required package: iterators
## Package 'GA' version 3.1.1
## Type 'citation("GA")' for citing this R package in publications.
persp3D(x, y, f, theta = 50, phi = 20, color.palette = rainbow);

# Optimise with GA
entropy2 <- function(p,x,y)
{
-(p*x*log(x)+p*(1-x)*log(1-x)+(1-p)*y*log(y)+(1-p)*(1-y)*log(1-y))
}
GA <- ga(type = "real-valued", fitness = function(x) entropy2(x[1], x[2],x[3]), lower = c(0,0,0), upper = c(0.5, 0.5, 0.5), popSize = 50, maxiter = 100, optim = F)
summary(GA)
## ── 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 x3
## lower 0.0 0.0 0.0
## upper 0.5 0.5 0.5
##
## GA results:
## Iterations = 100
## Fitness function value = 0.6929294
## Solution =
## x1 x2 x3
## [1,] 0.001671553 0.3443958 0.4917633