update the solver sandbox elastic constraint impl

This commit is contained in:
Adrian Manteza 2022-03-17 14:01:44 +00:00
parent efbc83ca8d
commit 34418066ca

View File

@ -61,30 +61,39 @@ class SolverSandbox:
print(v.name, "=", v.varValue) print(v.name, "=", v.varValue)
break break
def yas_elastic(tif_target = 140.0): # 140 is the optimal def yas_elastic(tif_targets, tcc_targets): # [50, 55, 46], [60, 40, 50]
Items = [1,2,3,4,5] Items = [1,2,3,4,5,6,7,8,9,10]
Bundles = [1,2]
# For TIF target
tif = {
1: 10,
2: 20
}
iif = { iif = {
1: 10, 1: 5,
2: 20, 2: 5,
3: 30, 3: 5,
4: 40, 4: 10,
5: 50 5: 10,
6: 10,
7: 15,
8: 20,
9: 20,
10: 20
} }
# --- # ---
irf = {
1: 5,
2: 5,
3: 5,
4: 10,
5: 10,
6: 10,
7: 15,
8: 20,
9: 20,
10: 20
}
items = LpVariable.dicts('Item', Items, cat='Binary') items = LpVariable.dicts('Item', Items, cat='Binary')
bundles = LpVariable.dicts('Bundle', Bundles, cat='Binary')
drift = 0 drift = 0
max_drift = 10 # 10% elasticity max_drift = 25# 25% elasticity
while drift <= max_drift: while drift <= max_drift:
drift_percent = drift / 100 drift_percent = drift / 100
@ -94,32 +103,35 @@ class SolverSandbox:
problem += lpSum([items[i] for i in Items]) problem += lpSum([items[i] for i in Items])
# Constraint 1 # Constraint 1
problem += lpSum([items[i] for i in Items] + [bundles[b] for b in Bundles]) == 2, 'TotalItems' problem += lpSum([items[i] for i in Items]) == 5, 'TotalItems'
print(f"Calculating TIF target of {tif_target} with drift of {drift}%")
# Our own "Elastic Constraints" # Our own "Elastic Constraints"
problem += lpSum( for tif_target in tif_targets:
[iif[i] * items[i] for i in Items] print(f"Calculating TIF target of {tif_target} with drift of {drift}%")
) >= tif_target - (tif_target * drift_percent), 'ItemIifMin' problem += lpSum(
problem += lpSum( [iif[i] * items[i] for i in Items]
[iif[i] * items[i] for i in Items] ) >= tif_target - (tif_target * drift_percent)
) <= tif_target + (tif_target * drift_percent), 'ItemIifMax' problem += lpSum(
[iif[i] * items[i] for i in Items]
) <= tif_target + (tif_target * drift_percent)
problem += lpSum( for tcc_target in tcc_targets:
[tif[i] * bundles[i] for i in Bundles] print(f"Calculating TIF target of {tcc_target} with drift of {drift}%")
) >= tif_target - (tif_target * drift_percent), 'BundleTifMin' problem += lpSum(
problem += lpSum( [irf[i] * items[i] for i in Items]
[tif[i] * bundles[i] for i in Bundles] ) >= tcc_target - (tcc_target * drift_percent)
) <= tif_target + (tif_target * drift_percent), 'BundleTifMax' problem += lpSum(
[irf[i] * items[i] for i in Items]
) <= tcc_target + (tcc_target * drift_percent)
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)
# if drift == max_drift: breakpoint();
print(problem.objective.value()) print(problem.objective.value())
print(problem.constraints) print(problem.constraints)
@ -129,9 +141,9 @@ class SolverSandbox:
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.objective.value())
print(problem.constraints) print(problem.constraints)
print(problem.objective) print(problem.objective)