make it a single target calculation for yas_elastic

This commit is contained in:
Adrian Manteza 2022-03-11 15:24:57 +00:00
parent 26571cffc6
commit e1176365f3

View File

@ -61,7 +61,7 @@ class SolverSandbox:
print(v.name, "=", v.varValue) print(v.name, "=", v.varValue)
break break
def yas_elastic(tif_target = 50.0): def yas_elastic(tif_target = 140.0): # 140 is the optimal
Items = [1,2,3,4,5] Items = [1,2,3,4,5]
# For TIF target # For TIF target
@ -82,57 +82,55 @@ class SolverSandbox:
} }
# --- # ---
total_forms = 2
items = LpVariable.dicts('Item', Items, cat='Binary') items = LpVariable.dicts('Item', Items, cat='Binary')
for form in range(total_forms): drift = 0
drift = 0 max_drift = 10 # 10% elasticity
max_drift = 10 # 10% elasticity
while drift <= max_drift: while drift <= max_drift:
drift_percent = drift / 100 drift_percent = drift / 100
problem = LpProblem('TIF_TCC', LpMinimize) problem = LpProblem('TIF_TCC', LpMinimize)
# objective function # objective function
problem += lpSum([(tif[i] + iif[i]) * items[i] for i in Items]) problem += lpSum([(tif[i] + iif[i]) * items[i] for i in Items])
# Constraint 1 # Constraint 1
problem += lpSum([items[i] for i in Items]) == 3, 'TotalItems' problem += lpSum([items[i] for i in Items]) == 3, 'TotalItems'
print(f"Calculating TIF target of {tif_target} with drift of {drift} for Form {form + 1}") print(f"Calculating TIF target of {tif_target} with drift of {drift}%")
# Our own "Elastic Constraints" # Our own "Elastic Constraints"
problem += lpSum( problem += lpSum(
[(tif[i] + iif[i]) * items[i] for i in Items] [(tif[i] + iif[i]) * items[i] for i in Items]
) >= tif_target - (tif_target * drift_percent), 'TifIifMin' ) >= tif_target - (tif_target * drift_percent), 'TifIifMin'
problem += lpSum( problem += lpSum(
[(tif[i] + iif[i]) * items[i] for i in Items] [(tif[i] + iif[i]) * items[i] for i in Items]
) <= tif_target + (tif_target * drift_percent), 'TifIifMax' ) <= tif_target + (tif_target * drift_percent), 'TifIifMax'
problem.solve() problem.solve()
if LpStatus[problem.status] == 'Infeasible': if LpStatus[problem.status] == 'Infeasible':
print(f"attempt infeasible for drift of {drift}") print(f"attempt infeasible for drift of {drift}")
for v in problem.variables(): for v in problem.variables():
print(v.name, "=", v.varValue) print(v.name, "=", v.varValue)
print(problem.constraints) print(problem.objective.value())
print(problem.objective) print(problem.constraints)
print(problem.objective)
drift += 1 drift += 1
else: else:
print(f"solution found with drift of {drift}!") print(f"solution found with drift of {drift}!")
for v in problem.variables(): for v in problem.variables():
print(v.name, "=", v.varValue) print(v.name, "=", v.varValue)
print(problem.constraints) print(problem.constraints)
print(problem.objective) print(problem.objective)
break break
# Implementation of the Whiskas Cat problem, with elastic constraints # Implementation of the Whiskas Cat problem, with elastic constraints
# https://www.coin-or.org/PuLP/CaseStudies/a_blending_problem.html # https://www.coin-or.org/PuLP/CaseStudies/a_blending_problem.html