-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.Rmd
423 lines (310 loc) · 10.6 KB
/
index.Rmd
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
---
title: "Vote combinations"
output:
html_document:
theme: paper
code_folding: hide
toc: true
toc_float:
collapsed: false
---
Initialization
===================
```{r}
# loading required libraries
packagesList <- c("dplyr","tidyr","arrangements","knitr","kableExtra","data.table","plotly")
packages <- suppressWarnings(suppressMessages(lapply(packagesList, function(x){ if(!require(x, character.only = T, quietly = T)){ install.packages(x); return(paste0("installed missing package: ", x)) } else paste0("required package is installed: ", x) } )))
options(stringsAsFactors = FALSE)
```
---------------
Solving the hard way
===================
### Three companies
```{r}
# Solving for three companies and three possible plans
companies <- c("A","B","C") # companies
possiblePlans <- c("Plan 1","Plan 2","Plan 3") # possible plans to be voted
# Plan 1
df <- NULL
for (c in 1:length(companies)){ # from 1 to n companies can commit for first plan
possCombinations <- combinations(companies, c) # possible combinations for companies to commit to
for (comb in 1:nrow(possCombinations)){
df <- rbind(df, paste(possCombinations[comb,],collapse=","))
}
}
# Plan 2
ndf <- NULL
for (p in 1:nrow(df)){ # for each of possible combination already existent
remainingCompanies <- setdiff(companies, unlist(strsplit(df[p,],","))) # remaining companies without a plan
if(length(remainingCompanies)>0){
for (c in 1:length(remainingCompanies)){ # from 1 to n remainingCompanies can commit to current plan
possCombinations <- combinations(remainingCompanies, c) # possible combinations for companies to commit to
for (comb in 1:nrow(possCombinations)){
ndf <- rbind(ndf, c(df[p,], paste(possCombinations[comb,],collapse=",")))
}
}
} else {
ndf <- rbind(ndf, c(df[p,], ""))
}
}
# Plan 3
df <- ndf
ndf <- NULL
for (p in 1:nrow(df)){ # for each of possible combination already existent
remainingCompanies <- setdiff(companies, unlist(strsplit(df[p,],","))) # remaining companies without a plan
if(length(remainingCompanies)>0){
for (c in 1:length(remainingCompanies)){ # from 1 to n remainingCompanies can commit to current plan
possCombinations <- combinations(remainingCompanies, c) # possible combinations for companies to commit to
for (comb in 1:nrow(possCombinations)){
ndf <- rbind(ndf, c(df[p,], paste(possCombinations[comb,],collapse=",")))
}
}
} else {
ndf <- rbind(ndf, c(df[p,], ""))
}
}
#Removing duplicates
out <- data.frame(ndf)
out <- unite(out, aux, sep = "|", remove=F)
out$aux <- duplicated(lapply(strsplit(out$aux,"\\|") , function(x){
tmp <- sort(x)
tmp <- tmp[!(tmp == "|")]
return(tmp)}))
out <- out[!(out$aux),-which(names(out) == "aux")]
names(out) <- paste("Plan ", seq(1,length(out),1))
```
<b>Number:</b>
Combination number for different voting clusters: <b> `r nrow(out) `</b>
<b>Table:</b>
Table with all possible assembly vote combinations:
```{r, echo = FALSE}
out %>%
kable(row.names = FALSE) %>%
kable_styling()
```
---------------
Generalizing the solution
===================
```{r}
#generalizing
createVoteTable <- function(companies, numberofPlans, df=NULL){
if(is.null(df)){ #first run
firstRun <- TRUE
companies <- sort(companies)
df <- data.frame("empty")
} else{
firstRun <- FALSE
}
dat <- NULL
for (p in 1:nrow(df)){
if(firstRun) {
remainingCompanies <- companies
} else {
remainingCompanies <- companies[!(companies %in% unlist(strsplit(as.character(df[p,]),",")))]
}
if(length(remainingCompanies)>0){
for (c in 1:length(remainingCompanies)){ # from 1 to n companies can commit for first plan
possCombinations <- combinations(remainingCompanies, c) # possible combinations for companies to commit to
dat <- rbindlist(list(dat,rbindlist(lapply(1:nrow(possCombinations),function(comb) {
if(firstRun)
return(as.list(paste(possCombinations[comb,],collapse=",")))
else
return(c(as.list(df[p,]),paste(possCombinations[comb,],collapse=",")))
}))))
}
} else {
dat <- rbindlist(list(dat,c(as.list(df[p,]), "")))
}
}
ndf <- data.frame(dat)
#removing duplicate plans
out <- as.data.frame(ndf)
out <- unite(out, aux, sep = "|", remove=F)
out$aux <- duplicated(lapply(strsplit(out$aux,"\\|") , function(x){
tmp <- sort(x)
tmp <- tmp[!(tmp == "|")]
return(tmp)}))
out <- out[!(out$aux),-which(names(out) == "aux")]
out <- as.data.frame(out)
numberofPlans = numberofPlans - 1
if(numberofPlans == 0){
names(out) <- paste("Plan ", seq(1,length(out),1))
return(out)
}
createVoteTable(companies=companies, numberofPlans=numberofPlans, df=out)
}
```
---------------
### Two companies
```{r}
# Two companies, two plans
companies <- c("C1","C2") # companies
numberofPlans <- 2 # possible plans to be voted
result <- list()
result$'2' <- createVoteTable(companies,numberofPlans)
saveRDS(result$'2', file = paste0("result_2.rds"))
```
<b>Number:</b>
Combination number for different voting clusters: <b> `r nrow(result$'2') `</b>
<b>Table:</b>
Table with all possible assembly vote combinations:
```{r, echo = FALSE}
result$'2' %>%
kable(row.names = FALSE) %>%
kable_styling()
```
### Three companies
```{r}
# Three companies, Three plans
companies <- as.vector(paste0("C",1:3)) # companies
numberofPlans <- 3 # possible plans to be voted
result$'3' <- createVoteTable(companies,numberofPlans)
saveRDS(result$'3', file = paste0("result_3.rds"))
```
<b>Number:</b>
Combination number for different voting clusters: <b> `r nrow(result$'3') `</b>
<b>Table:</b>
Table with all possible assembly vote combinations:
<div style="height: 300px; overflow-y: scroll; overflow-x: hidden;">
```{r, echo = FALSE}
result$'3' %>%
kable(row.names = FALSE) %>%
kable_styling()
```
</div>
### Four companies
```{r}
# Three companies, Three plans
companies <- as.vector(paste0("C",1:4)) # companies
numberofPlans <- 4 # possible plans to be voted
result$'4' <- createVoteTable(companies,numberofPlans)
saveRDS(result$'4', file = paste0("result_4.rds"))
#result$'4' <- readRDS("result_4.rds") #loading previous calculated result
```
<b>Number:</b>
Combination number for different voting clusters: <b> `r nrow(result$'4') `</b>
<b>Table:</b>
Table with all possible assembly vote combinations:
<div style="height: 300px; overflow-y: scroll; overflow-x: hidden;">
```{r, echo = FALSE}
result$'4' %>%
kable(row.names = FALSE) %>%
kable_styling()
```
</div>
### Five companies
```{r}
# Three companies, Three plans
companies <- as.vector(paste0("C",1:5)) # companies
numberofPlans <- 5 # possible plans to be voted
result$'5' <- createVoteTable(companies,numberofPlans)
saveRDS(result$'5', file = paste0("result_5.rds"))
#result$'5' <- readRDS("result_5.rds") #loading previous calculated result
```
<b>Number:</b>
Combination number for different voting clusters: <b> `r nrow(result$'5') `</b>
<b>Table:</b>
Table with all possible assembly vote combinations:
<div style="height: 300px; overflow-y: scroll; overflow-x: hidden;">
```{r, echo = FALSE}
result$'5' %>%
kable(row.names = FALSE) %>%
kable_styling()
```
</div>
### Six companies
```{r}
# Three companies, Three plans
companies <- as.vector(paste0("C",1:6)) # companies
numberofPlans <- 6 # possible plans to be voted
result$'6' <- createVoteTable(companies,numberofPlans)
saveRDS(result$'6', file = paste0("result_6.rds"))
#result$'6' <- readRDS("result_6.rds") #loading previous calculated result
```
<b>Number:</b>
Combination number for different voting clusters: <b> `r nrow(result$'6') `</b>
<b>Table:</b>
Table with all possible assembly vote combinations:
<div style="height: 300px; overflow-y: scroll; overflow-x: hidden;">
```{r, echo = FALSE}
result$'6' %>%
kable(row.names = FALSE) %>%
kable_styling()
```
</div>
### Seven companies
```{r}
# Three companies, Three plans
companies <- as.vector(paste0("C",1:7)) # companies
numberofPlans <- 7 # possible plans to be voted
result$'7' <- createVoteTable(companies,numberofPlans)
saveRDS(result$'7', file = paste0("result_5.rds"))
#result$'7' <- readRDS("result_7.rds") #loading previous calculated result
```
<b>Number:</b>
Combination number for different voting clusters: <b> `r nrow(result$'7') `</b>
<b>Table:</b>
Table with all possible assembly vote combinations:
<div style="height: 300px; overflow-y: scroll; overflow-x: hidden;">
```{r, echo = FALSE}
result$'7' %>%
kable(row.names = FALSE) %>%
kable_styling()
```
</div>
### Eight companies
```{r}
# Three companies, Three plans
companies <- as.vector(paste0("C",1:8)) # companies
numberofPlans <- 8 # possible plans to be voted
#result$'8' <- createVoteTable(companies,numberofPlans)
#saveRDS(result$'8', file = paste0("result_5.rds"))
result$'8' <- readRDS("result_8.rds") #loading previous calculated result
```
<b>Number:</b>
Combination number for different voting clusters: <b> `r nrow(result$'8') `</b>
<b>Table:</b>
Table with all possible assembly vote combinations:
<div style="height: 300px; overflow-y: scroll; overflow-x: hidden;">
```{r, echo = FALSE}
result$'8' %>%
kable(row.names = FALSE) %>%
kable_styling()
```
</div>
### Nine companies
```{r}
# Three companies, Three plans
companies <- as.vector(paste0("C",1:9)) # companies
numberofPlans <- 9 # possible plans to be voted
#result$'9' <- createVoteTable(companies,numberofPlans)
#saveRDS(result$'9', file = paste0("result_9.rds"))
result$'9' <- readRDS("result_9.rds") #loading previous calculated result
```
<b>Number:</b>
Combination number for different voting clusters: <b> `r nrow(result$'9') `</b>
<b>Table:</b>
Table with all possible assembly vote combinations hidden due to size.
### Ten companies
```{r}
# Three companies, Three plans
companies <- as.vector(paste0("C",1:10)) # companies
numberofPlans <- 10 # possible plans to be voted
#result$'10' <- createVoteTable(companies,numberofPlans)
#saveRDS(result$'10', file = paste0("result_10.rds"))
result$'10' <- readRDS("result_10.rds") #loading previous calculated result
```
<b>Number:</b>
Combination number for different voting clusters: <b> `r nrow(result$'10') `</b>
<b>Table:</b>
Table with all possible assembly vote combinations hidden due to size.
---------------
Chart Summary
===================
```{r}
df <- data.frame(Companies = seq(2,10,1), Combinations = unlist(lapply(1:9,function(i){return(nrow(result[[i]]))})))
plot_ly(df, x = ~Companies, y = ~Combinations, type = 'scatter', mode = 'lines+markers',
hoverinfo = 'text',
text = ~paste(Combinations, ' possible combinations.')) %>%
layout(xaxis = list(title = "Companies/Plans"))
```