# Optimization algorithm on the number of items

I want to create an algorithm in R in which by passing some measures it calculates the filling of elements on a cylinder.
The articles are as follows:

• Eraser = 10 mm and 32 pieces
• Eraser = 15 mm and 48 pieces
• Eraser = 20 mm and 28 pieces
• Spacer = 1.83mm and 30pcs
• Spacer = 1.85mm and 20pcs
• Spacer = 2 mm and 20 pieces
These items put into a table

The constraints are as follows:

• I have to add the measurements to find the maximum constraint of the cylinder size, so I know how many items I will have to insert to reach the target objective which is the sum of the measurements
Example: I have three sizes like 20, 50 and 10 mm. The total of these widths is 80 so when I insert the items I calculate the sum of the widths of the tools which must not exceed 80 mm
• Each tire must have a spacer
Ex: if I have 3 tires in the cylinder, I must certainly have 3 spacers
• When I use the items in the cylinder I'm going to reduce the quantities in stock since I'm using them

Using a quantity optimization algorithm, thus minimizing the number of items in the cylinder

I tried several times to create this algorithm but I failed, I can ask you for a hand

I approach a problem like this as an exercise in school algebraâ€”f(x) = y where

x is what is to hand
y is what is desired
f is a function to transform x to y

Any of these three objects may be composite.

The first step is to represent the parts inventory, x. Here's one way.

``````# Translate the following representations
# Eraser = 10 mm and 32 pieces
# Eraser = 15 mm and 48 pieces
# Eraser = 20 mm and 28 pieces
# Spacer = 1.83mm and 30pcs
# Spacer = 1.85mm and 20pcs
# Spacer = 2 mm and 20 pieces

# into vectors

e10 <- rep(10,32)
e15 <- rep(15,48)
e20 <- rep(20,28)
s183 <- rep(1.83,30)
s185 <- rep(1.85,20)
s2 <- rep(2,20)

# because the vectors are of different length
# they cannot be easily brought together in a
# data frame, so assemble into a list

(l <- list(e10,e15,e20,s183,s185,s2))
#> [[1]]
#>  [1] 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
#> [26] 10 10 10 10 10 10 10
#>
#> [[2]]
#>  [1] 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15
#> [26] 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15
#>
#> [[3]]
#>  [1] 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
#> [26] 20 20 20
#>
#> [[4]]
#>  [1] 1.83 1.83 1.83 1.83 1.83 1.83 1.83 1.83 1.83 1.83 1.83 1.83 1.83 1.83 1.83
#> [16] 1.83 1.83 1.83 1.83 1.83 1.83 1.83 1.83 1.83 1.83 1.83 1.83 1.83 1.83 1.83
#>
#> [[5]]
#>  [1] 1.85 1.85 1.85 1.85 1.85 1.85 1.85 1.85 1.85 1.85 1.85 1.85 1.85 1.85 1.85
#> [16] 1.85 1.85 1.85 1.85 1.85
#>
#> [[6]]
#>  [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
``````

Created on 2023-03-31 with reprex v2.0.2

The next step is to construct an object to represent an assembled cylinder, y. How would you do that? Could it be a vector with at least one eraser and one spacer?

With that in mind, can you write a function to test if y has an equal number of erasers and spacers? Where would you go from there?

The idea was to have an algorithm that inserts the number of items inside the cylinder but paying attention to these constraints:

• Each tire must have a spacer inserted with it
• The maximum constraint of items that I can insert corresponds to the number of widths requested, eg: I want 10 mm and 20 mm then inside the cylinder there can be a number of items that reaches the maximum width of 30 mm
• I have to be careful to only insert items in the warehouse, so if I use it for a cylinder the warehouse decreases
• The objective is to minimize the number of items present inside the cylinder but respecting the required widths

Same suggestion

An algorithm abstracts the method of solving a problem from the particular facts of any instance of a problem and to do so must represent the solution symbolically.

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
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) {
next
} else {
numeroDistanziali <- nrow(magDistanzialeTOT)
break
}
} else {
numeroCilindroItems <- numeroCilindroItems + 1
qualiItemsPresenti <-
rbind(qualiItemsPresenti, magDistanzialeTOT[i,])
j <- j + 1
magDistanzialeTOT[i,]\$quantita<-magDistanzialeTOT[i,]\$quantita - 1

``````}
``````

}

}

I know nothing about the mechanical process you are trying to optomise....
It may be hard for you to disentangle your own subject matter expertise from the statement of your requirements.
I recommend you either a)
Teach us how to make cylinders explaining everything like we are simple minded....
b)
Abstract away from asking about constructing cylinders and set us a logic puzzle based on abstract objects with concrete rules about how we are allowed to play with them. Assuming you can conceptualise your task in such a way

2 Likes

Imagine the cylinder as a tube in which I add discs inside, these discs can be made of two types:

• Eraser
• Spacer (iron disc, typically characterized by a smaller width than rubber discs)

The goal is to fill this cylinder with these discs until you reach the desired size.
The problem is trying to build this cylinder out of the least number of disks.

Thatâ€™s a good illustration of the verbal distinction. The next task is to translate the illustration into something that R can understand. How would you do that?

I think of doing it like this:

• I create the tools warehouse
• I put them in descending order by width (thereby optimizing the number of items)
• I make a for loop reading line by line and doing any checks, i.e. compliance with the two constraints (presence in the warehouse and respected width)

But I have a problem with the for loop

``````elementi <- 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)
)
elementi\$idUnivoco <- paste(elementi\$tipo, elementi\$dimensione, sep = "-")
elementi2 <- aggregate(quantita ~ idUnivoco, dati = elementi, FUN = somma)

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)

magGommaTOT <- magGommaTOT[ordine(magGommaTOT\$Misura, decrescente = TRUE),]
magDistanzialeTOT <- magDistanzialeTOT[ordine(magDistanzialeTOT\$Misura, decrescente = TRUE),]

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
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, ]\$quantitĂ  - 1
i <- i + 1

}for (j in magDistanzialeTOT\$Misura) {
itemSceltodistanziale <- magDistanzialeTOT[i,]
if (itemScelto\$Misura < vincoloMaxItem ||
itemScelto\$quantita == 0) {
next
} else {
numeroDistanziali <- nrow( magDistanzialeTOT)
break
}
} else {
numeroCilindroItems <- numeroCilindroItems + 1
qualiItemsPresenti <-
rbind(qualiItemsPresenti, magDistanzialeTOT[i,])
j <- j + 1
magDistanzialeTOT[i,]\$ quantita<-magDistanzialeTOT[i,]\$quantita - 1

}
}

}
``````

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.