Last active
March 10, 2019 05:45
-
-
Save djhurio/b48a5cf3fd720896db3f83425148feb8 to your computer and use it in GitHub Desktop.
VDVV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# n: Nodarbināto skaits uzņēmumā | |
# v: Vietējo vienību skaits uzņēmumā | |
# n <- 5 | |
# v <- 3 | |
rm(list = ls()) | |
gc() | |
gen.all.matrix <- function(n, v) { | |
# Pārbaude | |
if (n < 1) stop("Nodarbināto skaits par mazu") | |
if (v < 1) stop("VV skaits par mazu") | |
# Pārbaude, lai VV skaits nebūtu lielāks par nodarbināto skaitu | |
if (v > n) stop("VV skaits lielāks par nodarbināto skaitu") | |
# Ja tikai viena VV | |
if (v == 1) { | |
a <- matrix(rep(1, n), nrow = n) | |
rownames(a) <- paste0("nod", 1:n) | |
colnames(a) <- paste0("VV", 1:v) | |
return(list(a)) | |
} | |
# Katra nodarbinātā iespējamā darba vieta | |
job.places <- unname(split(diag(rep(1, v)), f = 1:v)) | |
# Visi iespējamie varianti | |
all0 <- sapply(1:n, function(i) rep(1:v, times = v**(n-i), each = v**(i-1))) | |
# Pārveido par sarakstu | |
all1 <- split(all0, f = row(all0)) | |
# Atlasa tikai tos variantus, kur katrā VV ir vismaz 1 nodarbinātais | |
all2 <- unname(all1[sapply(all1, function(x) all(1:v %in% x))]) | |
# Matricas | |
lapply(all2, function(j) { | |
a <- do.call(rbind, job.places[j]) | |
rownames(a) <- paste0("nod", 1:n) | |
colnames(a) <- paste0("VV", 1:v) | |
a | |
}) | |
} | |
system.time(tmp <- gen.all.matrix(n = 3, v = 1)) | |
system.time(tmp <- gen.all.matrix(n = 2, v = 2)) | |
system.time(tmp <- gen.all.matrix(n = 3, v = 2)) | |
system.time(tmp <- gen.all.matrix(n = 3, v = 3)) | |
system.time(tmp <- gen.all.matrix(n = 4, v = 3)) | |
system.time(tmp <- gen.all.matrix(n = 5, v = 5)) | |
system.time(tmp <- gen.all.matrix(n = 6, v = 6)) | |
system.time(tmp <- gen.all.matrix(n = 7, v = 7)) | |
# user system elapsed | |
# 9.6 0.0 9.8 | |
# system.time(tmp <- gen.all.matrix(n = 8, v = 8)) | |
# Error: memory exhausted (limit reached?) | |
# Error during wrapup: memory exhausted (limit reached?) | |
# Timing stopped at: 94.89 9.97 117 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment