Sorry, I don't understand, here is my possible solution:
Costruzione dataset per prendere il dato
items <- data.frame(
tipo = c("gomma", "gomma", "gomma", "distanziale", "distanziale", "distanziale"),
dimensione = c(10, 15, 20, 1.83, 1.85, 2),
quantita = c(32, 48, 28, 30, 20, 20)
)
items$idUnivoco <- paste(items$tipo, items$dimensione, sep = "-")
items2 <- aggregate(quantita ~ idUnivoco, data = items, FUN = sum)
items2$tipo<-items2$idUnivoco
items2$tipo <- sapply(strsplit(items2$tipo, "-"), [
, 1)
magGommaTOT <- items2[items2$tipo == "gomma", ]
magGommaTOT<-subset(magGommaTOT, select = - tipo)
magGommaTOT$Misura <- strsplit(as.character(magGommaTOT$idUnivoco), "-")
magGommaTOT$Misura <- sapply(magGommaTOT$Misura, '[', 2)
magDistanzialeTOT <- items2[items2$tipo == "distanziale", ]
magDistanzialeTOT<-subset(magDistanzialeTOT, select = - tipo)
magDistanzialeTOT$Misura <- strsplit(as.character(magDistanzialeTOT$idUnivoco), "-")
magDistanzialeTOT$Misura <- sapply(magDistanzialeTOT$Misura, '[', 2)
#Ordino i magazzini in ordine crescente
magGommaTOT <- magGommaTOT[order(magGommaTOT$Misura, decreasing = TRUE),]
magDistanzialeTOT <- magDistanzialeTOT[order(magDistanzialeTOT$Misura, decreasing = TRUE),]
Definizione vincoli e inizializzazione di alcune variabili
numeroCilindroItems <- 0
qualiItemsPresenti <- data.frame(tipo = character(0), dimensione = numeric(0), quantita = numeric(0))
misureCFdaTagliare <- c(20, 50)
vincoloMaxItem <- sum(misureCFdaTagliare)
numDistanzialiInserite <- 0
numGommeInserite <- 0
condizione_soddisfattaGomma <- TRUE
condizione_soddisfattaDistanziale <- TRUE
countCondizioneGomma <- 0
qualiItemsPresenti <- data.frame()
for (i in magGommaTOT$Misura) {
itemSceltoGomma <- magGommaTOT[i,]
if (itemSceltoGomma$Misura < vincoloMaxItem ||
itemSceltoGomma$quantita == 0) {
countCondizioneGomma <- countCondizioneGomma + 1
next
} else {
numeroGomme <- nrow(magGommaTOT)
if (numeroGomme == countCondizioneGomma) {
condizione_soddisfattaGomma <- FALSE
break
}
} else {
numeroCilindroItems <- numeroCilindroItems + 1
qualiItemsPresenti <-
rbind(qualiItemsPresenti, magGommaTOT[i,])
magGommaTOT[i,]$quantita<-magGommaTOT[i,]$quantita - 1
i <- i + 1
}for (j in magDistanzialeTOT$Misura) {
itemSceltodistanziale <- magDistanzialeTOT[i,]
if (itemScelto$Misura < vincoloMaxItem ||
itemScelto$quantita == 0) {
condizione_soddisfattaDistanziale <-
condizione_soddisfattaDistanziale + 1
next
} else {
numeroDistanziali <- nrow(magDistanzialeTOT)
if (numeroDistanziali == condizione_soddisfattaDistanziale) {
condizione_soddisfattaDistanziale <- FALSE
break
}
} else {
numeroCilindroItems <- numeroCilindroItems + 1
qualiItemsPresenti <-
rbind(qualiItemsPresenti, magDistanzialeTOT[i,])
j <- j + 1
magDistanzialeTOT[i,]$quantita<-magDistanzialeTOT[i,]$quantita - 1
}
}
}