the following Python code was translated to R Python code:
plan_no_harvest_collision = []
for plan in only_all_plants_full:
harvest_plan = []
for house in plan:
harvest_plan_per_house = []
days = 0
for plant in range(0, len(house) - 1, 2):
days += house[plant + 1]
harvest_plan_per_house.append(house[plant] + '_' + str(days))
harvest_plan += harvest_plan_per_house
if len(harvest_plan) == len(set(harvest_plan)):
plan = []
for plant in harvest_plan:
plan.append(plant.split('_')[0])
plan_no_harvest_collision.append(plan)
else:
pass
R code:
plan_no_harvest_collision <- list()
i <- 1
for (plan in plans_by_plants) {
harvest_plan <- list()
for (house in plan) {
harvest_plan_per_house <- list()
j <- 1
days <- 0
for (plant in seq(1, length(house))) {
days <- days + df[df$veg == house[[plant]], 'time']
harvest_plan_per_house[[j]] <- paste(house[[plant]], days, sep='_')
j <- j + 1
}
harvest_plan <- c(harvest_plan, harvest_plan_per_house)
}
if (length(harvest_plan) == length(unique(harvest_plan))) {
plan <- list()
n <- 1
for (plant in harvest_plan) {
plan[[n]] <- strsplit(plant, split='_')[[1]][1]
n <- n + 1
}
plan_no_harvest_collision[[i]] <- plan
i <- i + 1
} else {
# pass
}
}
the length of plan_no_harvest_collision
from Python is 45024 but from R it is only 4056.
The only difference I can spot is when appending to a list in R where I use two different methods. One is e.g. plan_no_harvest_collision[[i]] <- plan
and increasing the counter and the other is harvest_plan <- c(harvest_plan, harvest_plan_per_house)
. So what is the difference between the two options of appending to a list in R?
Full Python code:
anbaudauer = 12
pflanzen = {
'tomaten': 6,
'karotten': 4,
'paprika': 7,
'erdbeeren': 5,
'koriander': 2,
'salat': 3,
'zucchini': 4,
'gurke': 5,
'petersilie': 2,
'radieschen': 3,
}
planzen_list = [
'tomaten_6',
'karotten_4',
'paprika_7',
'erdbeeren_5',
'koriander_2',
'salat_3',
'zucchini_4',
'gurke_5',
'petersilie_2',
'radieschen_3'
]
# create all possible combinations that take in total 12 days to grow
def possible_combinations(values, target, with_replacement=True):
def sub_combinations(index, l, r, t, w):
if t == sum(filter(lambda i: isinstance(i, int), l)):
r.append(l)
elif t < sum(filter(lambda i: isinstance(i, int), l)):
return
for u in range(index, len(values)):
sub_combinations(u if w else (u + 1), l + [values[u].split('_')[0], int(values[u].split('_')[1])], r, t, w)
return r
return sub_combinations(0, [], [], target, with_replacement)
raw_combinations = possible_combinations(planzen_list, anbaudauer)
# remove combinations that have a repeating plant
combinations_without_repetition = []
for combi in raw_combinations:
pflanzen_only = [x for x in combi if isinstance(x, str)]
not_repeating = True
for i in range(0, len(pflanzen_only) - 1, 1):
if pflanzen_only[i] == pflanzen_only[i + 1]:
not_repeating = False
else:
pass
if not_repeating:
combinations_without_repetition.append(combi)
else:
pass
# create all possible combinations that can be grown in the given number of farm houses,
# check if all plants are grown at least once, and remove combinations that do not grow all plants at least once
only_all_plants_full = []
for c1 in combinations_without_repetition:
for c2 in combinations_without_repetition:
for c3 in combinations_without_repetition:
for c4 in combinations_without_repetition:
only_plant_names = []
for el in c1:
if isinstance(el, str):
only_plant_names.append(el)
else:
pass
for el in c2:
if isinstance(el, str):
only_plant_names.append(el)
else:
pass
for el in c3:
if isinstance(el, str):
only_plant_names.append(el)
else:
pass
for el in c4:
if isinstance(el, str):
only_plant_names.append(el)
else:
pass
if len(set(only_plant_names)) == len(planzen_list):
only_all_plants_full.append([c1, c2, c3, c4])
else:
pass
# check if same plants across the farm houses are harvested at the same time
# and remove all combinations that would harvest same plants at the same time
plan_no_harvest_collision = []
for plan in only_all_plants_full:
harvest_plan = []
for house in plan:
harvest_plan_per_house = []
days = 0
for plant in range(0, len(house) - 1, 2):
days += house[plant + 1]
harvest_plan_per_house.append(house[plant] + '_' + str(days))
harvest_plan += harvest_plan_per_house
if len(harvest_plan) == len(set(harvest_plan)):
plan = []
for plant in harvest_plan:
plan.append(plant.split('_')[0])
plan_no_harvest_collision.append(plan)
else:
pass
Full R code:
anbaudauer = 12
w <- c(
'tomaten_6',
'karotten_4',
'paprika_7',
'erdbeeren_5',
'koriander_2',
'salat_3',
'zucchini_4',
'gurke_5',
'petersilie_2',
'radieschen_3'
)
# alle kombinationen aus pflanzen die eine anbaudauer von 12 ergeben
df <- as.data.frame(do.call(rbind, strsplit(w, "_")), stringsAsFactors = FALSE)
df$V2 <- as.numeric(df$V2)
names(df) <- c("veg", "time")
max_n <- anbaudauer %/% min(df$time)
t <- unique(df$time)
combos <- do.call(rbind, lapply(seq(max_n), function(n)
{
m <- expand.grid(as.data.frame(do.call(cbind, lapply(seq(n), function(x) t))))
m <- m[which(apply(as.matrix(m), 1, sum) == 12), ]
if(length(nrow(m)) > 0)
{
for(i in 1:nrow(m)) m[i, ] <- sort(m[i, ])
m <- unique(m)
if(ncol(m) < max_n) m <- cbind(m, matrix(NA, nrow(m), max_n - ncol(m)))
names(m) <- 1:max_n
return(m)
}
}))
combos <- lapply(split(combos, seq(nrow(combos))),
function(x) {x <- as.numeric(x); x[!is.na(x)]})
combos <- lapply(combos, function(x)
{
expand.grid(as.data.frame(lapply(x, function(y) df$veg[which(df$time == y)]),
stringsAsFactors = FALSE), stringsAsFactors = FALSE)
})
combos <- do.call(rbind, lapply(combos, function(x)
{
for(i in seq(nrow(x))) x[i,] <- sort(x[i,])
x <- unique(x)
if(ncol(x) < max_n)
x <- cbind(x, matrix("", nrow(x), max_n - ncol(x)), stringsAsFactors = FALSE)
names(x) <- seq(max_n)
return(x)
}))
combos <- lapply(split(combos, seq(nrow(combos))),
function(x) { x <- as.character(x[1, ]); x[x != ""] })
names(combos) <- NULL
# entferne alle kombinationen in der eine pflanze auf sich selbst folgt
combinations_without_repetition <- list()
count = 1
for (combination in combos) {
not_repeating = TRUE
for (plant in seq(1, length(combination) - 1)) {
if (combination[plant] == combination[plant + 1]) {
not_repeating = FALSE
} else {
# pass
}
}
if (not_repeating == TRUE) {
combinations_without_repetition[[count]] <- combination
count <- count + 1
} else {
# pass
}
}
# erstelle alle möglichen kombinationen die in 4 gewächshäusern angebaut werden können
plans_by_plants <- list()
i <- 1
for (c1 in combinations_without_repetition) {
for (c2 in combinations_without_repetition) {
for (c3 in combinations_without_repetition) {
for (c4 in combinations_without_repetition) {
possible_plan <- list()
n <- 1
for (plant in c1) {
possible_plan[[n]] <- plant
n <- n + 1
}
for (plant in c2) {
possible_plan[[n]] <- plant
n <- n + 1
}
for (plant in c3) {
possible_plan[[n]] <- plant
n <- n + 1
}
for (plant in c4) {
possible_plan[[n]] <- plant
n <- n + 1
}
# überprüfe ob alle pflanzen mindestens ein mal angebaut werden
if (length(unique(possible_plan)) == length(df$veg)) {
plans_by_plants[[i]] <- list(c1, c2, c3, c4)
i <- i + 1
} else {
# pass
}
}
}
}
}
# überprüfe für jeden plan ob eine gleiche pflanze zur gleichen zeit geerntet wird
plan_no_harvest_collision <- list()
i <- 1
for (plan in plans_by_plants) {
harvest_plan <- list()
for (house in plan) {
harvest_plan_per_house <- list()
j <- 1
days <- 0
for (plant in seq(1, length(house))) {
days <- days + df[df$veg == house[[plant]], 'time']
harvest_plan_per_house[[j]] <- paste(house[[plant]], days, sep='_')
j <- j + 1
}
harvest_plan <- c(harvest_plan, harvest_plan_per_house)
}
if (length(harvest_plan) == length(unique(harvest_plan))) {
plan <- list()
n <- 1
for (plant in harvest_plan) {
plan[[n]] <- strsplit(plant, split='_')[[1]][1]
n <- n + 1
}
plan_no_harvest_collision[[i]] <- plan
i <- i + 1
} else {
# pass
}
}