in

Programación de enteros en Python

0VVAFznAQgHd89wVE

Programación de enteros en Python

Freddy Boulton

9 de marzo de 2018·6 min de lectura

0*VVAFznAQgHd89wVE
Usaremos la programación de enteros para tomar decisiones óptimas. Foto de Unsplash

El problema de la mochila

Código cvxpy completo para el problema de la mochila
print(selection.value)matrix([[1.],
[1.],
[1.],
[1.],
[0.],
[1.],
[0.],
[0.],
[0.],
[0.]])

El problema del marketing

print(test_probs[0:5])array([[0.0001, 0.0001, 0.3   , 0.0001, 0.2   , 0.0001, 0.2   , 0.3   ],
[0.1 , 0.1 , 0.1 , 0.2 , 0.2 , 0.2 , 0.1 , 0.0001],
[0.1 , 0.0001, 0.2 , 0.0001, 0.1 , 0.2 , 0.0001, 0.4 ],
[0.3 , 0.0001, 0.0001, 0.2 , 0.1 , 0.2 , 0.2 , 0.0001],
[0.2 , 0.3 , 0.1 , 0.0001, 0.2 , 0.1 , 0.1 , 0.0001]])
#clipping so that we don't take log of 0 or 1
test_probs = np.clip(test_probs, 0.0001, 0.9999)
#turning into costs
model_costs = -np.log10(test_probs)
supply = np.atleast_2d([150, 80, 25])
selection = cvxpy.Bool(*test_probs.shape)
TRANSFORMER = np.array([[1,0,0],
[0,1,0],
[0,0,1],
[1,1,0],
[1,0,1],
[0,1,1],
[1,1,1],
[0,0,0]])
print(np.dot(np.array([0,0,0,1,0,0,0,0]), TRANSFORMER))array([1, 1, 0])
supply_constraint = cvxpy.sum_entries(selection * TRANSFORMER, axis=0) <= supply
# We must make our choice per constituent
# remember that the last column is for "no materials"
feasibility_constraint = cvxpy.sum_entries(selection, axis=1) == 1constraints = [supply_constraint, feasibility_constraint]
cost = cvxpy.sum_entries(cvxpy.mul_elemwise(model_costs, selection))
# Solving the problemproblem = cvxpy.Problem(cvxpy.Minimize(cost), constraints=constraints)problem.solve(solver=cvxpy.GLPK_MI)
print(hard_assignments.value[0:5])matrix([[0., 0., 0., 0., 0., 0., 0., 1.],
[0., 0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0., 0., 0.]])
print(np.dot(hard_assignments.value, TRANSFORMER).sum(axis=0))matrix([[83., 80., 25.]])

Palabras de clausura

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

02kNRECItwty05uWknCBnvu 1.1632407036.fit lim.size 1200x630

Europa quiere que todos los dispositivos (incluidos los iPhones) utilicen USB-C para cargar

obic business incentives 3889689.svg.svgxml charset

Hechos corporativos | Oráculo