You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Crear el DataFrame con φ como cadenas de texto
varillas = pd.DataFrame({'φ(pulg)': ['3/8', '1/2', '3/4', '1']})
# Crear una nueva columna con los valores flotantes de φ
varillas['φ_float'] = varillas['φ(pulg)'].apply(lambda x: eval(x))
# Definir las conversiones
pulgadas_a_cm = 2.54 # 1 pulgada = 2.54 cm
# Calcular las áreas en cm² y los diámetros en cm
varillas['Area (cm²)'] = (varillas['φ_float'] ** 2) * np.pi * (pulgadas_a_cm / 2) ** 2
varillas['Diametro (cm)'] = varillas['φ_float'] * pulgadas_a_cm
# Redondear las áreas y diámetros a 2 decimales
varillas['Area (cm²)'] = varillas['Area (cm²)'].round(2)
varillas['Diametro (cm)'] = varillas['Diametro (cm)'].round(2)
from pulp import LpProblem, LpVariable, lpSum, LpMinimize
# Área total a cubrir
area_total_objetivo = 5.71
# Definir el problema de optimización
problema = LpProblem("Optimización_de_Varillas", LpMinimize)
# Crear las variables de decisión
variables_decision = LpVariable.dicts("Cantidad", varillas.index, lowBound=0, cat='Integer')
# Definir la función objetivo
problema += lpSum(variables_decision[i] * varillas.loc[i, 'Area (cm²)'] for i in varillas.index)
# Definir la restricción de área total
problema += lpSum(variables_decision[i] * varillas.loc[i, 'Area (cm²)'] for i in varillas.index) >= area_total_objetivo
# Resolver el problema de optimización
problema.solve()
print(problema)
# Imprimir la solución
print("Solución 1:")
for i, row in varillas.iterrows():
cantidad = variables_decision[i].varValue
if cantidad > 0:
print(f"{int(cantidad)} varillas de φ {row['φ(pulg)']}")
print(f"Área total: {sum(variables_decision[i].varValue * row['Area (cm²)'] for i, row in varillas.iterrows())} cm²")
I want to add a condition where at least one value in "variables_decision" must be even (not counting values where solution is 0)
I tried % shenanigans:
[variables_decision[i] % 2
But pulp complain with: TypeError: unsupported operand type(s) for %: 'LpVariable' and 'int'
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have this code:
I want to add a condition where at least one value in "variables_decision" must be even (not counting values where solution is 0)
I tried % shenanigans:
[variables_decision[i] % 2
But pulp complain with:
TypeError: unsupported operand type(s) for %: 'LpVariable' and 'int'
Any advice is welcome, thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions