I am trying to design a linear model with three features being weekly prices of three different elements. And I need to combine these prices to get the cheapest combination for each week.
I used Scipy, and when I put my data I only have NaN at the end, and I don’t understand why.
Here is what I tried :
import numpy as np
from scipy.optimize import linprog
wprices = np.array([205.5,215,225])
cprices = np.array([216.5,214,219])
bprices = np.array([184.5,192,202])
# Minimum percentage limits for each product
wprice_min_percentage = 0.25
cprice_min_percentage = 0.25
bprice_min_percentage = 0.25
# Number of weeks
num_weeks = len(wprices)
# Objective function coefficients (negative as we want to minimize the cost)
c = np.array([-wprices, -cprices, -bprices]).T
# Constraint matrix
A = np.array([[1, 1, 1]])
# Lower bounds for the constraints
b = np.array([1]) # Total percentage should be 1 (100%)
# Upper bounds for the constraints
A_ub = np.array([[-wprice_min_percentage, -cprice_min_percentage, -bprice_min_percentage]])
b_ub = np.array([-1]) # Each product's percentage should be greater than or equal to the minimum
# Solve the linear programming problem for each week
optimal_percentages = []
for week in range(num_weeks):
result = linprog(c[week], A_ub=A_ub, b_ub=b_ub, A_eq=A, b_eq=b)
if result.success:
optimal_percentages.append(result.x * 100)
else:
optimal_percentages.append([np.nan, np.nan, np.nan])