/
restrict_action.R
199 lines (169 loc) · 6.56 KB
/
restrict_action.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#' @title Restrict actions of an extensive-form game
#' @description \code{restrict_actions} restricts some actions of an extensive-
#' form game defined by \code{extensive_form()}.
#' @param game An "extensive_form" class object created by
#' \code{extensive_form()}.
#' @param action A named list of actions that can be chosen by players. The
#' names must be node names: "n1", "n2", etc. A player can choose any action
#' at the nodes that are not listed in this argument.
#' @return A "restricted_game" class object in which players have to choose the
#' actions specified by the user.
#' @include extensive_strategy.R
#' @author Yoshio Kamijo and Yuki Yanai <yanai.yuki@@kochi-tech.ac.jp>
#' @export
restrict_action <- function(game, action) {
id <- node_from <- node_to <- info_sets <- NULL
s <- player <- linetype <- NULL
nodes <- names(action) |>
stringr::str_replace("n", "") |>
as.integer()
play_nodes <- game$data$node |>
dplyr::filter(type == "play") |>
dplyr::pull(id)
## check if each node specified above belongs to an info set
nodes_extra <- action_extra <- NULL
for (i in 1:length(nodes)) {
if (nodes[i] %in% unlist(game$info_sets)) {
for (j in 1:length(game$info_sets)) {
if (nodes[i] %in% game$info_sets[[j]]) {
nodes_extra <- c(nodes_extra,
game$info_sets[[j]][nodes[i] != game$info_sets[[j]]])
action_extra <- c(action_extra,
rep(action[[i]], sum(nodes[i] != game$info_sets[[j]])))
}
}
}
}
## duplicate specified actions for nodes in an info set
if (!is.null(nodes_extra)) {
for (i in 1:length(nodes_extra)) {
new_element <- list(action_extra[i])
names(new_element) <- paste0("n", nodes_extra[i])
action <- c(action, new_element)
}
}
nodes <- c(nodes, nodes_extra)
n_spec <- length(action)
action_vec <- unlist(action)
## check if specified actions are available
for (i in 1:n_spec) {
if (!(action_vec[i] %in% game$action[[which(play_nodes == nodes[i])]])) {
stop(paste(action_vec[i], "is not an avilable action for", names(action)[i]))
}
type <- game$data$node |>
dplyr::filter(id == nodes[i]) |>
dplyr::pull(type)
if (type != "play") stop(paste(names(action)[i], "is a terminal node"))
}
df_path <- game$data$path
df_path$linetype <- "1"
df_path$bold <- FALSE
for (i in 1:n_spec) {
for (j in 1:nrow(df_path)) {
if (df_path$node_from[j] == nodes[i]) {
if (df_path$s[j] == action_vec[i]) {
df_path$bold[j] <- TRUE
} else {
df_path$linetype[j] <- "2"
}
}
}
}
new_tree <- draw_tree(df_path = df_path,
df_node = game$data$node,
df_sol = NULL,
show_node_id = game$tree_params$show_node_id,
info_sets = game$info_sets,
info_line = game$tree_params$info_line,
direction = game$tree_params$direction,
color_palette = game$tree_params$color_palette,
family = game$tree_params$family,
size_player = game$tree_params$size_player,
size_payoff = game$tree_params$size_payoff,
size_action = game$tree_params$size_action,
size_node_id = game$tree_params$size_node_id,
size_terminal = game$tree_params$size_terminal,
scale = game$tree_params$scale,
restriction = TRUE)
actions <- game$action
for (i in 1:n_spec) {
actions[which(play_nodes == nodes[i])] <- action_vec[i]
}
if (!is.null(game$info_sets)) {
## check if a node is included in only one information set
info_sets_elements <- unlist(game$info_sets)
info_sets_elements_u <- unique(info_sets_elements)
if (length(info_sets_elements) != length(info_sets_elements_u))
stop("A node can belong to only one information set.")
## check if actions are compatible with info sets
for (i in 1:length(game$info_sets)) {
n_nodes <- length(game$info_sets[[i]])
if (n_nodes == 1) next
action_list <- list()
for (j in 1:n_nodes) {
action_list[[j]] <- df_path |>
dplyr::filter(node_from == game$info_sets[[i]][j]) |>
dplyr::pull(s)
}
for (j in 2:n_nodes) {
if (!setequal(action_list[[j]], action_list[[j - 1]]))
stop("Different sets of actions are given at different nodes within an information set.")
}
}
## find players corresponding to info sets
n_info_sets <- length(game$info_sets)
info_sets_player <- rep(NA, n_info_sets)
for (i in 1:n_info_sets) {
info_node <- game$info_sets[[i]][1]
info_sets_player[i] <- game$data$node |>
dplyr::filter(id == info_node) |>
dplyr::pull(player)
}
} else {
info_sets_player <- NULL
}
node_to_play <- list()
u_players <- unique(unlist(game$player))
for (i in 1:length(u_players)) {
node_to_play[[i]] <- game$data$node |>
dplyr::filter(player == u_players[i]) |>
dplyr::pull(id)
}
df_path <- df_path |>
dplyr::filter(linetype == "1") |>
dplyr::select(!linetype)
cont <- TRUE
while (cont) {
reaches <- c(1, dplyr::pull(df_path, node_to))
df_node <- game$data$node |>
dplyr::filter(id %in% reaches)
nrow_check <- nrow(df_path)
df_path <- df_path |>
dplyr::filter(node_from %in% reaches)
if (nrow(df_path) == nrow_check) cont <- FALSE
}
payoffs <- game$payoff
for (p in seq_along(u_players)) {
payoffs[[p]] <- df_node |>
dplyr::pull(u_players[p]) |>
stats::na.omit() |>
as.vector()
}
strategies <- extensive_strategy(player = unlist(game$player),
action_list = actions,
info_sets = game$info_sets,
info_sets_player = info_sets_player,
node_to_play = node_to_play)
value <- list(player = game$player,
action = actions,
strategy = strategies$strategy,
action_prof = strategies$action_profile,
payoff = payoffs,
info_sets = game$info_sets,
info_sets_player = info_sets_player,
data = list(node = df_node,
path = df_path),
tree = new_tree,
tree_params = game$tree_params)
structure(value, class = "restricted_game")
}