# \donttest{
################################################################
# Example 1: Nash equilibrium, 2 variables, 2 players, no filter
################################################################
# Define objective function (R^2 -> R^2)
fun1 <- function (x)
{
if (is.null(dim(x))) x <- matrix(x, nrow = 1)
b1 <- 15 * x[, 1] - 5
b2 <- 15 * x[, 2]
return(cbind((b2 - 5.1*(b1/(2*pi))^2 + 5/pi*b1 - 6)^2 + 10*((1 - 1/(8*pi)) * cos(b1) + 1),
-sqrt((10.5 - b1)*(b1 + 5.5)*(b2 + 0.5)) - 1/30*(b2 - 5.1*(b1/(2*pi))^2 - 6)^2-
1/3 * ((1 - 1/(8 * pi)) * cos(b1) + 1)))
}
# To use parallel computation (turn off on Windows)
library(parallel)
parallel <- FALSE #TRUE #
if(parallel) ncores <- detectCores() else ncores <- 1
# Simple configuration: no filter, discretization is a 21x21 grid
# Grid definition
n.s <- rep(21, 2)
x.to.obj <- c(1,2)
gridtype <- 'cartesian'
# Run solver with 6 initial points, 4 iterations
# Increase n.ite to at least 10 for better results
res <- solve_game(fun1, equilibrium = "NE", crit = "sur", n.init=6, n.ite=4,
d = 2, nobj=2, x.to.obj = x.to.obj,
integcontrol=list(n.s=n.s, gridtype=gridtype),
ncores = ncores, trace=1, seed=1)
# Get estimated equilibrium and corresponding pay-off
NE <- res$Eq.design
Poff <- res$Eq.poff
# Draw results
plotGame(res)
################################################################
# Example 2: same example, KS equilibrium with given Nadir
################################################################
# Run solver with 6 initial points, 4 iterations
# Increase n.ite to at least 10 for better results
res <- solve_game(fun1, equilibrium = "KSE", crit = "sur", n.init=6, n.ite=4,
d = 2, nobj=2, x.to.obj = x.to.obj,
integcontrol=list(n.s=400, gridtype="lhs"),
ncores = ncores, trace=1, seed=1, Nadir=c(Inf, -20))
# Get estimated equilibrium and corresponding pay-off
NE <- res$Eq.design
Poff <- res$Eq.poff
# Draw results
plotGame(res, equilibrium = "KSE", Nadir=c(Inf, -20))
################################################################
# Example 3: Nash equilibrium, 4 variables, 2 players, filtering
################################################################
fun2 <- function(x, nobj = 2){
if (is.null(dim(x))) x <- matrix(x, 1)
y <- matrix(x[, 1:(nobj - 1)], nrow(x))
z <- matrix(x[, nobj:ncol(x)], nrow(x))
g <- rowSums((z - 0.5)^2)
tmp <- t(apply(cos(y * pi/2), 1, cumprod))
tmp <- cbind(t(apply(tmp, 1, rev)), 1)
tmp2 <- cbind(1, t(apply(sin(y * pi/2), 1, rev)))
return(tmp * tmp2 * (1 + g))
}
# Grid definition: player 1 plays x1 and x2, player 2 x3 and x4
# The grid is a lattice made of two LHS designs of different sizes
n.s <- c(44, 43)
x.to.obj <- c(1,1,2,2)
gridtype <- 'lhs'
# Set filtercontrol: window filter applied for integration and candidate points
# 500 simulation and 200 candidate points are retained.
filtercontrol <- list(nsimPoints=500, ncandPoints=200,
filter=c("window", "window"))
# Set km control: lower bound is specified for the covariance range
# Covariance type and model trend are specified
kmcontrol <- list(lb=rep(.2,4), model.trend=~1, covtype="matern3_2")
# Run solver with 20 initial points, 4 iterations
# Increase n.ite to at least 20 for better results
res <- solve_game(fun2, equilibrium = "NE", crit = "psim", n.init=20, n.ite=2,
d = 4, nobj=2, x.to.obj = x.to.obj,
integcontrol=list(n.s=n.s, gridtype=gridtype),
filtercontrol=filtercontrol,
kmcontrol=kmcontrol,
ncores = 1, trace=1, seed=1)
# Get estimated equilibrium and corresponding pay-off
NE <- res$Eq.design
Poff <- res$Eq.poff
# Draw results
plotGame(res)
################################################################
# Example 4: same example, KS equilibrium
################################################################
# Grid definition: simple lhs
integcontrol=list(n.s=1e4, gridtype='lhs')
# Run solver with 20 initial points, 4 iterations
# Increase n.ite to at least 20 for better results
res <- solve_game(fun2, equilibrium = "KSE", crit = "sur", n.init=20, n.ite=2,
d = 4, nobj=2,
integcontrol=integcontrol,
filtercontrol=filtercontrol,
kmcontrol=kmcontrol,
ncores = 1, trace=1, seed=1)
# Get estimated equilibrium and corresponding pay-off
NE <- res$Eq.design
Poff <- res$Eq.poff
# Draw results
plotGame(res, equilibrium = "KSE")
# }
Run the code above in your browser using DataLab